-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser_gemtext.c
317 lines (262 loc) · 6.58 KB
/
parser_gemtext.c
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* Copyright (c) 2021, 2022 Omar Polo <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* A streaming gemtext parser.
*
* TODO:
* - handle NULs
* - UTF8
*/
#include "compat.h"
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include "defaults.h"
#include "parser.h"
#include "telescope.h"
#include "utf8.h"
#include "xwrapper.h"
static int gemtext_parse_line(struct buffer *, const char *, size_t);
static int gemtext_free(struct buffer *);
static int gemtext_serialize(struct buffer *, FILE *);
static int parse_link(struct buffer *, const char*, size_t);
static int parse_title(struct buffer *, const char*, size_t);
static void search_title(struct buffer *, enum line_type);
const struct parser gemtext_parser = {
.name = "text/gemini",
.parseline = &gemtext_parse_line,
.free = &gemtext_free,
.serialize = &gemtext_serialize,
};
static inline int
emit_line(struct buffer *b, enum line_type type, char *line, char *alt)
{
struct line *l;
l = xcalloc(1, sizeof(*l));
l->type = type;
l->line = line;
l->alt = alt;
switch (l->type) {
case LINE_PRE_START:
case LINE_PRE_END:
if (hide_pre_context)
l->flags = L_HIDDEN;
if (l->type == LINE_PRE_END &&
hide_pre_closing_line)
l->flags = L_HIDDEN;
break;
case LINE_PRE_CONTENT:
if (hide_pre_blocks)
l->flags = L_HIDDEN;
break;
case LINE_LINK:
if (emojify_link &&
!emojied_line(line, (const char **)&l->data))
l->data = NULL;
break;
default:
break;
}
if (dont_apply_styling)
l->flags &= ~L_HIDDEN;
TAILQ_INSERT_TAIL(&b->head, l, lines);
return 1;
}
static int
parse_link(struct buffer *b, const char *line, size_t len)
{
char *label, *url;
const char *start;
if (len <= 2)
return emit_line(b, LINE_TEXT, NULL, NULL);
line += 2, len -= 2;
while (len > 0 && isspace((unsigned char)line[0]))
line++, len--;
if (len == 0)
return emit_line(b, LINE_TEXT, NULL, NULL);
start = line;
while (len > 0 && !isspace((unsigned char)line[0]))
line++, len--;
url = xstrndup(start, line - start);
while (len > 0 && isspace(line[0]))
line++, len--;
if (len == 0) {
label = xstrdup(url);
} else {
label = xstrndup(line, len);
}
return emit_line(b, LINE_LINK, label, url);
}
static int
parse_title(struct buffer *b, const char *line, size_t len)
{
enum line_type t = LINE_TITLE_1;
char *l;
line++, len--;
while (len > 0 && *line == '#') {
line++, len--;
t++;
if (t == LINE_TITLE_3)
break;
}
while (len > 0 && isspace((unsigned char)*line))
line++, len--;
if (len == 0)
return emit_line(b, t, NULL, NULL);
if (t == LINE_TITLE_1 && *b->title == '\0')
strncpy(b->title, line, MIN(sizeof(b->title)-1, len));
l = xstrndup(line, len);
return emit_line(b, t, l, NULL);
}
static int
gemtext_parse_line(struct buffer *b, const char *line, size_t len)
{
char *l;
if (b->parser_flags & PARSER_IN_PRE) {
if (len >= 3 && !strncmp(line, "```", 3)) {
b->parser_flags ^= PARSER_IN_PRE;
return emit_line(b, LINE_PRE_END, NULL, NULL);
}
if (len == 0)
return emit_line(b, LINE_PRE_CONTENT, NULL, NULL);
l = xstrndup(line, len);
return emit_line(b, LINE_PRE_CONTENT, l, NULL);
}
if (len == 0)
return emit_line(b, LINE_TEXT, NULL, NULL);
switch (*line) {
case '*':
if (len < 1 || line[1] != ' ')
break;
line += 2, len -= 2;
while (len > 0 && isspace((unsigned char)*line))
line++, len--;
if (len == 0)
return emit_line(b, LINE_ITEM, NULL, NULL);
l = xstrndup(line, len);
return emit_line(b, LINE_ITEM, l, NULL);
case '>':
line++, len--;
while (len > 0 && isspace((unsigned char)*line))
line++, len--;
if (len == 0)
return emit_line(b, LINE_QUOTE, NULL, NULL);
l = xstrndup(line, len);
return emit_line(b, LINE_QUOTE, l, NULL);
case '=':
if (len > 1 && line[1] == '>')
return parse_link(b, line, len);
break;
case '#':
return parse_title(b, line, len);
case '`':
if (len < 3 || strncmp(line, "```", 3) != 0)
break;
b->parser_flags |= PARSER_IN_PRE;
line += 3, len -= 3;
while (len > 0 && isspace((unsigned char)*line))
line++, len--;
if (len == 0)
return emit_line(b, LINE_PRE_START,
NULL, NULL);
l = xstrndup(line, len);
return emit_line(b, LINE_PRE_START, l, NULL);
}
l = xstrndup(line, len);
return emit_line(b, LINE_TEXT, l, NULL);
}
static int
gemtext_free(struct buffer *b)
{
/* flush the buffer */
if (b->len != 0) {
if (!gemtext_parse_line(b, b->buf, b->len))
return 0;
if ((b->parser_flags & PARSER_IN_PRE) &&
!emit_line(b, LINE_PRE_END, NULL, NULL))
return 0;
}
/*
* use the first level 2 or 3 header as page title if none
* found yet.
*/
if (*b->title == '\0')
search_title(b, LINE_TITLE_2);
if (*b->title == '\0')
search_title(b, LINE_TITLE_3);
return 1;
}
static void
search_title(struct buffer *b, enum line_type level)
{
struct line *l;
TAILQ_FOREACH(l, &b->head, lines) {
if (l->type == level) {
if (l->line == NULL)
continue;
strlcpy(b->title, l->line, sizeof(b->title));
break;
}
}
}
static const char *gemtext_prefixes[] = {
[LINE_TEXT] = "",
[LINE_TITLE_1] = "# ",
[LINE_TITLE_2] = "## ",
[LINE_TITLE_3] = "### ",
[LINE_ITEM] = "* ",
[LINE_QUOTE] = "> ",
[LINE_PRE_START] = "``` ",
[LINE_PRE_CONTENT] = "",
[LINE_PRE_END] = "```",
};
static int
gemtext_serialize(struct buffer *b, FILE *fp)
{
struct line *line;
const char *text;
const char *alt;
int r;
TAILQ_FOREACH(line, &b->head, lines) {
if ((text = line->line) == NULL)
text = "";
if ((alt = line->alt) == NULL)
alt = "";
switch (line->type) {
case LINE_TEXT:
case LINE_TITLE_1:
case LINE_TITLE_2:
case LINE_TITLE_3:
case LINE_ITEM:
case LINE_QUOTE:
case LINE_PRE_START:
case LINE_PRE_CONTENT:
case LINE_PRE_END:
r = fprintf(fp, "%s%s\n", gemtext_prefixes[line->type],
text);
break;
case LINE_LINK:
r = fprintf(fp, "=> %s %s\n", alt, text);
break;
default:
/* not reached */
abort();
}
if (r == -1)
return 0;
}
return 1;
}