Merge branch 'master' into issue#16
authorFreeBirdLjj <ljj11011@mail.ustc.edu.cn>
Sun, 28 Sep 2014 11:58:24 +0000 (19:58 +0800)
committerFreeBirdLjj <ljj11011@mail.ustc.edu.cn>
Sun, 28 Sep 2014 11:58:24 +0000 (19:58 +0800)
Makefile
README.md
include/main.h
include/parser.h
sample.md
src/Makefile
src/parser.c
src/viewer.c

index f922b7e..5801f84 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -18,9 +18,9 @@
 # along with this program. If not, see <http://www.gnu.org/licenses/>.
 #
 
-OSTYPE  := $(shell uname -o)
 CURSES   = ncursesw
 ifeq (Windows_NT, $(OS))
+OSTYPE  := $(shell uname -o)
 ifneq (Cygwin, $(OSTYPE))
 CURSES  := pdcurses
 endif
index 9bb7d8a..cbf3326 100644 (file)
--- a/README.md
+++ b/README.md
@@ -9,7 +9,8 @@
 
 mdp needs the ncursesw headers to compile.
 So make sure you have them installed:
-- On Ubuntu/Debian you need `libncursesw5` and `libncursesw5-dev` to be installed.
+- on Ubuntu/Debian you need `libncursesw5` and `libncursesw5-dev`
+- on Cygwin you need `libncursesw10` and `libncurses-devel`
 
 Now download and install mdp:
 
index 05d05f4..0e65cd3 100644 (file)
@@ -25,6 +25,6 @@
 
 #define MDP_VER_MAJOR 0
 #define MDP_VER_MINOR 91
-#define MDP_VER_REVISION 1
+#define MDP_VER_REVISION 2
 
 #endif // !defined( MAIN_H )
index 1a113e0..4eb9dfe 100644 (file)
@@ -48,6 +48,7 @@ void markdown_debug(deck_t *deck, int debug);
 int is_utf8(char ch);
 int length_utf8(char ch);
 int next_nonblank(cstring_t *text, int i);
+int prev_blank(cstring_t *text, int i);
 int next_blank(cstring_t *text, int i);
 int next_word(cstring_t *text, int i);
 
index b8588db..57abf3d 100644 (file)
--- a/sample.md
+++ b/sample.md
@@ -135,31 +135,23 @@ becomes
 
 # Supported markdown formatting's
 
-The leading stars or minuses make list.
-
-TODO list:
-\* TODO major 1
-\    \- TODO minor 1.1
-\        \- Detail 1.1.1
-\        \- Detail 1.1.2
-\    \- TODO minor 1.2
-\* TODO major 2
-\* TODO major 3
-\    \* TODO minor 3.1
-\    \* TODO minor 3.2
+Leading *\** or *-* indicate lists.
+
+TODO list
+\* major 1
+\    \- minor 1.1
+\        \- detail 1.1.1
+\        \- detail 1.1.2
+\    \- minor 1.2
 
 becomes
 
-TODO list:
-* TODO major 1
-    - TODO minor 1.1
-        - Detail 1.1.1
-        - Detail 1.1.2
-    - TODO minor 1.2
-* TODO major 2
-* TODO major 3
-    * TODO minor 3.1
-    * TODO minor 3.2
+TODO list
+* major 1
+    - minor 1.1
+        - detail 1.1.1
+        - detail 1.1.2
+    - minor 1.2
 
 -------------------------------------------------
 
index f0783e9..42d5563 100644 (file)
@@ -26,8 +26,8 @@ ifeq ($(DEBUG),1)
 CFLAGS  := -O0 -Wall -g -I../include
 endif
 
-OSTYPE  := $(shell uname -o)
 ifeq (Windows_NT, $(OS))
+OSTYPE  := $(shell uname -o)
 ifneq (Cygwin, $(OSTYPE))
 CFLAGS  += -DWIN32=1
 endif
