From da9a28e4cc9106a8eba89cc2bf99692c7b3a4baf Mon Sep 17 00:00:00 2001 From: thing1 Date: Mon, 10 Feb 2025 09:27:37 +0000 Subject: added new output formats --- Makefile | 4 ++-- TODO.gn | 4 ++-- TODO.html | 9 ++++++++ gn.c | 78 +++++++++++++++++++++++++++++++++++++++++--------------------- output.c | 39 +++++++++++++++++++++++++++++++ output.h | 5 ++++ test.gn | 10 -------- tmp.pdf | Bin 0 -> 11706 bytes tokens.h | 16 +++++++++++++ util.c | 7 ++++++ util.h | 1 + 11 files changed, 133 insertions(+), 40 deletions(-) create mode 100644 TODO.html create mode 100644 output.c create mode 100644 output.h delete mode 100644 test.gn create mode 100644 tmp.pdf create mode 100644 tokens.h create mode 100644 util.c create mode 100644 util.h diff --git a/Makefile b/Makefile index 56da911..ca69f8c 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CFLAGS=-ggdb all: gn -gn: gn.c - cc gn.c -o gn ${CFLAGS} +gn: gn.c util.c output.c tokens.h util.h output.h + cc gn.c util.c output.c -o gn ${CFLAGS} clean: rm gn diff --git a/TODO.gn b/TODO.gn index 3292ba5..24aa57d 100644 --- a/TODO.gn +++ b/TODO.gn @@ -1,5 +1,5 @@ -* Syntax checking * {2025.02.10} {todo} +* Syntax checking * [2025.02.10] {done} - loop through tokens and find if they are in the right order -* More output formats * {2025.02.10} {todo} +* More output formats * [2025.02.10] {todo} - Latex? - Groff? diff --git a/TODO.html b/TODO.html new file mode 100644 index 0000000..86e0356 --- /dev/null +++ b/TODO.html @@ -0,0 +1,9 @@ +

Syntax checking

+

2025.02.10

+

done

+

loop through tokens and find if they are in the right order

+

More output formats

+

2025.02.10

+

todo

+

Latex?

+

Groff?

diff --git a/gn.c b/gn.c index 58b5296..faae1f3 100644 --- a/gn.c +++ b/gn.c @@ -4,43 +4,29 @@ #include #include +#include "tokens.h" +#include "output.h" +#include "util.h" + typedef enum mode { NONE, LIST, + CHECK, + OUTPUT, } mode; -typedef enum types { - HEADING, - DATE, - TODO, - BULLET, -} types; +typedef enum outputformats { + GOATNOTE, + HTML, + GROFF, +} outputformats; -typedef struct token { - char data[256]; - types type; -} token; +outputformats outputformat; mode m = NONE; token tokens[1024]; int tokcount = 0; - -void eprint(char *msg) { - fprintf(stderr, "error: %s\n", msg); - exit(1); -} - -void printashtml(token t) { - char *start, *end; - switch (t.type) { - case HEADING: start = "

"; end = "

"; break; - case DATE: start = "

"; end = "

"; break; - case TODO: start = "

"; end = "

"; break; - case BULLET: start = "

"; end = "

