diff options
| -rw-r--r-- | g2x.c | 88 |
1 files changed, 59 insertions, 29 deletions
@@ -3,7 +3,6 @@ #define BUFSIZE 4096 #define html(s) fputs(s,stdout) -#define getbuf() fgets(buf,BUFSIZE,stdin) #define xmlput(n)\ for(pos = n; buf[pos] != '\n'; pos++) {\ switch(buf[pos]) {\ @@ -30,45 +29,76 @@ int main(void) "<title></title></head><body>\n" ); - while (getbuf() != NULL) + // Reading a line from stdin into buf + while (fgets(buf,BUFSIZE,stdin) != NULL) { - if (!strncmp(buf,"###",3)) { - html("<h3>"); xmlput(4); html("</h3>\n"); - } else if (!strncmp(buf,"##",2)) { - html("<h2>"); xmlput(3); html("</h2>\n"); - } else if (buf[0] == '#') { - html("<h1>"); xmlput(2); html("</h1>\n"); + if (!strncmp(buf,"###",3)) // Match ### + { + html("<h3>"); + xmlput(4); + html("</h3>\n"); } - else if (buf[0] == '>') { + else if (!strncmp(buf,"##",2)) // Match ## + { + html("<h2>"); + xmlput(3); + html("</h2>\n"); + } + else if (buf[0] == '#') // Match # + { + html("<h1>"); + xmlput(2); + html("</h1>\n"); + } + else if (buf[0] == '>') // Match > for blockquote + { html("<p><blockquote>"); xmlput(2); html("</blockquote></p>\n"); } - else if (buf[0] == '*') { + else if (buf[0] == '*') // Match lists + { if (list_state == 0) html("<ul>\n"); html("<li>"); xmlput(2); html("</li>\n"); - list_state = 1; - } else if (list_state == 1) { + list_state = 1; // in a list + } + else if (list_state == 1) + { html("</ul>\n"); - list_state = 0; + list_state = 0; // out of a list } - else if (!strncmp(buf,"=>",2)) { + else if (!strncmp(buf,"=>",2)) // Match links + { html("<p><a href=\""); - for (pos = 3; buf[pos] != '.' && buf[pos] != ' ';) + for (pos = 3; buf[pos] != ' ';) putchar(buf[pos++]); - if (buf[pos] == '.') { - int i = 0; char suffix[12]; - while (buf[pos] != ' ') - suffix[i++] = buf[pos++]; - suffix[i] = '\0'; - if (!strncmp(suffix,".gmi",4)) html(".html"); - else html(suffix); - } html("\">"); xmlput(pos+1); html("</a></p>\n"); + // read entire URL + if ( + buf[pos-3] == 'g' && + buf[pos-2] == 'm' && + buf[pos-1] == 'i' // check for .gmi + ){ + fseek(stdout,-3,1); // move back 3 chars + html("html"); // put .html + } + html("\">"); xmlput(pos+1); html("</a></p>\n"); + } + else if (!strncmp(buf,"```",3)) // Match codeblocks + { + fgets(buf,BUFSIZE,stdin); html("<pre><code>"); + while (strncmp(buf,"```",3)) + { + xmlput(0); + putchar('\n'); + fgets(buf,BUFSIZE,stdin); + } + html("</code></pre>\n"); + } + else // Match paragraphs + { + html("<p>"); + xmlput(0); + html("</p>\n"); } - else if (!strncmp(buf,"```",3)) { - getbuf(); html("<pre><code>"); - while (strncmp(buf,"```",3)) { - xmlput(0); putchar('\n'); getbuf(); - } html("</code></pre>\n"); - } else { html("<p>"); xmlput(0); html("</p>\n"); } } html("</body>"); html("</html>"); + // I love gemini! } |