Commit aeddaf587f5e3faceb2d88a45ef7987bdebfe837

Martin Mitas 2024-01-25T22:24:17

Simplify and fix handling of newline in code span. Fixes #223 properly (one corner case has been unnoticed/hidden due test suite normalization feature). Fixes #230 (strictly speaking duplicate of the corner case).

diff --git a/src/md4c.c b/src/md4c.c
index 92ebe63..5075e0f 100644
--- a/src/md4c.c
+++ b/src/md4c.c
@@ -2506,7 +2506,6 @@ struct MD_MARK_tag {
 #define MD_MARK_RESOLVED                    0x10  /* Resolved in any definite way. */
 
 /* Mark flags specific for various mark types (so they can share bits). */
-#define MD_MARK_CODE_EXTRA_SPACE            0x20
 #define MD_MARK_EMPH_OC                     0x20  /* Opener/closer mixed candidate. Helper for the "rule of 3". */
 #define MD_MARK_EMPH_MOD3_0                 0x40
 #define MD_MARK_EMPH_MOD3_1                 0x80
@@ -2754,8 +2753,6 @@ md_is_code_span(MD_CTX* ctx, const MD_LINE* lines, int n_lines, OFF beg,
     int has_space_before_closer = FALSE;
     int has_eol_before_closer = FALSE;
     int has_only_space = TRUE;
-    int opener_needs_extra_space = FALSE;
-    int closer_needs_extra_space = FALSE;
     int line_index = 0;
 
     line_end = lines[0].end;
@@ -2842,27 +2839,20 @@ md_is_code_span(MD_CTX* ctx, const MD_LINE* lines, int n_lines, OFF beg,
         if(has_space_before_closer)
             closer_beg--;
         else {
+            /* Go back to the end of prev line */
             closer_beg = lines[line_index-1].end;
-            /* We need to eat the preceding "\r\n" but not any line trailing
-             * spaces. */
+            /* But restore any trailing whitespace */
             while(closer_beg < ctx->size  &&  ISBLANK(closer_beg))
                 closer_beg++;
         }
-    } else {
-        if(has_eol_after_opener)
-            opener_needs_extra_space = TRUE;
-        if(has_eol_before_closer)
-            closer_needs_extra_space = TRUE;
     }
 
     opener->ch = _T('`');
     opener->beg = opener_beg;
     opener->end = opener_end;
-    opener->flags = (opener_needs_extra_space ? MD_MARK_CODE_EXTRA_SPACE : 0);
     closer->ch = _T('`');
     closer->beg = closer_beg;
     closer->end = closer_end;
-    closer->flags = (closer_needs_extra_space ? MD_MARK_CODE_EXTRA_SPACE : 0);
     return TRUE;
 }
 
@@ -4248,11 +4238,7 @@ md_process_inlines(MD_CTX* ctx, const MD_LINE* lines, int n_lines)
                     if(mark->flags & MD_MARK_OPENER) {
                         MD_ENTER_SPAN(MD_SPAN_CODE, NULL);
                         text_type = MD_TEXT_CODE;
-                        if(mark->flags & MD_MARK_CODE_EXTRA_SPACE)
-                            MD_TEXT(text_type, _T(" "), 1);
                     } else {
-                        if(mark->flags & MD_MARK_CODE_EXTRA_SPACE)
-                            MD_TEXT(text_type, _T(" "), 1);
                         MD_LEAVE_SPAN(MD_SPAN_CODE, NULL);
                         text_type = MD_TEXT_NORMAL;
                     }
@@ -4454,7 +4440,7 @@ md_process_inlines(MD_CTX* ctx, const MD_LINE* lines, int n_lines)
                     MD_TEXT(text_type, STR(tmp), off-tmp);
 
                 /* and new lines are transformed into single spaces. */
-                if(prev_mark->end < off  &&  off < mark->beg)
+                if(off == line->end)
                     MD_TEXT(text_type, _T(" "), 1);
             } else if(text_type == MD_TEXT_HTML) {
                 /* Inside raw HTML, we output the new line verbatim, including
diff --git a/test/spec-latex-math.txt b/test/spec-latex-math.txt
index d939bf9..3284991 100644
--- a/test/spec-latex-math.txt
+++ b/test/spec-latex-math.txt
@@ -63,7 +63,7 @@ $$
 f(x) dx
 $$
 .
-<p><x-equation type="display">\int_a^b f(x) dx </x-equation></p>
+<p><x-equation type="display"> \int_a^b f(x) dx </x-equation></p>
 .
 --flatex-math
 ````````````````````````````````