-
Notifications
You must be signed in to change notification settings - Fork 36
/
nroff.c
2250 lines (1958 loc) · 52.2 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
/*
* 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 <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>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lowdown.h"
#include "extern.h"
enum nfont {
NFONT_ITALIC = 0, /* italic */
NFONT_BOLD, /* bold */
NFONT_FIXED, /* fixed-width */
NFONT__MAX
};
struct nroff {
struct hentryq headers_used; /* headers we've seen */
int man; /* whether man(7) */
int post_para; /* for choosing PP/LP */
unsigned int flags; /* output flags */
ssize_t headers_offs; /* header offset */
enum nfont fonts[NFONT__MAX]; /* see bqueue_font() */
struct bnodeq **foots; /* footnotes */
size_t footsz; /* footnote size */
size_t footpos; /* footnote position (-tms) */
size_t footdepth; /* printing/parsing footnotes */
size_t indent; /* indentation width */
const char *cr; /* fixed-width font */
const char *cb; /* fixed-width bold font */
const char *ci; /* fixed-width italic font */
const char *cbi; /* fixed-width bold-italic font */
};
enum bscope {
BSCOPE_BLOCK = 0,
BSCOPE_SPAN,
BSCOPE_SEMI,
BSCOPE_SEMI_CLOSE,
BSCOPE_LITERAL,
BSCOPE_FONT,
BSCOPE_COLOUR
};
/*
* Instead of writing directly into the output buffer, we write
* temporarily into bnodes, which are converted into output. These
* nodes are aware of whether they need surrounding newlines.
*/
struct bnode {
char *nbuf; /* (safe) 1st data */
char *buf; /* (unsafe) 2nd data */
char *nargs; /* (safe) 1st args */
char *args; /* (unsafe) 2nd args */
int close; /* BNODE_COLOUR/FONT */
int tblhack; /* BSCOPE_SPAN */
int headerhack; /* BSCOPE_BLOCK */
enum bscope scope; /* scope */
unsigned int font; /* if BNODE_FONT */
#define BFONT_ITALIC 0x01
#define BFONT_BOLD 0x02
#define BFONT_FIXED 0x04
unsigned int colour; /* if BNODE_COLOUR */
#define BFONT_BLUE 0x01
#define BFONT_RED 0x02
TAILQ_ENTRY(bnode) entries;
};
TAILQ_HEAD(bnodeq, bnode);
/*
* Escape unsafe text into roff output such that no roff features are
* invoked by the text (macros, escapes, etc.).
* If "oneline" is non-zero, newlines are replaced with spaces.
* If "literal", doesn't strip leading space.
* Return zero on failure, non-zero on success.
*/
static int
hesc_nroff(struct lowdown_buf *ob, const char *data, size_t size,
int oneline, int literal, int esc)
{
size_t i = 0;
unsigned char ch;
if (size == 0)
return 1;
if (!esc && oneline) {
assert(!literal);
for (i = 0; i < size; i++) {
ch = data[i] == '\n' ? ' ' : data[i];
if (!hbuf_putc(ob, ch))
return 0;
if (ch != ' ')
continue;
while (i < size && isspace((unsigned char)data[i]))
i++;
i--;
}
return 1;
} else if (!esc)
return hbuf_put(ob, data, size);
/* Strip leading whitespace. */
if (!literal && ob->size > 0 && ob->data[ob->size - 1] == '\n')
while (i < size && (data[i] == ' ' || data[i] == '\n'))
i++;
/*
* According to mandoc_char(7), we need to escape the backtick,
* single apostrophe, and tilde or else they'll be considered as
* special Unicode output.
* Slashes need to be escaped too.
* We also escape double-quotes because this text might be used
* within quoted macro arguments.
*/
for ( ; i < size; i++)
switch (data[i]) {
case '^':
if (!HBUF_PUTSL(ob, "\\(ha"))
return 0;
break;
case '~':
if (!HBUF_PUTSL(ob, "\\(ti"))
return 0;
break;
case '`':
if (!HBUF_PUTSL(ob, "\\(ga"))
return 0;
break;
case '"':
if (!HBUF_PUTSL(ob, "\\(dq"))
return 0;
break;
case '\n':
if (!hbuf_putc(ob, oneline ? ' ' : '\n'))
return 0;
if (literal)
break;
/* Prevent leading spaces on the line. */
while (i + 1 < size &&
(data[i + 1] == ' ' ||
data[i + 1] == '\n'))
i++;
break;
case '\\':
if (!HBUF_PUTSL(ob, "\\e"))
return 0;
break;
case '\'':
case '.':
if (!oneline &&
ob->size > 0 &&
ob->data[ob->size - 1] == '\n' &&
!HBUF_PUTSL(ob, "\\&"))
return 0;
/* FALLTHROUGH */
default:
if (!hbuf_putc(ob, data[i]))
return 0;
break;
}
return 1;
}
static const char *
nstate_colour_buf(unsigned int ft)
{
static char fonts[10];
fonts[0] = '\0';
if (ft == BFONT_BLUE)
strlcat(fonts, "blue", sizeof(fonts));
else if (ft == BFONT_RED)
strlcat(fonts, "red", sizeof(fonts));
else
strlcat(fonts, "black", sizeof(fonts));
return fonts;
}
/*
* For compatibility with traditional troff, return non-block font code
* using the correct sequence of \fX, \f(xx, and \f[xxx].
*/
static int
nstate_font(const struct nroff *st, struct lowdown_buf *ob,
unsigned int ft, int enclose)
{
const char *font = NULL;
char fonts[3];
char *cp = fonts;
size_t sz;
if (ft & BFONT_FIXED) {
if ((ft & BFONT_BOLD) && (ft & BFONT_ITALIC))
font = st->cbi;
else if (ft & BFONT_BOLD)
font = st->cb;
else if (ft & BFONT_ITALIC)
font = st->ci;
else
font = st->cr;
} else {
font = cp = fonts;
if (ft & BFONT_BOLD)
(*cp++) = 'B';
if (ft & BFONT_ITALIC)
(*cp++) = 'I';
if (ft == 0)
(*cp++) = 'R';
*cp = '\0';
}
assert(font != NULL);
sz = strlen(font);
assert(sz > 0);
if (!enclose || sz == 1)
return hbuf_puts(ob, font);
if (sz > 2)
return HBUF_PUTSL(ob, "[") &&
hbuf_puts(ob, font) && HBUF_PUTSL(ob, "]");
return HBUF_PUTSL(ob, "(") && hbuf_puts(ob, font);
}
static int
bqueue_colour(struct bnodeq *bq, enum lowdown_chng chng, int close)
{
struct bnode *bn;
if ((bn = calloc(1, sizeof(struct bnode))) == NULL)
return 0;
TAILQ_INSERT_TAIL(bq, bn, entries);
bn->scope = BSCOPE_COLOUR;
bn->close = close;
bn->colour = close ? 0 :
chng == LOWDOWN_CHNG_INSERT ?
BFONT_BLUE : BFONT_RED;
return 1;
}
static int
bqueue_font(const struct nroff *st, struct bnodeq *bq, int close)
{
struct bnode *bn;
if ((bn = calloc(1, sizeof(struct bnode))) == NULL)
return 0;
TAILQ_INSERT_TAIL(bq, bn, entries);
bn->scope = BSCOPE_FONT;
bn->close = close;
if (st->fonts[NFONT_FIXED])
bn->font |= BFONT_FIXED;
if (st->fonts[NFONT_BOLD])
bn->font |= BFONT_BOLD;
if (st->fonts[NFONT_ITALIC])
bn->font |= BFONT_ITALIC;
return 1;
}
static struct bnode *
bqueue_node(struct bnodeq *bq, enum bscope scope, const char *text)
{
struct bnode *bn;
if ((bn = calloc(1, sizeof(struct bnode))) == NULL)
return NULL;
bn->scope = scope;
if (text != NULL && (bn->nbuf = strdup(text)) == NULL) {
free(bn);
return NULL;
}
TAILQ_INSERT_TAIL(bq, bn, entries);
return bn;
}
static struct bnode *
bqueue_span(struct bnodeq *bq, const char *text)
{
return bqueue_node(bq, BSCOPE_SPAN, text);
}
static struct bnode *
bqueue_block(struct nroff *st, struct bnodeq *bq, const char *text)
{
return bqueue_node(bq, BSCOPE_BLOCK, text);
}
static void
bnode_free(struct bnode *bn)
{
free(bn->args);
free(bn->nargs);
free(bn->nbuf);
free(bn->buf);
free(bn);
}
static void
bqueue_free(struct bnodeq *bq)
{
struct bnode *bn;
while ((bn = TAILQ_FIRST(bq)) != NULL) {
TAILQ_REMOVE(bq, bn, entries);
bnode_free(bn);
}
}
static void
bqueue_strip_paras(struct bnodeq *bq)
{
struct bnode *bn;
while ((bn = TAILQ_FIRST(bq)) != NULL) {
if (bn->scope != BSCOPE_BLOCK || bn->nbuf == NULL)
break;
if (strcmp(bn->nbuf, ".PP") &&
strcmp(bn->nbuf, ".IP") &&
strcmp(bn->nbuf, ".LP"))
break;
TAILQ_REMOVE(bq, bn, entries);
bnode_free(bn);
}
}
/**
* Flush nodes from "bq" into "ob". This does not remove any nodes from
* "bq" (hence it being const).
*/
static int
bqueue_flush(const struct nroff *st, struct lowdown_buf *ob,
const struct bnodeq *bq)
{
const struct bnode *bn, *chk, *next;
const char *cp;
int nextblk;
char trailingchar;
TAILQ_FOREACH(bn, bq, entries) {
nextblk = 0;
/*
* The semi-block macro is a span (within text content),
* but has block syntax. If there's no leading space,
* use "\c" to inhibit whitespace prior to the macro.
*/
if (bn->scope == BSCOPE_SEMI &&
ob->size > 0 &&
ob->data[ob->size - 1] != '\n' &&
!hbuf_puts(ob, "\\c"))
return 0;
/*
* Block/semi scopes start with a newline.
* Also have colours use their own block, as otherwise
* (bugs in groff?) inline colour selection after a
* hyperlink macro causes line breaks.
* Besides, having spaces around changing colour, which
* indicates differences, improves readability.
*/
if (bn->scope == BSCOPE_BLOCK ||
bn->scope == BSCOPE_SEMI ||
bn->scope == BSCOPE_SEMI_CLOSE ||
bn->scope == BSCOPE_COLOUR) {
if (ob->size > 0 &&
ob->data[ob->size - 1] != '\n' &&
!hbuf_putc(ob, '\n'))
return 0;
nextblk = 1;
}
/*
* Fonts can either be macros or inline depending upon
* where they set relative to a macro block.
*/
if (bn->scope == BSCOPE_FONT) {
chk = bn->close ?
TAILQ_PREV(bn, bnodeq, entries) :
TAILQ_NEXT(bn, entries);
if (chk != NULL &&
(chk->scope == BSCOPE_SEMI ||
chk->scope == BSCOPE_SEMI_CLOSE ||
chk->scope == BSCOPE_BLOCK)) {
if (ob->size > 0 &&
ob->data[ob->size - 1] != '\n' &&
!hbuf_putc(ob, '\n'))
return 0;
nextblk = 1;
}
}
/* Print font and colour escapes. */
if (bn->scope == BSCOPE_FONT && nextblk) {
if (!HBUF_PUTSL(ob, ".ft "))
return 0;
if (!nstate_font(st, ob, bn->font, 0))
return 0;
} else if (bn->scope == BSCOPE_FONT) {
if (!HBUF_PUTSL(ob, "\\f"))
return 0;
if (!nstate_font(st, ob, bn->font, 1))
return 0;
} else if (bn->scope == BSCOPE_COLOUR) {
assert(nextblk);
if (!hbuf_printf(ob, ".gcolor %s",
nstate_colour_buf(bn->colour)))
return 0;
}
/*
* A "tblhack" is used by a span macro to indicate
* that it should start its own line, but that data
* continues to flow after it. This is only used in
* tables with T}, at this point.
*/
if (bn->scope == BSCOPE_SPAN && bn->tblhack &&
ob->size > 0 && ob->data[ob->size - 1] != '\n')
if (!hbuf_putc(ob, '\n'))
return 0;
/*
* If we're a span, double-check to see whether we
* should introduce a line with an escape.
*/
if (bn->scope == BSCOPE_SPAN &&
bn->nbuf != NULL && ob->size > 0 &&
ob->data[ob->size - 1] == '\n' &&
(bn->nbuf[0] == '.' || bn->nbuf[0] == '\'') &&
!HBUF_PUTSL(ob, "\\&"))
return 0;
/* Safe data need not be escaped. */
if (bn->nbuf != NULL && !hbuf_puts(ob, bn->nbuf))
return 0;
/* Unsafe data must be escaped. */
if (bn->scope == BSCOPE_LITERAL) {
assert(bn->buf != NULL);
if (!hesc_nroff(ob, bn->buf,
strlen(bn->buf), 0, 1, 1))
return 0;
} else if (bn->buf != NULL)
if (!hesc_nroff(ob, bn->buf,
strlen(bn->buf), 0, 0, 1))
return 0;
/*
* Special handling of the semi-blocks, specifically
* either ".pdfhref" or ".UE", where the next text has
* no leading space. In this case, we need to terminate
* the semi-block macro with "\c" to inhibit whitespace.
*/
if ((next = TAILQ_NEXT(bn, entries)) != NULL &&
next->scope == BSCOPE_SPAN &&
next->buf != NULL &&
next->buf[0] != ' ' &&
next->buf[0] != '\n') {
if (!st->man &&
bn->scope == BSCOPE_SEMI &&
!HBUF_PUTSL(ob, " -A \"\\c\""))
return 0;
if (st->man &&
bn->scope == BSCOPE_SEMI_CLOSE &&
!HBUF_PUTSL(ob, " \\c"))
return 0;
}
/*
* FIXME: BSCOPE_SEMI_CLOSE in a font context needs to
* reopen the font (e.g., \fB), as it internally will
* unset any font context.
*/
/*
* Macro arguments follow after space. For links, these
* must all be printed on the same line.
*/
if (bn->nargs != NULL &&
(bn->scope == BSCOPE_BLOCK ||
bn->scope == BSCOPE_SEMI)) {
assert(nextblk);
if (!hbuf_putc(ob, ' '))
return 0;
for (cp = bn->nargs; *cp != '\0'; cp++)
if (!hbuf_putc(ob,
*cp == '\n' ? ' ' : *cp))
return 0;
}
if (bn->args != NULL) {
assert(nextblk);
assert(bn->scope == BSCOPE_BLOCK ||
bn->scope == BSCOPE_SEMI);
if (!hbuf_putc(ob, ' '))
return 0;
if (!hesc_nroff(ob, bn->args,
strlen(bn->args), 1, 0, 1))
return 0;
}
/*
* The "headerhack" is used by SH block macros to
* indicate that their children are all normal text or
* entities, and so to output the content on the same
* line as the SH macro. This is a hack because man(7)
* specifically allows for next-line content; however,
* buggy software (e.g., Mac OS X's makewhatis) don't
* correctly parse empty SH properly.
*/
trailingchar = bn->headerhack ? ' ' : '\n';
if (nextblk && ob->size > 0 &&
ob->data[ob->size - 1] != trailingchar &&
!hbuf_putc(ob, trailingchar))
return 0;
}
return 1;
}
/*
* Convert a link into a short-link and place the escaped output into a
* returned string.
* Returns NULL on memory allocation failure.
*/
static char *
hbuf2shortlink(const struct lowdown_buf *link)
{
struct lowdown_buf *tmp = NULL, *slink = NULL;
char *ret = NULL;
if ((tmp = hbuf_new(32)) == NULL)
goto out;
if ((slink = hbuf_new(32)) == NULL)
goto out;
if (!hbuf_shortlink(tmp, link))
goto out;
if (!hesc_nroff(slink, tmp->data, tmp->size, 1, 0, 1))
goto out;
ret = strndup(slink->data, slink->size);
out:
hbuf_free(tmp);
hbuf_free(slink);
return ret;
}
/**
* Escape a URL. Return FALSE on error (memory), TRUE on success.
*/
static int
putlinkhref(struct lowdown_buf *ob, const struct lowdown_buf *link,
enum halink_type *type)
{
size_t i = 0;
if (type != NULL && *type == HALINK_EMAIL &&
hbuf_strprefix(link, "mailto:"))
i = strlen("mailto:");
for ( ; i < link->size; i++) {
if (!isprint((unsigned char)link->data[i]) ||
strchr("<>\\^`{|}\"", link->data[i]) != NULL) {
if (!hbuf_printf(ob, "%%%.2X", link->data[i]))
return 0;
} else if (!hbuf_putc(ob, link->data[i]))
return 0;
}
return 1;
}
/*
* Manage hypertext linking with the groff "pdfhref" macro, UR/UE, or
* simply using italics. Return FALSE on error (memory), TRUE on
* success.
*/
static int
putlink(struct bnodeq *obq, struct nroff *st,
const struct lowdown_node *n, const struct lowdown_buf *link,
const struct lowdown_buf *id, struct bnodeq *bq,
enum halink_type type)
{
struct lowdown_buf *ob = NULL;
struct bnode *bn;
int rc = 0, classic = 0;
classic = !(st->flags & LOWDOWN_NROFF_GROFF);
if (type != HALINK_EMAIL && hbuf_strprefix(link, "mailto:"))
type = HALINK_EMAIL;
/*
* XXX: override as classic if -tman and in a section header or
* definition title.
* This is because UR/UE or MT/ME don't work properly in at
* least mandoc when invoked with link text: the parser things
* that the content is the next line, then puts all UE content
* in the subsequent body.
*/
if (st->man && (st->flags & LOWDOWN_NROFF_GROFF))
for ( ; n != NULL && !classic; n = n->parent)
if (n->type == LOWDOWN_HEADER ||
n->type == LOWDOWN_DEFINITION_TITLE)
classic = 1;
if (classic) {
/*
* For output without .pdfhref ("groff") or UR/UE,
* format the link as-is, with text then link, or use
* the various shorteners.
*/
if (bq == NULL) {
/*
* No link content: format the URL according to
* the user's style and output it in italics.
*/
st->fonts[NFONT_ITALIC]++;
if (!bqueue_font(st, obq, 0))
goto out;
if ((bn = bqueue_span(obq, NULL)) == NULL)
goto out;
if (st->flags & LOWDOWN_NROFF_SHORTLINK) {
bn->nbuf = hbuf2shortlink(link);
if (bn->nbuf == NULL)
goto out;
} else {
bn->buf = strndup(link->data, link->size);
if (bn->buf == NULL)
goto out;
}
st->fonts[NFONT_ITALIC]--;
if (!bqueue_font(st, obq, 1))
goto out;
rc = 1;
goto out;
}
/* Link content exists: output it in bold. */
st->fonts[NFONT_BOLD]++;
if (!bqueue_font(st, obq, 0))
goto out;
TAILQ_CONCAT(obq, bq, entries);
st->fonts[NFONT_BOLD]--;
if (!bqueue_font(st, obq, 1))
goto out;
if (st->flags & LOWDOWN_NROFF_NOLINK) {
rc = 1;
goto out;
}
/* Optionally output the link following the content. */
if (bqueue_span(obq, " <") == NULL)
goto out;
st->fonts[NFONT_ITALIC]++;
if (!bqueue_font(st, obq, 0))
goto out;
if ((bn = bqueue_span(obq, NULL)) == NULL)
goto out;
if (st->flags & LOWDOWN_NROFF_SHORTLINK) {
bn->nbuf = hbuf2shortlink(link);
if (bn->nbuf == NULL)
goto out;
} else {
bn->buf = strndup(link->data, link->size);
if (bn->buf == NULL)
goto out;
}
st->fonts[NFONT_ITALIC]--;
if (!bqueue_font(st, obq, 1))
goto out;
if (bqueue_span(obq, ">") == NULL)
goto out;
rc = 1;
goto out;
} else if (st->man) {
/*
* For man(7) documents, use UR/UE or MT/ME. According
* to the groff documentation, these are supported by
* all modern formatters.
*/
if ((ob = hbuf_new(32)) == NULL)
goto out;
/* This will strip out the "mailto:", if defined. */
if (!putlinkhref(ob, link, &type))
goto out;
/* Either MT or UR depending on if a URL. */
bn = bqueue_node(obq, BSCOPE_SEMI, type == HALINK_EMAIL ?
".MT" : ".UR");
if (bn == NULL)
goto out;
if ((bn->nargs = strndup(ob->data, ob->size)) == NULL)
goto out;
/* Link text (optional). */
if (bq != NULL)
TAILQ_CONCAT(obq, bq, entries);
/* Close out the link content. */
bn = bqueue_node(obq, BSCOPE_SEMI_CLOSE,
type == HALINK_EMAIL ? ".ME" : ".UE");
if (bn == NULL)
goto out;
rc = 1;
goto out;
}
/* This is an ms document with groff extensions: use pdfhref. */
if ((ob = hbuf_new(32)) == NULL)
goto out;
/* Encode the URL. */
if (!HBUF_PUTSL(ob, "-D "))
goto out;
/*
* If this link an e-mail, make sure it doesn't already start
* with "mailto:", which can happen when we override te type by
* investigating whether the prefix is "mailto:".
*/
if (type == HALINK_EMAIL && !hbuf_strprefix(link, "mailto:") &&
!HBUF_PUTSL(ob, "mailto:"))
goto out;
if (!putlinkhref(ob, link, NULL))
goto out;
if (!HBUF_PUTSL(ob, " -- "))
goto out;
/*
* If no content, ouput the link (possibly stripping "mailto:").
* Otherwise, flush the content to the output.
*/
if (bq == NULL && !putlinkhref(ob, link, &type))
goto out;
else if (bq != NULL && !bqueue_flush(st, ob, bq))
goto out;
/*
* If we have an ID, emit it before the link part. This is
* important because this isn't printed, so using "-A \c" will
* have no effect, so that's used on the subsequent link.
*/
if (id != NULL && id->size > 0) {
bn = bqueue_node(obq, BSCOPE_SEMI, ".pdfhref M");
if (bn == NULL)
goto out;
bn->args = strndup(id->data, id->size);
if (bn->args == NULL)
goto out;
}
/* Finally, emit the external or in-page link contents. */
bn = bqueue_node(obq, BSCOPE_SEMI,
(type != HALINK_EMAIL && link->size > 0 &&
link->data[0] == '#') ?
".pdfhref L" : ".pdfhref W");
if (bn == NULL)
goto out;
if ((bn->nargs = strndup(ob->data, ob->size)) == NULL)
goto out;
rc = 1;
out:
hbuf_free(ob);
return rc;
}
/*
* Return FALSE on failure, TRUE on success.
*/
static int
rndr_autolink(struct nroff *st, struct bnodeq *obq,
const struct lowdown_node *n)
{
return putlink(obq, st, n, &n->rndr_autolink.link, NULL, NULL,
n->rndr_autolink.type);
}
static int
rndr_blockcode(struct nroff *st, struct bnodeq *obq,
const struct rndr_blockcode *param)
{
struct bnode *bn;
/*
* XXX: intentionally don't use LD/DE because it introduces
* vertical space. This means that subsequent blocks
* (paragraphs, etc.) will have a double-newline.
*/
if (bqueue_block(st, obq, ".LP") == NULL)
return 0;
if (st->man && (st->flags & LOWDOWN_NROFF_GROFF)) {
if (bqueue_block(st, obq, ".EX") == NULL)
return 0;
} else {
if (bqueue_block(st, obq, ".nf") == NULL)
return 0;
if (bqueue_block(st, obq, ".ft CR") == NULL)
return 0;
}
if ((bn = calloc(1, sizeof(struct bnode))) == NULL)
return 0;
TAILQ_INSERT_TAIL(obq, bn, entries);
bn->scope = BSCOPE_LITERAL;
bn->buf = strndup(param->text.data, param->text.size);
if (bn->buf == NULL)
return 0;
if (st->man && (st->flags & LOWDOWN_NROFF_GROFF))
return bqueue_block(st, obq, ".EE") != NULL;
if (bqueue_block(st, obq, ".ft") == NULL)
return 0;
return bqueue_block(st, obq, ".fi") != NULL;
}
static int
rndr_definition_title(struct nroff *st, struct bnodeq *obq,
struct bnodeq *bq)
{
char buf[32];
snprintf(buf, sizeof(buf), ".TP %zu", st->indent);
if (st->man && bqueue_block(st, obq, buf) == NULL)
return 0;
else if (!st->man && bqueue_block(st, obq, ".XP") == NULL)
return 0;
TAILQ_CONCAT(obq, bq, entries);
if (st->man && bqueue_span(obq, "\n") == NULL)
return 0;
else if (!st->man && bqueue_block(st, obq, ".br") == NULL)
return 0;
return 1;
}
static int
rndr_definition_data(const struct nroff *st, struct bnodeq *obq,
struct bnodeq *bq)
{
/* Strip out leading paragraphs. */
bqueue_strip_paras(bq);
TAILQ_CONCAT(obq, bq, entries);
return 1;
}
static int
rndr_list(struct nroff *st, struct bnodeq *obq,
const struct lowdown_node *n, struct bnodeq *bq)
{
/*
* If we have a nested list, we need to use RS/RE to indent the
* nested component. Otherwise the "IP" used for the titles and
* contained paragraphs won't indent properly.
*/
for (n = n->parent; n != NULL; n = n->parent)
if (n->type == LOWDOWN_LISTITEM)
break;
if (n != NULL && bqueue_block(st, obq, ".RS") == NULL)
return 0;
TAILQ_CONCAT(obq, bq, entries);
if (n != NULL && bqueue_block(st, obq, ".RE") == NULL)
return 0;
st->post_para = 1;
return 1;
}
static int
rndr_blockquote(struct nroff *st, struct bnodeq *obq,
struct bnodeq *bq)
{
if (bqueue_block(st, obq, ".RS") == NULL)
return 0;
TAILQ_CONCAT(obq, bq, entries);
return bqueue_block(st, obq, ".RE") != NULL;
}
static int
rndr_codespan(struct bnodeq *obq, const struct rndr_codespan *param)
{
struct bnode *bn;
if ((bn = bqueue_span(obq, NULL)) == NULL)
return 0;
bn->buf = strndup(param->text.data, param->text.size);
return bn->buf != NULL;
}
static int
rndr_linebreak(struct nroff *st, struct bnodeq *obq)
{
return bqueue_block(st, obq, ".br") != NULL;
}
static int
rndr_header(struct nroff *st, struct bnodeq *obq, struct bnodeq *bq,
const struct lowdown_node *n)
{
ssize_t level;
struct bnode *bn;
struct lowdown_buf *buf = NULL;
const struct lowdown_buf *nbuf;
int rc = 0;
const struct lowdown_node *child;
level = (ssize_t)n->rndr_header.level + st->headers_offs;
if (level < 1)
level = 1;
/*
* For man(7), we use SH for the first-level section, SS for
* other sections. TODO: use PP then italics or something for
* third-level etc.
*/
if (st->man) {
bn = level == 1 ?
bqueue_block(st, obq, ".SH") :
bqueue_block(st, obq, ".SS");
if (bn == NULL)
return 0;
/*
* Do a scan of the contents of the SH. If there's only
* normal text (or entities), then record this fact. It
* will be used in bqueue_flush() for how the macro is
* serialised.