Welcome to little lamb

Code » md4c » commit 48a215a

Add HIGHLIGHT extension so =foo= gets highlighted

author Olivier Brunel
2022-12-29 20:44:22 UTC
committer Olivier Brunel
2023-07-19 12:47:28 UTC
parent a99dc05873ebdb765fa652bbee1fae631e8d3c1a

Add HIGHLIGHT extension so =foo= gets highlighted

src/md4c.c +34 -3
src/md4c.h +6 -0

diff --git a/src/md4c.c b/src/md4c.c
index d000393..136edce 100644
--- a/src/md4c.c
+++ b/src/md4c.c
@@ -178,7 +178,7 @@ struct MD_CTX_tag {
 #endif
 
     /* For resolving of inline spans. */
-    MD_MARKCHAIN mark_chains[14];
+    MD_MARKCHAIN mark_chains[15];
 #define PTR_CHAIN                               (ctx->mark_chains[0])
 #define TABLECELLBOUNDARIES                     (ctx->mark_chains[1])
 #define ASTERISK_OPENERS_extraword_mod3_0       (ctx->mark_chains[2])
@@ -193,8 +193,9 @@ struct MD_CTX_tag {
 #define BRACKET_OPENERS                         (ctx->mark_chains[11])
 #define DOLLAR_OPENERS                          (ctx->mark_chains[12])
 #define ITALIC_OPENERS                          (ctx->mark_chains[13])
+#define HIGHLIGHT_OPENERS                       (ctx->mark_chains[14])
 #define OPENERS_CHAIN_FIRST                     1
-#define OPENERS_CHAIN_LAST                      13
+#define OPENERS_CHAIN_LAST                      14
 
     int n_table_cell_boundaries;
 
@@ -2437,6 +2438,7 @@ md_free_ref_defs(MD_CTX* ctx)
  *  '_': Maybe (strong) emphasis start/end.
  *  '~': Maybe strikethrough start/end (needs MD_FLAG_STRIKETHROUGH).
  *  '/': Maybe italic start/end (needs MD_FLAG_ITALIC).
+ *  '=': Maybe highlight start/end (needs MD_FLAG_HIGHLIGHT).
  *  '`': Maybe code span start/end.
  *  '&': Maybe start of entity.
  *  ';': Maybe end of entity.
@@ -2517,6 +2519,7 @@ md_mark_chain(MD_CTX* ctx, int mark_index)
         case _T('_'):   return &UNDERSCORE_OPENERS;
         case _T('~'):   return (mark->end - mark->beg == 1) ? &TILDE_OPENERS_1 : &TILDE_OPENERS_2;
         case _T('/'):   return &ITALIC_OPENERS;
+        case _T('='):   return &HIGHLIGHT_OPENERS;
         case _T('!'):   MD_FALLTHROUGH();
         case _T('['):   return &BRACKET_OPENERS;
         case _T('|'):   return &TABLECELLBOUNDARIES;
@@ -2745,6 +2748,9 @@ md_build_mark_char_map(MD_CTX* ctx)
     if(ctx->parser.flags & MD_FLAG_ITALIC)
         ctx->mark_char_map['/'] = 1;
 
+    if(ctx->parser.flags & MD_FLAG_HIGHLIGHT)
+        ctx->mark_char_map['='] = 1;
+
     if(ctx->parser.flags & MD_FLAG_LATEXMATHSPANS)
         ctx->mark_char_map['$'] = 1;
 
@@ -3377,6 +3383,23 @@ md_collect_marks(MD_CTX* ctx, const MD_LINE* lines, int n_lines, int table_mode)
                 continue;
             }
 
+            /* A potential hghlight start/end. */
+            if(ch == _T('=')) {
+                OFF tmp = off+1;
+
+                while(tmp < line_end  &&  CH(tmp) == _T('='))
+                    tmp++;
+
+                if(tmp - off < 3) {
+                    unsigned flags = md_get_potential_flags(ctx, off, tmp, line->beg, line_end);
+                    if(flags != 0)
+                        PUSH_MARK(ch, off, tmp, flags);
+                }
+
+                off = tmp;
+                continue;
+            }
+
             /* A potential equation start/end */
             if(ch == _T('$')) {
                 /* We can have at most two consecutive $ signs,
@@ -4095,6 +4118,7 @@ md_analyze_marks(MD_CTX* ctx, const MD_LINE* lines, int n_lines,
             case '_':   /* Pass through. */
             case '*':   md_analyze_emph(ctx, i); break;
             case '/':   /* Pass through */
+            case '=':   /* Pass through */
             case '~':   md_analyze_tilde(ctx, i); break;
             case '$':   md_analyze_dollar(ctx, i); break;
             case '.':   /* Pass through. */
@@ -4152,7 +4176,7 @@ md_analyze_link_contents(MD_CTX* ctx, const MD_LINE* lines, int n_lines,
     int i;
 
     md_analyze_marks(ctx, lines, n_lines, mark_beg, mark_end, _T("&"));
-    md_analyze_marks(ctx, lines, n_lines, mark_beg, mark_end, _T("*_~$@:./"));
+    md_analyze_marks(ctx, lines, n_lines, mark_beg, mark_end, _T("*_~$@:./="));
 
     for(i = OPENERS_CHAIN_FIRST; i <= OPENERS_CHAIN_LAST; i++) {
         ctx->mark_chains[i].head = -1;
@@ -4318,6 +4342,13 @@ md_process_inlines(MD_CTX* ctx, const MD_LINE* lines, int n_lines)
                         MD_LEAVE_SPAN(MD_SPAN_EM, NULL);
                     break;
 
+                case '=':
+                    if(mark->flags & MD_MARK_OPENER)
+                        MD_ENTER_SPAN(MD_SPAN_HIGHLIGHT, NULL);
+                    else
+                        MD_LEAVE_SPAN(MD_SPAN_HIGHLIGHT, NULL);
+                    break;
+
                 case '~':
                     if(mark->flags & MD_MARK_OPENER)
                         MD_ENTER_SPAN(MD_SPAN_DEL, NULL);
diff --git a/src/md4c.h b/src/md4c.h
index dd633e8..c1b8b3e 100644
--- a/src/md4c.h
+++ b/src/md4c.h
@@ -135,6 +135,11 @@ typedef enum MD_SPANTYPE {
      */
     MD_SPAN_DEL,
 
+    /* <span class="highlight">...</span>
+     * Note: Recognized only when MD_FLAG_HIGHLIGHT is enabled.
+     */
+    MD_SPAN_HIGHLIGHT,
+
     /* For recognizing inline ($) and display ($$) equations
      * Note: Recognized only when MD_FLAG_LATEXMATHSPANS is enabled.
      */
@@ -328,6 +333,7 @@ typedef struct MD_SPAN_WIKILINK {
 #define MD_FLAG_ITALIC                      0x8000  /* Enable italic (/foo/) extension. */
 #define MD_FLAG_BOLD                        0x10000 /* Enable bold extension */
 #define MD_FLAG_BOX                         0x20000 /* Enable box extension */
+#define MD_FLAG_HIGHLIGHT                   0x40000 /* Enable highlight extension */
 
 #define MD_FLAG_PERMISSIVEAUTOLINKS         (MD_FLAG_PERMISSIVEEMAILAUTOLINKS | MD_FLAG_PERMISSIVEURLAUTOLINKS | MD_FLAG_PERMISSIVEWWWAUTOLINKS)
 #define MD_FLAG_NOHTML                      (MD_FLAG_NOHTMLBLOCKS | MD_FLAG_NOHTMLSPANS)