Debian Codegraph @

ext/mkdio.c

raw symbols SCIP
1 /*
2 * mkdio -- markdown front end input functions
3 *
4 * Copyright (C) 2007 David L Parsons.
5 * The redistribution terms are provided in the COPYRIGHT file that must
6 * be distributed with this source code.
7 */
8 #include "config.h"
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <ctype.h>
12
13 #include "cstring.h"
14 #include "markdown.h"
15 #include "amalloc.h"
16
17 typedef ANCHOR(Line) LineAnchor;
18
19
20 /* create a new blank Document
21 */
22 Document*
23 __mkd_new_Document()
24 {
25 Document *ret = calloc(sizeof(Document), 1);
26
27 if ( ret ) {
28 if ( ret->ctx = calloc(sizeof(MMIOT), 1) ) {
29 ret->magic = VALID_DOCUMENT;
30 return ret;
31 }
32 free(ret);
33 }
34 return 0;
35 }
36
37
38 /* add a line to the markdown input chain, expanding tabs and
39 * noting the presence of special characters as we go.
40 */
41 void
42 __mkd_enqueue(Document* a, Cstring *line)
43 {
44 Line *p = calloc(sizeof *p, 1);
45 unsigned char c;
46 int xp = 0;
47 int size = S(*line);
48 unsigned char *str = (unsigned char*)T(*line);
49
50 CREATE(p->text);
51 ATTACH(a->content, p);
52
53 while ( size-- ) {
54 if ( (c = *str++) == '\t' ) {
55 /* expand tabs into ->tabstop spaces. We use ->tabstop
56 * because the ENTIRE FREAKING COMPUTER WORLD uses editors
57 * that don't do ^T/^D, but instead use tabs for indentation,
58 * and, of course, set their tabs down to 4 spaces
59 */
60 do {
61 EXPAND(p->text) = ' ';
62 } while ( ++xp % a->tabstop );
63 }
64 else if ( c >= ' ' ) {
65 if ( c == '|' )
66 p->flags |= PIPECHAR;
67 EXPAND(p->text) = c;
68 ++xp;
69 }
70 }
71 EXPAND(p->text) = 0;
72 S(p->text)--;
73 p->dle = mkd_firstnonblank(p);
74 }
75
76
77 /* trim leading characters from a line, then adjust the dle.
78 */
79 void
80 __mkd_trim_line(Line *p, int clip)
81 {
82 if ( clip >= S(p->text) ) {
83 S(p->text) = p->dle = 0;
84 T(p->text)[0] = 0;
85 }
86 else if ( clip > 0 ) {
87 CLIP(p->text, 0, clip);
88 p->dle = mkd_firstnonblank(p);
89 }
90 }
91
92
93 /* build a Document from any old input.
94 */
95 typedef int (*getc_func)(void*);
96
97 Document *
98 populate(getc_func getc, void* ctx, mkd_flag_t flags)
99 {
100 Cstring line;
101 Document *a = __mkd_new_Document();
102 int c;
103 int pandoc = 0;
104
105 if ( !a ) return 0;
106
107 a->tabstop = is_flag_set(flags, MKD_TABSTOP) ? 4 : TABSTOP;
108
109 CREATE(line);
110
111 while ( (c = (*getc)(ctx)) != EOF ) {
112 if ( c == '\n' ) {
113 if ( pandoc != EOF && pandoc < 3 ) {
114 if ( S(line) && (T(line)[0] == '%') )
115 pandoc++;
116 else
117 pandoc = EOF;
118 }
119 __mkd_enqueue(a, &line);
120 S(line) = 0;
121 }
122 else if ( isprint(c) || isspace(c) || (c & 0x80) )
123 EXPAND(line) = c;
124 }
125
126 if ( S(line) )
127 __mkd_enqueue(a, &line);
128
129 DELETE(line);
130
131 if ( (pandoc == 3) && !(is_flag_set(flags, MKD_NOHEADER) || is_flag_set(flags, MKD_STRICT)) ) {
132 /* the first three lines started with %, so we have a header.
133 * clip the first three lines out of content and hang them
134 * off header.
135 */
136 Line *headers = T(a->content);
137
138 a->title = headers; __mkd_trim_line(a->title, 1);
139 a->author= headers->next; __mkd_trim_line(a->author, 1);
140 a->date = headers->next->next; __mkd_trim_line(a->date, 1);
141
142 T(a->content) = headers->next->next->next;
143 }
144
145 return a;
146 }
147
148
149 /* convert a file into a linked list
150 */
151 Document *
152 mkd_in(FILE *f, mkd_flag_t flags)
153 {
154 return populate((getc_func)fgetc, f, flags & INPUT_MASK);
155 }
156
157
158 /* return a single character out of a buffer
159 */
160 int
161 __mkd_io_strget(struct string_stream *in)
162 {
163 if ( !in->size ) return EOF;
164
165 --(in->size);
166
167 return *(in->data)++;
168 }
169
170
171 /* convert a block of text into a linked list
172 */
173 Document *
174 mkd_string(const char *buf, int len, mkd_flag_t flags)
175 {
176 struct string_stream about;
177
178 about.data = buf;
179 about.size = len;
180
181 return populate((getc_func)__mkd_io_strget, &about, flags & INPUT_MASK);
182 }
183
184
185 /* write the html to a file (xmlified if necessary)
186 */
187 int
188 mkd_generatehtml(Document *p, FILE *output)
189 {
190 char *doc;
191 int szdoc;
192
193 DO_OR_DIE( szdoc = mkd_document(p,&doc) );
194 if ( is_flag_set(p->ctx->flags, MKD_CDATA) )
195 DO_OR_DIE( mkd_generatexml(doc, szdoc, output) );
196 else if ( fwrite(doc, szdoc, 1, output) != 1 )
197 return EOF;
198 DO_OR_DIE( putc('\n', output) );
199 return 0;
200 }
201
202
203 /* convert some markdown text to html
204 */
205 int
206 markdown(Document *document, FILE *out, mkd_flag_t flags)
207 {
208 if ( mkd_compile(document, flags) ) {
209 mkd_generatehtml(document, out);
210 mkd_cleanup(document);
211 return 0;
212 }
213 return -1;
214 }
215
216
217 /* anchor_format a string, returning the formatted string in malloc()ed space
218 * MKD_URLENCODEDANCHOR is now perverted to being a html5 anchor
219 *
220 * !labelformat: print all characters
221 * labelformat && h4anchor: prefix nonalpha label with L,
222 * expand all nonalnum, _, ':', '.' to hex
223 * except space which maps to -
224 * labelformat && !h4anchor:expand space to -, other isspace() & '%' to hex
225 */
226 static char *
227 mkd_anchor_format(char *s, int len, int labelformat, mkd_flag_t flags)
228 {
229 char *res;
230 unsigned char c;
231 int i, needed, out = 0;
232 int h4anchor = !is_flag_set(flags, MKD_URLENCODEDANCHOR);
233 static const unsigned char hexchars[] = "0123456789abcdef";
234
235 needed = (labelformat ? (4*len) : len) + 2 /* 'L', trailing null */;
236
237 if ( (res = malloc(needed)) == NULL )
238 return NULL;
239
240 if ( h4anchor && labelformat && !isalpha(s[0]) )
241 res[out++] = 'L';
242
243
244 for ( i=0; i < len ; i++ ) {
245 c = s[i];
246 if ( labelformat ) {
247 if ( h4anchor
248 ? (isalnum(c) || (c == '_') || (c == ':') || (c == '.' ) )
249 : !(isspace(c) || c == '%') )
250 res[out++] = c;
251 else if ( c == ' ' )
252 res[out++] = '-';
253 else {
254 res[out++] = h4anchor ? '-' : '%';
255 res[out++] = hexchars[c >> 4 & 0xf];
256 res[out++] = hexchars[c & 0xf];
257 if ( h4anchor )
258 res[out++] = '-';
259 }
260 }
261 else
262 res[out++] = c;
263 }
264
265 res[out++] = 0;
266 return res;
267 } /* mkd_anchor_format */
268
269
270 /* write out a Cstring, mangled into a form suitable for `<a href=` or `<a id=`
271 */
272 void
273 mkd_string_to_anchor(char *s, int len, mkd_sta_function_t outchar,
274 void *out, int labelformat,
275 MMIOT *f)
276 {
277 char *res;
278 char *line;
279 int size;
280
281 int i;
282
283 size = mkd_line(s, len, &line, IS_LABEL);
284
285 if ( !line )
286 return;
287
288 if ( f->cb->e_anchor )
289 res = (*(f->cb->e_anchor))(line, size, f->cb->e_data);
290 else
291 res = mkd_anchor_format(line, size, labelformat, f->flags);
292
293 free(line);
294
295 if ( !res )
296 return;
297
298 for ( i=0; res[i]; i++ )
299 (*outchar)(res[i], out);
300
301 if ( f->cb->e_anchor ) {
302 if ( f->cb->e_free )
303 (*(f->cb->e_free))(res, f->cb->e_data);
304 }
305 else
306 free(res);
307 }
308
309
310 /* ___mkd_reparse() a line
311 */
312 static void
313 mkd_parse_line(char *bfr, int size, MMIOT *f, mkd_flag_t flags)
314 {
315 ___mkd_initmmiot(f, 0);
316 f->flags = flags & USER_FLAGS;
317 ___mkd_reparse(bfr, size, 0, f, 0);
318 ___mkd_emblock(f);
319 }
320
321
322 /* ___mkd_reparse() a line, returning it in malloc()ed memory
323 */
324 int
325 mkd_line(char *bfr, int size, char **res, mkd_flag_t flags)
326 {
327 MMIOT f;
328 int len;
329
330 mkd_parse_line(bfr, size, &f, flags);
331
332 if ( len = S(f.out) ) {
333 EXPAND(f.out) = 0;
334 /* strdup() doesn't use amalloc(), so in an amalloc()ed
335 * build this copies the string safely out of our memory
336 * paranoia arena. In a non-amalloc world, it's a spurious
337 * memory allocation, but it avoids unintentional hilarity
338 * with amalloc()
339 */
340 *res = strdup(T(f.out));
341 }
342 else {
343 *res = 0;
344 len = EOF;
345 }
346 ___mkd_freemmiot(&f, 0);
347 return len;
348 }
349
350
351 /* ___mkd_reparse() a line, writing it to a FILE
352 */
353 int
354 mkd_generateline(char *bfr, int size, FILE *output, mkd_flag_t flags)
355 {
356 MMIOT f;
357 int status;
358
359 mkd_parse_line(bfr, size, &f, flags);
360 if ( is_flag_set(flags, MKD_CDATA) )
361 status = mkd_generatexml(T(f.out), S(f.out), output) != EOF;
362 else
363 status = fwrite(T(f.out), S(f.out), 1, output) == S(f.out);
364
365 ___mkd_freemmiot(&f, 0);
366 return status ? 0 : EOF;
367 }
368
369
370 /* set the url display callback
371 */
372 void
373 mkd_e_url(Document *f, mkd_callback_t edit)
374 {
375 if ( f ) {
376 if ( f->cb.e_url != edit )
377 f->dirty = 1;
378 f->cb.e_url = edit;
379 }
380 }
381
382
383 /* set the url options callback
384 */
385 void
386 mkd_e_flags(Document *f, mkd_callback_t edit)
387 {
388 if ( f ) {
389 if ( f->cb.e_flags != edit )
390 f->dirty = 1;
391 f->cb.e_flags = edit;
392 }
393 }
394
395
396 /* set the anchor formatter
397 */
398 void
399 mkd_e_anchor(Document *f, mkd_callback_t format)
400 {
401 if ( f ) {
402 if ( f->cb.e_anchor != format )
403 f->dirty = 1;
404 f->cb.e_anchor = format;
405 }
406 }
407
408
409 /* set the url display/options deallocator
410 */
411 void
412 mkd_e_free(Document *f, mkd_free_t dealloc)
413 {
414 if ( f ) {
415 if ( f->cb.e_free != dealloc )
416 f->dirty = 1;
417 f->cb.e_free = dealloc;
418 }
419 }
420
421
422 /* set the url display/options context data field
423 */
424 void
425 mkd_e_data(Document *f, void *data)
426 {
427 if ( f ) {
428 if ( f->cb.e_data != data )
429 f->dirty = 1;
430 f->cb.e_data = data;
431 }
432 }
433
434
435 /* set the code block display callback
436 */
437 void
438 mkd_e_code_format(Document *f, mkd_callback_t codefmt)
439 {
440 if ( f && (f->cb.e_codefmt != codefmt) ) {
441 f->dirty = 1;
442 f->cb.e_codefmt = codefmt;
443 }
444 }
445
446
447 /* set the href prefix for markdown extra style footnotes
448 */
449 void
450 mkd_ref_prefix(Document *f, char *data)
451 {
452 if ( f ) {
453 if ( f->ref_prefix != data )
454 f->dirty = 1;
455 f->ref_prefix = data;
456 }
457 }