1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "tokens.h"
#include "output.h"
#include "util.h"
typedef enum mode {
NONE = 0,
LIST = 1,
CHECK = 2,
OUTPUT = 4,
PRETTY = 8,
} mode;
typedef enum outputformats {
GOATNOTE,
HTML,
GROFF,
TEXT,
} outputformats;
outputformats outputformat;
mode m = NONE;
token tokens[1024];
int tokcount = 0;
char *header = NULL, *footer = NULL;
char *stripwhitespace(char *expr) {
while (isblank(expr[0])) expr++;
while (isblank(expr[strlen(expr) - 1])) expr[strlen(expr) - 1] = 0;
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];
off++;
while (file[off] != end) {
if (file[off] == 0) eprint("Never closed expression!") ;
tok[j] = file[off];
off++;
j++;
}
tok[j] = 0;
memcpy(tokens[tokcount].data, stripwhitespace(tok), 256);
tokens[tokcount].type = t;
tokcount++;
return off;
}
void usage() {
fprintf(stderr,
"usage: gn [-l -c -o format -p -H header -F footer -h] < note.gn\n"
);
exit(1);
}
int main(int argc, char **argv) {
for (int i = 1; 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 (!(i < argc)) eprint("expecet additional argument after -o!") ;
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;
else if (strcmp(argv[i], "text") == 0) outputformat = TEXT;
m |= OUTPUT;
}
else if (strcmp(argv[i], "-p") == 0) m |= PRETTY;
else if (strcmp(argv[i], "-H") == 0) {
i++;
if (!(i < argc)) eprint("expecet additional argument after -H!");
header = argv[i];
}
else if (strcmp(argv[i], "-F") == 0) {
i++;
if (!(i < argc)) eprint("expecet additional argument after -F!");
footer = argv[i];
}
else if (strcmp(argv[i], "-h") == 0) usage();
else usage();
}
char file[INT16_MAX];
int i;
char c;
while ((c = getchar()) != EOF) {
file[i] = c;
i++;
}
file[i] = 0;
for (i = 0; i < strlen(file); i++) {
switch (file[i]) {
case '*': i = readuntil(file, i, '*', HEADING); break;
case '[': i = readuntil(file, i, ']', DATE); break;
case '{': i = readuntil(file, i, '}', TODO); break;
case '-': i = readuntil(file, i, '\n', BULLET); break;
case ' ':
case '\n':
case '\t':
break;
default:
eprint("unknown char found!");
}
}
if (header != NULL) printf("%s\n", header);
if ((m & LIST) == LIST) {
formatcheck();
for (int i = 0; i < tokcount; i++) {
if (tokens[i].type == HEADING) {
if ((m & PRETTY) == PRETTY)
printf("\e[1m%s\e[0m \e[4m%s\e[0m \e[2m%s\e[0m\n", tokens[i].data, tokens[i+1].data, tokens[i+2].data);
else
printf("%s %s %s\n", tokens[i].data, tokens[i+1].data, tokens[i+2].data);
i += 2;
}
}
}
if ((m & CHECK) == CHECK) {
formatcheck();
}
if ((m & OUTPUT) == 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 TEXT: printastext(tokens[i]); break;
}
}
}
if (footer != NULL) printf("%s\n", footer);
return 0;
}
|