-
Notifications
You must be signed in to change notification settings - Fork 35
/
html.c
1009 lines (851 loc) · 20.8 KB
/
html.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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $Id$ */
/*
* Copyright (c) 2008, Natacha Porté
* Copyright (c) 2011, Vicent Martí
* Copyright (c) 2014, Xavier Mendez, Devin Torres and the Hoedown authors
* Copyright (c) 2016--2017, 2020 Kristaps Dzonsons <kristaps@bsd.lv>
*
* 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.
*/
#include "config.h"
#if HAVE_SYS_QUEUE
# include <sys/queue.h>
#endif
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "lowdown.h"
#include "extern.h"
/*
* Queue entry for header names.
* Keep these so we can make sure that headers have a unique "id" for
* themselves.
*/
struct hentry {
char *str; /* header name (raw) */
size_t count; /* references */
TAILQ_ENTRY(hentry) entries;
};
/*
* Our internal state object.
*/
struct hstate {
TAILQ_HEAD(, hentry) headers_used;
unsigned int flags; /* output flags */
};
enum rndr_meta_key {
RNDR_META_AFFIL,
RNDR_META_AUTHOR,
RNDR_META_CSS,
RNDR_META_DATE,
RNDR_META_RCSAUTHOR,
RNDR_META_RCSDATE,
RNDR_META_SCRIPT,
RNDR_META_TITLE,
RNDR_META__MAX
};
static const char *rndr_meta_keys[RNDR_META__MAX] = {
"affiliation", /* RNDR_META_AFFIL */
"author", /* RNDR_META_AUTHOR */
"css", /* RNDR_META_CSS */
"date", /* RNDR_META_DATE */
"rcsauthor", /* RNDR_META_RCSAUTHOR */
"rcsdate", /* RNDR_META_RCSDATE */
"javascript", /* RNDR_META_SCRIPT */
"title", /* RNDR_META_TITLE */
};
static void
escape_html(hbuf *ob, const char *source, size_t length)
{
hesc_html(ob, source, length, 0);
}
static void
escape_href(hbuf *ob, const char *source, size_t length)
{
hesc_href(ob, source, length);
}
static void
rndr_autolink(hbuf *ob, const hbuf *link, enum halink_type type)
{
if (link->size == 0)
return;
HBUF_PUTSL(ob, "<a href=\"");
if (type == HALINK_EMAIL)
HBUF_PUTSL(ob, "mailto:");
escape_href(ob, link->data, link->size);
HBUF_PUTSL(ob, "\">");
/*
* Pretty printing: if we get an email address as
* an actual URI, e.g. `mailto:foo@bar.com`, we don't
* want to print the `mailto:` prefix
*/
if (hbuf_prefix(link, "mailto:") == 0)
escape_html(ob, link->data + 7, link->size - 7);
else
escape_html(ob, link->data, link->size);
HBUF_PUTSL(ob, "</a>");
}
static void
rndr_blockcode(hbuf *ob, const hbuf *text, const hbuf *lang)
{
if (ob->size)
hbuf_putc(ob, '\n');
if (lang->size) {
HBUF_PUTSL(ob, "<pre><code class=\"language-");
escape_html(ob, lang->data, lang->size);
HBUF_PUTSL(ob, "\">");
} else
HBUF_PUTSL(ob, "<pre><code>");
escape_html(ob, text->data, text->size);
HBUF_PUTSL(ob, "</code></pre>\n");
}
static void
rndr_blockquote(hbuf *ob, const hbuf *content)
{
if (ob->size)
hbuf_putc(ob, '\n');
HBUF_PUTSL(ob, "<blockquote>\n");
hbuf_put(ob, content->data, content->size);
HBUF_PUTSL(ob, "</blockquote>\n");
}
static void
rndr_codespan(hbuf *ob, const hbuf *text)
{
HBUF_PUTSL(ob, "<code>");
escape_html(ob, text->data, text->size);
HBUF_PUTSL(ob, "</code>");
}
static void
rndr_strikethrough(hbuf *ob, const hbuf *content)
{
HBUF_PUTSL(ob, "<del>");
hbuf_putb(ob, content);
HBUF_PUTSL(ob, "</del>");
}
static void
rndr_double_emphasis(hbuf *ob, const hbuf *content)
{
HBUF_PUTSL(ob, "<strong>");
hbuf_putb(ob, content);
HBUF_PUTSL(ob, "</strong>");
}
static void
rndr_emphasis(hbuf *ob, const hbuf *content)
{
HBUF_PUTSL(ob, "<em>");
hbuf_putb(ob, content);
HBUF_PUTSL(ob, "</em>");
}
static void
rndr_highlight(hbuf *ob, const hbuf *content)
{
HBUF_PUTSL(ob, "<mark>");
hbuf_putb(ob, content);
HBUF_PUTSL(ob, "</mark>");
}
static void
rndr_linebreak(hbuf *ob)
{
HBUF_PUTSL(ob, "<br/>\n");
}
/*
* Given the header with non-empty content "header", fill "ob" with the
* identifier used for the header.
* This will reference-count the header so we don't have duplicates.
*/
static void
rndr_header_id(hbuf *ob, const hbuf *header, struct hstate *state)
{
struct hentry *hentry;
/*
* See if the header was previously already defind.
* Note that in HTML5, the identifier is case sensitive.
*/
TAILQ_FOREACH(hentry, &state->headers_used, entries) {
if (strlen(hentry->str) != header->size)
continue;
if (strncmp(hentry->str,
header->data, header->size) == 0)
break;
}
/* Convert to escaped values. */
escape_href(ob, header->data, header->size);
/*
* If we're non-unique, then append a "count" value.
* XXX: if we have a header named "foo-2", then two headers
* named "foo", we'll inadvertently have a collision.
* This is a bit much to keep track of, though...
*/
if (hentry != NULL) {
hentry->count++;
hbuf_printf(ob, "-%zu", hentry->count);
return;
}
/* Create new header entry. */
hentry = xcalloc(1, sizeof(struct hentry));
hentry->count = 1;
hentry->str = xstrndup(header->data, header->size);
TAILQ_INSERT_TAIL(&state->headers_used, hentry, entries);
}
static void
rndr_header(hbuf *ob, const hbuf *content,
int level, struct hstate *state)
{
if (ob->size)
hbuf_putc(ob, '\n');
if (content->size && (state->flags & LOWDOWN_HTML_HEAD_IDS)) {
hbuf_printf(ob, "<h%d id=\"", level);
rndr_header_id(ob, content, state);
HBUF_PUTSL(ob, "\">");
} else
hbuf_printf(ob, "<h%d>", level);
hbuf_putb(ob, content);
hbuf_printf(ob, "</h%d>\n", level);
}
static void
rndr_link(hbuf *ob, const hbuf *content, const hbuf *link, const hbuf *title)
{
HBUF_PUTSL(ob, "<a href=\"");
escape_href(ob, link->data, link->size);
if (title->size) {
HBUF_PUTSL(ob, "\" title=\"");
escape_html(ob, title->data, title->size);
}
HBUF_PUTSL(ob, "\">");
hbuf_putb(ob, content);
HBUF_PUTSL(ob, "</a>");
}
static void
rndr_list(hbuf *ob, const hbuf *content, const struct rndr_list *p)
{
if (ob->size)
hbuf_putc(ob, '\n');
if ((p->flags & HLIST_FL_ORDERED)) {
if (p->start[0] != '\0')
hbuf_printf(ob, "<ol start=\"%s\">\n", p->start);
else
HBUF_PUTSL(ob, "<ol>\n");
} else
HBUF_PUTSL(ob, "<ul>\n");
hbuf_putb(ob, content);
if ((p->flags & HLIST_FL_ORDERED))
HBUF_PUTSL(ob, "</ol>\n");
else
HBUF_PUTSL(ob, "</ul>\n");
}
static void
rndr_listitem(hbuf *ob, const hbuf *content,
enum hlist_fl flags, size_t num)
{
size_t size;
HBUF_PUTSL(ob, "<li>");
/*
* Cut off any trailing space.
* XXX: this should be removed.
*/
if (content) {
size = content->size;
while (size && content->data[size - 1] == '\n')
size--;
hbuf_put(ob, content->data, size);
}
HBUF_PUTSL(ob, "</li>\n");
}
static void
rndr_paragraph(hbuf *ob, const hbuf *content, struct hstate *state)
{
size_t i = 0, org;
if (ob->size)
hbuf_putc(ob, '\n');
if (content->size == 0)
return;
while (i < content->size &&
isspace((unsigned char)content->data[i]))
i++;
if (i == content->size)
return;
HBUF_PUTSL(ob, "<p>");
if (state->flags & LOWDOWN_HTML_HARD_WRAP) {
while (i < content->size) {
org = i;
while (i < content->size && content->data[i] != '\n')
i++;
if (i > org)
hbuf_put(ob, content->data + org, i - org);
/*
* do not insert a line break if this newline
* is the last character on the paragraph
*/
if (i >= content->size - 1)
break;
rndr_linebreak(ob);
i++;
}
} else
hbuf_put(ob, content->data + i, content->size - i);
HBUF_PUTSL(ob, "</p>\n");
}
/*
* FIXME: verify behaviour.
*/
static void
rndr_raw_block(hbuf *ob, const hbuf *text, const struct hstate *state)
{
size_t org, sz;
if ((state->flags & LOWDOWN_HTML_SKIP_HTML) ||
(state->flags & LOWDOWN_HTML_ESCAPE)) {
escape_html(ob, text->data, text->size);
return;
}
/*
* FIXME: Do we *really* need to trim the HTML? How does that
* make a difference?
*/
sz = text->size;
while (sz > 0 && text->data[sz - 1] == '\n')
sz--;
org = 0;
while (org < sz && text->data[org] == '\n')
org++;
if (org >= sz)
return;
if (ob->size)
hbuf_putc(ob, '\n');
hbuf_put(ob, text->data + org, sz - org);
hbuf_putc(ob, '\n');
}
static void
rndr_triple_emphasis(hbuf *ob, const hbuf *content)
{
HBUF_PUTSL(ob, "<strong><em>");
hbuf_putb(ob, content);
HBUF_PUTSL(ob, "</em></strong>");
}
static void
rndr_hrule(hbuf *ob)
{
if (ob->size)
hbuf_putc(ob, '\n');
hbuf_puts(ob, "<hr/>\n");
}
static void
rndr_image(hbuf *ob, const hbuf *link, const hbuf *title,
const hbuf *dims, const hbuf *alt)
{
char dimbuf[32];
int x, y, rc = 0;
/*
* Scan in our dimensions, if applicable.
* It's unreasonable for them to be over 32 characters, so use
* that as a cap to the size.
*/
if (dims->size && dims->size < sizeof(dimbuf) - 1) {
memset(dimbuf, 0, sizeof(dimbuf));
memcpy(dimbuf, dims->data, dims->size);
rc = sscanf(dimbuf, "%ux%u", &x, &y);
}
/* Require an "alt", even if blank. */
HBUF_PUTSL(ob, "<img src=\"");
escape_href(ob, link->data, link->size);
HBUF_PUTSL(ob, "\" alt=\"");
escape_html(ob, alt->data, alt->size);
HBUF_PUTSL(ob, "\"");
if (dims->size && rc > 0) {
hbuf_printf(ob, " width=\"%u\"", x);
if (rc > 1)
hbuf_printf(ob, " height=\"%u\"", y);
}
if (title->size) {
HBUF_PUTSL(ob, " title=\"");
escape_html(ob, title->data, title->size);
HBUF_PUTSL(ob, "\"");
}
hbuf_puts(ob, " />");
}
static void
rndr_raw_html(hbuf *ob, const hbuf *text, const struct hstate *state)
{
/*
* ESCAPE overrides SKIP_HTML.
* It doesn't look to see if there are any valid tags, just
* escapes all of them.
*/
if ((state->flags & LOWDOWN_HTML_ESCAPE)) {
escape_html(ob, text->data, text->size);
return;
}
if ((state->flags & LOWDOWN_HTML_SKIP_HTML))
return;
hbuf_putb(ob, text);
}
static void
rndr_table(hbuf *ob, const hbuf *content)
{
if (ob->size)
hbuf_putc(ob, '\n');
HBUF_PUTSL(ob, "<table>\n");
hbuf_putb(ob, content);
HBUF_PUTSL(ob, "</table>\n");
}
static void
rndr_table_header(hbuf *ob, const hbuf *content,
const enum htbl_flags *fl, size_t columns)
{
if (ob->size)
hbuf_putc(ob, '\n');
HBUF_PUTSL(ob, "<thead>\n");
hbuf_putb(ob, content);
HBUF_PUTSL(ob, "</thead>\n");
}
static void
rndr_table_body(hbuf *ob, const hbuf *content)
{
if (ob->size)
hbuf_putc(ob, '\n');
HBUF_PUTSL(ob, "<tbody>\n");
hbuf_putb(ob, content);
HBUF_PUTSL(ob, "</tbody>\n");
}
static void
rndr_tablerow(hbuf *ob, const hbuf *content)
{
HBUF_PUTSL(ob, "<tr>\n");
hbuf_putb(ob, content);
HBUF_PUTSL(ob, "</tr>\n");
}
static void
rndr_tablecell(hbuf *ob, const hbuf *content,
enum htbl_flags flags, size_t col, size_t columns)
{
if ((flags & HTBL_FL_HEADER))
HBUF_PUTSL(ob, "<th");
else
HBUF_PUTSL(ob, "<td");
switch (flags & HTBL_FL_ALIGNMASK) {
case HTBL_FL_ALIGN_CENTER:
HBUF_PUTSL(ob, " style=\"text-align: center\">");
break;
case HTBL_FL_ALIGN_LEFT:
HBUF_PUTSL(ob, " style=\"text-align: left\">");
break;
case HTBL_FL_ALIGN_RIGHT:
HBUF_PUTSL(ob, " style=\"text-align: right\">");
break;
default:
HBUF_PUTSL(ob, ">");
}
hbuf_putb(ob, content);
if ((flags & HTBL_FL_HEADER))
HBUF_PUTSL(ob, "</th>\n");
else
HBUF_PUTSL(ob, "</td>\n");
}
static void
rndr_superscript(hbuf *ob, const hbuf *content)
{
HBUF_PUTSL(ob, "<sup>");
hbuf_putb(ob, content);
HBUF_PUTSL(ob, "</sup>");
}
static void
rndr_normal_text(hbuf *ob, const hbuf *content)
{
if (content)
escape_html(ob, content->data, content->size);
}
static void
rndr_footnotes(hbuf *ob, const hbuf *content)
{
if (ob->size)
hbuf_putc(ob, '\n');
HBUF_PUTSL(ob, "<div class=\"footnotes\">\n");
hbuf_puts(ob, "<hr/>\n");
HBUF_PUTSL(ob, "<ol>\n");
hbuf_putb(ob, content);
HBUF_PUTSL(ob, "\n</ol>\n</div>\n");
}
static void
rndr_footnote_def(hbuf *ob, const hbuf *content, unsigned int num)
{
size_t i = 0;
int pfound = 0;
/* Insert anchor at the end of first paragraph block. */
while ((i+3) < content->size) {
if (content->data[i++] != '<')
continue;
if (content->data[i++] != '/')
continue;
if (content->data[i++] != 'p' &&
content->data[i] != 'P')
continue;
if (content->data[i] != '>')
continue;
i -= 3;
pfound = 1;
break;
}
hbuf_printf(ob, "\n<li id=\"fn%d\">\n", num);
if (pfound) {
hbuf_put(ob, content->data, i);
hbuf_printf(ob, " "
"<a href=\"#fnref%d\" rev=\"footnote\">"
"↩</a>", num);
hbuf_put(ob, content->data + i, content->size - i);
} else
hbuf_putb(ob, content);
HBUF_PUTSL(ob, "</li>\n");
}
static void
rndr_footnote_ref(hbuf *ob, unsigned int num)
{
hbuf_printf(ob,
"<sup id=\"fnref%d\">"
"<a href=\"#fn%d\" rel=\"footnote\">"
"%d</a></sup>", num, num, num);
}
static void
rndr_math(hbuf *ob, const struct rndr_math *n)
{
if (n->blockmode)
HBUF_PUTSL(ob, "\\[");
else
HBUF_PUTSL(ob, "\\(");
escape_html(ob, n->text.data, n->text.size);
if (n->blockmode)
HBUF_PUTSL(ob, "\\]");
else
HBUF_PUTSL(ob, "\\)");
}
static void
rndr_doc_footer(hbuf *ob, const struct hstate *st)
{
if ((st->flags & LOWDOWN_STANDALONE))
HBUF_PUTSL(ob, "</body>\n");
}
static void
rndr_root(hbuf *ob, const hbuf *content, const struct hstate *st)
{
if ((st->flags & LOWDOWN_STANDALONE))
HBUF_PUTSL(ob,
"<!DOCTYPE html>\n"
"<html>\n");
hbuf_putb(ob, content);
if ((st->flags & LOWDOWN_STANDALONE))
HBUF_PUTSL(ob, "</html>\n");
}
/*
* Split "val" into multiple strings delimited by two or more whitespace
* characters, padding the output with "starttag" and "endtag".
*/
static void
rndr_doc_header_multi(hbuf *ob, const hbuf *b,
const char *starttag, const char *endtag)
{
const char *start;
size_t sz, i;
for (i = 0; i < b->size; i++) {
while (i < b->size &&
isspace((unsigned char)b->data[i]))
i++;
if (i == b->size)
continue;
start = &b->data[i];
sz = 0;
for (; i < b->size; i++)
if (i < b->size - 1 &&
isspace((unsigned char)b->data[i]) &&
isspace((unsigned char)b->data[i + 1]))
break;
if ((sz = &b->data[i] - start) == 0)
continue;
hbuf_puts(ob, starttag);
HBUF_PUTSL(ob, "\"");
hbuf_put(ob, start, sz);
HBUF_PUTSL(ob, "\"");
hbuf_puts(ob, endtag);
HBUF_PUTSL(ob, "\n");
}
}
static void
rndr_meta(hbuf *ob, const hbuf *tmp, struct lowdown_metaq *mq,
const struct lowdown_node *n, const struct hstate *st)
{
enum rndr_meta_key key;
struct lowdown_meta *m;
if (mq != NULL) {
m = xcalloc(1, sizeof(struct lowdown_meta));
TAILQ_INSERT_TAIL(mq, m, entries);
m->key = malloc(n->rndr_meta.key.size);
memcpy(m->key, n->rndr_meta.key.data,
n->rndr_meta.key.size);
m->value = malloc(tmp->size);
memcpy(m->value, tmp->data, tmp->size);
}
if (!(st->flags & LOWDOWN_STANDALONE))
return;
for (key = 0; key < RNDR_META__MAX; key++)
if (hbuf_streq(&n->rndr_meta.key, rndr_meta_keys[key]))
break;
/* TODO: rcsauthor, rcsdate. */
switch (key) {
case RNDR_META_AFFIL:
rndr_doc_header_multi(ob, tmp,
"<meta name=\"creator\" content=",
" />");
break;
case RNDR_META_AUTHOR:
rndr_doc_header_multi(ob, tmp,
"<meta name=\"author\" content=",
" />");
break;
case RNDR_META_CSS:
rndr_doc_header_multi(ob, tmp,
"<link rel=\"stylesheet\" href=",
" />");
break;
case RNDR_META_DATE:
hbuf_printf(ob, "<meta name=\"date\" "
"scheme=\"YYYY-MM-DD\" content=\"");
hbuf_putb(ob, tmp);
HBUF_PUTSL(ob, "\" />\n");
break;
case RNDR_META_SCRIPT:
rndr_doc_header_multi(ob, tmp,
"<script src=",
"></script>");
break;
case RNDR_META_TITLE:
HBUF_PUTSL(ob, "<title>");
hbuf_putb(ob, tmp);
HBUF_PUTSL(ob, "</title>\n");
break;
default:
break;
};
}
static void
rndr_doc_header(hbuf *ob, const hbuf *content,
const struct hstate *st, const struct lowdown_node *n)
{
struct lowdown_node *nn;
if (!(LOWDOWN_STANDALONE & st->flags))
return;
HBUF_PUTSL(ob,
"<head>\n"
"<meta charset=\"utf-8\" />\n"
"<meta name=\"viewport\""
" content=\"width=device-width,initial-scale=1\" />\n");
hbuf_putb(ob, content);
/* If we don't have a title, print out a default one. */
TAILQ_FOREACH(nn, &n->children, entries) {
if (nn->type != LOWDOWN_META)
continue;
if (hbuf_streq(&nn->rndr_meta.key, "title"))
break;
}
if (nn == NULL)
hbuf_puts(ob, "<title>Untitled Article</title>");
HBUF_PUTSL(ob, "</head>\n<body>\n");
}
void
lowdown_html_rndr(hbuf *ob, struct lowdown_metaq *metaq,
void *ref, const struct lowdown_node *root)
{
const struct lowdown_node *n;
hbuf *tmp;
int32_t ent;
struct hstate *st = ref;
tmp = hbuf_new(64);
TAILQ_FOREACH(n, &root->children, entries)
lowdown_html_rndr(tmp, metaq, st, n);
/*
* These elements can be put in either a block or an inline
* context, so we're safe to just use them and forget.
*/
if (root->chng == LOWDOWN_CHNG_INSERT)
HBUF_PUTSL(ob, "<ins>");
if (root->chng == LOWDOWN_CHNG_DELETE)
HBUF_PUTSL(ob, "<del>");
switch (root->type) {
case LOWDOWN_ROOT:
rndr_root(ob, tmp, st);
break;
case LOWDOWN_BLOCKCODE:
rndr_blockcode(ob,
&root->rndr_blockcode.text,
&root->rndr_blockcode.lang);
break;
case LOWDOWN_BLOCKQUOTE:
rndr_blockquote(ob, tmp);
break;
case LOWDOWN_DOC_HEADER:
rndr_doc_header(ob, tmp, st, root);
break;
case LOWDOWN_META:
rndr_meta(ob, tmp, metaq, root, st);
break;
case LOWDOWN_DOC_FOOTER:
rndr_doc_footer(ob, st);
break;
case LOWDOWN_HEADER:
rndr_header(ob, tmp,
root->rndr_header.level, st);
break;
case LOWDOWN_HRULE:
rndr_hrule(ob);
break;
case LOWDOWN_LIST:
rndr_list(ob, tmp, &root->rndr_list);
break;
case LOWDOWN_LISTITEM:
rndr_listitem(ob, tmp,
root->rndr_listitem.flags,
root->rndr_listitem.num);
break;
case LOWDOWN_PARAGRAPH:
rndr_paragraph(ob, tmp, st);
break;
case LOWDOWN_TABLE_BLOCK:
rndr_table(ob, tmp);
break;
case LOWDOWN_TABLE_HEADER:
rndr_table_header(ob, tmp,
root->rndr_table_header.flags,
root->rndr_table_header.columns);
break;
case LOWDOWN_TABLE_BODY:
rndr_table_body(ob, tmp);
break;
case LOWDOWN_TABLE_ROW:
rndr_tablerow(ob, tmp);
break;
case LOWDOWN_TABLE_CELL:
rndr_tablecell(ob, tmp,
root->rndr_table_cell.flags,
root->rndr_table_cell.col,
root->rndr_table_cell.columns);
break;
case LOWDOWN_FOOTNOTES_BLOCK:
rndr_footnotes(ob, tmp);
break;
case LOWDOWN_FOOTNOTE_DEF:
rndr_footnote_def(ob, tmp,
root->rndr_footnote_def.num);
break;
case LOWDOWN_BLOCKHTML:
rndr_raw_block(ob,
&root->rndr_blockhtml.text, st);
break;
case LOWDOWN_LINK_AUTO:
rndr_autolink(ob,
&root->rndr_autolink.link,
root->rndr_autolink.type);
break;
case LOWDOWN_CODESPAN:
rndr_codespan(ob, &root->rndr_codespan.text);
break;
case LOWDOWN_DOUBLE_EMPHASIS:
rndr_double_emphasis(ob, tmp);
break;
case LOWDOWN_EMPHASIS:
rndr_emphasis(ob, tmp);
break;
case LOWDOWN_HIGHLIGHT:
rndr_highlight(ob, tmp);
break;
case LOWDOWN_IMAGE:
rndr_image(ob,
&root->rndr_image.link,
&root->rndr_image.title,
&root->rndr_image.dims,
&root->rndr_image.alt);
break;
case LOWDOWN_LINEBREAK:
rndr_linebreak(ob);
break;
case LOWDOWN_LINK:
rndr_link(ob, tmp,
&root->rndr_link.link,
&root->rndr_link.title);
break;
case LOWDOWN_TRIPLE_EMPHASIS:
rndr_triple_emphasis(ob, tmp);
break;
case LOWDOWN_STRIKETHROUGH:
rndr_strikethrough(ob, tmp);
break;
case LOWDOWN_SUPERSCRIPT:
rndr_superscript(ob, tmp);
break;
case LOWDOWN_FOOTNOTE_REF:
rndr_footnote_ref(ob,
root->rndr_footnote_ref.num);
break;
case LOWDOWN_MATH_BLOCK:
rndr_math(ob, &root->rndr_math);
break;
case LOWDOWN_RAW_HTML:
rndr_raw_html(ob, &root->rndr_raw_html.text, st);
break;
case LOWDOWN_NORMAL_TEXT:
rndr_normal_text(ob, &root->rndr_normal_text.text);
break;
case LOWDOWN_ENTITY:
/*
* Prefer numeric entities.
* This is because we're emitting XML (XHTML5) and it's
* not clear whether the processor can handle HTML
* entities.
*/
ent = entity_find(&root->rndr_entity.text);
if (ent > 0)
hbuf_printf(ob, "&#%lld;", (long long)ent);
else
hbuf_put(ob,
root->rndr_entity.text.data,
root->rndr_entity.text.size);
break;
default:
hbuf_put(ob, tmp->data, tmp->size);
break;
}
if (root->chng == LOWDOWN_CHNG_INSERT)
HBUF_PUTSL(ob, "</ins>");
if (root->chng == LOWDOWN_CHNG_DELETE)
HBUF_PUTSL(ob, "</del>");
hbuf_free(tmp);
}
void *
lowdown_html_new(const struct lowdown_opts *opts)
{
struct hstate *state;
state = xcalloc(1, sizeof(struct hstate));
TAILQ_INIT(&state->headers_used);
state->flags = NULL == opts ? 0 : opts->oflags;
return state;
}
void
lowdown_html_free(void *renderer)
{
struct hstate *state = renderer;
struct hentry *hentry;