From f0bc377e7d6b57bfcfc86fd61c2fa648cd225a33 Mon Sep 17 00:00:00 2001
From: Alex Karle \n");
- }
+/*
+ * All inline types should start the paragraph if no other major type present.
+ * This function does just that, IFF we aren't already in a major type.
+ */
+void
+maybe_startp(void)
+{
+ if (in == NONE) {
+ in = PARAGRAPH;
+ printf(" \n");
+ }
}
-void handle_lf() {
- indent = 0;
- linestarted = false;
+void
+handle_lf(void)
+{
+ indent = 0;
+ linestarted = false;
- /* single line types (one lf to close) */
- if (in == HEADER) {
- in = NONE;
- printf("\n", hlvl);
- }
+ /* single line types (one lf to close) */
+ if (in == HEADER) {
+ in = NONE;
+ printf("\n", hlvl);
+ }
- /* multi-line types (two lf to close) */
- if (lastc == '\n' || (lastc == '>' && blockquote)) {
- switch (in) {
- case PARAGRAPH: printf("
");
- } else if (lastc == '\n' && c == '\t') {
- /* no op */
- } else {
- putesc(c);
- }
- break;
- case '>':
- if (in == NONE) {
- in = PARAGRAPH;
- /* only start a new blockquote block is not already in one */
- printf("%s", blockquote ? "\n" : "
\n\n");
- blockquote = true;
- } else if (lastc == '\n' && c == '>') {
- /* no op */
- } else {
- putesc(c);
- }
- break;
- case '[':
- if (in_link == NOL && !fmt_disabled(c)) {
- maybe_startp();
- in_link = DESC_PARSE;
- lnkidx = 0;
- } else {
- putesc(c);
- }
- break;
- case ']':
- if (in_link == DESC_PARSE) {
- in_link = OPT_URL;
- } else {
- putesc(c);
- }
- break;
- case '(':
- if (in_link == OPT_URL) {
- /* don't print it, start parsing */
- in_link = URL_PARSE;
- printf("");
- for (int i = 0; i < lnkidx; i++) {
- handlec(lnkdes[i]);
- }
- printf("");
- } else {
- putesc(c);
- }
- break;
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- case '.':
- case '-':
- if (in == NONE || !linestarted) {
- ol = c != '-';
- in = LIST_PARSE;
- } else if (in != LIST_PARSE) {
- putesc(c);
- }
- break;
- case '\n':
- handle_lf();
- if (in != NONE) {
- putesc(c);
- }
- break;
- default:
- maybe_startp();
- putesc(c);
- break;
- }
- lastc = c;
- linestarted = c != '\n';
-}
+ /* Handle Escapes before any other bit of the main switch */
+ if (escape) {
+ maybe_startp();
+ putesc(c);
+ escape = false;
+ return;
+ }
-int parse() {
- /* Mini state machine (home grown spaghetti code) */
- int c;
- while ((c = getchar()) != EOF) {
- /* Store links as we go, before the main loop */
- if (in_link == DESC_PARSE && c != ']') {
- lnkdes[lnkidx++] = c;
- /* skip the regular loop -- it'll be played back later */
- continue;
- }
+ /* Store the indentation and return without printing */
+ if (!linestarted && c == ' ') {
+ indent++;
+ return;
+ }
- /* Any character other than a '(' terminates a link at ']' */
- if (in_link == OPT_URL && c != '(') {
- in_link = NOL;
- lnkdes[lnkidx] = '\0';
- printf("%s", lnkdes, lnkdes);
- }
-
- handlec(c);
- }
- /* pretend there's a final LF to close any blocks */
- handle_lf();
- return 0;
+ switch (c) {
+ case '\\':
+ escape = true;
+ break;
+ case '#':
+ if (in == NONE) {
+ in = HEADER_PARSE;
+ hlvl = 1;
+ } else if (in == HEADER_PARSE)
+ hlvl++;
+ else
+ putesc(c);
+ break;
+ case ' ':
+ if (in == HEADER_PARSE) {
+ printf("", hlvl);
+ in = HEADER;
+ } else if (in == LIST_PARSE) {
+ if (!listdepth) {
+ newlist();
+ } else {
+ if (previndent < indent) {
+ newlist();
+ } else if (previndent > indent) {
+ endlist();
+ printf("
");
+ } else if (lastc != '\n')
+ putesc(c);
+ break;
+ case '>':
+ if (in == NONE) {
+ in = PARAGRAPH;
+ printf("%s", blockquote ? "\n" : "
\n\n");
+ blockquote = true;
+ } else if (lastc != '\n')
+ putesc(c);
+ break;
+ case '[':
+ if (in_link == NOL && !fmt_disabled(c)) {
+ maybe_startp();
+ in_link = DESC_PARSE;
+ lnkidx = 0;
+ } else
+ putesc(c);
+ break;
+ case ']':
+ if (in_link == DESC_PARSE)
+ in_link = OPT_URL;
+ else
+ putesc(c);
+ break;
+ case '(':
+ if (in_link == OPT_URL) {
+ in_link = URL_PARSE;
+ printf("");
+ for (int i = 0; i < lnkidx; i++)
+ handlec(lnkdes[i]);
+ printf("");
+ } else
+ putesc(c);
+ break;
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ case '.':
+ case '-':
+ if (in == NONE || !linestarted) {
+ ol = c != '-';
+ in = LIST_PARSE;
+ } else if (in != LIST_PARSE)
+ putesc(c);
+ break;
+ case '\n':
+ handle_lf();
+ if (in != NONE)
+ putesc(c);
+ break;
+ default:
+ maybe_startp();
+ putesc(c);
+ break;
+ }
+ lastc = c;
+ linestarted = c != '\n';
}
-int main(int argc, char *argv[]) {
- if (argc > 1) {
- fprintf(stderr, "error: %s takes no arguments\n", argv[0]);
- return 1;
- }
+
+/* nihdoc: a text -> HTML parser */
+int
+main(int argc, char *argv[])
+{
+ int c;
+
#ifdef __OpenBSD__
- pledge("stdio", "stdio");
+ pledge("stdio", "stdio");
#endif
- return parse();
+
+ if (argc > 1) {
+ fprintf(stderr, "error: %s takes no arguments\n", argv[0]);
+ return 1;
+ }
+
+ while ((c = getchar()) != EOF)
+ handlec(c);
+
+ /* pretend there's a final LF to close any blocks */
+ handle_lf();
+
+ return 0;
}
--
libgit2 1.1.1