Welcome to little lamb

Code » md4c » commit 3c914ff

Add ITALIC extension: Use /foo/ to put in italic

author Olivier Brunel
2022-12-28 10:37:12 UTC
committer Olivier Brunel
2022-12-28 10:37:12 UTC
parent e9ff661ff818ee94a4a231958d9b6768dc6882c9

Add ITALIC extension: Use /foo/ to put in italic

src/md4c.c +39 -3
src/md4c.h +1 -0

diff --git a/src/md4c.c b/src/md4c.c
index 3677c0e..f479d38 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[13];
+    MD_MARKCHAIN mark_chains[14];
 #define PTR_CHAIN                               (ctx->mark_chains[0])
 #define TABLECELLBOUNDARIES                     (ctx->mark_chains[1])
 #define ASTERISK_OPENERS_extraword_mod3_0       (ctx->mark_chains[2])
@@ -192,8 +192,9 @@ struct MD_CTX_tag {
 #define TILDE_OPENERS_2                         (ctx->mark_chains[10])
 #define BRACKET_OPENERS                         (ctx->mark_chains[11])
 #define DOLLAR_OPENERS                          (ctx->mark_chains[12])
+#define ITALIC_OPENERS                          (ctx->mark_chains[13])
 #define OPENERS_CHAIN_FIRST                     1
-#define OPENERS_CHAIN_LAST                      12
+#define OPENERS_CHAIN_LAST                      13
 
     int n_table_cell_boundaries;
 
@@ -2434,6 +2435,7 @@ md_free_ref_defs(MD_CTX* ctx)
  *  '*': Maybe (strong) emphasis start/end.
  *  '_': Maybe (strong) emphasis start/end.
  *  '~': Maybe strikethrough start/end (needs MD_FLAG_STRIKETHROUGH).
+ *  '/': Maybe italic start/end (needs MD_FLAG_ITALIC).
  *  '`': Maybe code span start/end.
  *  '&': Maybe start of entity.
  *  ';': Maybe end of entity.
@@ -2513,6 +2515,7 @@ md_mark_chain(MD_CTX* ctx, int mark_index)
         case _T('*'):   return md_asterisk_chain(ctx, mark->flags);
         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('!'):   MD_FALLTHROUGH();
         case _T('['):   return &BRACKET_OPENERS;
         case _T('|'):   return &TABLECELLBOUNDARIES;
@@ -2738,6 +2741,9 @@ md_build_mark_char_map(MD_CTX* ctx)
     if(ctx->parser.flags & MD_FLAG_STRIKETHROUGH)
         ctx->mark_char_map['~'] = 1;
 
+    if(ctx->parser.flags & MD_FLAG_ITALIC)
+        ctx->mark_char_map['/'] = 1;
+
     if(ctx->parser.flags & MD_FLAG_LATEXMATHSPANS)
         ctx->mark_char_map['$'] = 1;
 
@@ -3317,6 +3323,28 @@ md_collect_marks(MD_CTX* ctx, const MD_LINE* lines, int n_lines, int table_mode)
                 continue;
             }
 
+            /* A potential italic start/end. */
+            if(ch == _T('/')) {
+                OFF tmp = off+1;
+
+                while(tmp < line_end  &&  CH(tmp) == _T('/'))
+                    tmp++;
+
+                if(tmp - off < 3) {
+                    unsigned flags = 0;
+
+                    if(tmp < line_end  &&  !ISUNICODEWHITESPACE(tmp))
+                        flags |= MD_MARK_POTENTIAL_OPENER;
+                    if(off > line->beg  &&  !ISUNICODEWHITESPACEBEFORE(off))
+                        flags |= MD_MARK_POTENTIAL_CLOSER;
+                    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,
@@ -4034,6 +4062,7 @@ md_analyze_marks(MD_CTX* ctx, const MD_LINE* lines, int n_lines,
             case '|':   md_analyze_table_cell_boundary(ctx, i); break;
             case '_':   /* Pass through. */
             case '*':   md_analyze_emph(ctx, i); break;
+            case '/':   /* Pass through */
             case '~':   md_analyze_tilde(ctx, i); break;
             case '$':   md_analyze_dollar(ctx, i); break;
             case '.':   /* Pass through. */
@@ -4091,7 +4120,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;
@@ -4243,6 +4272,13 @@ md_process_inlines(MD_CTX* ctx, const MD_LINE* lines, int n_lines)
                     }
                     break;
 
+                case '/':
+                    if(mark->flags & MD_MARK_OPENER)
+                        MD_ENTER_SPAN(MD_SPAN_EM, NULL);
+                    else
+                        MD_LEAVE_SPAN(MD_SPAN_EM, 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 95f78f9..38619eb 100644
--- a/src/md4c.h
+++ b/src/md4c.h
@@ -316,6 +316,7 @@ typedef struct MD_SPAN_WIKILINK {
 #define MD_FLAG_LATEXMATHSPANS              0x1000  /* Enable $ and $$ containing LaTeX equations. */
 #define MD_FLAG_WIKILINKS                   0x2000  /* Enable wiki links extension. */
 #define MD_FLAG_UNDERLINE                   0x4000  /* Enable underline extension (and disables '_' for normal emphasis). */
+#define MD_FLAG_ITALIC                      0x8000  /* Enable italic (/foo/) extension. */
 
 #define MD_FLAG_PERMISSIVEAUTOLINKS         (MD_FLAG_PERMISSIVEEMAILAUTOLINKS | MD_FLAG_PERMISSIVEURLAUTOLINKS | MD_FLAG_PERMISSIVEWWWAUTOLINKS)
 #define MD_FLAG_NOHTML                      (MD_FLAG_NOHTMLBLOCKS | MD_FLAG_NOHTMLSPANS)