~aleteoryx/gloss

ref: cb36f073844d5ebbd05ca30507acc17222ec8357 gloss/markup.c -rw-r--r-- 7.9 KiB
cb36f073Aleteoryx cleanup debug statements 3 months ago
                                                                                
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/* a minimal markup language */

#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>


#define SEG_HEAD 0	// never export
#define SEG_HTML 1	// OK to include in the output
#define SEG_TEXT 2	// raw user-generated text
#define SEG_IESC 3	// should be immediately escaped python-side and treated as html

#ifdef DEBUG
#undef DEBUG
#define PDEBUG(...) fprintf(stderr, __VA_ARGS__);
#define PEXPR(exp) fprintf(stderr, "(%s) = %ld\n", #exp, (long)(exp));
#define DEBUG 1
#else
#define PDEBUG(...)
#define PEXPR(exp)
#define DEBUG 0
#endif

#define PLINE() fprintf(stderr, "on line %d in func %s\n", __LINE__, __FILE__)

#define LEN(x) (sizeof(x) / sizeof(x[0]))

static char *argv0 = "markup";


struct segment {
	int type;
	size_t length;
	const char *text;
	struct segment *next;
};

static int
isword(char c)
{
	return c == '_' || c == '<' || c == '>' || isalnum(c);
}

static void
print_segment(struct segment *seg)
{
	const char *ty;
	switch(seg->type){
	case SEG_HEAD:
		ty = "HEAD";
		break;
	case SEG_HTML:
		ty = "HTML";
		break;
	case SEG_TEXT:
		ty = "TEXT";
		break;
	case SEG_IESC:
		ty = "IESC";
		break;
	}
	if(DEBUG)
		fprintf(stderr, "%s(%04zu)->%p: %.*s\n", ty, seg->length, seg->next, (int)seg->length, seg->text ?: "");
	printf("%s(%04zu): %.*s\n", ty, seg->length, (int)seg->length, seg->text ?: "");
}

static inline struct segment *
new_segment(int type, size_t length, const char *text)
{
	struct segment *new = malloc(sizeof (struct segment));
	new->type = type;
	new->length = length;
	new->text = text;
	new->next = NULL;

	if (DEBUG)
		fprintf(stderr, "new_segment @ %p: %d / %zu / %.*s\n", new, type, length, (int)length, text);

	return new;
}

static inline void
push_segment(struct segment **seg, int type, size_t length, const char *text)
{
	struct segment *new = new_segment(type, length, text);
	(*seg)->next = new;
	*seg = new;
}

static inline void
push_html(struct segment **seg, size_t length, const char *text)
{
	push_segment(seg, SEG_HTML, length, text);
}
static inline void
push_text(struct segment **seg, size_t length, const char *text)
{
	push_segment(seg, SEG_TEXT, length, text);
}
static inline void
push_iesc(struct segment **seg, size_t length, const char *text)
{
	push_segment(seg, SEG_IESC, length, text);
}

/*
 * attempt to convert a string of the form <href[|name]> into either:
 * - H'<a href="'  I[href]  H'">&lt;'  I[href]  H'&gt;</a>'
 * - H'<a href="'  I[href]  H'">'      I[name]      H'</a>'
 */
static struct segment *
try_link(const char **ip, const char *end, struct segment **tailp)
{
	const char *i = *ip+1, *href = i, *hend = NULL, *name = NULL;
	struct segment *head, *tail;

	for(; i < end; i++){
		switch(*i){
		case '|':
			if(name == NULL){
				name = i+1;
				hend = i;
			}
			break;
		case '>':
			if(hend == NULL)
				hend = i;

			head = tail = new_segment(SEG_HTML, 9, "<a href=\"");
			push_iesc(&tail, hend - href, href);
			if(name == NULL){
				push_html(&tail, 5, "\"&lt;");
				push_iesc(&tail, hend - href, href);
				push_html(&tail, 8, "&gt;</a>");
			}else{
				push_html(&tail, 2, "\">");
				push_iesc(&tail, i - name, name);
				push_html(&tail, 4, "</a>");
			}
			
			*ip = i;
			*tailp = tail;
			return head;
		}
	}

	return NULL;
}

