-
Notifications
You must be signed in to change notification settings - Fork 35
/
nroff.c
1368 lines (1175 loc) · 30.3 KB
/
nroff.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 <assert.h>
#include <ctype.h>
#if HAVE_ERR
# include <err.h>
#endif
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "lowdown.h"
#include "extern.h"
/*
* Emit a newline if and only if the given buffer doesn't end in one.
*/
#define BUFFER_NEWLINE(_buf, _sz, _ob) \
do if ((_sz) > 0 && '\n' != (_buf)[(_sz) - 1]) \
hbuf_putc((_ob), '\n'); \
while (/* CONSTCOND */ 0)
/*
* See BUFFER_NEWLINE().
*/
#define HBUF_NEWLINE(_buf, _ob) \
BUFFER_NEWLINE((_buf)->data, (_buf)->size, (_ob))
enum nfont {
NFONT_ITALIC = 0, /* italic */
NFONT_BOLD, /* bold */
NFONT_FIXED, /* fixed-width */
NFONT__MAX
};
/*
* This relates to the roff output, not the node type.
* If NSCOPE_BLOCK, the output is newline-terminated.
* Otherwise, it is not.
*/
enum nscope {
NSCOPE_BLOCK,
NSCOPE_SPAN
};
enum rndr_meta_key {
RNDR_META_AFFIL,
RNDR_META_AUTHOR,
RNDR_META_COPY,
RNDR_META_DATE,
RNDR_META_RCSAUTHOR,
RNDR_META_RCSDATE,
RNDR_META_TITLE,
RNDR_META__MAX
};
static const char *rndr_meta_keys[RNDR_META__MAX] = {
"affiliation", /* RNDR_META_AFFIL */
"author", /* RNDR_META_AUTHOR */
"copyright", /* RNDR_META_COPY */
"date", /* RNDR_META_DATE */
"rcsauthor", /* RNDR_META_RCSAUTHOR */
"rcsdate", /* RNDR_META_RCSDATE */
"title", /* RNDR_META_TITLE */
};
struct nstate {
int mdoc; /* whether mdoc(7) */
unsigned int flags; /* output flags */
enum nfont fonts[NFONT__MAX]; /* see nstate_fonts() */
};
static const enum nscope nscopes[LOWDOWN__MAX] = {
NSCOPE_BLOCK, /* LOWDOWN_ROOT */
NSCOPE_BLOCK, /* LOWDOWN_BLOCKCODE */
NSCOPE_BLOCK, /* LOWDOWN_BLOCKQUOTE */
NSCOPE_BLOCK, /* LOWDOWN_HEADER */
NSCOPE_BLOCK, /* LOWDOWN_HRULE */
NSCOPE_BLOCK, /* LOWDOWN_LIST */
NSCOPE_BLOCK, /* LOWDOWN_LISTITEM */
NSCOPE_BLOCK, /* LOWDOWN_PARAGRAPH */
NSCOPE_BLOCK, /* LOWDOWN_TABLE_BLOCK */
NSCOPE_BLOCK, /* LOWDOWN_TABLE_HEADER */
NSCOPE_BLOCK, /* LOWDOWN_TABLE_BODY */
NSCOPE_BLOCK, /* LOWDOWN_TABLE_ROW */
NSCOPE_BLOCK, /* LOWDOWN_TABLE_CELL */
NSCOPE_BLOCK, /* LOWDOWN_FOOTNOTES_BLOCK */
NSCOPE_BLOCK, /* LOWDOWN_FOOTNOTE_DEF */
NSCOPE_BLOCK, /* LOWDOWN_BLOCKHTML */
NSCOPE_BLOCK, /* LOWDOWN_LINK_AUTO */
NSCOPE_SPAN, /* LOWDOWN_CODESPAN */
NSCOPE_SPAN, /* LOWDOWN_DOUBLE_EMPHASIS */
NSCOPE_SPAN, /* LOWDOWN_EMPHASIS */
NSCOPE_SPAN, /* LOWDOWN_HIGHLIGHT */
NSCOPE_BLOCK, /* LOWDOWN_IMAGE */
NSCOPE_BLOCK, /* LOWDOWN_LINEBREAK */
NSCOPE_BLOCK, /* LOWDOWN_LINK */
NSCOPE_SPAN, /* LOWDOWN_TRIPLE_EMPHASIS */
NSCOPE_SPAN, /* LOWDOWN_STRIKETHROUGH */
NSCOPE_SPAN, /* LOWDOWN_SUPERSCRIPT */
NSCOPE_SPAN, /* LOWDOWN_FOOTNOTE_REF */
NSCOPE_BLOCK, /* LOWDOWN_MATH_BLOCK */
NSCOPE_SPAN, /* LOWDOWN_RAW_HTML */
NSCOPE_SPAN, /* LOWDOWN_ENTITY */
NSCOPE_SPAN, /* LOWDOWN_NORMAL_TEXT */
NSCOPE_BLOCK, /* LOWDOWN_DOC_HEADER */
NSCOPE_BLOCK, /* LOWDOWN_META */
NSCOPE_BLOCK /* LOWDOWN_DOC_FOOTER */
};
/*
* Output "source" of size "length" on as many lines as required,
* starting on a line with existing content.
* Escapes text so as not to be roff.
*/
static void
rndr_span(hbuf *ob, const char *source, size_t length)
{
hesc_nroff(ob, source, length, 1, 0);
}
/*
* Output "source" of size "length" on as many lines as required,
* starting on its own line.
* Escapes text so as not to be roff.
*/
static void
rndr_block(hbuf *ob, const char *source, size_t length)
{
hesc_nroff(ob, source, length, 0, 0);
}
/*
* Output "source" of size "length" on one line, starting on a line with
* existing content.
* Escapes text so as not to be roff.
*/
static void
rndr_one_line_span(hbuf *ob, const char *source, size_t length)
{
hesc_nroff(ob, source, length, 1, 1);
}
/*
* Output "source" of size "length" on a single line.
* Does not escape the given text, which should already have been
* escaped, unless "ownline" is given, in which case make sure we don't
* start with roff.
*/
static void
rndr_one_line_noescape(hbuf *ob,
const char *source, size_t length, int ownline)
{
size_t i;
if (ownline && length && source[0] == '.')
HBUF_PUTSL(ob, "\\&");
for (i = 0; i < length; i++)
if (isspace((unsigned char)source[i]))
HBUF_PUTSL(ob, " ");
else
hbuf_putc(ob, source[i]);
}
/*
* See rndr_one_line_noescape().
*/
static void
rndr_one_lineb_noescape(hbuf *ob, const hbuf *b, int ownline)
{
return rndr_one_line_noescape(ob, b->data, b->size, ownline);
}
/*
* Return the font string for the current set of fonts.
* FIXME: I don't think this works for combinations of fixed-width,
* bold, and italic.
*/
static const char *
nstate_fonts(const struct nstate *st)
{
static char fonts[10];
char *cp = fonts;
(*cp++) = '\\';
(*cp++) = 'f';
(*cp++) = '[';
if (st->fonts[NFONT_FIXED])
(*cp++) = 'C';
if (st->fonts[NFONT_BOLD])
(*cp++) = 'B';
if (st->fonts[NFONT_ITALIC])
(*cp++) = 'I';
if (st->fonts[NFONT_FIXED] &&
(st->fonts[NFONT_BOLD] == 0 &&
st->fonts[NFONT_ITALIC] == 0))
(*cp++) = 'R';
/* Reset. */
if (st->fonts[NFONT_BOLD] == 0 &&
st->fonts[NFONT_FIXED] == 0 &&
st->fonts[NFONT_ITALIC] == 0)
(*cp++) = 'R';
(*cp++) = ']';
(*cp++) = '\0';
return fonts;
}
/*
* Manage hypertext linking with the groff "pdfhref" macro or simply
* using italics.
* We use italics because the UR/UE macro doesn't support leading
* un-spaced content, so "[foo](https://foo.com)" wouldn't work.
* Until a solution is found, let's just italicise the link text (or
* link, if no text is found).
*/
static int
putlink(hbuf *ob, struct nstate *st, const hbuf *link,
const hbuf *text, struct lowdown_node *next,
struct lowdown_node *prev, enum halink_type type)
{
const hbuf *buf;
size_t i, pos;
int ret = 1, usepdf;
usepdf = !st->mdoc && (st->flags & LOWDOWN_NROFF_GROFF);
if (usepdf)
HBUF_PUTSL(ob, ".pdfhref W ");
/*
* If we're preceded by normal text that doesn't end with space,
* then put that text into the "-P" (prefix) argument.
* If we're not in groff mode, emit the data, but without -P.
*/
if (prev != NULL &&
prev->type == LOWDOWN_NORMAL_TEXT) {
buf = &prev->rndr_normal_text.text;
i = buf->size;
while (i && !isspace((unsigned char)buf->data[i - 1]))
i--;
if (i != buf->size && usepdf)
HBUF_PUTSL(ob, "-P \"");
for (pos = i; pos < buf->size; pos++) {
/* Be sure to escape... */
if (buf->data[pos] == '"') {
HBUF_PUTSL(ob, "\\(dq");
continue;
} else if (buf->data[pos] == '\\') {
HBUF_PUTSL(ob, "\\e");
continue;
}
hbuf_putc(ob, buf->data[pos]);
}
if (i != buf->size && usepdf)
HBUF_PUTSL(ob, "\" ");
}
if (!usepdf) {
st->fonts[NFONT_ITALIC]++;
hbuf_puts(ob, nstate_fonts(st));
if (text == NULL) {
if (0 == hbuf_prefix(link, "mailto:"))
hbuf_put(ob, link->data + 7, link->size - 7);
else
hbuf_put(ob, link->data, link->size);
} else
hbuf_put(ob, text->data, text->size);
st->fonts[NFONT_ITALIC]--;
hbuf_puts(ob, nstate_fonts(st));
}
/*
* If we're followed by normal text that doesn't begin with a
* space, use the "-A" (affix) option to prevent a space before
* what follows.
* But first, initialise the offset.
*/
if (next != NULL &&
next->type == LOWDOWN_NORMAL_TEXT)
next->rndr_normal_text.offs = 0;
if (next != NULL &&
next->type == LOWDOWN_NORMAL_TEXT &&
next->rndr_normal_text.text.size > 0 &&
next->rndr_normal_text.text.data[0] != ' ') {
buf = &next->rndr_normal_text.text;
if (usepdf)
HBUF_PUTSL(ob, "-A \"");
for (pos = 0; pos < buf->size; pos++) {
if (isspace((unsigned char)buf->data[pos]))
break;
/* Be sure to escape... */
if (buf->data[pos] == '"') {
HBUF_PUTSL(ob, "\\(dq");
continue;
} else if (buf->data[pos] == '\\') {
HBUF_PUTSL(ob, "\\e");
continue;
}
hbuf_putc(ob, buf->data[pos]);
}
ret = pos < buf->size;
next->rndr_normal_text.offs = pos;
if (usepdf)
HBUF_PUTSL(ob, "\" ");
}
/* Encode the URL. */
if (usepdf) {
HBUF_PUTSL(ob, "-D ");
if (HALINK_EMAIL == type)
HBUF_PUTSL(ob, "mailto:");
for (i = 0; i < link->size; i++) {
if (!isprint((unsigned char)link->data[i]) ||
strchr("<>\\^`{|}\"", link->data[i]) != NULL)
hbuf_printf(ob, "%%%.2X", link->data[i]);
else
hbuf_putc(ob, link->data[i]);
}
HBUF_PUTSL(ob, " ");
if (text == NULL) {
if (hbuf_prefix(link, "mailto:") == 0)
hbuf_put(ob, link->data + 7, link->size - 7);
else
hbuf_put(ob, link->data, link->size);
} else
hesc_nroff(ob, text->data, text->size, 0, 1);
}
HBUF_PUTSL(ob, "\n");
return ret;
}
static int
rndr_autolink(hbuf *ob, const hbuf *link, enum halink_type type,
struct lowdown_node *prev, struct lowdown_node *next,
struct nstate *st, int nln)
{
if (link->size == 0)
return 1;
if (!nln)
if (prev == NULL ||
(ob->size && ob->data[ob->size - 1] != '\n'))
HBUF_PUTSL(ob, "\n");
return putlink(ob, st, link, NULL, next, prev, type);
}
static void
rndr_blockcode(hbuf *ob, const hbuf *content,
const hbuf *lang, const struct nstate *st)
{
if (content->size == 0)
return;
if (st->mdoc) {
HBUF_PUTSL(ob, ".sp 1\n");
HBUF_PUTSL(ob, ".nf\n");
} else
HBUF_PUTSL(ob, ".LD\n");
HBUF_PUTSL(ob, ".ft CR\n");
rndr_block(ob, content->data, content->size);
HBUF_NEWLINE(content, ob);
HBUF_PUTSL(ob, ".ft\n");
if (st->mdoc)
HBUF_PUTSL(ob, ".fi\n");
else
HBUF_PUTSL(ob, ".DE\n");
}
static void
rndr_blockquote(hbuf *ob, const hbuf *content)
{
if (content->size == 0)
return;
HBUF_PUTSL(ob, ".RS\n");
hbuf_put(ob, content->data, content->size);
HBUF_NEWLINE(content, ob);
HBUF_PUTSL(ob, ".RE\n");
}
static void
rndr_codespan(hbuf *ob, const hbuf *content)
{
rndr_span(ob, content->data, content->size);
}
/*
* FIXME: not supported.
*/
static void
rndr_strikethrough(hbuf *ob, const hbuf *content)
{
hbuf_putb(ob, content);
}
static void
rndr_linebreak(hbuf *ob)
{
/* FIXME: should this always have a newline? */
HBUF_PUTSL(ob, "\n.br\n");
}
/*
* For man(7), we use SH for the first-level section, SS for other
* sections.
* FIXME: use PP then italics or something for third-level etc.
* For ms(7), just use SH.
* (Again, see above FIXME.)
* If we're using ms(7) w/groff extensions, used the numbered version of
* the SH macro.
* If we're numbered ms(7), use NH.
* If we're numbered ms(7) w/extensions, use NH and XN (-mspdf).
*/
static void
rndr_header(hbuf *ob, const hbuf *content, int level,
const struct nstate *st)
{
if (content->size == 0)
return;
if (st->mdoc) {
if (level == 1)
HBUF_PUTSL(ob, ".SH ");
else
HBUF_PUTSL(ob, ".SS ");
rndr_one_line_span(ob, content->data, content->size);
HBUF_PUTSL(ob, "\n");
return;
}
if ((st->flags & LOWDOWN_NROFF_NUMBERED))
hbuf_printf(ob, ".NH %d\n", level);
else if ((st->flags & LOWDOWN_NROFF_GROFF))
hbuf_printf(ob, ".SH %d\n", level);
else
hbuf_printf(ob, ".SH\n");
if ((st->flags & LOWDOWN_NROFF_NUMBERED) &&
(st->flags & LOWDOWN_NROFF_GROFF)) {
HBUF_PUTSL(ob, ".XN ");
rndr_one_line_span(ob, content->data, content->size);
HBUF_PUTSL(ob, "\n");
} else {
hbuf_put(ob, content->data, content->size);
HBUF_NEWLINE(content, ob);
}
}
static int
rndr_link(hbuf *ob, const hbuf *content, const hbuf *link,
struct nstate *st, struct lowdown_node *prev,
struct lowdown_node *next, int nln)
{
if (content->size == 0 && link->size == 0)
return 1;
if (!nln)
if (prev == NULL ||
(ob->size && ob->data[ob->size - 1] != '\n'))
HBUF_PUTSL(ob, "\n");
return putlink(ob, st, link,
content, next, prev, HALINK_NORMAL);
}
static void
rndr_listitem(hbuf *ob, const hbuf *content,
const struct lowdown_node *prev,
enum hlist_fl flags, size_t num)
{
char *cdata;
size_t csize;
if (content->size == 0)
return;
/*
* If we're in a "block" list item or are starting the list,
* start vertical spacing.
* Then put us into an indented paragraph.
*/
if (prev == NULL ||
(content->size > 3 &&
memcmp(content->data, ".LP\n", 4) == 0))
HBUF_PUTSL(ob, ".sp 1.0v\n");
HBUF_PUTSL(ob, ".RS\n");
/*
* Now back out by the size of our list glyph(s) and print the
* glyph(s) (padding with two spaces).
*/
if ((flags & HLIST_FL_ORDERED))
hbuf_printf(ob, ".ti -\\w'%zu. \'u\n%zu. ",
num, num);
else
HBUF_PUTSL(ob, ".ti -\\w'\\(bu \'u\n\\(bu ");
/*
* Now we get shitty.
* If we have macros on the content, we need to handle them in a
* special way.
* Paragraphs (.LP) can be stripped out.
* Links need a newline.
*/
cdata = content->data;
csize = content->size;
/* Start by stripping out all paragraphs. */
while (csize > 3 && memcmp(cdata, ".LP\n", 4) == 0) {
cdata += 4;
csize -= 4;
}
/* Now make sure we have a newline before links. */
if (csize > 8 && memcmp(cdata, ".pdfhref ", 9) == 0)
HBUF_PUTSL(ob, "\n");
hbuf_put(ob, cdata, csize);
BUFFER_NEWLINE(cdata, csize, ob);
HBUF_PUTSL(ob, ".RE\n");
}
static void
rndr_paragraph(hbuf *ob, const hbuf *content,
const struct nstate *st, const struct lowdown_node *np)
{
size_t i = 0, org;
if (content->size == 0)
return;
/* Strip away initial white-space. */
while (i < content->size &&
isspace((unsigned char)content->data[i]))
i++;
if (i == content->size)
return;
HBUF_PUTSL(ob, ".LP\n");
if ((st->flags & LOWDOWN_NROFF_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);
BUFFER_NEWLINE(content->data + i, content->size - i, ob);
}
/*
* FIXME: verify behaviour.
*/
static void
rndr_raw_block(hbuf *ob, const hbuf *content, const struct nstate *st)
{
size_t org, sz;
if (content->size == 0)
return;
if ((st->flags & LOWDOWN_NROFF_SKIP_HTML)) {
rndr_block(ob, content->data, content->size);
return;
}
/*
* FIXME: Do we *really* need to trim the HTML? How does that
* make a difference?
*/
sz = content->size;
while (sz > 0 && content->data[sz - 1] == '\n')
sz--;
org = 0;
while (org < sz && content->data[org] == '\n')
org++;
if (org >= sz)
return;
if (ob->size)
hbuf_putc(ob, '\n');
hbuf_put(ob, content->data + org, sz - org);
hbuf_putc(ob, '\n');
}
static void
rndr_hrule(hbuf *ob, const struct nstate *st)
{
/*
* I'm not sure how else to do horizontal lines.
* The LP is to reset the margins.
*/
HBUF_PUTSL(ob, ".LP\n");
if (!st->mdoc)
HBUF_PUTSL(ob, "\\l\'\\n(.lu-\\n(\\n[.in]u\'\n");
}
static void
rndr_image(hbuf *ob, const hbuf *link, const struct nstate *st,
int nln, const struct lowdown_node *prev)
{
const char *cp;
size_t sz;
if (st->mdoc) {
warnx("warning: images not supported");
return;
}
if ((cp = memrchr(link->data, '.', link->size)) == NULL) {
warnx("warning: no image suffix (ignoring)");
return;
}
cp++;
sz = link->size - (cp - link->data);
if (sz == 0) {
warnx("warning: empty image suffix (ignoring)");
return;
}
if (!(sz == 2 && memcmp(cp, "ps", 2) == 0) &&
!(sz == 3 && memcmp(cp, "eps", 3) == 0)) {
warnx("warning: unknown image suffix (ignoring)");
return;
}
if (!nln)
if (prev == NULL ||
(ob->size && ob->data[ob->size - 1] != '\n'))
HBUF_PUTSL(ob, "\n");
hbuf_printf(ob, ".PSPIC %.*s\n",
(int)link->size, link->data);
}
static void
rndr_raw_html(hbuf *ob, const hbuf *text, const struct nstate *st)
{
if ((st->flags & LOWDOWN_NROFF_SKIP_HTML))
return;
rndr_block(ob, text->data, text->size);
}
static void
rndr_table(hbuf *ob, const hbuf *content)
{
HBUF_PUTSL(ob, ".TS\n");
HBUF_PUTSL(ob, "tab(|) expand allbox;\n");
hbuf_put(ob, content->data, content->size);
HBUF_NEWLINE(content, ob);
HBUF_PUTSL(ob, ".TE\n");
}
static void
rndr_table_header(hbuf *ob, const hbuf *content,
const enum htbl_flags *fl, size_t columns)
{
size_t i;
/*
* This specifies the header layout.
* We make the header bold, but this is arbitrary.
*/
for (i = 0; i < columns; i++) {
if (i > 0)
HBUF_PUTSL(ob, " ");
switch (fl[i] & HTBL_FL_ALIGNMASK) {
case HTBL_FL_ALIGN_CENTER:
HBUF_PUTSL(ob, "cb");
break;
case HTBL_FL_ALIGN_RIGHT:
HBUF_PUTSL(ob, "rb");
break;
default:
HBUF_PUTSL(ob, "lb");
break;
}
}
HBUF_PUTSL(ob, "\n");
/* Now the body layout. */
for (i = 0; i < columns; i++) {
if (i > 0)
HBUF_PUTSL(ob, " ");
switch (fl[i] & HTBL_FL_ALIGNMASK) {
case HTBL_FL_ALIGN_CENTER:
HBUF_PUTSL(ob, "c");
break;
case HTBL_FL_ALIGN_RIGHT:
HBUF_PUTSL(ob, "r");
break;
default:
HBUF_PUTSL(ob, "l");
break;
}
}
HBUF_PUTSL(ob, ".\n");
/* Now the table data. */
hbuf_putb(ob, content);
}
static void
rndr_table_body(hbuf *ob, const hbuf *content)
{
hbuf_putb(ob, content);
}
static void
rndr_tablerow(hbuf *ob, const hbuf *content)
{
hbuf_putb(ob, content);
HBUF_PUTSL(ob, "\n");
}
static void
rndr_tablecell(hbuf *ob, const hbuf *content, size_t col)
{
if (col > 0)
HBUF_PUTSL(ob, "|");
if (content->size) {
HBUF_PUTSL(ob, "T{\n");
hbuf_putb(ob, content);
HBUF_PUTSL(ob, "\nT}");
}
}
static void
rndr_superscript(hbuf *ob, const hbuf *content)
{
if (content->size == 0)
return;
/*
* If we have a macro contents, it might be the usual macro
* (solo in its buffer) or starting with a newline.
*/
if (content->data[0] == '.' ||
(content->size &&
content->data[0] == '\n' &&
content->data[1] == '.')) {
HBUF_PUTSL(ob, "\\u\\s-3");
if (content->data[0] != '\n')
HBUF_PUTSL(ob, "\n");
hbuf_putb(ob, content);
HBUF_NEWLINE(content, ob);
HBUF_PUTSL(ob, "\\s+3\\d\n");
} else {
HBUF_PUTSL(ob, "\\u\\s-3");
hbuf_putb(ob, content);
HBUF_PUTSL(ob, "\\s+3\\d");
}
}
static void
rndr_normal_text(hbuf *ob, const hbuf *content, size_t offs,
const struct lowdown_node *prev,
const struct lowdown_node *next,
const struct nstate *st, int nl)
{
size_t i, size;
const char *data;
if (content->size == 0)
return;
data = content->data + offs;
size = content->size - offs;
/*
* If we don't have trailing space, omit the final word because
* we'll put that in the link's pdfhref.
*/
if (next != NULL &&
(next->type == LOWDOWN_LINK_AUTO ||
next->type == LOWDOWN_LINK))
while (size &&
!isspace((unsigned char)data[size - 1]))
size--;
if (nl) {
for (i = 0; i < size; i++)
if (!isspace((unsigned char)data[i]))
break;
rndr_block(ob, data + i, size - i);
} else
rndr_span(ob, data, size);
}
static void
rndr_footnotes(hbuf *ob, const hbuf *content, const struct nstate *st)
{
/* Put a horizontal line in the case of man(7). */
if (content->size && st->mdoc) {
HBUF_PUTSL(ob, ".LP\n");
HBUF_PUTSL(ob, ".sp 3\n");
HBUF_PUTSL(ob, "\\l\'2i'\n");
}
hbuf_putb(ob, content);
}
static void
rndr_footnote_def(hbuf *ob, const hbuf *content, unsigned int num,
const struct nstate *st)
{
/*
* Use groff_ms(7)-style footnotes.
* We know that the definitions are delivered in the same order
* as the footnotes are made, so we can use the automatic
* ordering facilities.
*/
if (!st->mdoc) {
HBUF_PUTSL(ob, ".FS\n");
/* Ignore leading paragraph marker. */
if (content->size > 3 &&
memcmp(content->data, ".LP\n", 4) == 0)
hbuf_put(ob, content->data + 4, content->size - 4);
else
hbuf_put(ob, content->data, content->size);
HBUF_NEWLINE(content, ob);
HBUF_PUTSL(ob, ".FE\n");
return;
}
/*
* For man(7), just print as normal, with a leading footnote
* number in italics and superscripted.
*/
HBUF_PUTSL(ob, ".LP\n");
hbuf_printf(ob, "\\0\\fI\\u\\s-3%u\\s+3\\d\\fP\\0", num);
if (content->size > 3 &&
memcmp(content->data, ".LP\n", 4) == 0)
hbuf_put(ob, content->data + 4, content->size - 4);
else
hbuf_put(ob, content->data, content->size);
HBUF_NEWLINE(content, ob);
}
static void
rndr_footnote_ref(hbuf *ob, unsigned int num, const struct nstate *st)
{
/*
* Use groff_ms(7)-style automatic footnoting, else just put a
* reference number in small superscripts.
*/
if (!st->mdoc)
HBUF_PUTSL(ob, "\\**");
else
hbuf_printf(ob, "\\u\\s-3%u\\s+3\\d", num);
}
static void
rndr_math(void)
{
/* FIXME: use lowdown_opts warnings. */
warnx("warning: math not supported");
}
/*
* Split "b" at sequential white-space, outputting the results in the
* line-based "env" macro.
* The content in "b" has already been escaped, so there's no need to do
* anything but manage white-space.
*/
static void
rndr_meta_multi(hbuf *ob, const hbuf *b, const char *env)
{
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_printf(ob, ".%s\n", env);
rndr_one_line_noescape(ob, start, sz, 1);
HBUF_PUTSL(ob, "\n");
}
}
static void
rndr_meta(hbuf *ob, const hbuf *tmp, struct lowdown_metaq *mq,
const struct lowdown_node *n, const struct nstate *st)
{
enum rndr_meta_key key;
const struct lowdown_node *copy;
struct lowdown_meta *m;
if (mq != NULL) {