"; break; - } - printf("%s%s%s\n", start, t.data, end); -} char *stripwhitespace(char *expr) { while (isblank(expr[0])) expr++; @@ -50,6 +36,24 @@ char *stripwhitespace(char *expr) { return expr; } +void formatcheck() { + types prev = NIL; + for (int i = 0; i < tokcount; i++) { + switch (prev) { + case NIL: + if (tokens[i].type != HEADING) eprint("Expected heading!"); + break; + case HEADING: + if (tokens[i].type != DATE) eprint("Expected date"); + break; + case DATE: + if (tokens[i].type != TODO) eprint("Expected todo|done marker"); + break; + } + prev = tokens[i].type; + } +} + int readuntil(char *file, int off, char end, types t) { int j = 0; char tok[256]; @@ -74,6 +78,15 @@ int readuntil(char *file, int off, char end, types t) { int main(int argc, char **argv) { for (int i = 0; i < argc && argc != 1; i++) { if (strcmp(argv[i], "-l") == 0) m = LIST; + else if (strcmp(argv[i], "-c") == 0) m = CHECK; + else if (strcmp(argv[i], "-o") == 0) { + i++; + if (strcmp(argv[i], "goatnote") == 0) outputformat = GOATNOTE; + else if (strcmp(argv[i], "html") == 0) outputformat = HTML; + else if (strcmp(argv[i], "groff") == 0) outputformat = GROFF; + + m = OUTPUT; + } } char file[INT16_MAX]; @@ -99,6 +112,19 @@ int main(int argc, char **argv) { for (int i = 0; i < tokcount; i++) if (tokens[i].type == HEADING) printf("%s\n", tokens[i].data); break; + case CHECK: + formatcheck(); + break; + case OUTPUT: + formatcheck(); + for (int i = 0; i < tokcount; i++){ + switch (outputformat) { + case HTML: printashtml(tokens[i]); break; + case GOATNOTE: printasgn(tokens[i]); break; + case GROFF: printasgroff(tokens[i]); break; + } + } + case NONE: return 0; } return 0; diff --git a/output.c b/output.c new file mode 100644 index 0000000..6f9b53b --- /dev/null +++ b/output.c @@ -0,0 +1,39 @@ +#include + +#include "tokens.h" + +extern token tokens[1024]; +extern int tokcount; + +void printashtml(token t) { + char *start, *end; + switch (t.type) { + case HEADING: start = "

"; end = "

\n"; break; + case DATE: start = "

"; end = "

\n"; break; + case TODO: start = "

"; end = "

\n"; break; + case BULLET: start = "

"; end = "

\n"; break; + } + printf("%s%s%s", start, t.data, end); +} + +void printasgn(token t) { + char *start, *end; + switch (t.type) { + case HEADING: start = "* "; end = " *"; break; + case DATE: start = " ["; end = "]"; break; + case TODO: start = " {"; end = "}\n"; break; + case BULLET: start = "- "; end = "\n"; break; + } + printf("%s%s%s", start, t.data, end); +} + +void printasgroff(token t) { + char *start, *end; + switch (t.type) { + case HEADING: start = ".NH\n"; end = "\n"; break; + case DATE: start = ".LP\n"; end = "\n"; break; + case TODO: start = ".LP\n"; end = "\n"; break; + case BULLET: start = ".PP\n"; end = "\n"; break; + } + printf("%s%s%s", start, t.data, end); +} diff --git a/output.h b/output.h new file mode 100644 index 0000000..734968b --- /dev/null +++ b/output.h @@ -0,0 +1,5 @@ +#include "tokens.h" + +void printashtml(token t); +void printasgn(token t); +void printasgroff(token t); diff --git a/test.gn b/test.gn deleted file mode 100644 index 054d660..0000000 --- a/test.gn +++ /dev/null @@ -1,10 +0,0 @@ -* my awsome note * [2025.02.09] {done} -- sub point -- sub point two -* my awsome note * [2025.02.09] {done} -* my awsome note * [2025.02.09] {done} -* my awsome note * [2025.02.09] {done} -* my awsome note * [2025.02.09] {done} -* my awsome note * [2025.02.09] {done} -* my awsome note * [2025.02.09] {done} -* my awsome note * [2025.02.09] {done} diff --git a/tmp.pdf b/tmp.pdf new file mode 100644 index 0000000..46e280b Binary files /dev/null and b/tmp.pdf differ diff --git a/tokens.h b/tokens.h new file mode 100644 index 0000000..c3305fc --- /dev/null +++ b/tokens.h @@ -0,0 +1,16 @@ +#ifndef __TOKENS_H +#define __TOKENS_H + +typedef enum types { + NIL, + HEADING, + DATE, + TODO, + BULLET, +} types; + +typedef struct token { + char data[256]; + types type; +} token; +#endif diff --git a/util.c b/util.c new file mode 100644 index 0000000..a46f853 --- /dev/null +++ b/util.c @@ -0,0 +1,7 @@ +#include +#include + +void eprint(char *msg) { + fprintf(stderr, "error: %s\n", msg); + exit(1); +} diff --git a/util.h b/util.h new file mode 100644 index 0000000..11961cc --- /dev/null +++ b/util.h @@ -0,0 +1 @@ +void eprint(char *msg); -- cgit v1.2.3