Added link support
[smdp.git] / src / viewer.c
index 13ff6f6..593d9d3 100644 (file)
  *
  */
 
+#include <ctype.h>  // isalnum
 #include <locale.h> // setlocale
-#include <stdlib.h>
 #include <string.h> // strchr
-#include <unistd.h>
+#include <unistd.h> // usleep
 
 #include "viewer.h"
+#include "url.h"
 
 // color ramp for fading from black to color
 static short white_ramp[24] = { 16, 232, 233, 234, 235, 236,
@@ -234,6 +235,9 @@ int ncurses_display(deck_t *deck, int notrans, int nofade, int invert) {
 
     slide = deck->slide;
     while(slide) {
+               
+               url_init();
+               
         // clear windows
         werase(content);
         werase(stdscr);
@@ -266,6 +270,7 @@ int ncurses_display(deck_t *deck, int notrans, int nofade, int invert) {
                   "%d / %d", sc, deck->slides);
 
         // make header + fooder visible
+        wrefresh(content);
         wrefresh(stdscr);
 
         line = slide->line;
@@ -277,6 +282,13 @@ int ncurses_display(deck_t *deck, int notrans, int nofade, int invert) {
             l += (line->length / COLS) + 1;
             line = line->next;
         }
+        
+        int i, ymax;
+        getmaxyx( content, ymax, i );
+        for (i = 0; i < url_get_amount(); i++) {
+                       mvwprintw(content, ymax - url_get_amount() - 1 + i, 3, 
+                               "[%d] %s", i, url_get_target(i));
+               }
 
         // make content visible
         wrefresh(content);
@@ -393,6 +405,8 @@ int ncurses_display(deck_t *deck, int notrans, int nofade, int invert) {
         // fade out
         if(fade)
             fade_out(content, trans, colors, invert);
+           
+        url_purge();
     }
 
     endwin();
@@ -559,22 +573,28 @@ void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colo
 }
 
 void inline_display(WINDOW *window, const char *c, const int colors) {
-    const static char *special = "\\*_`"; // list of interpreted chars
+    const static char *special = "\\*_`["; // list of interpreted chars
+    const char *i = c; // iterator
+    const char *start_link_name, *start_url;
+    int length_link_name, url_num;
     cstack_t *stack = cstack_init();
 
+
     // for each char in line
-    for(; *c; c++) {
+    for(; *i; i++) {
 
         // if char is in special char list
-        if(strchr(special, *c)) {
+        if(strchr(special, *i)) {
 
             // closing special char (or second backslash)
-            if((stack->top)(stack, *c)) {
+            // only if not followed by :alnum:
+            if((stack->top)(stack, *i) &&
+               (!isalnum((int)i[1]) || *(i + 1) == '\0' || *i == '\\')) {
 
-                switch(*c) {
+                switch(*i) {
                     // print escaped backslash
                     case '\\':
-                        wprintw(window, "%c", *c);
+                        wprintw(window, "%c", *i);
                         break;
                     // disable highlight
                     case '*':
@@ -597,33 +617,80 @@ void inline_display(WINDOW *window, const char *c, const int colors) {
 
             // treat special as regular char
             } else if((stack->top)(stack, '\\')) {
-                wprintw(window, "%c", *c);
+                wprintw(window, "%c", *i);
 
                 // remove backslash from stack
                 (stack->pop)(stack);
 
             // opening special char
             } else {
-                switch(*c) {
-                    // enable highlight
-                    case '*':
-                        if(colors)
-                            wattron(window, COLOR_PAIR(CP_RED));
-                        break;
-                    // enable underline
-                    case '_':
-                        wattron(window, A_UNDERLINE);
-                        break;
-                    // enable inline code
-                    case '`':
-                        if(colors)
-                            wattron(window, COLOR_PAIR(CP_BLACK));
-                        break;
-                    // do nothing for backslashes
-                }
 
-                // push special char to stack
-                (stack->push)(stack, *c);
+                // emphasis or code span can start after new-line or space only
+                // and of cause after another emphasis markup
+                //TODO this condition looks ugly
+                if(i == c ||
+                   *(i - 1) == ' ' ||
+                   ((*(i - 1) == '_' || *(i - 1) == '*') && ((i - 1) == c || *(i - 2) == ' ')) ||
+                   *i == '\\') {
+
+                    if (*i == '[' && strchr(i, ']')) {
+                                               
+                                               if (strchr(i, ']')[1] == '(') {// url in pandoc style
+                                                       i++;
+                                                       // turn higlighting and underlining on
+                                                       if (colors)
+                                                               wattron(window, COLOR_PAIR(CP_BLUE));
+                                                       wattron(window, A_UNDERLINE);
+                                                       
+                                                       start_link_name = i;
+                                                       
+                                                       // print the content of the label
+                                                       // the label is printed as is
+                                                       do {
+                                                               wprintw(window, "%c", *i);
+                                                               i++;
+                                                       } while (*i != ']');
+                                                       
+                                                       length_link_name = i - 1 - start_link_name;
+                                                       
+                                                       i++;
+                                                       i++;
+                                                       
+                                                       start_url = i;
+                                                       
+                                                       while (*i != ')') i++;
+                                                       
+                                                       url_num = url_add(start_link_name, length_link_name, start_url, i - start_url, 0,0);
+                                                       
+                                                       wprintw(window, "[%d]", url_num);
+                                                       // turn highlighting and undelining off
+                                                       wattroff(window, A_UNDERLINE);
+                                                       wattron(window, COLOR_PAIR(CP_WHITE));
+                                               } else wprintw(window, "[");
+                                       } else switch(*i) {
+                        // enable highlight
+                        case '*':
+                            if(colors)
+                                wattron(window, COLOR_PAIR(CP_RED));
+                            break;
+                        // enable underline
+                        case '_':
+                            wattron(window, A_UNDERLINE);
+                            break;
+                        // enable inline code
+                        case '`':
+                            if(colors)
+                                wattron(window, COLOR_PAIR(CP_BLACK));
+                            break;
+                        // do nothing for backslashes
+                    }
+
+                    // push special char to stack
+                    (stack->push)(stack, *i);
+
+                } else {
+                    wprintw(window, "%c", *i);
+                }
             }
 
         } else {
@@ -632,7 +699,7 @@ void inline_display(WINDOW *window, const char *c, const int colors) {
                 (stack->pop)(stack);
 
             // print regular char
-            wprintw(window, "%c", *c);
+            wprintw(window, "%c", *i);
         }
     }