Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for sourcepos for latex output #29

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions R/markdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions man/commonmark.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions src/cmark/latex.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,22 @@

#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 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, 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));
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) {
Expand Down