From: Fletcher T. Penney Date: Thu, 4 May 2017 18:24:23 +0000 (-0400) Subject: CHANGED: Restructure strong/emph to a nested structure X-Git-Tag: 6.0.6^2~16 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=69a9311451367d0603ddd8c5ff219c3cc5119fa3;p=multimarkdown CHANGED: Restructure strong/emph to a nested structure --- diff --git a/Sources/libMultiMarkdown/html.c b/Sources/libMultiMarkdown/html.c index f252d37..1a5b21b 100644 --- a/Sources/libMultiMarkdown/html.c +++ b/Sources/libMultiMarkdown/html.c @@ -1508,11 +1508,13 @@ void mmd_export_token_html(DString * out, const char * source, token * t, scratc case PAIR_HTML_COMMENT: print_token(t); break; + case PAIR_EMPH: case PAIR_MATH: case PAIR_PAREN: case PAIR_QUOTE_DOUBLE: case PAIR_QUOTE_SINGLE: case PAIR_STAR: + case PAIR_STRONG: case PAIR_UL: mmd_export_token_tree_html(out, source, t->child, scratch); break; diff --git a/Sources/libMultiMarkdown/include/libMultiMarkdown.h b/Sources/libMultiMarkdown/include/libMultiMarkdown.h index 2950a1a..649706f 100644 --- a/Sources/libMultiMarkdown/include/libMultiMarkdown.h +++ b/Sources/libMultiMarkdown/include/libMultiMarkdown.h @@ -202,6 +202,7 @@ enum token_types { PAIR_BRACKET_CITATION, PAIR_BRACKET_IMAGE, PAIR_BRACKET_VARIABLE, + PAIR_EMPH, PAIR_MATH, PAIR_PAREN, PAIR_QUOTE_SINGLE, @@ -209,6 +210,7 @@ enum token_types { PAIR_QUOTE_ALT, PAIR_SUPERSCRIPT, PAIR_STAR, + PAIR_STRONG, PAIR_UL, PAIR_BRACES, diff --git a/Sources/libMultiMarkdown/latex.c b/Sources/libMultiMarkdown/latex.c index 309f92f..836bdf4 100644 --- a/Sources/libMultiMarkdown/latex.c +++ b/Sources/libMultiMarkdown/latex.c @@ -1460,11 +1460,13 @@ void mmd_export_token_latex(DString * out, const char * source, token * t, scrat if (strncmp(&source[t->child->start + t->child->len], "\\begin", 6) != 0) mmd_export_token_latex(out, source, t->child->mate, scratch); - break; + break; + case PAIR_EMPH: case PAIR_PAREN: case PAIR_QUOTE_DOUBLE: case PAIR_QUOTE_SINGLE: case PAIR_STAR: + case PAIR_STRONG: case PAIR_UL: mmd_export_token_tree_latex(out, source, t->child, scratch); break; diff --git a/Sources/libMultiMarkdown/mmd.c b/Sources/libMultiMarkdown/mmd.c index 399d0c2..e6f3704 100644 --- a/Sources/libMultiMarkdown/mmd.c +++ b/Sources/libMultiMarkdown/mmd.c @@ -1432,9 +1432,12 @@ void pair_emphasis_tokens(token * t) { tokens_prune(t->next, t->next); tokens_prune(closer->prev, closer->prev); + + token_prune_graft(t, closer, PAIR_STRONG); } else { t->type = EMPH_START; closer->type = EMPH_STOP; + token_prune_graft(t, closer, PAIR_EMPH); } break; @@ -1668,17 +1671,12 @@ void parse_table_row_into_cells(token * row) { last = first; } - walker = walker->next; while (walker) { switch (walker->type) { case PIPE: - if (row->child == first) { - row->child = token_prune_graft(first, last, TABLE_CELL); - } else { - token_prune_graft(first, last, TABLE_CELL); - } + token_prune_graft(first, last, TABLE_CELL); first = NULL; last = NULL; walker->type = TABLE_DIVIDER; @@ -1696,11 +1694,7 @@ void parse_table_row_into_cells(token * row) { } if (first) { - if (row->child == first) { - row->child = token_prune_graft(first, last, TABLE_CELL); - } else { - token_prune_graft(first, last, TABLE_CELL); - } + token_prune_graft(first, last, TABLE_CELL); } } diff --git a/Sources/libMultiMarkdown/odf.c b/Sources/libMultiMarkdown/odf.c index 7eaf5e8..dccb0b3 100644 --- a/Sources/libMultiMarkdown/odf.c +++ b/Sources/libMultiMarkdown/odf.c @@ -1384,11 +1384,13 @@ void mmd_export_token_odf(DString * out, const char * source, token * t, scratch break; case PAIR_HTML_COMMENT: break; + case PAIR_EMPH: case PAIR_MATH: case PAIR_PAREN: case PAIR_QUOTE_DOUBLE: case PAIR_QUOTE_SINGLE: case PAIR_STAR: + case PAIR_STRONG: case PAIR_UL: mmd_export_token_tree_odf(out, source, t->child, scratch); break; diff --git a/Sources/libMultiMarkdown/token.c b/Sources/libMultiMarkdown/token.c index 32a719f..a1a1b50 100644 --- a/Sources/libMultiMarkdown/token.c +++ b/Sources/libMultiMarkdown/token.c @@ -142,6 +142,22 @@ token * token_new(unsigned short type, size_t start, size_t len) { } +/// Duplicate an existing token +token * token_copy(token * original) { +#ifdef kUseObjectPool + token * t = pool_allocate_object(token_pool); +#else + token * t = malloc(sizeof(token)); +#endif + + if (t) { + * t = * original; + } + + return t; +} + + /// Create a parent for a chain of tokens token * token_new_parent(token * child, unsigned short type) { if (child == NULL) { @@ -307,34 +323,39 @@ token * token_prune_graft(token * first, token * last, unsigned short container_ token * prev = first->prev; token * next = last->next; - // If we are head of chain, remember tail - token * tail = NULL; - if (prev == NULL) - tail = first->tail; - - - token * container = token_new(container_type, first->start, last->start + last->len - first->start); - - container->child = first; - container->next = next; - container->prev = prev; - container->can_close = 0; - container->can_open = 0; - - if (tail) - container->tail = tail; - - if (prev) - prev->next = container; + // Duplicate first token -- this will be child of new container + token * new_child = token_copy(first); + new_child->prev = NULL; + new_child->tail = last; + if (new_child->next) { + new_child->next->prev = new_child; + } - first->prev = NULL; + // Swap last (if necessary) + if (first == last) + last = new_child; + + // Existing first token will be new container + first->child = new_child; + first->type = container_type; + first->len = last->start + last->len - first->start; + first->next = next; + first->can_close = 0; + first->can_open = 0; + + // Fix mating + if (first->mate) { + first->mate = NULL; + new_child->mate->mate = new_child; + } + // Disconnect last token last->next = NULL; if (next) - next->prev = container; + next->prev = first; - return container; + return first; } diff --git a/Sources/libMultiMarkdown/writer.c b/Sources/libMultiMarkdown/writer.c index 8947517..7eac17b 100644 --- a/Sources/libMultiMarkdown/writer.c +++ b/Sources/libMultiMarkdown/writer.c @@ -336,6 +336,10 @@ void print_token_raw(DString * out, const char * source, token * t) { case STRONG_STOP: case TEXT_EMPTY: break; + case PAIR_EMPH: + case PAIR_STRONG: + print_token_tree_raw(out, source, t->child); + break; default: d_string_append_c_array(out, &source[t->start], t->len); break;