version bump
[smdp.git] / src / parser.c
index f084667..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.
  *
 
 #include "parser.h"
 
-deck_t *markdown_load(FILE *input) {
+// char entry translation table
+static struct named_character_entity {
+    wchar_t        ucs;
+    const wchar_t *name;
+} named_character_entities[] = {
+   { L'\x0022', L"quot" },
+   { L'\x0026', L"amp" },
+   { L'\x0027', L"apos" },
+   { L'\x003C', L"lt" },
+   { L'\x003E', L"gt" },
+   { L'\x00A2', L"cent" },
+   { L'\x00A3', L"pound" },
+   { L'\x00A5', L"yen" },
+   { L'\x00A7', L"sect" },
+   { L'\x00A9', L"copy" },
+   { L'\x00AA', L"laquo" },
+   { L'\x00AE', L"reg" },
+   { L'\x00B0', L"deg" },
+   { L'\x00B1', L"plusmn" },
+   { L'\x00B2', L"sup2" },
+   { L'\x00B3', L"sup3" },
+   { L'\x00B6', L"para" },
+   { L'\x00B9', L"sup1" },
+   { L'\x00BB', L"raquo" },
+   { L'\x00BC', L"frac14" },
+   { L'\x00BD', L"frac12" },
+   { L'\x00BE', L"frac34" },
+   { L'\x00D7', L"times" },
+   { L'\x00F7', L"divide" },
+   { L'\x2018', L"lsquo" },
+   { L'\x2019', L"rsquo" },
+   { L'\x201C', L"ldquo" },
+   { L'\x201D', L"rdquo" },
+   { L'\x2020', L"dagger" },
+   { L'\x2021', L"Dagger" },
+   { L'\x2022', L"bull" },
+   { L'\x2026', L"hellip" },
+   { L'\x2030', L"permil" },
+   { L'\x2032', L"prime" },
+   { L'\x2033', L"Prime" },
+   { L'\x2039', L"lsaquo" },
+   { L'\x203A', L"rsaquo" },
+   { L'\x20AC', L"euro" },
+   { L'\x2122', L"trade" },
+   { L'\x2190', L"larr" },
+   { L'\x2191', L"uarr" },
+   { L'\x2192', L"rarr" },
+   { L'\x2193', L"darr" },
+   { L'\x2194', L"harr" },
+   { L'\x21B5', L"crarr" },
+   { L'\x21D0', L"lArr" },
+   { L'\x21D1', L"uArr" },
+   { L'\x21D2', L"rArr" },
+   { L'\x21D3', L"dArr" },
+   { L'\x21D4', L"hArr" },
+   { L'\x221E', L"infin" },
+   { L'\x2261', L"equiv" },
+   { L'\x2308', L"lceil" },
+   { L'\x2309', L"rceil" },
+   { L'\x230A', L"lfloor" },
+   { L'\x230B', L"rfloor" },
+   { L'\x25CA', L"loz" },
+   { L'\x2660', L"spades" },
+   { L'\x2663', L"clubs" },
+   { L'\x2665', L"hearts" },
+   { L'\x2666', L"diams" },
+   { L'\0', NULL },
+};
+
+deck_t *markdown_load(FILE *input, int noexpand) {
 
     wchar_t c = L'\0';    // char
     int i = 0;    // increment
@@ -89,7 +158,8 @@ deck_t *markdown_load(FILE *input) {
                 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);
@@ -122,7 +192,9 @@ deck_t *markdown_load(FILE *input) {
                 line->offset = next_nonblank(text, 0);
 
                 // expand character entities if enabled
-                if(line->text->value)
+                if(line->text->value &&
+                   !noexpand &&
+                   !CHECK_BIT(line->bits, IS_CODE))
                     expand_character_entities(line);
 
                 // adjust line length dynamicaly - excluding markup
@@ -329,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
@@ -376,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) ||
@@ -387,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 &&
@@ -607,97 +702,11 @@ void markdown_debug(deck_t *deck, int debug) {
     }
 }
 
-static int enable_character_entities = 0;
-static struct named_character_entity {
-    wchar_t        ucs;
-    const wchar_t *name;
-} named_character_entities[] = {
-   { L'\x0022', L"quot" },
-   { L'\x0026', L"amp" },
-   { L'\x0027', L"apos" },
-   { L'\x003C', L"lt" },
-   { L'\x003E', L"gt" },
-   { L'\x00A2', L"cent" },
-   { L'\x00A3', L"pound" },
-   { L'\x00A5', L"yen" },
-   { L'\x00A7', L"sect" },
-   { L'\x00A9', L"copy" },
-   { L'\x00AA', L"laquo" },
-   { L'\x00AE', L"reg" },
-   { L'\x00B0', L"deg" },
-   { L'\x00B1', L"plusmn" },
-   { L'\x00B2', L"sup2" },
-   { L'\x00B3', L"sup3" },
-   { L'\x00B6', L"para" },
-   { L'\x00B9', L"sup1" },
-   { L'\x00BB', L"raquo" },
-   { L'\x00BC', L"frac14" },
-   { L'\x00BD', L"frac12" },
-   { L'\x00BE', L"frac34" },
-   { L'\x00D7', L"times" },
-   { L'\x00F7', L"divide" },
-   { L'\x2018', L"lsquo" },
-   { L'\x2019', L"rsquo" },
-   { L'\x201C', L"ldquo" },
-   { L'\x201D', L"rdquo" },
-   { L'\x2020', L"dagger" },
-   { L'\x2021', L"Dagger" },
-   { L'\x2022', L"bull" },
-   { L'\x2026', L"hellip" },
-   { L'\x2030', L"permil" },
-   { L'\x2032', L"prime" },
-   { L'\x2033', L"Prime" },
-   { L'\x2039', L"lsaquo" },
-   { L'\x203A', L"rsaquo" },
-   { L'\x20AC', L"euro" },
-   { L'\x2122', L"trade" },
-   { L'\x2190', L"larr" },
-   { L'\x2191', L"uarr" },
-   { L'\x2192', L"rarr" },
-   { L'\x2193', L"darr" },
-   { L'\x2194', L"harr" },
-   { L'\x21B5', L"crarr" },
-   { L'\x21D0', L"lArr" },
-   { L'\x21D1', L"uArr" },
-   { L'\x21D2', L"rArr" },
-   { L'\x21D3', L"dArr" },
-   { L'\x21D4', L"hArr" },
-   { L'\x221E', L"infin" },
-   { L'\x2261', L"equiv" },
-   { L'\x2308', L"lceil" },
-   { L'\x2309', L"rceil" },
-   { L'\x230A', L"lfloor" },
-   { L'\x230B', L"rfloor" },
-   { L'\x25CA', L"loz" },
-   { L'\x2660', L"spades" },
-   { L'\x2663', L"clubs" },
-   { L'\x2665', L"hearts" },
-   { L'\x2666', L"diams" },
-   { L'\0', NULL },
-};
-
-/*
-export MDP_ENABLE_CHARENT=1
-*/
-void setup_character_entities(void)
-{
-    char *str = getenv("MDP_ENABLE_CHARENT");
-    if (str == NULL)
-        enable_character_entities = 0;
-    else if (str[0] == '\0')
-        enable_character_entities = 1;
-    else
-        enable_character_entities = atoi(str);
-}
-
 void expand_character_entities(line_t *line)
 {
     wchar_t *ampersand;
     wchar_t *prev, *curr;
 
-    if (!enable_character_entities)
-        return;
-
     ampersand = NULL;
     curr = &line->text->value[0];
 
@@ -713,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;
@@ -721,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;
@@ -746,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;
     }
 }
 
@@ -835,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;
+}
+