version bump
[smdp.git] / src / parser.c
index 2157310..0898b60 100644 (file)
@@ -2,7 +2,7 @@
  * Functions necessary to parse a file and transform its content into
  * a deck of slides containing lines. All based on markdown formating
  * rules.
- * Copyright (C) 2016 Michael Goehler
+ * Copyright (C) 2018 Michael Goehler
  *
  * This file is part of mdp.
  *
@@ -158,7 +158,8 @@ deck_t *markdown_load(FILE *input, int noexpand) {
                 slide = next_slide(slide);
                 sc++;
 
-            } else if(CHECK_BIT(bits, IS_TILDE_CODE) &&
+            } else if((CHECK_BIT(bits, IS_TILDE_CODE) ||
+                      CHECK_BIT(bits, IS_GFM_CODE)) &&
                       CHECK_BIT(bits, IS_EMPTY)) {
                 // remove tilde code markers
                 (text->reset)(text);
@@ -400,6 +401,7 @@ int markdown_analyse(cstring_t *text, int prev) {
     static int unordered_list_level = 0;
     static int unordered_list_level_offset[] = {-1, -1, -1, -1};
     static int num_tilde_characters = 0;
+    static int num_backticks = 0;
 
     int i = 0;      // increment
     int bits = 0;   // markdown bits
@@ -447,6 +449,27 @@ int markdown_analyse(cstring_t *text, int prev) {
         return bits;
     }
 
+    // IS_GFM_CODE
+    if (wcsncmp(text->value, L"```", 3) == 0) {
+        int backticks_in_line = next_nonbacktick(text, 0);
+        if (backticks_in_line >= num_backticks) {
+            if (num_backticks > 0) {
+                num_backticks = 0;
+            } else {
+                num_backticks = backticks_in_line;
+            }
+            SET_BIT(bits, IS_EMPTY);
+            SET_BIT(bits, IS_GFM_CODE);
+            return bits;
+        }
+    }
+
+    if (num_backticks > 0) {
+        SET_BIT(bits, IS_CODE);
+        SET_BIT(bits, IS_GFM_CODE);
+        return bits;
+    }
+
     // IS_STOP
     if((offset < CODE_INDENT || !CHECK_BIT(prev, IS_CODE)) &&
        (!wcsncmp(&text->value[offset], L"<br>", 4) ||
@@ -458,6 +481,7 @@ int markdown_analyse(cstring_t *text, int prev) {
 
     // strip trailing spaces
     for(eol = text->size; eol > offset && iswspace(text->value[eol - 1]); eol--);
+    text->size = eol;
 
     // IS_UNORDERED_LIST_#
     if(text->size >= offset + 2 &&
@@ -698,7 +722,8 @@ void expand_character_entities(line_t *line)
         if (*curr == L'#') {
             if (prev == ampersand)
                 continue;
-            goto clean;
+            ampersand = NULL;
+            continue;
         }
         if (iswalpha(*curr) || iswxdigit(*curr)) {
             continue;
@@ -706,22 +731,32 @@ void expand_character_entities(line_t *line)
         if (*curr == L';') {
             int cnt;
             wchar_t ucs = L'\0';
-            if (ampersand + 1 >= curr || ampersand + 16 < curr) // what is a good limit?
-                goto clean;
+            if (ampersand + 1 >= curr || ampersand + 16 < curr) { // what is a good limit?
+                ampersand = NULL;
+                continue;
+            }
             if (ampersand[1] == L'#') { // &#nnnn; or &#xhhhh;
-                if (ampersand + 2 >= curr)
-                    goto clean;
+                if (ampersand + 2 >= curr) {
+                    ampersand = NULL;
+                    continue;
+                }
                 if (ampersand[2] != L'x') { // &#nnnn;
                     cnt = wcsspn(&ampersand[2], L"0123456789");
-                    if (ampersand + 2 + cnt != curr)
-                        goto clean;
+                    if (ampersand + 2 + cnt != curr) {
+                        ampersand = NULL;
+                        continue;
+                    }
                     ucs = wcstoul(&ampersand[2], NULL, 10);
                 } else { // &#xhhhh;
-                    if (ampersand + 3 >= curr)
-                        goto clean;
+                    if (ampersand + 3 >= curr) {
+                        ampersand = NULL;
+                        continue;
+                    }
                     cnt = wcsspn(&ampersand[3], L"0123456789abcdefABCDEF");
-                    if (ampersand + 3 + cnt != curr)
-                        goto clean;
+                    if (ampersand + 3 + cnt != curr) {
+                        ampersand = NULL;
+                        continue;
+                    }
                     ucs = wcstoul(&ampersand[3], NULL, 16);
                 }
             } else { // &name;
@@ -731,16 +766,16 @@ void expand_character_entities(line_t *line)
                     ucs = named_character_entities[cnt].ucs;
                     break;
                 }
-                if (ucs == L'\0')
-                    goto clean;
+                if (ucs == L'\0') {
+                    ampersand = NULL;
+                    continue;
+                }
             }
             *ampersand = ucs;
             cstring_strip(line->text, ampersand + 1 - &line->text->value[0], curr - ampersand);
             curr = ampersand;
-            continue;
+            ampersand = NULL;
         }
-clean:
-        ampersand = NULL;
     }
 }
 
@@ -820,3 +855,10 @@ int next_nontilde(cstring_t *text, int i) {
     return i;
 }
 
+int next_nonbacktick(cstring_t *text, int i) {
+    while ((i < text->size) && text->value[i] == L'`')
+        i++;
+
+    return i;
+}
+