added special char for horizontal centering (#3)
[smdp.git] / src / parser.c
index e59f1be..815b9a2 100644 (file)
@@ -36,19 +36,15 @@ deck_t *markdown_load(FILE *input) {
     int l = 0;    // line length
     int hc = 0;   // header count
     int lc = 0;   // line count
-    int sc = 0;   // slide count
+    int sc = 1;   // slide count
     int bits = 0; // markdown bits
 
     deck_t *deck = new_deck();
-    slide_t *slide = new_slide();
+    slide_t *slide = deck->slide;
     line_t *line = NULL;
     line_t *tmp = NULL;
     cstring_t *text = cstring_init();
 
-    // assign first slide to deck
-    deck->slide = slide;
-    sc++;
-
     while ((c = fgetc(input)) != EOF) {
         if (ferror(input)) {
             fprintf(stderr, "markdown_load() failed to read input: %s\n", strerror(errno));
@@ -185,8 +181,8 @@ deck_t *markdown_load(FILE *input) {
         }
 
         // split linked list
-        line->prev->next = (void*)0;
-        line->prev = (void*)0;
+        line->prev->next = NULL;
+        line->prev = NULL;
 
         // remove header lines from slide
         deck->slide->line = line;
@@ -229,6 +225,7 @@ deck_t *markdown_load(FILE *input) {
                 // delete line
                 (tmp->text->delete)(tmp->text);
                 free(tmp);
+
             } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
                 tmp = line->next;
                 line_t *list_last_level_3 = line;
@@ -244,6 +241,7 @@ deck_t *markdown_load(FILE *input) {
                 for(tmp = line; tmp != list_last_level_3; tmp = tmp->next) {
                     SET_BIT(tmp->bits, IS_UNORDERED_LIST_3);
                 }
+
             } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
                 tmp = line->next;
                 line_t *list_last_level_2 = line;
@@ -260,6 +258,7 @@ deck_t *markdown_load(FILE *input) {
                 for(tmp = line; tmp != list_last_level_2; tmp = tmp->next) {
                     SET_BIT(tmp->bits, IS_UNORDERED_LIST_2);
                 }
+
             } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
                 tmp = line->next;
                 line_t *list_last_level_1 = line;
@@ -303,6 +302,12 @@ int markdown_analyse(cstring_t *text) {
 
     const int unordered_list_offset = unordered_list_level_offset[unordered_list_level];
 
+    // return IS_EMPTY on null pointers
+    if(!text || !text->text) {
+        SET_BIT(bits, IS_EMPTY);
+        return bits;
+    }
+
     // count leading spaces
     offset = next_nonblank(text, 0);
 
@@ -362,14 +367,41 @@ int markdown_analyse(cstring_t *text) {
 
         } else {
 
+            // IS_QUOTE
+            if(text->text[offset] == '>') {
+                SET_BIT(bits, IS_QUOTE);
+            }
+
+            // IS_CENTER
+            if(text->size >= offset + 3 &&
+               text->text[offset] == '-' &&
+               text->text[offset + 1] == '>' &&
+               text->text[offset + 2] == ' ') {
+                SET_BIT(bits, IS_CENTER);
+
+                // remove start tag
+                (text->strip)(text, offset, 3);
+                eol -= 3;
+
+                if(text->size >= offset + 3 &&
+                   text->text[eol - 1] == '-' &&
+                   text->text[eol - 2] == '<' &&
+                   text->text[eol - 3] == ' ') {
+
+                    // remove end tags
+                    (text->strip)(text, eol - 3, 3);
+
+                    // adjust end of line
+                    for(eol = text->size; eol > offset && isspace((unsigned char) text->text[eol - 1]); eol--);
+
+                }
+            }
+
             for(i = offset; i < eol; i++) {
 
                 if(text->text[i] == ' ') {
                     spaces++;
 
-                } else if(CHECK_BIT(bits, IS_CODE)) {
-                    other++;
-
                 } else {
                     switch(text->text[i]) {
                         case '=': equals++;  break;
@@ -385,9 +417,7 @@ int markdown_analyse(cstring_t *text) {
             // IS_H1
             if((equals > 0 &&
                 hashes + stars + minus + spaces + other == 0) ||
-               (text &&
-                text->text &&
-                text->text[offset] == '#' &&
+               (text->text[offset] == '#' &&
                 text->text[offset+1] != '#')) {
 
                 SET_BIT(bits, IS_H1);
@@ -396,22 +426,12 @@ int markdown_analyse(cstring_t *text) {
             // IS_H2
             if((minus > 0 &&
                 equals + hashes + stars + spaces + other == 0) ||
-               (text &&
-                text->text &&
-                text->text[offset] == '#' &&
+               (text->text[offset] == '#' &&
                 text->text[offset+1] == '#')) {
 
                 SET_BIT(bits, IS_H2);
             }
 
-            // IS_QUOTE
-            if(text &&
-               text->text &&
-               text->text[offset] == '>') {
-
-                SET_BIT(bits, IS_QUOTE);
-            }
-
             // IS_HR
             if((minus >= 3 && equals + hashes + stars + other == 0) ||
                (stars >= 3 && equals + hashes + minus + other == 0)) {
@@ -485,8 +505,8 @@ void markdown_debug(deck_t *deck, int debug) {
     }
 }
 
-int is_utf8(char ch) {
-    return (ch & 0x80);
+bool is_utf8(char ch) {
+    return (ch & 0x80) != 0x00;
 }
 
 int length_utf8(char ch) {