diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/commonmark.c | 180 | ||||
-rw-r--r-- | src/latex.c | 278 | ||||
-rw-r--r-- | src/render.c | 112 | ||||
-rw-r--r-- | src/render.h | 18 |
4 files changed, 294 insertions, 294 deletions
diff --git a/src/commonmark.c b/src/commonmark.c index c6a13e8..5bbc8d4 100644 --- a/src/commonmark.c +++ b/src/commonmark.c @@ -14,7 +14,7 @@ // Functions to convert cmark_nodes to commonmark strings. -static inline void outc(cmark_render_state *state, +static inline void outc(cmark_renderer *renderer, cmark_escaping escape, int32_t c, unsigned char nextc) @@ -27,10 +27,10 @@ static inline void outc(cmark_render_state *state, c == '<' || c == '>' || c == '\\' || c == '`' || (c == '&' && isalpha(nextc)) || (c == '!' && nextc == '[') || - (state->begin_line && + (renderer->begin_line && (c == '-' || c == '+' || c == '#' || c == '=')) || ((c == '.' || c == ')') && - isdigit(state->buffer->ptr[state->buffer->size - 1])))) || + isdigit(renderer->buffer->ptr[renderer->buffer->size - 1])))) || (escape == URL && (c == '`' || c == '<' || c == '>' || isspace(c) || c == '\\' || c == ')' || c == '(')) || @@ -41,18 +41,18 @@ static inline void outc(cmark_render_state *state, if (needs_escaping) { if (isspace(c)) { // use percent encoding for spaces - cmark_strbuf_printf(state->buffer, "%%%2x", c); - state->column += 3; + cmark_strbuf_printf(renderer->buffer, "%%%2x", c); + renderer->column += 3; } else { - cmark_strbuf_putc(state->buffer, '\\'); - utf8proc_encode_char(c, state->buffer); - state->column += 2; + cmark_strbuf_putc(renderer->buffer, '\\'); + utf8proc_encode_char(c, renderer->buffer); + renderer->column += 2; } - state->begin_line = false; + renderer->begin_line = false; } else { - utf8proc_encode_char(c, state->buffer); - state->column += 1; - state->begin_line = false; + utf8proc_encode_char(c, renderer->buffer); + renderer->column += 1; + renderer->begin_line = false; } } @@ -156,7 +156,7 @@ get_containing_block(cmark_node *node) static int S_render_node(cmark_node *node, cmark_event_type ev_type, - cmark_render_state *state) + cmark_renderer *renderer) { cmark_node *tmp; cmark_chunk *code; @@ -177,7 +177,7 @@ S_render_node(cmark_node *node, cmark_event_type ev_type, if (!(node->type == CMARK_NODE_ITEM && node->prev == NULL && entering)) { tmp = get_containing_block(node); - state->in_tight_list_item = + renderer->in_tight_list_item = (tmp->type == CMARK_NODE_ITEM && cmark_node_get_list_tight(tmp->parent)) || (tmp && @@ -189,18 +189,18 @@ S_render_node(cmark_node *node, cmark_event_type ev_type, switch (node->type) { case CMARK_NODE_DOCUMENT: if (!entering) { - cmark_strbuf_putc(state->buffer, '\n'); + cmark_strbuf_putc(renderer->buffer, '\n'); } break; case CMARK_NODE_BLOCK_QUOTE: if (entering) { - lit(state, "> ", false); - cmark_strbuf_puts(state->prefix, "> "); + lit(renderer, "> ", false); + cmark_strbuf_puts(renderer->prefix, "> "); } else { - cmark_strbuf_truncate(state->prefix, - state->prefix->size - 2); - blankline(state); + cmark_strbuf_truncate(renderer->prefix, + renderer->prefix->size - 2); + blankline(renderer); } break; @@ -210,8 +210,8 @@ S_render_node(cmark_node *node, cmark_event_type ev_type, node->next->type == CMARK_NODE_LIST)) { // this ensures 2 blank lines after list, // if before code block or list: - lit(state, "\n", false); - state->need_cr = 0; + lit(renderer, "\n", false); + renderer->need_cr = 0; } break; @@ -240,19 +240,19 @@ S_render_node(cmark_node *node, cmark_event_type ev_type, if (entering) { if (cmark_node_get_list_type(node->parent) == CMARK_BULLET_LIST) { - lit(state, "* ", false); - cmark_strbuf_puts(state->prefix, " "); + lit(renderer, "* ", false); + cmark_strbuf_puts(renderer->prefix, " "); } else { - lit(state, (char *)listmarker.ptr, false); + lit(renderer, (char *)listmarker.ptr, false); for (i = marker_width; i--;) { - cmark_strbuf_putc(state->prefix, ' '); + cmark_strbuf_putc(renderer->prefix, ' '); } } } else { - cmark_strbuf_truncate(state->prefix, - state->prefix->size - + cmark_strbuf_truncate(renderer->prefix, + renderer->prefix->size - marker_width); - cr(state); + cr(renderer); } cmark_strbuf_free(&listmarker); break; @@ -260,18 +260,18 @@ S_render_node(cmark_node *node, cmark_event_type ev_type, case CMARK_NODE_HEADER: if (entering) { for (int i = cmark_node_get_header_level(node); i > 0; i--) { - lit(state, "#", false); + lit(renderer, "#", false); } - lit(state, " ", false); - state->no_wrap = true; + lit(renderer, " ", false); + renderer->no_wrap = true; } else { - state->no_wrap = false; - blankline(state); + renderer->no_wrap = false; + blankline(renderer); } break; case CMARK_NODE_CODE_BLOCK: - blankline(state); + blankline(renderer); info = &node->as.code.info; code = &node->as.code.literal; // use indented form if no info, and code doesn't @@ -284,65 +284,65 @@ S_render_node(cmark_node *node, cmark_event_type ev_type, isspace(code->data[code->len - 2]))) && !(node->prev == NULL && node->parent && node->parent->type == CMARK_NODE_ITEM)) { - lit(state, " ", false); - cmark_strbuf_puts(state->prefix, " "); - out(state, node->as.code.literal, false, LITERAL); - cmark_strbuf_truncate(state->prefix, - state->prefix->size - 4); + lit(renderer, " ", false); + cmark_strbuf_puts(renderer->prefix, " "); + out(renderer, node->as.code.literal, false, LITERAL); + cmark_strbuf_truncate(renderer->prefix, + renderer->prefix->size - 4); } else { numticks = longest_backtick_sequence(code) + 1; if (numticks < 3) { numticks = 3; } for (i = 0; i < numticks; i++) { - lit(state, "`", false); + lit(renderer, "`", false); } - lit(state, " ", false); - out(state, *info, false, LITERAL); - cr(state); - out(state, node->as.code.literal, false, LITERAL); - cr(state); + lit(renderer, " ", false); + out(renderer, *info, false, LITERAL); + cr(renderer); + out(renderer, node->as.code.literal, false, LITERAL); + cr(renderer); for (i = 0; i < numticks; i++) { - lit(state, "`", false); + lit(renderer, "`", false); } } - blankline(state); + blankline(renderer); break; case CMARK_NODE_HTML: - blankline(state); - out(state, node->as.literal, false, LITERAL); - blankline(state); + blankline(renderer); + out(renderer, node->as.literal, false, LITERAL); + blankline(renderer); break; case CMARK_NODE_HRULE: - blankline(state); - lit(state, "-----", false); - blankline(state); + blankline(renderer); + lit(renderer, "-----", false); + blankline(renderer); break; case CMARK_NODE_PARAGRAPH: if (!entering) { - blankline(state); + blankline(renderer); } break; case CMARK_NODE_TEXT: - out(state, node->as.literal, true, NORMAL); + out(renderer, node->as.literal, true, NORMAL); break; case CMARK_NODE_LINEBREAK: - if (!(CMARK_OPT_HARDBREAKS & state->options)) { - lit(state, "\\", false); + if (!(CMARK_OPT_HARDBREAKS & renderer->options)) { + lit(renderer, "\\", false); } - cr(state); + cr(renderer); break; case CMARK_NODE_SOFTBREAK: - if (state->width == 0) { - cr(state); + if (renderer->width == 0) { + cr(renderer); } else { - lit(state, " ", true); + lit(renderer, " ", true); } break; @@ -350,29 +350,29 @@ S_render_node(cmark_node *node, cmark_event_type ev_type, code = &node->as.literal; numticks = shortest_unused_backtick_sequence(code); for (i = 0; i < numticks; i++) { - lit(state, "`", false); + lit(renderer, "`", false); } if (code->len == 0 || code->data[0] == '`') { - lit(state, " ", false); + lit(renderer, " ", false); } - out(state, node->as.literal, true, LITERAL); + out(renderer, node->as.literal, true, LITERAL); if (code->len == 0 || code->data[code->len - 1] == '`') { - lit(state, " ", false); + lit(renderer, " ", false); } for (i = 0; i < numticks; i++) { - lit(state, "`", false); + lit(renderer, "`", false); } break; case CMARK_NODE_INLINE_HTML: - out(state, node->as.literal, false, LITERAL); + out(renderer, node->as.literal, false, LITERAL); break; case CMARK_NODE_STRONG: if (entering) { - lit(state, "**", false); + lit(renderer, "**", false); } else { - lit(state, "**", false); + lit(renderer, "**", false); } break; @@ -386,62 +386,62 @@ S_render_node(cmark_node *node, cmark_event_type ev_type, emph_delim = "*"; } if (entering) { - lit(state, emph_delim, false); + lit(renderer, emph_delim, false); } else { - lit(state, emph_delim, false); + lit(renderer, emph_delim, false); } break; case CMARK_NODE_LINK: if (is_autolink(node)) { if (entering) { - lit(state, "<", false); + lit(renderer, "<", false); if (strncmp(cmark_node_get_url(node), "mailto:", 7) == 0) { - lit(state, + lit(renderer, (char *)cmark_node_get_url(node) + 7, false); } else { - lit(state, + lit(renderer, (char *)cmark_node_get_url(node), false); } - lit(state, ">", false); + lit(renderer, ">", false); // return signal to skip contents of node... return 0; } } else { if (entering) { - lit(state, "[", false); + lit(renderer, "[", false); } else { - lit(state, "](", false); - out(state, + lit(renderer, "](", false); + out(renderer, cmark_chunk_literal(cmark_node_get_url(node)), false, URL); title = &node->as.link.title; if (title->len > 0) { - lit(state, " \"", true); - out(state, *title, false, TITLE); - lit(state, "\"", false); + lit(renderer, " \"", true); + out(renderer, *title, false, TITLE); + lit(renderer, "\"", false); } - lit(state, ")", false); + lit(renderer, ")", false); } } break; case CMARK_NODE_IMAGE: if (entering) { - lit(state, "![", false); + lit(renderer, "![", false); } else { - lit(state, "](", false); - out(state, cmark_chunk_literal(cmark_node_get_url(node)), false, URL); + lit(renderer, "](", false); + out(renderer, cmark_chunk_literal(cmark_node_get_url(node)), false, URL); title = &node->as.link.title; if (title->len > 0) { - lit(state, " \"", true); - out(state, *title, false, TITLE); - lit(state, "\"", false); + lit(renderer, " \"", true); + out(renderer, *title, false, TITLE); + lit(renderer, "\"", false); } - lit(state, ")", false); + lit(renderer, ")", false); } break; diff --git a/src/latex.c b/src/latex.c index c3d39da..6cca96e 100644 --- a/src/latex.c +++ b/src/latex.c @@ -12,14 +12,14 @@ #include "scanners.h" #include "render.h" -static inline void outc(cmark_render_state *state, +static inline void outc(cmark_renderer *renderer, cmark_escaping escape, int32_t c, unsigned char nextc) { if (escape == LITERAL) { - utf8proc_encode_char(c, state->buffer); - state->column += 1; + utf8proc_encode_char(c, renderer->buffer); + renderer->column += 1; } else { switch(c) { case 123: // '{' @@ -27,153 +27,153 @@ static inline void outc(cmark_render_state *state, case 35: // '#' case 37: // '%' case 38: // '&' - cmark_strbuf_putc(state->buffer, '\\'); - utf8proc_encode_char(c, state->buffer); - state->column += 2; + cmark_strbuf_putc(renderer->buffer, '\\'); + utf8proc_encode_char(c, renderer->buffer); + renderer->column += 2; break; case 36: // '$' case 95: // '_' if (escape == NORMAL) { - cmark_strbuf_putc(state->buffer, '\\'); - state->column += 1; + cmark_strbuf_putc(renderer->buffer, '\\'); + renderer->column += 1; } - utf8proc_encode_char(c, state->buffer); - state->column += 1; + utf8proc_encode_char(c, renderer->buffer); + renderer->column += 1; break; case 45 : // '-' if (nextc == 45) { // prevent ligature - cmark_strbuf_putc(state->buffer, '\\'); - state->column += 1; + cmark_strbuf_putc(renderer->buffer, '\\'); + renderer->column += 1; } - utf8proc_encode_char(c, state->buffer); - state->column += 1; + utf8proc_encode_char(c, renderer->buffer); + renderer->column += 1; break; case 126: // '~' if (escape == NORMAL) { - cmark_strbuf_puts(state->buffer, + cmark_strbuf_puts(renderer->buffer, "\\textasciitilde{}"); - state->column += 17; + renderer->column += 17; } else { - utf8proc_encode_char(c, state->buffer); - state->column += 1; + utf8proc_encode_char(c, renderer->buffer); + renderer->column += 1; } break; case 94: // '^' - cmark_strbuf_puts(state->buffer, + cmark_strbuf_puts(renderer->buffer, "\\^{}"); - state->column += 4; + renderer->column += 4; break; case 92: // '\\' if (escape == URL) { // / acts as path sep even on windows: - cmark_strbuf_puts(state->buffer, "/"); - state->column += 1; + cmark_strbuf_puts(renderer->buffer, "/"); + renderer->column += 1; } else { - cmark_strbuf_puts(state->buffer, + cmark_strbuf_puts(renderer->buffer, "\\textbackslash{}"); - state->column += 16; + renderer->column += 16; } break; case 124: // '|' - cmark_strbuf_puts(state->buffer, + cmark_strbuf_puts(renderer->buffer, "\\textbar{}"); - state->column += 10; + renderer->column += 10; break; case 60: // '<' - cmark_strbuf_puts(state->buffer, + cmark_strbuf_puts(renderer->buffer, "\\textless{}"); - state->column += 11; + renderer->column += 11; break; case 62: // '>' - cmark_strbuf_puts(state->buffer, + cmark_strbuf_puts(renderer->buffer, "\\textgreater{}"); - state->column += 14; + renderer->column += 14; break; case 91: // '[' case 93: // ']' - cmark_strbuf_putc(state->buffer, '{'); - utf8proc_encode_char(c, state->buffer); - cmark_strbuf_putc(state->buffer, '}'); - state->column += 3; + cmark_strbuf_putc(renderer->buffer, '{'); + utf8proc_encode_char(c, renderer->buffer); + cmark_strbuf_putc(renderer->buffer, '}'); + renderer->column += 3; break; case 34: // '"' - cmark_strbuf_puts(state->buffer, + cmark_strbuf_puts(renderer->buffer, "\\textquotedbl{}"); // requires \usepackage[T1]{fontenc} - state->column += 15; + renderer->column += 15; break; case 39: // '\'' - cmark_strbuf_puts(state->buffer, + cmark_strbuf_puts(renderer->buffer, "\\textquotesingle{}"); - state->column += 18; + renderer->column += 18; // requires \usepackage{textcomp} break; case 160: // nbsp - cmark_strbuf_putc(state->buffer, '~'); - state->column += 1; + cmark_strbuf_putc(renderer->buffer, '~'); + renderer->column += 1; break; case 8230: // hellip - cmark_strbuf_puts(state->buffer, "\\ldots{}"); - state->column += 8; + cmark_strbuf_puts(renderer->buffer, "\\ldots{}"); + renderer->column += 8; break; case 8216: // lsquo if (escape == NORMAL) { - cmark_strbuf_putc(state->buffer, '`'); - state->column += 1; + cmark_strbuf_putc(renderer->buffer, '`'); + renderer->column += 1; } else { - utf8proc_encode_char(c, state->buffer); - state->column += 1; + utf8proc_encode_char(c, renderer->buffer); + renderer->column += 1; } break; case 8217: // rsquo if (escape == NORMAL) { - cmark_strbuf_putc(state->buffer, '\''); - state->column += 1; + cmark_strbuf_putc(renderer->buffer, '\''); + renderer->column += 1; } else { - utf8proc_encode_char(c, state->buffer); - state->column += 1; + utf8proc_encode_char(c, renderer->buffer); + renderer->column += 1; } break; case 8220: // ldquo if (escape == NORMAL) { - cmark_strbuf_puts(state->buffer, "``"); - state->column += 2; + cmark_strbuf_puts(renderer->buffer, "``"); + renderer->column += 2; } else { - utf8proc_encode_char(c, state->buffer); - state->column += 1; + utf8proc_encode_char(c, renderer->buffer); + renderer->column += 1; } break; case 8221: // rdquo if (escape == NORMAL) { - cmark_strbuf_puts(state->buffer, "''"); - state->column += 2; + cmark_strbuf_puts(renderer->buffer, "''"); + renderer->column += 2; } else { - utf8proc_encode_char(c, state->buffer); - state->column += 1; + utf8proc_encode_char(c, renderer->buffer); + renderer->column += 1; } break; case 8212: // emdash if (escape == NORMAL) { - cmark_strbuf_puts(state->buffer, "---"); - state->column += 3; + cmark_strbuf_puts(renderer->buffer, "---"); + renderer->column += 3; } else { - utf8proc_encode_char(c, state->buffer); - state->column += 1; + utf8proc_encode_char(c, renderer->buffer); + renderer->column += 1; } break; case 8211: // endash if (escape == NORMAL) { - cmark_strbuf_puts(state->buffer, "--"); - state->column += 2; + cmark_strbuf_puts(renderer->buffer, "--"); + renderer->column += 2; } else { - utf8proc_encode_char(c, state->buffer); - state->column += 1; + utf8proc_encode_char(c, renderer->buffer); + renderer->column += 1; } break; default: - utf8proc_encode_char(c, state->buffer); - state->column += 1; - state->begin_line = false; + utf8proc_encode_char(c, renderer->buffer); + renderer->column += 1; + renderer->begin_line = false; } } } @@ -248,7 +248,7 @@ get_containing_block(cmark_node *node) static int S_render_node(cmark_node *node, cmark_event_type ev_type, - cmark_render_state *state) + cmark_renderer *renderer) { cmark_node *tmp; int list_number; @@ -266,7 +266,7 @@ S_render_node(cmark_node *node, cmark_event_type ev_type, if (!(node->type == CMARK_NODE_ITEM && node->prev == NULL && entering)) { tmp = get_containing_block(node); - state->in_tight_list_item = + renderer->in_tight_list_item = (tmp->type == CMARK_NODE_ITEM && cmark_node_get_list_tight(tmp->parent)) || (tmp && @@ -278,17 +278,17 @@ S_render_node(cmark_node *node, cmark_event_type ev_type, switch (node->type) { case CMARK_NODE_DOCUMENT: if (!entering) { - cmark_strbuf_putc(state->buffer, '\n'); + cmark_strbuf_putc(renderer->buffer, '\n'); } break; case CMARK_NODE_BLOCK_QUOTE: if (entering) { - lit(state, "\\begin{quote}", false); - cr(state); + lit(renderer, "\\begin{quote}", false); + cr(renderer); } else { - lit(state, "\\end{quote}", false); - blankline(state); + lit(renderer, "\\end{quote}", false); + blankline(renderer); } break; @@ -296,46 +296,46 @@ S_render_node(cmark_node *node, cmark_event_type ev_type, list_type = cmark_node_get_list_type(node); if (entering) { if (list_type == CMARK_ORDERED_LIST) { - state->enumlevel++; + renderer->enumlevel++; } - lit(state, "\\begin{", false); - lit(state, + lit(renderer, "\\begin{", false); + lit(renderer, list_type == CMARK_ORDERED_LIST ? "enumerate" : "itemize", false); - lit(state, "}", false); - cr(state); + lit(renderer, "}", false); + cr(renderer); list_number = cmark_node_get_list_start(node); if (list_number > 1) { sprintf(list_number_string, "%d", list_number); - lit(state, "\\setcounter{enum", false); - lit(state, (char *)roman_numerals[state->enumlevel], + lit(renderer, "\\setcounter{enum", false); + lit(renderer, (char *)roman_numerals[renderer->enumlevel], false); - lit(state, "}{", false); - out(state, + lit(renderer, "}{", false); + out(renderer, cmark_chunk_literal(list_number_string), false, NORMAL); - lit(state, "}", false); - cr(state); + lit(renderer, "}", false); + cr(renderer); } } else { if (list_type == CMARK_ORDERED_LIST) { - state->enumlevel--; + renderer->enumlevel--; } - lit(state, "\\end{", false); - lit(state, + lit(renderer, "\\end{", false); + lit(renderer, list_type == CMARK_ORDERED_LIST ? "enumerate" : "itemize", false); - lit(state, "}", false); - blankline(state); + lit(renderer, "}", false); + blankline(renderer); } break; case CMARK_NODE_ITEM: if (entering) { - lit(state, "\\item ", false); + lit(renderer, "\\item ", false); } else { - cr(state); + cr(renderer); } break; @@ -343,74 +343,74 @@ S_render_node(cmark_node *node, cmark_event_type ev_type, if (entering) { switch (cmark_node_get_header_level(node)) { case 1: - lit(state, "\\section", false); + lit(renderer, "\\section", false); break; case 2: - lit(state, "\\subsection", false); + lit(renderer, "\\subsection", false); break; case 3: - lit(state, "\\subsubsection", false); + lit(renderer, "\\subsubsection", false); break; case 4: - lit(state, "\\paragraph", false); + lit(renderer, "\\paragraph", false); break; case 5: - lit(state, "\\subparagraph", false); + lit(renderer, "\\subparagraph", false); break; } - lit(state, "{", false); + lit(renderer, "{", false); } else { - lit(state, "}", false); - blankline(state); + lit(renderer, "}", false); + blankline(renderer); } break; case CMARK_NODE_CODE_BLOCK: - cr(state); - lit(state, "\\begin{verbatim}", false); - cr(state); - out(state, node->as.code.literal, false, LITERAL); - cr(state); - lit(state, "\\end{verbatim}", false); - blankline(state); + cr(renderer); + lit(renderer, "\\begin{verbatim}", false); + cr(renderer); + out(renderer, node->as.code.literal, false, LITERAL); + cr(renderer); + lit(renderer, "\\end{verbatim}", false); + blankline(renderer); break; case CMARK_NODE_HTML: break; case CMARK_NODE_HRULE: - blankline(state); - lit(state, "\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}", false); - blankline(state); + blankline(renderer); + lit(renderer, "\\begin{center}\\rule{0.5\\linewidth}{\\linethickness}\\end{center}", false); + blankline(renderer); break; case CMARK_NODE_PARAGRAPH: if (!entering) { - blankline(state); + blankline(renderer); } break; case CMARK_NODE_TEXT: - out(state, node->as.literal, true, NORMAL); + out(renderer, node->as.literal, true, NORMAL); break; case CMARK_NODE_LINEBREAK: - lit(state, "\\\\", false); - cr(state); + lit(renderer, "\\\\", false); + cr(renderer); break; case CMARK_NODE_SOFTBREAK: - if (state->width == 0) { - cr(state); + if (renderer->width == 0) { + cr(renderer); } else { - lit(state, " ", true); + lit(renderer, " ", true); } break; case CMARK_NODE_CODE: - lit(state, "\\texttt{", false); - out(state, node->as.literal, false, NORMAL); - lit(state, "}", false); + lit(renderer, "\\texttt{", false); + out(renderer, node->as.literal, false, NORMAL); + lit(renderer, "}", false); break; case CMARK_NODE_INLINE_HTML: @@ -418,17 +418,17 @@ S_render_node(cmark_node *node, cmark_event_type ev_type, case CMARK_NODE_STRONG: if (entering) { - lit(state, "\\textbf{", false); + lit(renderer, "\\textbf{", false); } else { - lit(state, "}", false); + lit(renderer, "}", false); } break; case CMARK_NODE_EMPH: if (entering) { - lit(state, "\\emph{", false); + lit(renderer, "\\emph{", false); } else { - lit(state, "}", false); + lit(renderer, "}", false); } break; @@ -438,24 +438,24 @@ S_render_node(cmark_node *node, cmark_event_type ev_type, // requires \usepackage{hyperref} switch(get_link_type(node)) { case URL_AUTOLINK: - lit(state, "\\url{", false); - out(state, url, false, URL); + lit(renderer, "\\url{", false); + out(renderer, url, false, URL); break; case EMAIL_AUTOLINK: - lit(state, "\\href{", false); - out(state, url, false, URL); - lit(state, "}\\nolinkurl{", false); + lit(renderer, "\\href{", false); + out(renderer, url, false, URL); + lit(renderer, "}\\nolinkurl{", false); break; case NORMAL_LINK: - lit(state, "\\href{", false); - out(state, url, false, URL); - lit(state, "}{", false); + lit(renderer, "\\href{", false); + out(renderer, url, false, URL); + lit(renderer, "}{", false); break; case NO_LINK: - lit(state, "{", false); // error? + lit(renderer, "{", false); // error? } } else { - lit(state, "}", false); + lit(renderer, "}", false); } break; @@ -463,10 +463,10 @@ S_render_node(cmark_node *node, cmark_event_type ev_type, case CMARK_NODE_IMAGE: if (entering) { url = cmark_chunk_literal(cmark_node_get_url(node)); - lit(state, "\\protect\\includegraphics{", false); + lit(renderer, "\\protect\\includegraphics{", false); // requires \include{graphicx} - out(state, url, false, URL); - lit(state, "}", false); + out(renderer, url, false, URL); + lit(renderer, "}", false); return 0; } break; diff --git a/src/render.c b/src/render.c index f4c56ac..442a7fc 100644 --- a/src/render.c +++ b/src/render.c @@ -5,21 +5,21 @@ #include "utf8.h" #include "render.h" -void cr(cmark_render_state *state) +void cr(cmark_renderer *renderer) { - if (state->need_cr < 1) { - state->need_cr = 1; + if (renderer->need_cr < 1) { + renderer->need_cr = 1; } } -void blankline(cmark_render_state *state) +void blankline(cmark_renderer *renderer) { - if (state->need_cr < 2) { - state->need_cr = 2; + if (renderer->need_cr < 2) { + renderer->need_cr = 2; } } -void out(cmark_render_state *state, +void out(cmark_renderer *renderer, cmark_chunk str, bool wrap, cmark_escaping escape) @@ -31,34 +31,34 @@ void out(cmark_render_state *state, int i = 0; int len; cmark_chunk remainder = cmark_chunk_literal(""); - int k = state->buffer->size - 1; + int k = renderer->buffer->size - 1; - wrap = wrap && !state->no_wrap; + wrap = wrap && !renderer->no_wrap; - if (state->in_tight_list_item && state->need_cr > 1) { - state->need_cr = 1; + if (renderer->in_tight_list_item && renderer->need_cr > 1) { + renderer->need_cr = 1; } - while (state->need_cr) { - if (k < 0 || state->buffer->ptr[k] == '\n') { + while (renderer->need_cr) { + if (k < 0 || renderer->buffer->ptr[k] == '\n') { k -= 1; } else { - cmark_strbuf_putc(state->buffer, '\n'); - if (state->need_cr > 1) { - cmark_strbuf_put(state->buffer, state->prefix->ptr, - state->prefix->size); + cmark_strbuf_putc(renderer->buffer, '\n'); + if (renderer->need_cr > 1) { + cmark_strbuf_put(renderer->buffer, renderer->prefix->ptr, + renderer->prefix->size); } } - state->column = 0; - state->begin_line = true; - state->need_cr -= 1; + renderer->column = 0; + renderer->begin_line = true; + renderer->need_cr -= 1; } while (i < length) { - if (state->begin_line) { - cmark_strbuf_put(state->buffer, state->prefix->ptr, - state->prefix->size); + if (renderer->begin_line) { + cmark_strbuf_put(renderer->buffer, renderer->prefix->ptr, + renderer->prefix->size); // note: this assumes prefix is ascii: - state->column = state->prefix->size; + renderer->column = renderer->prefix->size; } len = utf8proc_iterate(source + i, length - i, &c); @@ -67,11 +67,11 @@ void out(cmark_render_state *state, } nextc = source[i + len]; if (c == 32 && wrap) { - if (!state->begin_line) { - cmark_strbuf_putc(state->buffer, ' '); - state->column += 1; - state->begin_line = false; - state->last_breakable = state->buffer->size - + if (!renderer->begin_line) { + cmark_strbuf_putc(renderer->buffer, ' '); + renderer->column += 1; + renderer->begin_line = false; + renderer->last_breakable = renderer->buffer->size - 1; // skip following spaces while (source[i + 1] == ' ') { @@ -80,57 +80,57 @@ void out(cmark_render_state *state, } } else if (c == 10) { - cmark_strbuf_putc(state->buffer, '\n'); - state->column = 0; - state->begin_line = true; - state->last_breakable = 0; + cmark_strbuf_putc(renderer->buffer, '\n'); + renderer->column = 0; + renderer->begin_line = true; + renderer->last_breakable = 0; } else { - (state->outc)(state, escape, c, nextc); + (renderer->outc)(renderer, escape, c, nextc); } // If adding the character went beyond width, look for an // earlier place where the line could be broken: - if (state->width > 0 && - state->column > state->width && - !state->begin_line && - state->last_breakable > 0) { + if (renderer->width > 0 && + renderer->column > renderer->width && + !renderer->begin_line && + renderer->last_breakable > 0) { // copy from last_breakable to remainder - cmark_chunk_set_cstr(&remainder, (char *) state->buffer->ptr + state->last_breakable + 1); + cmark_chunk_set_cstr(&remainder, (char *) renderer->buffer->ptr + renderer->last_breakable + 1); // truncate at last_breakable - cmark_strbuf_truncate(state->buffer, state->last_breakable); + cmark_strbuf_truncate(renderer->buffer, renderer->last_breakable); // add newline, prefix, and remainder - cmark_strbuf_putc(state->buffer, '\n'); - cmark_strbuf_put(state->buffer, state->prefix->ptr, - state->prefix->size); - cmark_strbuf_put(state->buffer, remainder.data, remainder.len); - state->column = state->prefix->size + remainder.len; + cmark_strbuf_putc(renderer->buffer, '\n'); + cmark_strbuf_put(renderer->buffer, renderer->prefix->ptr, + renderer->prefix->size); + cmark_strbuf_put(renderer->buffer, remainder.data, remainder.len); + renderer->column = renderer->prefix->size + remainder.len; cmark_chunk_free(&remainder); - state->last_breakable = 0; - state->begin_line = false; + renderer->last_breakable = 0; + renderer->begin_line = false; } i += len; } } -void lit(cmark_render_state *state, char *s, bool wrap) +void lit(cmark_renderer *renderer, char *s, bool wrap) { cmark_chunk str = cmark_chunk_literal(s); - out(state, str, wrap, LITERAL); + out(renderer, str, wrap, LITERAL); } char* cmark_render(cmark_node *root, int options, int width, - void (*outc)(cmark_render_state*, + void (*outc)(cmark_renderer*, cmark_escaping, int32_t, unsigned char), int (*render_node)(cmark_node *node, cmark_event_type ev_type, - cmark_render_state *state)) + cmark_renderer *renderer)) { cmark_strbuf pref = GH_BUF_INIT; cmark_strbuf buf = GH_BUF_INIT; @@ -144,12 +144,12 @@ cmark_render(cmark_node *root, width = 0; } - cmark_render_state state = { options, &buf, &pref, 0, width, + cmark_renderer renderer = { options, &buf, &pref, 0, width, 0, 0, 0, true, false, false, outc }; while ((ev_type = cmark_iter_next(iter)) != CMARK_EVENT_DONE) { cur = cmark_iter_get_node(iter); - if (!render_node(cur, ev_type, &state)) { + if (!render_node(cur, ev_type, &renderer)) { // a false value causes us to skip processing // the node's contents. this is used for // autolinks. @@ -157,11 +157,11 @@ cmark_render(cmark_node *root, } } - result = (char *)cmark_strbuf_detach(state.buffer); + result = (char *)cmark_strbuf_detach(renderer.buffer); cmark_iter_free(iter); - cmark_strbuf_free(state.prefix); - cmark_strbuf_free(state.buffer); + cmark_strbuf_free(renderer.prefix); + cmark_strbuf_free(renderer.buffer); return result; } diff --git a/src/render.h b/src/render.h index 6c268a1..718050e 100644 --- a/src/render.h +++ b/src/render.h @@ -16,7 +16,7 @@ typedef enum { URL } cmark_escaping; -struct cmark_render_state { +struct cmark_renderer { int options; cmark_strbuf* buffer; cmark_strbuf* prefix; @@ -28,36 +28,36 @@ struct cmark_render_state { bool begin_line; bool no_wrap; bool in_tight_list_item; - void (*outc)(struct cmark_render_state*, + void (*outc)(struct cmark_renderer*, cmark_escaping, int32_t, unsigned char); }; -typedef struct cmark_render_state cmark_render_state; +typedef struct cmark_renderer cmark_renderer; -void cr(cmark_render_state *state); +void cr(cmark_renderer *renderer); -void blankline(cmark_render_state *state); +void blankline(cmark_renderer *renderer); -void out(cmark_render_state *state, +void out(cmark_renderer *renderer, cmark_chunk str, bool wrap, cmark_escaping escape); -void lit(cmark_render_state *state, char *s, bool wrap); +void lit(cmark_renderer *renderer, char *s, bool wrap); char* cmark_render(cmark_node *root, int options, int width, - void (*outc)(cmark_render_state*, + void (*outc)(cmark_renderer*, cmark_escaping, int32_t, unsigned char), int (*render_node)(cmark_node *node, cmark_event_type ev_type, - cmark_render_state *state)); + cmark_renderer *renderer)); #ifdef __cplusplus |