-
Notifications
You must be signed in to change notification settings - Fork 35
/
html.c
1478 lines (1278 loc) · 33.7 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
/*
* Copyright (c) 2008, Natacha Porté
* Copyright (c) 2011, Vicent Martí
* Copyright (c) 2014, Xavier Mendez, Devin Torres and the Hoedown authors
* Copyright (c) Kristaps Dzonsons <[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.
*/
#include "config.h"
#if HAVE_SYS_QUEUE
# include <sys/queue.h>
#endif
#include <assert.h>
#include <ctype.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "lowdown.h"
#include "extern.h"
/*
* Our internal state object.
*/
struct html {
struct hentryq headers_used; /* headers we've seen */
ssize_t headers_offs; /* header offset */
unsigned int flags; /* "oflags" in lowdown_opts */
struct lowdown_buf **foots; /* footnotes */
size_t footsz; /* footnotes size */
const char *templ; /* output template */
};
/*
* Escape regular text that shouldn't be HTML.
* Return zero on failure, non-zero on success.
*/
static int
escape_html(struct lowdown_buf *ob, const char *source, size_t length,
const struct html *st)
{
return lowdown_html_esc(ob, source, length,
st->flags & LOWDOWN_HTML_OWASP, 0,
st->flags & LOWDOWN_HTML_NUM_ENT);
}
/*
* See escape_html().
*/
static int
escape_htmlb(struct lowdown_buf *ob,
const struct lowdown_buf *in, const struct html *st)
{
return escape_html(ob, in->data, in->size, st);
}
/*
* Escape literal text.
* Like escape_html() except more restrictive.
* Return zero on failure, non-zero on success.
*/
static int
escape_literal(struct lowdown_buf *ob,
const struct lowdown_buf *in, const struct html *st)
{
return lowdown_html_esc(ob, in->data, in->size,
st->flags & LOWDOWN_HTML_OWASP, 1,
st->flags & LOWDOWN_HTML_NUM_ENT);
}
/*
* Escape an href link.
* Return zero on failure, non-zero on success.
*/
static int
escape_href(struct lowdown_buf *ob, const struct lowdown_buf *in,
const struct html *st)
{
return lowdown_html_esc_href(ob, in->data, in->size);
}
/*
* Escape an HTML attribute.
* Return zero on failure, non-zero on success.
*/
static int
escape_attr(struct lowdown_buf *ob, const struct lowdown_buf *in)
{
return lowdown_html_esc_attr(ob, in->data, in->size);
}
static int
newline(struct lowdown_buf *ob)
{
if (ob->size > 0 && ob->data[ob->size - 1] != '\n' &&
!hbuf_putc(ob, '\n'))
return 0;
return 1;
}
static int
rndr_autolink(struct lowdown_buf *ob,
const struct rndr_autolink *parm, const struct html *st)
{
if (parm->link.size == 0)
return 1;
if (!HBUF_PUTSL(ob, "<a href=\""))
return 0;
if (parm->type == HALINK_EMAIL && !HBUF_PUTSL(ob, "mailto:"))
return 0;
if (!escape_href(ob, &parm->link, st))
return 0;
if (!HBUF_PUTSL(ob, "\">"))
return 0;
/*
* Pretty printing: if we get an email address as
* an actual URI, e.g. `mailto:[email protected]`, we don't
* want to print the `mailto:` prefix
*/
if (hbuf_strprefix(&parm->link, "mailto:")) {
if (!escape_html(ob,
parm->link.data + 7,
parm->link.size - 7, st))
return 0;
} else {
if (!escape_htmlb(ob, &parm->link, st))
return 0;
}
return HBUF_PUTSL(ob, "</a>");
}
static int
rndr_blockcode(struct lowdown_buf *ob,
const struct rndr_blockcode *parm, const struct html *st)
{
if (!newline(ob))
return 0;
if (parm->lang.size) {
if (!HBUF_PUTSL(ob, "<pre><code class=\"language-"))
return 0;
if (!escape_href(ob, &parm->lang, st))
return 0;
if (!HBUF_PUTSL(ob, "\">"))
return 0;
} else {
if (! HBUF_PUTSL(ob, "<pre><code>"))
return 0;
}
if (!escape_literal(ob, &parm->text, st))
return 0;
return HBUF_PUTSL(ob, "</code></pre>\n");
}
static int
rndr_definition_data(struct lowdown_buf *ob,
const struct lowdown_buf *content)
{
if (!HBUF_PUTSL(ob, "<dd>\n"))
return 0;
if (!hbuf_putb(ob, content))
return 0;
return HBUF_PUTSL(ob, "\n</dd>\n");
}
static int
rndr_definition_title(struct lowdown_buf *ob,
const struct lowdown_buf *content)
{
size_t sz;
if (!HBUF_PUTSL(ob, "<dt>"))
return 0;
if ((sz = content->size) > 0) {
while (sz && content->data[sz - 1] == '\n')
sz--;
if (!hbuf_put(ob, content->data, sz))
return 0;
}
return HBUF_PUTSL(ob, "</dt>\n");
}
static int
rndr_definition(struct lowdown_buf *ob,
const struct lowdown_buf *content)
{
if (!newline(ob))
return 0;
if (!HBUF_PUTSL(ob, "<dl>\n"))
return 0;
if (!hbuf_putb(ob, content))
return 0;
return HBUF_PUTSL(ob, "</dl>\n");
}
static int
rndr_blockquote(const struct html *st,
const struct rndr_blockquote *param, struct lowdown_buf *ob,
const struct lowdown_buf *content)
{
size_t i;
if (!newline(ob))
return 0;
if (param->type == BLOCKQUOTE_REGULAR || !(st->flags &
(LOWDOWN_HTML_CALLOUT_GFM|LOWDOWN_HTML_CALLOUT_MDN)))
return HBUF_PUTSL(ob, "<blockquote>\n") &&
hbuf_putb(ob, content) &&
HBUF_PUTSL(ob, "</blockquote>\n");
if (!HBUF_PUTSL(ob, "<div class=\""))
return 0;
/* MDN/GFM style blockquotes. */
if (st->flags & LOWDOWN_HTML_CALLOUT_MDN) {
if (!hbuf_printf(ob, "%s",
(param->admonition == ADMONITION_NOTE) ?
"notecard note" :
(param->admonition == ADMONITION_WARNING) ?
"notecard warning" : "callout"))
return 0;
}
if (st->flags & LOWDOWN_HTML_CALLOUT_GFM) {
if (!hbuf_printf(ob, "%smarkdown-alert ",
(st->flags & LOWDOWN_HTML_CALLOUT_MDN) ? " " : ""))
return 0;
if (!hbuf_printf(ob, "markdown-alert-%s",
(param->admonition == ADMONITION_NOTE) ?
"note" :
(param->admonition == ADMONITION_WARNING) ?
"warning" : "callout"))
return 0;
}
if (!HBUF_PUTSL(ob, "\">\n"))
return 0;
/*
* Weird: content callouts have their initial callout type
* removed. Fortunately, this is hard-coded, so we know its
* exact length. Remove the callout type and any extra spacing
* that follows it.
*/
if (param->admonition == ADMONITION_CALLOUT && content->size > 28) {
i = 28;
while (i < content->size && content->data[i] == ' ')
i++;
if (!HBUF_PUTSL(ob, "<p>") ||
!hbuf_put(ob, content->data + i, content->size - i))
return 0;
} else if (!hbuf_putb(ob, content))
return 0;
return HBUF_PUTSL(ob, "</div>\n");
}
static int
rndr_codespan(struct lowdown_buf *ob,
const struct rndr_codespan *param, const struct html *st)
{
if (!HBUF_PUTSL(ob, "<code>"))
return 0;
if (!escape_htmlb(ob, ¶m->text, st))
return 0;
return HBUF_PUTSL(ob, "</code>");
}
static int
rndr_strikethrough(struct lowdown_buf *ob,
const struct lowdown_buf *content)
{
if (!HBUF_PUTSL(ob, "<del>"))
return 0;
if (!hbuf_putb(ob, content))
return 0;
return HBUF_PUTSL(ob, "</del>");
}
static int
rndr_double_emphasis(struct lowdown_buf *ob,
const struct lowdown_buf *content)
{
if (!HBUF_PUTSL(ob, "<strong>"))
return 0;
if (!hbuf_putb(ob, content))
return 0;
return HBUF_PUTSL(ob, "</strong>");
}
static int
rndr_emphasis(struct lowdown_buf *ob,
const struct lowdown_buf *content)
{
if (!HBUF_PUTSL(ob, "<em>"))
return 0;
if (!hbuf_putb(ob, content))
return 0;
return HBUF_PUTSL(ob, "</em>");
}
static int
rndr_highlight(struct lowdown_buf *ob,
const struct lowdown_buf *content)
{
if (!HBUF_PUTSL(ob, "<mark>"))
return 0;
if (!hbuf_putb(ob, content))
return 0;
return HBUF_PUTSL(ob, "</mark>");
}
static int
rndr_linebreak(struct lowdown_buf *ob)
{
return HBUF_PUTSL(ob, "<br/>\n");
}
static int
rndr_header(struct lowdown_buf *ob, const struct lowdown_buf *content,
const struct lowdown_node *n, struct html *st)
{
ssize_t level;
const struct lowdown_buf *buf;
/*
* The <hN> level take into account shifteheadinglevelby
* metadata, so offset it here. Bound us below <h6>.
*/
level = (ssize_t)n->rndr_header.level + st->headers_offs;
if (level < 1)
level = 1;
else if (level > 6)
level = 6;
if (!newline(ob))
return 0;
if (!hbuf_printf(ob, "<h%zu", level))
return 0;
/*
* Identifiers can either come from header attributes or as
* computed from the content of the header.
*/
if (n->rndr_header.attr_id.size) {
if (!HBUF_PUTSL(ob, " id=\""))
return 0;
if (!escape_href(ob, &n->rndr_header.attr_id, st))
return 0;
if (!HBUF_PUTSL(ob, "\""))
return 0;
} else if (st->flags & LOWDOWN_HTML_HEAD_IDS) {
if (!HBUF_PUTSL(ob, " id=\""))
return 0;
buf = hbuf_id(NULL, n, &st->headers_used);
if (buf == NULL)
return 0;
if (!hbuf_putb(ob, buf))
return 0;
if (!HBUF_PUTSL(ob, "\""))
return 0;
}
/* Optional header class. */
if (n->rndr_header.attr_cls.size) {
if (!HBUF_PUTSL(ob, " class=\""))
return 0;
if (!escape_attr(ob, &n->rndr_header.attr_cls))
return 0;
if (!HBUF_PUTSL(ob, "\""))
return 0;
}
if (!HBUF_PUTSL(ob, ">"))
return 0;
if (!hbuf_putb(ob, content))
return 0;
return hbuf_printf(ob, "</h%zu>\n", level);
}
static int
rndr_link(struct lowdown_buf *ob, const struct lowdown_buf *content,
const struct rndr_link *param, const struct html *st)
{
if (!HBUF_PUTSL(ob, "<a href=\"") ||
!escape_href(ob, ¶m->link, st))
return 0;
if (param->title.size)
if (!HBUF_PUTSL(ob, "\" title=\"") ||
!escape_attr(ob, ¶m->title))
return 0;
if (param->attr_cls.size)
if (!HBUF_PUTSL(ob, "\" class=\"") ||
!escape_attr(ob, ¶m->attr_cls))
return 0;
if (param->attr_id.size)
if (!HBUF_PUTSL(ob, "\" id=\"") ||
!escape_attr(ob, ¶m->attr_id))
return 0;
if (!HBUF_PUTSL(ob, "\">") ||
!hbuf_putb(ob, content) ||
!HBUF_PUTSL(ob, "</a>"))
return 0;
return 1;
}
static int
rndr_list(struct lowdown_buf *ob, const struct lowdown_buf *content,
const struct rndr_list *param)
{
if (!newline(ob))
return 0;
if (param->flags & HLIST_FL_ORDERED) {
if (param->start > 1) {
if (!hbuf_printf(ob,
"<ol start=\"%zu\">\n", param->start))
return 0;
} else {
if (!HBUF_PUTSL(ob, "<ol>\n"))
return 0;
}
} else if (!HBUF_PUTSL(ob, "<ul>\n"))
return 0;
if (!hbuf_putb(ob, content))
return 0;
return (param->flags & HLIST_FL_ORDERED) ?
HBUF_PUTSL(ob, "</ol>\n") :
HBUF_PUTSL(ob, "</ul>\n");
}
static int
rndr_listitem(struct lowdown_buf *ob,
const struct lowdown_buf *content, const struct lowdown_node *n)
{
size_t size;
int blk = 0;
/*
* If we're in block mode (which can be assigned post factum in
* the parser), make sure that we have an extra <p> around
* non-block content.
*/
if (((n->rndr_listitem.flags & HLIST_FL_DEF) &&
n->parent != NULL &&
n->parent->parent != NULL &&
n->parent->parent->type == LOWDOWN_DEFINITION &&
(n->parent->parent->rndr_definition.flags &
HLIST_FL_BLOCK)) ||
(!(n->rndr_listitem.flags & HLIST_FL_DEF) &&
n->parent != NULL &&
n->parent->type == LOWDOWN_LIST &&
(n->parent->rndr_list.flags & HLIST_FL_BLOCK))) {
if (!(hbuf_strprefix(content, "<ul") ||
hbuf_strprefix(content, "<ol") ||
hbuf_strprefix(content, "<dl") ||
hbuf_strprefix(content, "<div") ||
hbuf_strprefix(content, "<table") ||
hbuf_strprefix(content, "<blockquote") ||
hbuf_strprefix(content, "<pre>") ||
hbuf_strprefix(content, "<h") ||
hbuf_strprefix(content, "<p>")))
blk = 1;
}
/* Only emit <li> if we're not a <dl> list. */
if (!(n->rndr_listitem.flags & HLIST_FL_DEF) &&
!HBUF_PUTSL(ob, "<li>"))
return 0;
if (blk && !HBUF_PUTSL(ob, "<p>"))
return 0;
if (n->rndr_listitem.flags &
(HLIST_FL_CHECKED|HLIST_FL_UNCHECKED))
HBUF_PUTSL(ob, "<input type=\"checkbox\" ");
if (n->rndr_listitem.flags & HLIST_FL_CHECKED)
HBUF_PUTSL(ob, "checked=\"checked\" ");
if (n->rndr_listitem.flags &
(HLIST_FL_CHECKED|HLIST_FL_UNCHECKED))
HBUF_PUTSL(ob, "/>");
/* Cut off any trailing space. */
if ((size = content->size) > 0) {
while (size && content->data[size - 1] == '\n')
size--;
if (!hbuf_put(ob, content->data, size))
return 0;
}
if (blk && !HBUF_PUTSL(ob, "</p>"))
return 0;
if (!(n->rndr_listitem.flags & HLIST_FL_DEF) &&
!HBUF_PUTSL(ob, "</li>\n"))
return 0;
return 1;
}
static int
rndr_paragraph(struct lowdown_buf *ob,
const struct lowdown_buf *content, struct html *st)
{
size_t i = 0, org;
if (content->size == 0)
return 1;
while (i < content->size &&
isspace((unsigned char)content->data[i]))
i++;
if (i == content->size)
return 1;
if (!newline(ob))
return 0;
if (!HBUF_PUTSL(ob, "<p>"))
return 0;
if (st->flags & LOWDOWN_HTML_HARD_WRAP) {
for ( ; i < content->size; i++) {
org = i;
while (i < content->size &&
content->data[i] != '\n')
i++;
if (i > org && !hbuf_put
(ob, content->data + org, i - org))
return 0;
/*
* Do not insert a line break if this newline is
* the last character on the paragraph.
*/
if (i >= content->size - 1)
break;
if (!rndr_linebreak(ob))
return 0;
}
} else {
if (!hbuf_put(ob,
content->data + i, content->size - i))
return 0;
}
return HBUF_PUTSL(ob, "</p>\n");
}
static int
rndr_raw_block(struct lowdown_buf *ob,
const struct rndr_blockhtml *param, const struct html *st)
{
size_t org, sz;
if ((st->flags & LOWDOWN_HTML_SKIP_HTML))
return 1;
if ((st->flags & LOWDOWN_HTML_ESCAPE))
return escape_htmlb(ob, ¶m->text, st);
/*
* FIXME: Do we *really* need to trim the HTML? How does that
* make a difference?
*/
sz = param->text.size;
while (sz > 0 && param->text.data[sz - 1] == '\n')
sz--;
org = 0;
while (org < sz && param->text.data[org] == '\n')
org++;
if (org >= sz)
return 1;
if (!newline(ob))
return 0;
if (!hbuf_put(ob, param->text.data + org, sz - org))
return 0;
return hbuf_putc(ob, '\n');
}
static int
rndr_triple_emphasis(struct lowdown_buf *ob,
const struct lowdown_buf *content)
{
if (!HBUF_PUTSL(ob, "<strong><em>"))
return 0;
if (!hbuf_putb(ob, content))
return 0;
return HBUF_PUTSL(ob, "</em></strong>");
}
static int
rndr_hrule(struct lowdown_buf *ob)
{
if (!newline(ob))
return 0;
return hbuf_puts(ob, "<hr/>\n");
}
static int
rndr_image(struct lowdown_buf *ob, const struct rndr_image *param,
const struct html *st)
{
char dimbuf[32];
unsigned int x, y;
int 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 (param->dims.size &&
param->dims.size < sizeof(dimbuf) - 1) {
memset(dimbuf, 0, sizeof(dimbuf));
memcpy(dimbuf, param->dims.data, param->dims.size);
rc = sscanf(dimbuf, "%ux%u", &x, &y);
}
/* Require an "alt", even if blank. */
if (!HBUF_PUTSL(ob, "<img src=\"") ||
!escape_href(ob, ¶m->link, st) ||
!HBUF_PUTSL(ob, "\" alt=\"") ||
!escape_attr(ob, ¶m->alt) ||
!HBUF_PUTSL(ob, "\""))
return 0;
if (param->attr_cls.size)
if (!HBUF_PUTSL(ob, " class=\"") ||
!escape_attr(ob, ¶m->attr_cls) ||
!HBUF_PUTSL(ob, "\""))
return 0;
if (param->attr_id.size)
if (!HBUF_PUTSL(ob, " id=\"") ||
!escape_attr(ob, ¶m->attr_id) ||
!HBUF_PUTSL(ob, "\""))
return 0;
if (param->attr_width.size || param->attr_height.size) {
if (!HBUF_PUTSL(ob, " style=\""))
return 0;
if (param->attr_width.size)
if (!HBUF_PUTSL(ob, "width:") ||
!escape_attr(ob, ¶m->attr_width) ||
!HBUF_PUTSL(ob, ";"))
return 0;
if (param->attr_height.size)
if (!HBUF_PUTSL(ob, "height:") ||
!escape_attr(ob, ¶m->attr_height) ||
!HBUF_PUTSL(ob, ";"))
return 0;
if (!HBUF_PUTSL(ob, "\""))
return 0;
} else if (param->dims.size && rc > 0) {
if (!hbuf_printf(ob, " width=\"%u\"", x))
return 0;
if (rc > 1 && !hbuf_printf(ob, " height=\"%u\"", y))
return 0;
}
if (param->title.size)
if (!HBUF_PUTSL(ob, " title=\"") ||
!escape_htmlb(ob, ¶m->title, st) ||
!HBUF_PUTSL(ob, "\""))
return 0;
return hbuf_puts(ob, " />");
}
static int
rndr_raw_html(struct lowdown_buf *ob,
const struct rndr_raw_html *param, const struct html *st)
{
if (st->flags & LOWDOWN_HTML_SKIP_HTML)
return 1;
return (st->flags & LOWDOWN_HTML_ESCAPE) ?
escape_htmlb(ob, ¶m->text, st) :
hbuf_putb(ob, ¶m->text);
}
static int
rndr_table(struct lowdown_buf *ob,
const struct lowdown_buf *content)
{
if (!newline(ob))
return 0;
if (!HBUF_PUTSL(ob, "<table>\n"))
return 0;
if (!hbuf_putb(ob, content))
return 0;
return HBUF_PUTSL(ob, "</table>\n");
}
static int
rndr_table_header(struct lowdown_buf *ob,
const struct lowdown_buf *content)
{
if (!newline(ob))
return 0;
if (!HBUF_PUTSL(ob, "<thead>\n"))
return 0;
if (!hbuf_putb(ob, content))
return 0;
return HBUF_PUTSL(ob, "</thead>\n");
}
static int
rndr_table_body(struct lowdown_buf *ob,
const struct lowdown_buf *content)
{
if (content->size == 0)
return 1;
if (!newline(ob))
return 0;
if (!HBUF_PUTSL(ob, "<tbody>\n"))
return 0;
if (!hbuf_putb(ob, content))
return 0;
return HBUF_PUTSL(ob, "</tbody>\n");
}
static int
rndr_table_row(struct lowdown_buf *ob,
const struct lowdown_buf *content)
{
if (!HBUF_PUTSL(ob, "<tr>\n"))
return 0;
if (!hbuf_putb(ob, content))
return 0;
return HBUF_PUTSL(ob, "</tr>\n");
}
static int
rndr_tablecell(struct lowdown_buf *ob,
const struct lowdown_buf *content,
const struct rndr_table_cell *param)
{
if (param->flags & HTBL_FL_HEADER) {
if (!HBUF_PUTSL(ob, "<th"))
return 0;
} else {
if (!HBUF_PUTSL(ob, "<td"))
return 0;
}
switch (param->flags & HTBL_FL_ALIGNMASK) {
case HTBL_FL_ALIGN_CENTER:
if (!HBUF_PUTSL(ob, " style=\"text-align: center\">"))
return 0;
break;
case HTBL_FL_ALIGN_LEFT:
if (!HBUF_PUTSL(ob, " style=\"text-align: left\">"))
return 0;
break;
case HTBL_FL_ALIGN_RIGHT:
if (!HBUF_PUTSL(ob, " style=\"text-align: right\">"))
return 0;
break;
default:
if (!HBUF_PUTSL(ob, ">"))
return 0;
break;
}
if (!hbuf_putb(ob, content))
return 0;
return (param->flags & HTBL_FL_HEADER) ?
HBUF_PUTSL(ob, "</th>\n") :
HBUF_PUTSL(ob, "</td>\n");
}
static int
rndr_superscript(struct lowdown_buf *ob,
const struct lowdown_buf *content, enum lowdown_rndrt type)
{
const char *elem;
elem = (type == LOWDOWN_SUPERSCRIPT) ? "sup" : "sub";
return hbuf_printf(ob, "<%s>", elem) &&
hbuf_putb(ob, content) &&
hbuf_printf(ob, "</%s>", elem);
}
static int
rndr_normal_text(struct lowdown_buf *ob,
const struct rndr_normal_text *param, const struct html *st)
{
return escape_htmlb(ob, ¶m->text, st);
}
static int
rndr_footnote_def(struct lowdown_buf *ob,
const struct lowdown_buf *content, size_t 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;
}
if (!hbuf_printf(ob, "\n<li id=\"fn%zu\">\n", num))
return 0;
if (pfound) {
if (!hbuf_put(ob, content->data, i))
return 0;
if (!hbuf_printf(ob, " "
"<a href=\"#fnref%zu\" rev=\"footnote\">"
"↩"
"</a>", num))
return 0;
if (!hbuf_put(ob,
content->data + i, content->size - i))
return 0;
} else {
if (!hbuf_putb(ob, content))
return 0;
}
return HBUF_PUTSL(ob, "</li>\n");
}
static int
rndr_footnote_ref(struct lowdown_buf *ob,
const struct lowdown_buf *content, struct html *st)
{
void *pp;
size_t num = st->footsz + 1;
/*
* Keep a reference to this footnote definition, as we're going
* to print it out at the end of the document. For now,
* suppress printing of the content.
*/
pp = recallocarray(st->foots, st->footsz,
st->footsz + 1, sizeof(struct lowdown_buf *));
if (pp == NULL)
return 0;
st->foots = pp;
if ((st->foots[st->footsz++] = hbuf_dup(content)) == NULL)
return 0;
return hbuf_printf(ob,
"<sup id=\"fnref%zu\">"
"<a href=\"#fn%zu\" rel=\"footnote\">"
"%zu</a></sup>", num, num, num);
}
static int
rndr_math(struct lowdown_buf *ob, const struct rndr_math *param,
const struct html *st)
{
if (param->blockmode && !HBUF_PUTSL(ob, "\\["))
return 0;
else if (!param->blockmode && !HBUF_PUTSL(ob, "\\("))
return 0;
if (!escape_htmlb(ob, ¶m->text, st))
return 0;
return param->blockmode ?
HBUF_PUTSL(ob, "\\]") :
HBUF_PUTSL(ob, "\\)");
}
static int
rndr_doc_footer(struct lowdown_buf *ob, const struct html *st)
{
size_t i;
if (st->footsz == 0)
return 1;
/*
* Start by flushing out our footnotes. Footnotes are "sparse"
* in that we may not have them all defined (?).
*/
if (!newline(ob))
return 0;
if (!HBUF_PUTSL(ob, "<div class=\"footnotes\">\n<hr/>\n<ol>\n"))
return 0;
for (i = 0; i < st->footsz; i++)
if (!rndr_footnote_def(ob, st->foots[i], i + 1))
return 0;
return HBUF_PUTSL(ob, "\n</ol>\n</div>\n");
}
/*
* Split "val" into multiple strings delimited by two or more whitespace
* characters, padding the output with "starttag" and "endtag". If
* "href" is set, escape as if this were a link; if "attr" were set,
* escape as if this were an HTML attribute; if neither, then escape as
* if regular HTML.
* Return zero on failure, non-zero on success.
*/
static int
rndr_meta_multi(const struct html *st, struct lowdown_buf *ob,
const char *b, int href, int attr, const char *starttag,
const char *endtag)
{
const char *start;
size_t sz, i, bsz;
if (b == NULL)
return 1;
bsz = strlen(b);
for (i = 0; i < bsz; i++) {
while (i < bsz &&
isspace((unsigned char)b[i]))
i++;
if (i == bsz)
continue;
start = &b[i];
for (; i < bsz; i++)
if (i < bsz - 1 &&
isspace((unsigned char)b[i]) &&
isspace((unsigned char)b[i + 1]))