index a573106..c8120cd 100644 (file)
@@ -302,11 +302,12 @@ int markdown_analyse(cstring_t *text) {
     // strip trailing spaces
     for(eol = text->size; eol > offset && isspace((unsigned char) text->text[eol - 1]); eol--);
 
+    // IS_UNORDERED_LIST_#
     if(text->size >= offset + 2 &&
        (text->text[offset] == '*' || text->text[offset] == '-') &&
        text->text[offset + 1] == ' ') {
 
-        for(i = offset; i<eol; i++)
+        for(i = offset; i<eol; i++) {
             if(text->text[i] != '*' &&
                text->text[i] != '-' &&
                text->text[i] != ' ') {
@@ -328,7 +329,7 @@ int markdown_analyse(cstring_t *text) {
                 if(unordered_list_level == 0) {
                     unordered_list_level = 1;
                     unordered_list_level_offset[1] = offset;
-                 }
+                }
 
                 switch(unordered_list_level) {
                     case 1: SET_BIT(bits, IS_UNORDERED_LIST_1); break;
@@ -339,6 +340,7 @@ int markdown_analyse(cstring_t *text) {
                 
                 break;
             }
+        }
     }
     
     if(!CHECK_BIT(bits, IS_UNORDERED_LIST_1) &&
@@ -347,9 +349,10 @@ int markdown_analyse(cstring_t *text) {
 
         unordered_list_level = 0;
 
+        // IS_CODE
         if(offset >= CODE_INDENT) {
-            // IS_CODE
             SET_BIT(bits, IS_CODE);
+
         } else {
 
             for(i = offset; i < eol; i++) {
@@ -498,6 +501,13 @@ int next_nonblank(cstring_t *text, int i) {
     return i;
 }
 
+int prev_blank(cstring_t *text, int i) {
+    while ((i > 0) && !isspace((unsigned char) (text->text)[i]))
+        i--;
+
+    return i;
+}
+
 int next_blank(cstring_t *text, int i) {
     while ((i < text->size) && !isspace((unsigned char) (text->text)[i]))
         i++;
index 6b551de..162477f 100644 (file)
@@ -65,6 +65,7 @@ int ncurses_display(deck_t *deck, int notrans, int nofade, int invert) {
     int c = 0;          // char
     int i = 0;          // iterate
     int l = 0;          // line number
+    int lc = 0;         // line count
     int sc = 1;         // slide count
     int colors = 0;     // amount of colors supported
     int fade = 0;       // disable color fading by default
@@ -82,39 +83,71 @@ int ncurses_display(deck_t *deck, int notrans, int nofade, int invert) {
     slide_t *slide = deck->slide;
     line_t *line;
 
+    // set locale to display UTF-8 correctly in ncurses
+    setlocale(LC_CTYPE, "");
+
+    // init ncurses
+    initscr();
+
     while(slide) {
-        // set max_lines if line count exceeded
-        max_lines = (slide->lines > max_lines) ? slide->lines : max_lines;
+        lc = 0;
         line = slide->line;
+
         while(line) {
-            // set max_cols if length exceeded
-            max_cols = (line->length > max_cols) ? line->length : max_cols;
+            if(line->length > COLS) {
+                i = line->length;
+                offset = 0;
+                while(i > COLS) {
+
+                    i = prev_blank(line->text, offset + COLS) - offset;
+
+                    // single word is > COLS
+                    if(!i) {
+                        // calculate min_width
+                        i = next_blank(line->text, offset + COLS) - offset;
+
+                        // disable ncurses
+                        endwin();
+
+                        // print error
+                        fprintf(stderr, "Error: Terminal width (%i columns) too small. Need at least %i columns.\n", COLS, i);
+                        fprintf(stderr, "You may need to shorten some lines by inserting line breaks.\n");
+
+                        return(1);
+                    }
+
+                    // set max_cols
+                    max_cols = (i > max_cols) ? i : max_cols;
+
+                    // iterate to next line
+                    offset = prev_blank(line->text, offset + COLS);
+                    i = line->length - offset;
+                    lc++;
+                }
+                // set max_cols one last time
+                max_cols = (i > max_cols) ? i : max_cols;
+            } else {
+                // set max_cols
+                max_cols = (line->length > max_cols) ? line->length : max_cols;
+            }
+            lc++;
             line = line->next;
         }
-        slide = slide->next;
-    }
 
-    // set locale to display UTF-8 correctly in ncurses
-    setlocale(LC_CTYPE, "");
+        max_lines = (lc > max_lines) ? lc : max_lines;
 
-    // init ncurses
-    initscr();
+        slide = slide->next;
+    }
 
-    if((max_cols > COLS) ||
-       (max_lines + bar_top + bar_bottom + 2 > LINES)) {
+    // not enough lines
+    if(max_lines + bar_top + bar_bottom > LINES) {
 
         // disable ncurses
         endwin();
 
         // print error
-        fprintf(stderr, "Error: Terminal size %ix%i too small. Need at least %ix%i.\n",
-            COLS, LINES, max_cols, max_lines + bar_top + bar_bottom + 2);
-
-        // print hint to solve it
-        if(max_lines + bar_top + bar_bottom + 2 > LINES)
-            fprintf(stderr, "You may need to add additional horizontal rules ('***') to split your file in shorter slides.\n");
-        if(max_cols > COLS)
-            fprintf(stderr, "Automatic line wrapping is not supported jet. You may need to shorten some lines by inserting line breaks.\n");
+        fprintf(stderr, "Error: Terminal heigth (%i lines) too small. Need at least %i lines.\n", LINES, max_lines + bar_top + bar_bottom);
+        fprintf(stderr, "You may need to add additional horizontal rules ('***') to split your file in shorter slides.\n");
 
         return(1);
     }
@@ -372,38 +405,41 @@ void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colo
     if(line->text->text) {
         int offset = 0; // text offset
 
+        // IS_UNORDERED_LIST_3
         if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
             offset = next_nonblank(line->text, 0);
             char format_s[15];
-            strcpy(&format_s[0], CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? "  " : "    ");
-            strcpy(&format_s[4], CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)? "  " : "    ");
-            strcpy(&format_s[8], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_3)? "+-- %s" : "`-- %s");
+            strcpy(&format_s[0], CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? " |  " : "    ");
+            strcpy(&format_s[4], CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)? " |  " : "    ");
+            strcpy(&format_s[8], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_3)? " +- %s" : " `- %s");
             mvwprintw(window,
                       y, x,
                       format_s,
                       &line->text->text[offset + 2]);
+
+        // IS_UNORDERED_LIST_2
         } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
             offset = next_nonblank(line->text, 0);
             char format_s[11];
-            strcpy(&format_s[0], CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? "  " : "    ");
-            strcpy(&format_s[4], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_2)? "+-- %s" : "`-- %s");
+            strcpy(&format_s[0], CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? " |  " : "    ");
+            strcpy(&format_s[4], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_2)? " +- %s" : " `- %s");
             mvwprintw(window,
                       y, x,
                       format_s,
                       &line->text->text[offset + 2]);
+
+        // IS_UNORDERED_LIST_1
         } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
             offset = next_nonblank(line->text, 0);
             char format_s[7];
-            strcpy(&format_s[0], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_1)? "+-- %s" : "`-- %s");
+            strcpy(&format_s[0], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_1)? " +- %s" : " `- %s");
             mvwprintw(window,
                       y, x,
                       format_s,
                       &line->text->text[offset + 2]);
-        } else
-
 
         // IS_CODE
-        if(CHECK_BIT(line->bits, IS_CODE)) {
+        } else if(CHECK_BIT(line->bits, IS_CODE)) {
 
             // set static offset for code
             offset = CODE_INDENT;