From 2f032ad5454063341228eec8b2477dce09abdba4 Mon Sep 17 00:00:00 2001 From: Jeroen Ooms Date: Wed, 13 Dec 2023 11:05:56 +0100 Subject: [PATCH 1/3] Test support for sourcepos for latex output --- R/markdown.R | 4 ++-- man/commonmark.Rd | 1 + src/cmark/latex.c | 12 +++++++++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/R/markdown.R b/R/markdown.R index 562fd22..fdba80f 100644 --- a/R/markdown.R +++ b/R/markdown.R @@ -74,8 +74,8 @@ markdown_text <- function(text, hardbreaks = FALSE, smart = FALSE, normalize = F #' @export #' @rdname commonmark -markdown_latex <- function(text, hardbreaks = FALSE, smart = FALSE, normalize = FALSE, footnotes = FALSE, width = 0, extensions = FALSE){ +markdown_latex <- function(text, hardbreaks = FALSE, smart = FALSE, normalize = FALSE, sourcepos = FALSE, footnotes = FALSE, width = 0, extensions = FALSE){ text <- enc2utf8(paste(text, collapse="\n")) extensions <- get_extensions(extensions) - .Call(R_render_markdown, text, 6L, FALSE, hardbreaks, smart, normalize, footnotes, as.integer(width), extensions) + .Call(R_render_markdown, text, 6L, sourcepos, hardbreaks, smart, normalize, footnotes, as.integer(width), extensions) } diff --git a/man/commonmark.Rd b/man/commonmark.Rd index 6d9b26f..129891e 100644 --- a/man/commonmark.Rd +++ b/man/commonmark.Rd @@ -66,6 +66,7 @@ markdown_latex( hardbreaks = FALSE, smart = FALSE, normalize = FALSE, + sourcepos = FALSE, footnotes = FALSE, width = 0, extensions = FALSE diff --git a/src/cmark/latex.c b/src/cmark/latex.c index 8be15b0..e884691 100644 --- a/src/cmark/latex.c +++ b/src/cmark/latex.c @@ -15,9 +15,19 @@ #define OUT(s, wrap, escaping) renderer->out(renderer, node, s, wrap, escaping) #define LIT(s) renderer->out(renderer, node, s, false, LITERAL) #define CR() renderer->cr(renderer) -#define BLANKLINE() renderer->blankline(renderer) +#define BLANKLINE() latex_out_sourcepos(node, renderer, options); renderer->blankline(renderer) #define LIST_NUMBER_STRING_SIZE 20 +static CMARK_INLINE void latex_out_sourcepos(cmark_node *node, cmark_renderer *renderer, int options){ + char buffer[100]; + if (CMARK_OPT_SOURCEPOS & options) { + snprintf(buffer, 100, " %%sourcepos(%d:%d-%d:%d)", + cmark_node_get_start_line(node), cmark_node_get_start_column(node), + cmark_node_get_end_line(node), cmark_node_get_end_column(node)); + renderer->out(renderer, node, buffer, false, LITERAL); + } +} + static CMARK_INLINE void outc(cmark_renderer *renderer, cmark_node *node, cmark_escaping escape, int32_t c, unsigned char nextc) { From 78d97f604098216d313d7f2a954cf61d8ffb55a2 Mon Sep 17 00:00:00 2001 From: Jeroen Ooms Date: Thu, 14 Dec 2023 22:44:47 +0100 Subject: [PATCH 2/3] Add even more sourcepos --- src/cmark/latex.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cmark/latex.c b/src/cmark/latex.c index e884691..4f14d05 100644 --- a/src/cmark/latex.c +++ b/src/cmark/latex.c @@ -14,13 +14,13 @@ #define OUT(s, wrap, escaping) renderer->out(renderer, node, s, wrap, escaping) #define LIT(s) renderer->out(renderer, node, s, false, LITERAL) -#define CR() renderer->cr(renderer) -#define BLANKLINE() latex_out_sourcepos(node, renderer, options); renderer->blankline(renderer) +#define CR() latex_out_sourcepos(node, renderer, options, false); renderer->cr(renderer) +#define BLANKLINE() latex_out_sourcepos(node, renderer, options, true); renderer->blankline(renderer) #define LIST_NUMBER_STRING_SIZE 20 -static CMARK_INLINE void latex_out_sourcepos(cmark_node *node, cmark_renderer *renderer, int options){ +static CMARK_INLINE void latex_out_sourcepos(cmark_node *node, cmark_renderer *renderer, int options, int blankline){ char buffer[100]; - if (CMARK_OPT_SOURCEPOS & options) { + if (CMARK_OPT_SOURCEPOS & options && !renderer->need_cr && (node->type != CMARK_NODE_CODE_BLOCK || blankline)) { snprintf(buffer, 100, " %%sourcepos(%d:%d-%d:%d)", cmark_node_get_start_line(node), cmark_node_get_start_column(node), cmark_node_get_end_line(node), cmark_node_get_end_column(node)); From b0ef50902d62201682a3975927fd331bfe3a82dd Mon Sep 17 00:00:00 2001 From: Jeroen Ooms Date: Fri, 15 Dec 2023 11:33:27 +0100 Subject: [PATCH 3/3] Skip empty nodes --- src/cmark/latex.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/cmark/latex.c b/src/cmark/latex.c index 4f14d05..3fe1108 100644 --- a/src/cmark/latex.c +++ b/src/cmark/latex.c @@ -21,6 +21,8 @@ static CMARK_INLINE void latex_out_sourcepos(cmark_node *node, cmark_renderer *renderer, int options, int blankline){ char buffer[100]; if (CMARK_OPT_SOURCEPOS & options && !renderer->need_cr && (node->type != CMARK_NODE_CODE_BLOCK || blankline)) { + if(cmark_node_get_start_line(node) == 0) + return; //elements without node such as softbreaks snprintf(buffer, 100, " %%sourcepos(%d:%d-%d:%d)", cmark_node_get_start_line(node), cmark_node_get_start_column(node), cmark_node_get_end_line(node), cmark_node_get_end_column(node));