From 221ec2057832fdb0805db1fa779ee21bfab66842 Mon Sep 17 00:00:00 2001 From: Romain de Laage Date: Fri, 12 Feb 2021 18:44:52 +0100 Subject: [PATCH] Add list parsing --- src/gemparse.c | 76 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 65 insertions(+), 11 deletions(-) diff --git a/src/gemparse.c b/src/gemparse.c index 3b9a23f..bf9a6ac 100644 --- a/src/gemparse.c +++ b/src/gemparse.c @@ -10,7 +10,8 @@ void addH2 (const char *line); void addH3 (const char *line); void addLink (const char *url, const char *text); void addCode (const char *meta, const char *code); -void addQuote (const char *line); +void addQuote (const char *text); +void addUList (const char *text); // general functions @@ -115,21 +116,68 @@ parseFile (char *path) do { - if (fgets(codeline, 4096, fileToParse) == NULL) + if (fgets (codeline, 4096, fileToParse) == NULL) break; - size = strlen(codeline); + size = strlen (codeline); if (size < 3 || codeline[0] != '`' || codeline[1] != '`' || codeline[2] != '`') - strcat(code, codeline); + strcat (code, codeline); } while (codeline[0] != '`' || codeline[1] != '`' || codeline[2] != '`'); // Remove tailing '\n' for both meta and code - size = strlen(code); + size = strlen (code); code[size - 1] = '\0'; addCode (line + 3, code); } + else if (size > 1 && line[0] == '*') + { + char text[4096] = ""; + + do + { + char buffer[4096] = ""; + int linePos = 1; + int bufferPos; + + strcat (buffer, "•"); + bufferPos = strlen (buffer); + buffer[bufferPos++] = ' '; + + // remove heading space + while (line[linePos] == ' ' && linePos < size) + linePos++; + + while (line[linePos] != '\n' && linePos < size) + { + buffer[bufferPos] = line[linePos]; + linePos++; + bufferPos++; + } + + buffer[bufferPos] = '\n'; + buffer[bufferPos + 1] = '\0'; + + strcat (text, buffer); + + if (fgets (line, 4096, fileToParse) == NULL) + break; + + size = strlen (line); + } + while (line[0] == '*'); + + //we seek the previous line + if (! feof (fileToParse)) + fseek (fileToParse, -size, SEEK_CUR); + + // Remove tailing '\n' for both meta and code + size = strlen(text); + text[size - 1] = '\0'; + + addUList (text); + } else if (size > 1 && line[0] == '>') { int i = 1; @@ -168,21 +216,21 @@ main (int argc, } void -addH1 (const char *text) +addH1 (const char *line) { - printf ("h1 {\n%s\n}\n", text); + printf ("h1 {\n%s\n}\n", line); } void -addH2 (const char *text) +addH2 (const char *line) { - printf ("h2 {\n%s\n}\n", text); + printf ("h2 {\n%s\n}\n", line); } void -addH3 (const char *text) +addH3 (const char *line) { - printf ("h3 {\n%s\n}\n", text); + printf ("h3 {\n%s\n}\n", line); } void @@ -207,4 +255,10 @@ addQuote (const char *text) printf ("quote {\n%s\n}\n", text); } +void +addUList (const char *text) +{ + printf ("ulist {\n%s\n}\n", text); +} + #endif