-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdoc_html.c
2178 lines (1831 loc) · 42.4 KB
/
mdoc_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: mdoc_html.c,v 1.240 2016/01/08 17:48:09 schwarze Exp $ */
/*
* Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <[email protected]>
* Copyright (c) 2014, 2015, 2016 Ingo Schwarze <[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 AUTHORS DISCLAIM ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS 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"
#include <sys/types.h>
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "mandoc_aux.h"
#include "roff.h"
#include "mdoc.h"
#include "out.h"
#include "html.h"
#include "main.h"
#define INDENT 5
#define MDOC_ARGS const struct roff_meta *meta, \
struct roff_node *n, \
struct html *h
#ifndef MIN
#define MIN(a,b) ((/*CONSTCOND*/(a)<(b))?(a):(b))
#endif
struct htmlmdoc {
int (*pre)(MDOC_ARGS);
void (*post)(MDOC_ARGS);
};
static void print_mdoc_head(MDOC_ARGS);
static void print_mdoc_node(MDOC_ARGS);
static void print_mdoc_nodelist(MDOC_ARGS);
static void synopsis_pre(struct html *,
const struct roff_node *);
static void a2width(const char *, struct roffsu *);
static void mdoc_root_post(MDOC_ARGS);
static int mdoc_root_pre(MDOC_ARGS);
static void mdoc__x_post(MDOC_ARGS);
static int mdoc__x_pre(MDOC_ARGS);
static int mdoc_ad_pre(MDOC_ARGS);
static int mdoc_an_pre(MDOC_ARGS);
static int mdoc_ap_pre(MDOC_ARGS);
static int mdoc_ar_pre(MDOC_ARGS);
static int mdoc_bd_pre(MDOC_ARGS);
static int mdoc_bf_pre(MDOC_ARGS);
static void mdoc_bk_post(MDOC_ARGS);
static int mdoc_bk_pre(MDOC_ARGS);
static int mdoc_bl_pre(MDOC_ARGS);
static int mdoc_bt_pre(MDOC_ARGS);
static int mdoc_bx_pre(MDOC_ARGS);
static int mdoc_cd_pre(MDOC_ARGS);
static int mdoc_d1_pre(MDOC_ARGS);
static int mdoc_dv_pre(MDOC_ARGS);
static int mdoc_fa_pre(MDOC_ARGS);
static int mdoc_fd_pre(MDOC_ARGS);
static int mdoc_fl_pre(MDOC_ARGS);
static int mdoc_fn_pre(MDOC_ARGS);
static int mdoc_ft_pre(MDOC_ARGS);
static int mdoc_em_pre(MDOC_ARGS);
static void mdoc_eo_post(MDOC_ARGS);
static int mdoc_eo_pre(MDOC_ARGS);
static int mdoc_er_pre(MDOC_ARGS);
static int mdoc_ev_pre(MDOC_ARGS);
static int mdoc_ex_pre(MDOC_ARGS);
static void mdoc_fo_post(MDOC_ARGS);
static int mdoc_fo_pre(MDOC_ARGS);
static int mdoc_ic_pre(MDOC_ARGS);
static int mdoc_igndelim_pre(MDOC_ARGS);
static int mdoc_in_pre(MDOC_ARGS);
static int mdoc_it_pre(MDOC_ARGS);
static int mdoc_lb_pre(MDOC_ARGS);
static int mdoc_li_pre(MDOC_ARGS);
static int mdoc_lk_pre(MDOC_ARGS);
static int mdoc_mt_pre(MDOC_ARGS);
static int mdoc_ms_pre(MDOC_ARGS);
static int mdoc_nd_pre(MDOC_ARGS);
static int mdoc_nm_pre(MDOC_ARGS);
static int mdoc_no_pre(MDOC_ARGS);
static int mdoc_ns_pre(MDOC_ARGS);
static int mdoc_pa_pre(MDOC_ARGS);
static void mdoc_pf_post(MDOC_ARGS);
static int mdoc_pp_pre(MDOC_ARGS);
static void mdoc_quote_post(MDOC_ARGS);
static int mdoc_quote_pre(MDOC_ARGS);
static int mdoc_rs_pre(MDOC_ARGS);
static int mdoc_rv_pre(MDOC_ARGS);
static int mdoc_sh_pre(MDOC_ARGS);
static int mdoc_skip_pre(MDOC_ARGS);
static int mdoc_sm_pre(MDOC_ARGS);
static int mdoc_sp_pre(MDOC_ARGS);
static int mdoc_ss_pre(MDOC_ARGS);
static int mdoc_sx_pre(MDOC_ARGS);
static int mdoc_sy_pre(MDOC_ARGS);
static int mdoc_ud_pre(MDOC_ARGS);
static int mdoc_va_pre(MDOC_ARGS);
static int mdoc_vt_pre(MDOC_ARGS);
static int mdoc_xr_pre(MDOC_ARGS);
static int mdoc_xx_pre(MDOC_ARGS);
static const struct htmlmdoc mdocs[MDOC_MAX] = {
{mdoc_ap_pre, NULL}, /* Ap */
{NULL, NULL}, /* Dd */
{NULL, NULL}, /* Dt */
{NULL, NULL}, /* Os */
{mdoc_sh_pre, NULL }, /* Sh */
{mdoc_ss_pre, NULL }, /* Ss */
{mdoc_pp_pre, NULL}, /* Pp */
{mdoc_d1_pre, NULL}, /* D1 */
{mdoc_d1_pre, NULL}, /* Dl */
{mdoc_bd_pre, NULL}, /* Bd */
{NULL, NULL}, /* Ed */
{mdoc_bl_pre, NULL}, /* Bl */
{NULL, NULL}, /* El */
{mdoc_it_pre, NULL}, /* It */
{mdoc_ad_pre, NULL}, /* Ad */
{mdoc_an_pre, NULL}, /* An */
{mdoc_ar_pre, NULL}, /* Ar */
{mdoc_cd_pre, NULL}, /* Cd */
{mdoc_fl_pre, NULL}, /* Cm */
{mdoc_dv_pre, NULL}, /* Dv */
{mdoc_er_pre, NULL}, /* Er */
{mdoc_ev_pre, NULL}, /* Ev */
{mdoc_ex_pre, NULL}, /* Ex */
{mdoc_fa_pre, NULL}, /* Fa */
{mdoc_fd_pre, NULL}, /* Fd */
{mdoc_fl_pre, NULL}, /* Fl */
{mdoc_fn_pre, NULL}, /* Fn */
{mdoc_ft_pre, NULL}, /* Ft */
{mdoc_ic_pre, NULL}, /* Ic */
{mdoc_in_pre, NULL}, /* In */
{mdoc_li_pre, NULL}, /* Li */
{mdoc_nd_pre, NULL}, /* Nd */
{mdoc_nm_pre, NULL}, /* Nm */
{mdoc_quote_pre, mdoc_quote_post}, /* Op */
{mdoc_ft_pre, NULL}, /* Ot */
{mdoc_pa_pre, NULL}, /* Pa */
{mdoc_rv_pre, NULL}, /* Rv */
{NULL, NULL}, /* St */
{mdoc_va_pre, NULL}, /* Va */
{mdoc_vt_pre, NULL}, /* Vt */
{mdoc_xr_pre, NULL}, /* Xr */
{mdoc__x_pre, mdoc__x_post}, /* %A */
{mdoc__x_pre, mdoc__x_post}, /* %B */
{mdoc__x_pre, mdoc__x_post}, /* %D */
{mdoc__x_pre, mdoc__x_post}, /* %I */
{mdoc__x_pre, mdoc__x_post}, /* %J */
{mdoc__x_pre, mdoc__x_post}, /* %N */
{mdoc__x_pre, mdoc__x_post}, /* %O */
{mdoc__x_pre, mdoc__x_post}, /* %P */
{mdoc__x_pre, mdoc__x_post}, /* %R */
{mdoc__x_pre, mdoc__x_post}, /* %T */
{mdoc__x_pre, mdoc__x_post}, /* %V */
{NULL, NULL}, /* Ac */
{mdoc_quote_pre, mdoc_quote_post}, /* Ao */
{mdoc_quote_pre, mdoc_quote_post}, /* Aq */
{NULL, NULL}, /* At */
{NULL, NULL}, /* Bc */
{mdoc_bf_pre, NULL}, /* Bf */
{mdoc_quote_pre, mdoc_quote_post}, /* Bo */
{mdoc_quote_pre, mdoc_quote_post}, /* Bq */
{mdoc_xx_pre, NULL}, /* Bsx */
{mdoc_bx_pre, NULL}, /* Bx */
{mdoc_skip_pre, NULL}, /* Db */
{NULL, NULL}, /* Dc */
{mdoc_quote_pre, mdoc_quote_post}, /* Do */
{mdoc_quote_pre, mdoc_quote_post}, /* Dq */
{NULL, NULL}, /* Ec */ /* FIXME: no space */
{NULL, NULL}, /* Ef */
{mdoc_em_pre, NULL}, /* Em */
{mdoc_eo_pre, mdoc_eo_post}, /* Eo */
{mdoc_xx_pre, NULL}, /* Fx */
{mdoc_ms_pre, NULL}, /* Ms */
{mdoc_no_pre, NULL}, /* No */
{mdoc_ns_pre, NULL}, /* Ns */
{mdoc_xx_pre, NULL}, /* Nx */
{mdoc_xx_pre, NULL}, /* Ox */
{NULL, NULL}, /* Pc */
{mdoc_igndelim_pre, mdoc_pf_post}, /* Pf */
{mdoc_quote_pre, mdoc_quote_post}, /* Po */
{mdoc_quote_pre, mdoc_quote_post}, /* Pq */
{NULL, NULL}, /* Qc */
{mdoc_quote_pre, mdoc_quote_post}, /* Ql */
{mdoc_quote_pre, mdoc_quote_post}, /* Qo */
{mdoc_quote_pre, mdoc_quote_post}, /* Qq */
{NULL, NULL}, /* Re */
{mdoc_rs_pre, NULL}, /* Rs */
{NULL, NULL}, /* Sc */
{mdoc_quote_pre, mdoc_quote_post}, /* So */
{mdoc_quote_pre, mdoc_quote_post}, /* Sq */
{mdoc_sm_pre, NULL}, /* Sm */
{mdoc_sx_pre, NULL}, /* Sx */
{mdoc_sy_pre, NULL}, /* Sy */
{NULL, NULL}, /* Tn */
{mdoc_xx_pre, NULL}, /* Ux */
{NULL, NULL}, /* Xc */
{NULL, NULL}, /* Xo */
{mdoc_fo_pre, mdoc_fo_post}, /* Fo */
{NULL, NULL}, /* Fc */
{mdoc_quote_pre, mdoc_quote_post}, /* Oo */
{NULL, NULL}, /* Oc */
{mdoc_bk_pre, mdoc_bk_post}, /* Bk */
{NULL, NULL}, /* Ek */
{mdoc_bt_pre, NULL}, /* Bt */
{NULL, NULL}, /* Hf */
{mdoc_em_pre, NULL}, /* Fr */
{mdoc_ud_pre, NULL}, /* Ud */
{mdoc_lb_pre, NULL}, /* Lb */
{mdoc_pp_pre, NULL}, /* Lp */
{mdoc_lk_pre, NULL}, /* Lk */
{mdoc_mt_pre, NULL}, /* Mt */
{mdoc_quote_pre, mdoc_quote_post}, /* Brq */
{mdoc_quote_pre, mdoc_quote_post}, /* Bro */
{NULL, NULL}, /* Brc */
{mdoc__x_pre, mdoc__x_post}, /* %C */
{mdoc_skip_pre, NULL}, /* Es */
{mdoc_quote_pre, mdoc_quote_post}, /* En */
{mdoc_xx_pre, NULL}, /* Dx */
{mdoc__x_pre, mdoc__x_post}, /* %Q */
{mdoc_sp_pre, NULL}, /* br */
{mdoc_sp_pre, NULL}, /* sp */
{mdoc__x_pre, mdoc__x_post}, /* %U */
{NULL, NULL}, /* Ta */
{mdoc_skip_pre, NULL}, /* ll */
};
static const char * const lists[LIST_MAX] = {
NULL,
"list-bul",
"list-col",
"list-dash",
"list-diag",
"list-enum",
"list-hang",
"list-hyph",
"list-inset",
"list-item",
"list-ohang",
"list-tag"
};
/*
* Calculate the scaling unit passed in a `-width' argument. This uses
* either a native scaling unit (e.g., 1i, 2m) or the string length of
* the value.
*/
static void
a2width(const char *p, struct roffsu *su)
{
if (a2roffsu(p, su, SCALE_MAX) < 2) {
su->unit = SCALE_EN;
su->scale = html_strlen(p);
} else if (su->scale < 0.0)
su->scale = 0.0;
}
/*
* See the same function in mdoc_term.c for documentation.
*/
static void
synopsis_pre(struct html *h, const struct roff_node *n)
{
if (NULL == n->prev || ! (MDOC_SYNPRETTY & n->flags))
return;
if (n->prev->tok == n->tok &&
MDOC_Fo != n->tok &&
MDOC_Ft != n->tok &&
MDOC_Fn != n->tok) {
print_otag(h, TAG_BR, 0, NULL);
return;
}
switch (n->prev->tok) {
case MDOC_Fd:
case MDOC_Fn:
case MDOC_Fo:
case MDOC_In:
case MDOC_Vt:
print_paragraph(h);
break;
case MDOC_Ft:
if (MDOC_Fn != n->tok && MDOC_Fo != n->tok) {
print_paragraph(h);
break;
}
/* FALLTHROUGH */
default:
print_otag(h, TAG_BR, 0, NULL);
break;
}
}
void
html_mdoc(void *arg, const struct roff_man *mdoc)
{
struct htmlpair tag;
struct html *h;
struct tag *t, *tt;
PAIR_CLASS_INIT(&tag, "mandoc");
h = (struct html *)arg;
if ( ! (HTML_FRAGMENT & h->oflags)) {
print_gen_decls(h);
t = print_otag(h, TAG_HTML, 0, NULL);
tt = print_otag(h, TAG_HEAD, 0, NULL);
print_mdoc_head(&mdoc->meta, mdoc->first->child, h);
print_tagq(h, tt);
print_otag(h, TAG_BODY, 0, NULL);
print_otag(h, TAG_DIV, 1, &tag);
} else
t = print_otag(h, TAG_DIV, 1, &tag);
mdoc_root_pre(&mdoc->meta, mdoc->first->child, h);
print_mdoc_nodelist(&mdoc->meta, mdoc->first->child, h);
mdoc_root_post(&mdoc->meta, mdoc->first->child, h);
print_tagq(h, t);
putchar('\n');
}
static void
print_mdoc_head(MDOC_ARGS)
{
print_gen_head(h);
bufinit(h);
bufcat(h, meta->title);
if (meta->msec)
bufcat_fmt(h, "(%s)", meta->msec);
if (meta->arch)
bufcat_fmt(h, " (%s)", meta->arch);
print_otag(h, TAG_TITLE, 0, NULL);
print_text(h, h->buf);
}
static void
print_mdoc_nodelist(MDOC_ARGS)
{
while (n != NULL) {
print_mdoc_node(meta, n, h);
n = n->next;
}
}
static void
print_mdoc_node(MDOC_ARGS)
{
int child;
struct tag *t;
child = 1;
t = h->tags.head;
n->flags &= ~MDOC_ENDED;
switch (n->type) {
case ROFFT_TEXT:
/* No tables in this mode... */
assert(NULL == h->tblt);
/*
* Make sure that if we're in a literal mode already
* (i.e., within a <PRE>) don't print the newline.
*/
if (' ' == *n->string && MDOC_LINE & n->flags)
if ( ! (HTML_LITERAL & h->flags))
print_otag(h, TAG_BR, 0, NULL);
if (MDOC_DELIMC & n->flags)
h->flags |= HTML_NOSPACE;
print_text(h, n->string);
if (MDOC_DELIMO & n->flags)
h->flags |= HTML_NOSPACE;
return;
case ROFFT_EQN:
if (n->flags & MDOC_LINE)
putchar('\n');
print_eqn(h, n->eqn);
break;
case ROFFT_TBL:
/*
* This will take care of initialising all of the table
* state data for the first table, then tearing it down
* for the last one.
*/
print_tbl(h, n->span);
return;
default:
/*
* Close out the current table, if it's open, and unset
* the "meta" table state. This will be reopened on the
* next table element.
*/
if (h->tblt != NULL) {
print_tblclose(h);
t = h->tags.head;
}
assert(h->tblt == NULL);
if (mdocs[n->tok].pre && (n->end == ENDBODY_NOT || n->child))
child = (*mdocs[n->tok].pre)(meta, n, h);
break;
}
if (h->flags & HTML_KEEP && n->flags & MDOC_LINE) {
h->flags &= ~HTML_KEEP;
h->flags |= HTML_PREKEEP;
}
if (child && n->child)
print_mdoc_nodelist(meta, n->child, h);
print_stagq(h, t);
switch (n->type) {
case ROFFT_EQN:
break;
default:
if ( ! mdocs[n->tok].post || n->flags & MDOC_ENDED)
break;
(*mdocs[n->tok].post)(meta, n, h);
if (n->end != ENDBODY_NOT)
n->body->flags |= MDOC_ENDED;
if (n->end == ENDBODY_NOSPACE)
h->flags |= HTML_NOSPACE;
break;
}
}
static void
mdoc_root_post(MDOC_ARGS)
{
struct htmlpair tag;
struct tag *t, *tt;
PAIR_CLASS_INIT(&tag, "foot");
t = print_otag(h, TAG_TABLE, 1, &tag);
print_otag(h, TAG_TBODY, 0, NULL);
tt = print_otag(h, TAG_TR, 0, NULL);
PAIR_CLASS_INIT(&tag, "foot-date");
print_otag(h, TAG_TD, 1, &tag);
print_text(h, meta->date);
print_stagq(h, tt);
PAIR_CLASS_INIT(&tag, "foot-os");
print_otag(h, TAG_TD, 1, &tag);
print_text(h, meta->os);
print_tagq(h, t);
}
static int
mdoc_root_pre(MDOC_ARGS)
{
struct htmlpair tag;
struct tag *t, *tt;
char *volume, *title;
if (NULL == meta->arch)
volume = mandoc_strdup(meta->vol);
else
mandoc_asprintf(&volume, "%s (%s)",
meta->vol, meta->arch);
if (NULL == meta->msec)
title = mandoc_strdup(meta->title);
else
mandoc_asprintf(&title, "%s(%s)",
meta->title, meta->msec);
PAIR_CLASS_INIT(&tag, "head");
t = print_otag(h, TAG_TABLE, 1, &tag);
print_otag(h, TAG_TBODY, 0, NULL);
tt = print_otag(h, TAG_TR, 0, NULL);
PAIR_CLASS_INIT(&tag, "head-ltitle");
print_otag(h, TAG_TD, 1, &tag);
print_text(h, title);
print_stagq(h, tt);
PAIR_CLASS_INIT(&tag, "head-vol");
print_otag(h, TAG_TD, 1, &tag);
print_text(h, volume);
print_stagq(h, tt);
PAIR_CLASS_INIT(&tag, "head-rtitle");
print_otag(h, TAG_TD, 1, &tag);
print_text(h, title);
print_tagq(h, t);
free(title);
free(volume);
return 1;
}
static int
mdoc_sh_pre(MDOC_ARGS)
{
struct htmlpair tag;
switch (n->type) {
case ROFFT_BLOCK:
PAIR_CLASS_INIT(&tag, "section");
print_otag(h, TAG_DIV, 1, &tag);
return 1;
case ROFFT_BODY:
if (n->sec == SEC_AUTHORS)
h->flags &= ~(HTML_SPLIT|HTML_NOSPLIT);
return 1;
default:
break;
}
bufinit(h);
for (n = n->child; n != NULL && n->type == ROFFT_TEXT; ) {
bufcat_id(h, n->string);
if (NULL != (n = n->next))
bufcat_id(h, " ");
}
if (NULL == n) {
PAIR_ID_INIT(&tag, h->buf);
print_otag(h, TAG_H1, 1, &tag);
} else
print_otag(h, TAG_H1, 0, NULL);
return 1;
}
static int
mdoc_ss_pre(MDOC_ARGS)
{
struct htmlpair tag;
if (n->type == ROFFT_BLOCK) {
PAIR_CLASS_INIT(&tag, "subsection");
print_otag(h, TAG_DIV, 1, &tag);
return 1;
} else if (n->type == ROFFT_BODY)
return 1;
bufinit(h);
for (n = n->child; n != NULL && n->type == ROFFT_TEXT; ) {
bufcat_id(h, n->string);
if (NULL != (n = n->next))
bufcat_id(h, " ");
}
if (NULL == n) {
PAIR_ID_INIT(&tag, h->buf);
print_otag(h, TAG_H2, 1, &tag);
} else
print_otag(h, TAG_H2, 0, NULL);
return 1;
}
static int
mdoc_fl_pre(MDOC_ARGS)
{
struct htmlpair tag;
PAIR_CLASS_INIT(&tag, "flag");
print_otag(h, TAG_B, 1, &tag);
/* `Cm' has no leading hyphen. */
if (MDOC_Cm == n->tok)
return 1;
print_text(h, "\\-");
if (!(n->child == NULL &&
(n->next == NULL ||
n->next->type == ROFFT_TEXT ||
n->next->flags & MDOC_LINE)))
h->flags |= HTML_NOSPACE;
return 1;
}
static int
mdoc_nd_pre(MDOC_ARGS)
{
struct htmlpair tag;
if (n->type != ROFFT_BODY)
return 1;
/* XXX: this tag in theory can contain block elements. */
print_text(h, "\\(em");
PAIR_CLASS_INIT(&tag, "desc");
print_otag(h, TAG_SPAN, 1, &tag);
return 1;
}
static int
mdoc_nm_pre(MDOC_ARGS)
{
struct htmlpair tag;
struct roffsu su;
int len;
switch (n->type) {
case ROFFT_HEAD:
print_otag(h, TAG_TD, 0, NULL);
/* FALLTHROUGH */
case ROFFT_ELEM:
PAIR_CLASS_INIT(&tag, "name");
print_otag(h, TAG_B, 1, &tag);
if (n->child == NULL && meta->name != NULL)
print_text(h, meta->name);
return 1;
case ROFFT_BODY:
print_otag(h, TAG_TD, 0, NULL);
return 1;
default:
break;
}
synopsis_pre(h, n);
PAIR_CLASS_INIT(&tag, "synopsis");
print_otag(h, TAG_TABLE, 1, &tag);
for (len = 0, n = n->head->child; n; n = n->next)
if (n->type == ROFFT_TEXT)
len += html_strlen(n->string);
if (len == 0 && meta->name != NULL)
len = html_strlen(meta->name);
SCALE_HS_INIT(&su, len);
bufinit(h);
bufcat_su(h, "width", &su);
PAIR_STYLE_INIT(&tag, h);
print_otag(h, TAG_COL, 1, &tag);
print_otag(h, TAG_COL, 0, NULL);
print_otag(h, TAG_TBODY, 0, NULL);
print_otag(h, TAG_TR, 0, NULL);
return 1;
}
static int
mdoc_xr_pre(MDOC_ARGS)
{
struct htmlpair tag[2];
if (NULL == n->child)
return 0;
PAIR_CLASS_INIT(&tag[0], "link-man");
if (h->base_man) {
buffmt_man(h, n->child->string,
n->child->next ?
n->child->next->string : NULL);
PAIR_HREF_INIT(&tag[1], h->buf);
print_otag(h, TAG_A, 2, tag);
} else
print_otag(h, TAG_A, 1, tag);
n = n->child;
print_text(h, n->string);
if (NULL == (n = n->next))
return 0;
h->flags |= HTML_NOSPACE;
print_text(h, "(");
h->flags |= HTML_NOSPACE;
print_text(h, n->string);
h->flags |= HTML_NOSPACE;
print_text(h, ")");
return 0;
}
static int
mdoc_ns_pre(MDOC_ARGS)
{
if ( ! (MDOC_LINE & n->flags))
h->flags |= HTML_NOSPACE;
return 1;
}
static int
mdoc_ar_pre(MDOC_ARGS)
{
struct htmlpair tag;
PAIR_CLASS_INIT(&tag, "arg");
print_otag(h, TAG_I, 1, &tag);
return 1;
}
static int
mdoc_xx_pre(MDOC_ARGS)
{
const char *pp;
struct htmlpair tag;
int flags;
switch (n->tok) {
case MDOC_Bsx:
pp = "BSD/OS";
break;
case MDOC_Dx:
pp = "DragonFly";
break;
case MDOC_Fx:
pp = "FreeBSD";
break;
case MDOC_Nx:
pp = "NetBSD";
break;
case MDOC_Ox:
pp = "OpenBSD";
break;
case MDOC_Ux:
pp = "UNIX";
break;
default:
return 1;
}
PAIR_CLASS_INIT(&tag, "unix");
print_otag(h, TAG_SPAN, 1, &tag);
print_text(h, pp);
if (n->child) {
flags = h->flags;
h->flags |= HTML_KEEP;
print_text(h, n->child->string);
h->flags = flags;
}
return 0;
}
static int
mdoc_bx_pre(MDOC_ARGS)
{
struct htmlpair tag;
PAIR_CLASS_INIT(&tag, "unix");
print_otag(h, TAG_SPAN, 1, &tag);
if (NULL != (n = n->child)) {
print_text(h, n->string);
h->flags |= HTML_NOSPACE;
print_text(h, "BSD");
} else {
print_text(h, "BSD");
return 0;
}
if (NULL != (n = n->next)) {
h->flags |= HTML_NOSPACE;
print_text(h, "-");
h->flags |= HTML_NOSPACE;
print_text(h, n->string);
}
return 0;
}
static int
mdoc_it_pre(MDOC_ARGS)
{
struct roffsu su;
enum mdoc_list type;
struct htmlpair tag[2];
const struct roff_node *bl;
bl = n->parent;
while (bl && MDOC_Bl != bl->tok)
bl = bl->parent;
assert(bl);
type = bl->norm->Bl.type;
assert(lists[type]);
PAIR_CLASS_INIT(&tag[0], lists[type]);
bufinit(h);
if (n->type == ROFFT_HEAD) {
switch (type) {
case LIST_bullet:
case LIST_dash:
case LIST_item:
case LIST_hyphen:
case LIST_enum:
return 0;
case LIST_diag:
case LIST_hang:
case LIST_inset:
case LIST_ohang:
case LIST_tag:
SCALE_VS_INIT(&su, ! bl->norm->Bl.comp);
bufcat_su(h, "margin-top", &su);
PAIR_STYLE_INIT(&tag[1], h);
print_otag(h, TAG_DT, 2, tag);
if (LIST_diag != type)
break;
PAIR_CLASS_INIT(&tag[0], "diag");
print_otag(h, TAG_B, 1, tag);
break;
case LIST_column:
break;
default:
break;
}
} else if (n->type == ROFFT_BODY) {
switch (type) {
case LIST_bullet:
case LIST_hyphen:
case LIST_dash:
case LIST_enum:
case LIST_item:
SCALE_VS_INIT(&su, ! bl->norm->Bl.comp);
bufcat_su(h, "margin-top", &su);
PAIR_STYLE_INIT(&tag[1], h);
print_otag(h, TAG_LI, 2, tag);
break;
case LIST_diag:
case LIST_hang:
case LIST_inset:
case LIST_ohang:
case LIST_tag:
if (NULL == bl->norm->Bl.width) {
print_otag(h, TAG_DD, 1, tag);
break;
}
a2width(bl->norm->Bl.width, &su);
bufcat_su(h, "margin-left", &su);
PAIR_STYLE_INIT(&tag[1], h);
print_otag(h, TAG_DD, 2, tag);
break;
case LIST_column:
SCALE_VS_INIT(&su, ! bl->norm->Bl.comp);
bufcat_su(h, "margin-top", &su);
PAIR_STYLE_INIT(&tag[1], h);
print_otag(h, TAG_TD, 2, tag);
break;
default:
break;
}
} else {
switch (type) {
case LIST_column:
print_otag(h, TAG_TR, 1, tag);
break;
default:
break;
}
}
return 1;
}
static int
mdoc_bl_pre(MDOC_ARGS)
{
int i;
struct htmlpair tag[3];
struct roffsu su;
char buf[BUFSIZ];
if (n->type == ROFFT_BODY) {
if (LIST_column == n->norm->Bl.type)
print_otag(h, TAG_TBODY, 0, NULL);
return 1;
}
if (n->type == ROFFT_HEAD) {
if (LIST_column != n->norm->Bl.type)
return 0;
/*
* For each column, print out the <COL> tag with our
* suggested width. The last column gets min-width, as
* in terminal mode it auto-sizes to the width of the
* screen and we want to preserve that behaviour.
*/
for (i = 0; i < (int)n->norm->Bl.ncols; i++) {
bufinit(h);
a2width(n->norm->Bl.cols[i], &su);
if (i < (int)n->norm->Bl.ncols - 1)
bufcat_su(h, "width", &su);
else
bufcat_su(h, "min-width", &su);
PAIR_STYLE_INIT(&tag[0], h);
print_otag(h, TAG_COL, 1, tag);
}
return 0;
}
SCALE_VS_INIT(&su, 0);
bufinit(h);
bufcat_su(h, "margin-top", &su);
bufcat_su(h, "margin-bottom", &su);
PAIR_STYLE_INIT(&tag[0], h);
assert(lists[n->norm->Bl.type]);
(void)strlcpy(buf, "list ", BUFSIZ);
(void)strlcat(buf, lists[n->norm->Bl.type], BUFSIZ);
PAIR_INIT(&tag[1], ATTR_CLASS, buf);
/* Set the block's left-hand margin. */
if (n->norm->Bl.offs) {
a2width(n->norm->Bl.offs, &su);
bufcat_su(h, "margin-left", &su);
}
switch (n->norm->Bl.type) {
case LIST_bullet:
case LIST_dash:
case LIST_hyphen:
case LIST_item:
print_otag(h, TAG_UL, 2, tag);
break;
case LIST_enum:
print_otag(h, TAG_OL, 2, tag);
break;
case LIST_diag:
case LIST_hang:
case LIST_inset:
case LIST_ohang:
case LIST_tag:
print_otag(h, TAG_DL, 2, tag);
break;
case LIST_column:
print_otag(h, TAG_TABLE, 2, tag);
break;
default:
abort();
}
return 1;
}
static int
mdoc_ex_pre(MDOC_ARGS)
{
struct htmlpair tag;
struct tag *t;
struct roff_node *nch;
if (n->prev)
print_otag(h, TAG_BR, 0, NULL);
PAIR_CLASS_INIT(&tag, "utility");
print_text(h, "The");
for (nch = n->child; nch != NULL; nch = nch->next) {
assert(nch->type == ROFFT_TEXT);
t = print_otag(h, TAG_B, 1, &tag);
print_text(h, nch->string);
print_tagq(h, t);
if (nch->next == NULL)