forked from fletcher/MultiMarkdown-4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.c
576 lines (487 loc) · 13.8 KB
/
writer.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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
writer.c -- General routines for converting parse structure to various
output formats.
(c) 2013 Fletcher T. Penney (http://fletcherpenney.net/).
Derived from peg-multimarkdown, which was forked from peg-markdown,
which is (c) 2008 John MacFarlane (jgm at berkeley dot edu), and
licensed under GNU GPL or MIT.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License or the MIT
license. See LICENSE for details.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#include "writer.h"
/* export_node_tree -- given a tree, export as specified format */
char * export_node_tree(node *list, int format, unsigned long extensions) {
char *output;
GString *out = g_string_new("");
scratch_pad *scratch = mk_scratch_pad(extensions);
scratch->result_tree = list; /* Pointer to result tree to use later */
#ifdef DEBUG_ON
fprintf(stderr, "export_node_tree\n");
#endif
#ifdef DEBUG_ON
fprintf(stderr, "extract_references\n");
#endif
/* Parse for link, images, etc reference definitions */
if ((format != OPML_FORMAT) &&
(format != CRITIC_ACCEPT_FORMAT) &&
(format != CRITIC_REJECT_FORMAT) &&
(format != CRITIC_HTML_HIGHLIGHT_FORMAT))
extract_references(list, scratch);
/* Change our desired format based on metadata */
if (format == LATEX_FORMAT)
format = find_latex_mode(format, list);
switch (format) {
case TEXT_FORMAT:
print_text_node_tree(out, list, scratch);
break;
case HTML_FORMAT:
if (scratch->extensions & EXT_COMPLETE) {
g_string_append_printf(out,
"<!DOCTYPE html>\n<html>\n<head>\n\t<meta charset=\"utf-8\"/>\n");
}
#ifdef DEBUG_ON
fprintf(stderr, "print_html output\n");
#endif
print_html_node_tree(out, list, scratch);
#ifdef DEBUG_ON
fprintf(stderr, "print html endnotes\n");
#endif
print_html_endnotes(out, scratch);
#ifdef DEBUG_ON
fprintf(stderr, "finished printing html endnotes\n");
#endif
if (scratch->extensions & EXT_COMPLETE) {
pad(out,2, scratch);
g_string_append_printf(out, "</body>\n</html>");
}
#ifdef DEBUG_ON
fprintf(stderr, "closed HTML document\n");
#endif
break;
case LATEX_FORMAT:
print_latex_node_tree(out, list, scratch);
break;
case MEMOIR_FORMAT:
print_memoir_node_tree(out, list, scratch);
break;
case BEAMER_FORMAT:
print_beamer_node_tree(out, list, scratch);
break;
case LYX_FORMAT:
perform_lyx_output(out,list,scratch);
break;
case OPML_FORMAT:
#ifdef DEBUG_ON
fprintf(stderr, "export OPML\n");
#endif
begin_opml_output(out, list, scratch);
print_opml_node_tree(out, list, scratch);
end_opml_output(out, list, scratch);
break;
case ODF_FORMAT:
#ifdef DEBUG_ON
fprintf(stderr, "export ODF\n");
#endif
begin_odf_output(out, list, scratch);
print_odf_node_tree(out, list, scratch);
end_odf_output(out, list, scratch);
break;
case RTF_FORMAT:
#ifdef DEBUG_ON
fprintf(stderr, "export RTF\n");
#endif
if (!(scratch->extensions & EXT_SNIPPET))
begin_rtf_output(out, list, scratch);
print_rtf_node_tree(out, list, scratch);
if (!(scratch->extensions & EXT_SNIPPET))
end_rtf_output(out, list, scratch);
break;
case CRITIC_ACCEPT_FORMAT:
print_critic_accept_node_tree(out, list, scratch);
break;
case CRITIC_REJECT_FORMAT:
print_critic_reject_node_tree(out, list, scratch);
break;
case CRITIC_HTML_HIGHLIGHT_FORMAT:
print_critic_html_highlight_node_tree(out, list, scratch);
break;
default:
fprintf(stderr, "Unknown export format = %d\n",format);
exit(EXIT_FAILURE);
}
output = out->str;
g_string_free(out, false);
free_scratch_pad(scratch);
#ifdef DEBUG_ON
fprintf(stderr, "finish export_node_tree\n");
#endif
return output;
}
/* extract_references -- go through node tree and find elements we need to reference;
e.g. links, images, citations, footnotes
Copy them from main parse tree */
void extract_references(node *list, scratch_pad *scratch) {
node *temp;
link_data *l;
while (list != NULL) {
switch (list->key) {
case LINKREFERENCE:
l = list->link_data;
temp = mk_link(list->children, l->label, l->source, l->title, NULL);
temp->link_data->attr = copy_node_tree(l->attr);
/* store copy of link reference */
scratch->links = cons(temp, scratch->links);
break;
case NOTESOURCE:
case GLOSSARYSOURCE:
temp = copy_node(list);
scratch->notes = cons(temp, scratch->notes);
break;
case H1: case H2: case H3: case H4: case H5: case H6:
if ((list->children->key != AUTOLABEL) && !(scratch->extensions & EXT_NO_LABELS)
&& !(scratch->extensions & EXT_COMPATIBILITY)) {
char *label = label_from_node_tree(list->children);
/* create a label from header */
temp = mk_autolink(label);
scratch->links = cons(temp, scratch->links);
free(label);
}
break;
case TABLE:
if (list->children->key != TABLELABEL) {
char *label = label_from_node(list->children);
/* create a label from header */
temp = mk_autolink(label);
scratch->links = cons(temp, scratch->links);
free(label);
}
break;
case HEADINGSECTION:
case RAW:
case LIST:
extract_references(list->children, scratch);
break;
default:
break;
}
list = list->next;
}
}
/* extract_link_data -- given a label, parse the link data and return */
link_data * extract_link_data(char *label, scratch_pad *scratch) {
char *temp;
link_data *d;
node *ref = scratch->links;
bool debug = 0;
if (debug)
fprintf(stderr, "try to extract link for '%s'\n",label);
if ((label == NULL) || (strlen(label) == 0))
return NULL;
temp = clean_string(label);
/* look for label string as is */
while (ref != NULL) {
if (ref->key == KEY_COUNTER) {
ref = ref->next;
continue;
}
if (strcmp(ref->link_data->label, temp) == 0) {
if (debug)
fprintf(stderr,"a:matched %s to %s\n",ref->link_data->label, label);
/* matched */
d = ref->link_data;
d = mk_link_data(d->label, d->source, d->title, d->attr);
free(temp);
return d;
} else {
if (debug)
fprintf(stderr,"a:did not match %s to %s\n",ref->link_data->label, label);
}
ref = ref->next;
}
free(temp);
/* No match. Check for label()version */
if (scratch->extensions & EXT_COMPATIBILITY) {
/* not in compat mode */
return NULL;
}
temp = label_from_string(label);
ref = scratch->links;
while (ref != NULL) {
if (ref->key == KEY_COUNTER) {
ref = ref->next;
continue;
}
if (strcmp(ref->link_data->label, temp) == 0) {
if (debug)
fprintf(stderr,"b:matched %s to %s\n",ref->link_data->label, label);
/* matched */
d = ref->link_data;
d = mk_link_data(d->label, d->source, d->title, d->attr);
free(temp);
return d;
} else {
if (debug)
fprintf(stderr,"b:did not match %s to %s\n",ref->link_data->label, label);
}
ref = ref->next;
}
free(temp);
if (debug)
fprintf(stderr, "finish extract\n");
return NULL;
}
/* pad -- ensure that at least 'x' newlines are at end of output */
void pad(GString *out, int num, scratch_pad *scratch) {
while (num-- > scratch->padded)
g_string_append_c(out, '\n');
scratch->padded = num;
}
/* note_number_for_label -- given a label to match, determine number to be used*/
int note_number_for_label(char *text, scratch_pad *scratch) {
node *n = NULL;
char *clean;
char *label;
#ifdef DEBUG_ON
fprintf(stderr, "find note number for: %s\n",text);
#endif
if ((text == NULL) || (strlen(text) == 0))
return 0; /* Nothing to find */
clean = clean_string(text);
label = label_from_string(clean);
/* have we used this note already? */
/* look for label string as is */
n = node_matching_label(clean, scratch->used_notes);
/* if not, look in reserve queue */
if (n == NULL) {
n = node_matching_label(clean, scratch->notes);
if (n != NULL) {
/* move to used queue */
move_note_to_used(n, scratch);
}
}
/* Check label version */
if (n == NULL)
n = node_matching_label(label, scratch->used_notes);
if (n == NULL) {
n = node_matching_label(label, scratch->notes);
if (n != NULL) {
/* move to used queue */
move_note_to_used(n, scratch);
}
}
/* CAN recursively drill down to start counter at 0 and ++ */
/* if found, move to used queue and return the number */
free(label);
free(clean);
if (n != NULL)
return count_node_from_end(n);
else
return 0;
}
/* note_number_for_node -- given a note reference to match, determine number to be used*/
int note_number_for_node(node *ref, scratch_pad *scratch) {
char *label = ref->str;
node *n = NULL;
int num = 0;
num = note_number_for_label(label, scratch);
if (num > 0)
return num;
/* None found, so treat as inline note */
n = ref->children;
use_inline_footnote(ref, scratch);
return count_node_from_end(n);
}
/* node_matching_label -- given a string, return the node matching the string */
node * node_matching_label(char *label, node *n) {
while (n != NULL) {
if (n->key == KEY_COUNTER) {
n = n->next;
continue;
}
if (strcmp(n->str, label) == 0) {
return n;
}
n = n->next;
}
return NULL;
}
/* since lists are stored in reverse order, need to count from end */
int count_node_from_end(node *n) {
if (n->next == NULL) {
if (n->key == KEY_COUNTER)
return 0;
return 1; /* reserve 0 for not found */
}
return (count_node_from_end(n->next) + 1);
}
/* since lists are stored in reverse order, need to count from end
Only count cites (not footnotes) */
int cite_count_node_from_end(node *n) {
if (n->next == NULL) {
/* we're the last node */
if (n->key == CITATIONSOURCE)
return 1;
return 0; /* reserve 0 for not found */
}
if (n->key == CITATIONSOURCE) {
return (cite_count_node_from_end(n->next) + 1);
} else {
return (cite_count_node_from_end(n->next));
}
}
/* node_for_count -- given a number, get that node */
node * node_for_count(node *n, int count) {
if (n == NULL)
return NULL;
int total = count_node_from_end(n);
if (count > total)
return NULL;
if (count == total)
return n;
while (total > count) {
n = n->next;
if (n == NULL)
return NULL;
total--;
}
return n;
}
/* move_note_to_used -- snip note from ->notes and move to used_notes */
void move_note_to_used(node *list, scratch_pad *scratch) {
node * n = scratch->notes;
node * last = NULL;
while (n != NULL) {
if (n == list) {
if (last != NULL) {
last->next = n->next;
} else {
scratch->notes = n->next;
}
scratch->used_notes = cons(n, scratch->used_notes);
return;
}
last = n;
n = n->next;
}
}
/* use_inline_footnote -- create a new note definition from inline footnote */
void use_inline_footnote(node *ref, scratch_pad *scratch) {
scratch->used_notes = cons(ref->children, scratch->used_notes);
ref->children = NULL;
}
/* find attribute, if present */
node * node_for_attribute(char *querystring, node *list) {
#ifdef DEBUG_ON
fprintf(stderr, "start node_for_attribute\n");
#endif
node *step = NULL;
step = list;
char *query;
if (querystring == NULL)
return NULL;
query = label_from_string(querystring);
#ifdef DEBUG_ON
fprintf(stderr, "node_for_attribute 2: '%s'\n",query);
#endif
while (step != NULL) {
if ((step->str != NULL) && (strcmp(step->str,query) == 0)) {
free(query);
#ifdef DEBUG_ON
fprintf(stderr, "matched node_for_attribute\n");
#endif
return step;
}
#ifdef DEBUG_ON
fprintf(stderr, "'%s' doesn't match '%s'\n",query,step->str);
if (step->next == NULL)
fprintf(stderr, "no next node\n");
#endif
step = step->next;
}
free(query);
#ifdef DEBUG_ON
fprintf(stderr, "stop node_for_attribute\n");
#endif
return NULL;
}
/* convert attribute to dimensions suitable for LaTeX or ODF */
/* returns c string that needs to be freed */
char * dimension_for_attribute(char *querystring, node *list) {
#ifdef DEBUG_ON
fprintf(stderr, "start dimension_for_attribute\n");
#endif
node *attribute;
char *dimension;
char *ptr;
int i;
char *upper;
GString *result;
attribute = node_for_attribute(querystring, list);
if (attribute == NULL) return NULL;
#ifdef DEBUG_ON
fprintf(stderr, "a\n");
#endif
dimension = strdup(attribute->children->str);
upper = strdup(attribute->children->str);
for(i = 0; dimension[ i ]; i++)
dimension[i] = tolower(dimension[ i ]);
for(i = 0; upper[ i ]; i++)
upper[i] = toupper(upper[ i ]);
#ifdef DEBUG_ON
fprintf(stderr, "b\n");
#endif
if (strstr(dimension, "px")) {
ptr = strstr(dimension,"px");
ptr[0] = '\0';
strcat(ptr,"pt");
}
result = g_string_new(dimension);
if ((strcmp(dimension,upper) == 0) && (dimension[strlen(dimension) -1] != '%')) {
/* no units */
g_string_append_printf(result, "pt");
}
free(upper);
free(dimension);
dimension = result->str;
g_string_free(result, false);
#ifdef DEBUG_ON
fprintf(stderr, "finish dimension_for_attribute\n");
#endif
return(dimension);
}
/* Load available info for a link */
link_data * load_link_data(node *n, scratch_pad *scratch) {
link_data *r = NULL;
GString *temp_str = NULL;
char *temp;
r = mk_link_data(n->link_data->label, n->link_data->source, n->link_data->title, n->link_data->attr);
/* Do we have proper info? */
if ((r->label == NULL) &&
(r->source == NULL)) {
/* we seem to be a [foo][] style link */
/* so load a label */
temp_str = g_string_new("");
print_raw_node_tree(temp_str, n->children);
r->label = temp_str->str;
g_string_free(temp_str, FALSE);
}
/* Load data by reference */
if (r->label != NULL) {
temp = strdup(r->label);
r->attr = NULL;
free_link_data(r);
r = extract_link_data(temp, scratch);
if (r == NULL) {
/* return NULL since no definition found */
free(temp);
return NULL;
}
free(temp);
}
return r;
}