beautifying
[smdp.git] / src / parser.c
index 4d48e33..9d8e2ef 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) 2014 Michael Goehler
+ * Copyright (C) 2015 Michael Goehler
  *
  * This file is part of mdp.
  *
@@ -68,6 +68,14 @@ deck_t *markdown_load(FILE *input) {
                 // clear text
                 (text->reset)(text);
 
+            } else if(line && CHECK_BIT(bits, IS_STOP)) {
+
+                // set stop bit on last line
+                SET_BIT(line->bits, IS_STOP);
+
+                // clear text
+                (text->reset)(text);
+
             // if text is markdown hr
             } else if(CHECK_BIT(bits, IS_HR) &&
                       CHECK_BIT(line->bits, IS_EMPTY)) {
@@ -81,6 +89,11 @@ deck_t *markdown_load(FILE *input) {
                 slide = next_slide(slide);
                 sc++;
 
+            } else if(CHECK_BIT(bits, IS_TILDE_CODE) &&
+                      CHECK_BIT(bits, IS_EMPTY)) {
+                // remove tilde code markers
+                (text->reset)(text);
+
             } else {
 
                 // if slide ! has line
@@ -109,7 +122,7 @@ deck_t *markdown_load(FILE *input) {
                 line->offset = next_nonblank(text, 0);
 
                 // adjust line length dynamicaly - excluding markup
-                if(line->text->text)
+                if(line->text->value)
                     adjust_line_length(line);
 
                 // new text
@@ -149,13 +162,13 @@ deck_t *markdown_load(FILE *input) {
 
     // detect header
     line = deck->slide->line;
-    if(line && line->text->size > 0 && line->text->text[0] == L'%') {
+    if(line && line->text->size > 0 && line->text->value[0] == L'%') {
 
         // assign header to deck
         deck->header = line;
 
         // find first non-header line
-        while(line && line->text->size > 0 && line->text->text[0] == L'%') {
+        while(line && line->text->size > 0 && line->text->value[0] == L'%') {
             hc++;
             line = line->next;
         }
@@ -183,6 +196,30 @@ deck_t *markdown_load(FILE *input) {
     slide = deck->slide;
     while(slide) {
         line = slide->line;
+
+        // ignore mdpress format attributes
+        if(line &&
+           slide->lines > 1 &&
+           !CHECK_BIT(line->bits, IS_EMPTY) &&
+           line->text->value[line->offset] == L'=' &&
+           line->text->value[line->offset + 1] == L' ') {
+
+            // remove line from linked list
+            slide->line = line->next;
+            line->next->prev = NULL;
+
+            // maintain loop condition
+            tmp = line;
+            line = line->next;
+
+            // adjust line count
+            slide->lines -= 1;
+
+            // delete line
+            (tmp->text->delete)(tmp->text);
+            free(tmp);
+        }
+
         while(line) {
             // combine underlined H1/H2 in single line
             if((CHECK_BIT(line->bits, IS_H1) ||
@@ -287,6 +324,7 @@ int markdown_analyse(cstring_t *text, int prev) {
     // the program remembers their value on every function calls
     static int unordered_list_level = 0;
     static int unordered_list_level_offset[] = {-1, -1, -1, -1};
+    static int num_tilde_characters = 0;
 
     int i = 0;      // increment
     int bits = 0;   // markdown bits
@@ -300,7 +338,7 @@ int markdown_analyse(cstring_t *text, int prev) {
     const int unordered_list_offset = unordered_list_level_offset[unordered_list_level];
 
     // return IS_EMPTY on null pointers
-    if(!text || !text->text) {
+    if(!text || !text->value) {
         SET_BIT(bits, IS_EMPTY);
         return bits;
     }
@@ -308,13 +346,43 @@ int markdown_analyse(cstring_t *text, int prev) {
     // count leading spaces
     offset = next_nonblank(text, 0);
 
+    // IS_TILDE_CODE
+    if (wcsncmp(text->value, L"~~~", 3) == 0) {
+        int tildes_in_line = next_nontilde(text, 0);
+        if (tildes_in_line >= num_tilde_characters) {
+            if (num_tilde_characters > 0) {
+                num_tilde_characters = 0;
+            } else {
+                num_tilde_characters = tildes_in_line;
+            }
+            SET_BIT(bits, IS_EMPTY);
+            SET_BIT(bits, IS_TILDE_CODE);
+            return bits;
+        }
+    }
+
+    if (num_tilde_characters > 0) {
+        SET_BIT(bits, IS_CODE);
+        SET_BIT(bits, IS_TILDE_CODE);
+        return bits;
+    }
+
+    // IS_STOP
+    if((offset < CODE_INDENT || !CHECK_BIT(prev, IS_CODE)) &&
+       (!wcsncmp(&text->value[offset], L"<br>", 4) ||
+        !wcsncmp(&text->value[offset], L"<BR>", 4) ||
+        !wcsncmp(&text->value[offset], L"^", 1))) {
+        SET_BIT(bits, IS_STOP);
+        return bits;
+    }
+
     // strip trailing spaces
-    for(eol = text->size; eol > offset && iswspace(text->text[eol - 1]); eol--);
+    for(eol = text->size; eol > offset && iswspace(text->value[eol - 1]); eol--);
 
     // IS_UNORDERED_LIST_#
     if(text->size >= offset + 2 &&
-       (text->text[offset] == L'*' || text->text[offset] == L'-') &&
-       iswspace(text->text[offset + 1])) {
+       (text->value[offset] == L'*' || text->value[offset] == L'-') &&
+       iswspace(text->value[offset + 1])) {
 
         // if different from last lines offset
         if(offset != unordered_list_offset) {
@@ -381,21 +449,22 @@ int markdown_analyse(cstring_t *text, int prev) {
         // IS_CODE
         if(offset >= CODE_INDENT &&
            (CHECK_BIT(prev, IS_EMPTY) ||
-            CHECK_BIT(prev, IS_CODE))) {
+            CHECK_BIT(prev, IS_CODE)  ||
+            CHECK_BIT(prev, IS_STOP))) {
             SET_BIT(bits, IS_CODE);
 
         } else {
 
             // IS_QUOTE
-            if(text->text[offset] == L'>') {
+            if(text->value[offset] == L'>') {
                 SET_BIT(bits, IS_QUOTE);
             }
 
             // IS_CENTER
             if(text->size >= offset + 3 &&
-               text->text[offset] == L'-' &&
-               text->text[offset + 1] == L'>' &&
-               iswspace(text->text[offset + 2])) {
+               text->value[offset] == L'-' &&
+               text->value[offset + 1] == L'>' &&
+               iswspace(text->value[offset + 2])) {
                 SET_BIT(bits, IS_CENTER);
 
                 // remove start tag
@@ -403,26 +472,26 @@ int markdown_analyse(cstring_t *text, int prev) {
                 eol -= 3;
 
                 if(text->size >= offset + 3 &&
-                   text->text[eol - 1] == L'-' &&
-                   text->text[eol - 2] == L'<' &&
-                   iswspace(text->text[eol - 3])) {
+                   text->value[eol - 1] == L'-' &&
+                   text->value[eol - 2] == L'<' &&
+                   iswspace(text->value[eol - 3])) {
 
                     // remove end tags
                     (text->strip)(text, eol - 3, 3);
 
                     // adjust end of line
-                    for(eol = text->size; eol > offset && iswspace(text->text[eol - 1]); eol--);
+                    for(eol = text->size; eol > offset && iswspace(text->value[eol - 1]); eol--);
 
                 }
             }
 
             for(i = offset; i < eol; i++) {
 
-                if(iswspace(text->text[i])) {
+                if(iswspace(text->value[i])) {
                     spaces++;
 
                 } else {
-                    switch(text->text[i]) {
+                    switch(text->value[i]) {
                         case L'=': equals++;  break;
                         case L'#': hashes++;  break;
                         case L'*': stars++;   break;
@@ -438,8 +507,8 @@ int markdown_analyse(cstring_t *text, int prev) {
                hashes + stars + minus + spaces + other == 0) {
                 SET_BIT(bits, IS_H1);
             }
-            if(text->text[offset] == L'#' &&
-               iswspace(text->text[offset+1])) {
+            if(text->value[offset] == L'#' &&
+               iswspace(text->value[offset+1])) {
                 SET_BIT(bits, IS_H1);
                 SET_BIT(bits, IS_H1_ATX);
             }
@@ -449,9 +518,9 @@ int markdown_analyse(cstring_t *text, int prev) {
                equals + hashes + stars + spaces + other == 0) {
                 SET_BIT(bits, IS_H2);
             }
-            if(text->text[offset] == L'#' &&
-               text->text[offset+1] == L'#' &&
-               iswspace(text->text[offset+2])) {
+            if(text->value[offset] == L'#' &&
+               text->value[offset+1] == L'#' &&
+               iswspace(text->value[offset+2])) {
                 SET_BIT(bits, IS_H2);
                 SET_BIT(bits, IS_H2_ATX);
             }
@@ -491,12 +560,12 @@ void markdown_debug(deck_t *deck, int debug) {
             header = deck->header;
             while(header &&
                 header->length > 0 &&
-                header->text->text[0] == L'%') {
+                header->text->value[0] == L'%') {
 
                 // skip descriptor word (e.g. %title:)
                 offset = next_blank(header->text, 0) + 1;
 
-                fwprintf(stderr, L"header: %S\n", &header->text->text[offset]);
+                fwprintf(stderr, L"header: %S\n", &header->text->value[offset]);
                 header = header->next;
             }
         }
@@ -532,7 +601,7 @@ void markdown_debug(deck_t *deck, int debug) {
 void adjust_line_length(line_t *line) {
     int l = 0;
     const static wchar_t *special = L"\\*_`"; // list of interpreted chars
-    const wchar_t *c = &line->text->text[line->offset];
+    const wchar_t *c = &line->text->value[0];
     cstack_t *stack = cstack_init();
 
     // for each char in line
@@ -574,21 +643,21 @@ void adjust_line_length(line_t *line) {
 }
 
 int next_nonblank(cstring_t *text, int i) {
-    while ((i < text->size) && iswspace((text->text)[i]))
+    while ((i < text->size) && iswspace((text->value)[i]))
         i++;
 
     return i;
 }
 
 int prev_blank(cstring_t *text, int i) {
-    while ((i > 0) && !iswspace((text->text)[i]))
+    while ((i > 0) && !iswspace((text->value)[i]))
         i--;
 
     return i;
 }
 
 int next_blank(cstring_t *text, int i) {
-    while ((i < text->size) && !iswspace((text->text)[i]))
+    while ((i < text->size) && !iswspace((text->value)[i]))
         i++;
 
     return i;
@@ -597,3 +666,11 @@ int next_blank(cstring_t *text, int i) {
 int next_word(cstring_t *text, int i) {
     return next_nonblank(text, next_blank(text, i));
 }
+
+int next_nontilde(cstring_t *text, int i) {
+    while ((i < text->size) && text->value[i] == L'~')
+        i++;
+
+    return i;
+}
+