static struct segment * // TODO: rewrite with ICU
segmentize(size_t length, const char *source)
{
	// p is the start of the most recent text segment
	const char *p = source, *i, *newi, *end = source + length;
	int in_italic = 0;
	struct segment *head = new_segment(SEG_HEAD, 0, NULL), *ret;
	struct segment *tail = head;
	struct segment *italic_head, *italic_tail;
	struct segment *link_head, *link_tail;

	for(i = source; i < end; i++){
		switch(*i){
		case '\\':
			i++;
			break;
		case '/':
			if(!in_italic){	// begin italic section
				PDEBUG("starting italics @ source[%zu]\n", i - source);
				PEXPR(i != source);
				PEXPR(i != source && isword(i[-1]));
				PEXPR(end - i);
				PEXPR(!isword(i[1]));
				PEXPR((i != source && isword(i[-1])) || (end - i) < 3 || !isword(i[1]));
				if((i != source && isword(i[-1])) ||	// previous char must be ^ or non-word
				   (end - i) < 3 ||			// cannot start near EOB
				   !isword(i[1]))			// next char must be word
				   	continue;
				in_italic = 1;
				PDEBUG("head = %p, head->next = %p, tail = %p\n", head, head->next, tail);
				push_text(&tail, i - p, p);
				PDEBUG("head = %p, head->next = %p, tail = %p\n", head, head->next, tail);
				italic_head = italic_tail = new_segment(SEG_HTML, 3, "<i>");
				
				p = i;
			}else{		// end italic section
				PDEBUG("stopping italics @ source[%zu]\n", i - source);
				PEXPR(!isword(i[-1]));
				PEXPR((i+1) != end);
				PEXPR(isword(i[1]));
				PEXPR(!isword(i[-1]) || ((i+1) != end && isword(i[1])));
				if(!isword(i[-1]) ||			// previous char must be word
				   ((i+1) != end && isword(i[1])))	// next char must be EOB or non-word
					continue;
				
				in_italic = 0;
				push_text(&italic_tail, i - p + 1, p);
				push_html(&italic_tail, 4, "</i>");
				tail->next = italic_head;
				tail = italic_tail;
				p = i + 1;
			}
			break;
		case '<':
			newi = i;
			link_head = try_link(&newi, end, &link_tail);
			if(link_head == NULL){ // reached EOB before link could finish, bail out
				i = end;
				break;
			}
			
			if (DEBUG)
				print_segment(link_tail);
			PDEBUG("*newi = '%c'\n", *newi);
			
			if(in_italic){
				push_text(&italic_tail, i - p, p);
				italic_tail->next = link_head;
				italic_tail = link_tail;
			}else{
				push_text(&tail, i - p, p);
				tail->next = link_head;
				tail = link_tail;
			}
			i = newi;
			p = i+1;
			break;
		}
	}
	if(in_italic){
		PLINE();
		tail->next = italic_head->next;
		if(tail->next != NULL)
			tail = italic_tail;
		
		tail->next = italic_head; // reuse it :D
		tail = tail->next;
		tail->type = SEG_TEXT;
		tail->length = end - p;
		tail->text = p;
		tail->next = NULL;
	}else{
		tail->next = new_segment(SEG_TEXT, end - p, p);
	}

	ret = head->next;
	free(head);
	return ret;
}

static const char *
nextline()
{
	static size_t buflen = 1024;
	static char *buf = NULL;
	size_t off;
	char *p;

	if(buf == NULL)
		buf = malloc(buflen);
	p = buf;
	
	while((*p = getc(stdin)) != EOF){
		if(*p == '\n'){
			*p = '\0';
			return buf;
		}else if(*p == '\0'){
			p--;
		}

		if(++p == buf + buflen){
			off = p - buf;
			buflen *= 3;
			buflen /= 2;
			buf = realloc(buf, buflen);
			p = buf + off;
		}
	}
	return NULL;
}

static void
convert()
{
	size_t length;
	const char *s;
	struct segment *seg;

	while((s = nextline()) != NULL){
		if(DEBUG)
			fprintf(stderr, "processing line: %s\n", s);
		length = strlen(s);
		seg = segmentize(length, s);
		for(; seg != NULL; seg = seg->next)
			print_segment(seg);
		puts("NEXT");
		fflush(stdout);
	}
}

static void
usage()
{
	printf(
	"usage: %s <VERB> [...args]\n\n"
	"VERB may be one of:\n"
	"	test [N]  -  format the in-built test strings, or test string N if provided.\n"
	"	convert   -  read input lines, convert them, and print the result until EOF.\n"
	, argv0);
	exit(1);
}

const static char *strings[] = {
	"This string should be one SEG_TEXT.",
	"And this//",
	"This string should have an /italic/ part.",
	"And this /o/",
	"/This/ string should have /two/.",
	"<link>",
	"<example.com|link text>",
	"how about an /<href|italicized link>/?",
	"but no <href|/partially/ italicized ones>, right?",
	NULL
};

static void
run_test(const char *s)
{
	struct segment *seg;

	printf("\nsegmentizing: \"%s\"(%zu)\n", s, strlen(s));
	seg = segmentize(strlen(s), s);
	for(; seg != NULL; seg = seg->next)
		print_segment(seg);
}

static void
run_testn(int n)
{
	if(n < LEN(strings))
		run_test(strings[n]);
	else{
		fprintf(stderr, "test # out of range: %d\n", n);
		exit(2);
	}
}

static void
run_tests()
{
	const char **s;
	for(s = strings; *s != NULL; s++){
		run_test(*s);
	}
}

int
main(int argc, char **argv)
{
	if(argc < 2)
		usage();
	argv0 = (argv++)[0];
	argc--;

	if(strcmp(argv[0], "test") == 0){
		if(argc > 1)
			run_testn(atoi(argv[1]));
		else
			run_tests();
	}else if(strcmp(argv[0], "convert") == 0)
		convert();
	else
		usage();

	return 0;
}