1 /* $Id: mdoc_html.c,v 1.142 2011/01/07 13:20:58 kristaps Exp $ */
3 * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/types.h>
39 #define MDOC_ARGS const struct mdoc_meta *m, \
40 const struct mdoc_node *n, \
44 #define MIN(a,b) ((/*CONSTCOND*/(a)<(b))?(a):(b))
48 int (*pre)(MDOC_ARGS);
49 void (*post)(MDOC_ARGS);
52 static void print_mdoc(MDOC_ARGS);
53 static void print_mdoc_head(MDOC_ARGS);
54 static void print_mdoc_node(MDOC_ARGS);
55 static void print_mdoc_nodelist(MDOC_ARGS);
56 static void synopsis_pre(struct html *,
57 const struct mdoc_node *);
59 static void a2width(const char *, struct roffsu *);
60 static void a2offs(const char *, struct roffsu *);
62 static void mdoc_root_post(MDOC_ARGS);
63 static int mdoc_root_pre(MDOC_ARGS);
65 static void mdoc__x_post(MDOC_ARGS);
66 static int mdoc__x_pre(MDOC_ARGS);
67 static int mdoc_ad_pre(MDOC_ARGS);
68 static int mdoc_an_pre(MDOC_ARGS);
69 static int mdoc_ap_pre(MDOC_ARGS);
70 static int mdoc_ar_pre(MDOC_ARGS);
71 static int mdoc_bd_pre(MDOC_ARGS);
72 static int mdoc_bf_pre(MDOC_ARGS);
73 static void mdoc_bk_post(MDOC_ARGS);
74 static int mdoc_bk_pre(MDOC_ARGS);
75 static int mdoc_bl_pre(MDOC_ARGS);
76 static int mdoc_bt_pre(MDOC_ARGS);
77 static int mdoc_bx_pre(MDOC_ARGS);
78 static int mdoc_cd_pre(MDOC_ARGS);
79 static int mdoc_d1_pre(MDOC_ARGS);
80 static int mdoc_dv_pre(MDOC_ARGS);
81 static int mdoc_fa_pre(MDOC_ARGS);
82 static int mdoc_fd_pre(MDOC_ARGS);
83 static int mdoc_fl_pre(MDOC_ARGS);
84 static int mdoc_fn_pre(MDOC_ARGS);
85 static int mdoc_ft_pre(MDOC_ARGS);
86 static int mdoc_em_pre(MDOC_ARGS);
87 static int mdoc_er_pre(MDOC_ARGS);
88 static int mdoc_ev_pre(MDOC_ARGS);
89 static int mdoc_ex_pre(MDOC_ARGS);
90 static void mdoc_fo_post(MDOC_ARGS);
91 static int mdoc_fo_pre(MDOC_ARGS);
92 static int mdoc_ic_pre(MDOC_ARGS);
93 static int mdoc_igndelim_pre(MDOC_ARGS);
94 static int mdoc_in_pre(MDOC_ARGS);
95 static int mdoc_it_pre(MDOC_ARGS);
96 static int mdoc_lb_pre(MDOC_ARGS);
97 static int mdoc_li_pre(MDOC_ARGS);
98 static int mdoc_lk_pre(MDOC_ARGS);
99 static int mdoc_mt_pre(MDOC_ARGS);
100 static int mdoc_ms_pre(MDOC_ARGS);
101 static int mdoc_nd_pre(MDOC_ARGS);
102 static int mdoc_nm_pre(MDOC_ARGS);
103 static int mdoc_ns_pre(MDOC_ARGS);
104 static int mdoc_pa_pre(MDOC_ARGS);
105 static void mdoc_pf_post(MDOC_ARGS);
106 static int mdoc_pp_pre(MDOC_ARGS);
107 static void mdoc_quote_post(MDOC_ARGS);
108 static int mdoc_quote_pre(MDOC_ARGS);
109 static int mdoc_rs_pre(MDOC_ARGS);
110 static int mdoc_rv_pre(MDOC_ARGS);
111 static int mdoc_sh_pre(MDOC_ARGS);
112 static int mdoc_sm_pre(MDOC_ARGS);
113 static int mdoc_sp_pre(MDOC_ARGS);
114 static int mdoc_ss_pre(MDOC_ARGS);
115 static int mdoc_sx_pre(MDOC_ARGS);
116 static int mdoc_sy_pre(MDOC_ARGS);
117 static int mdoc_ud_pre(MDOC_ARGS);
118 static int mdoc_va_pre(MDOC_ARGS);
119 static int mdoc_vt_pre(MDOC_ARGS);
120 static int mdoc_xr_pre(MDOC_ARGS);
121 static int mdoc_xx_pre(MDOC_ARGS);
123 static const struct htmlmdoc mdocs[MDOC_MAX] = {
124 {mdoc_ap_pre, NULL}, /* Ap */
125 {NULL, NULL}, /* Dd */
126 {NULL, NULL}, /* Dt */
127 {NULL, NULL}, /* Os */
128 {mdoc_sh_pre, NULL }, /* Sh */
129 {mdoc_ss_pre, NULL }, /* Ss */
130 {mdoc_pp_pre, NULL}, /* Pp */
131 {mdoc_d1_pre, NULL}, /* D1 */
132 {mdoc_d1_pre, NULL}, /* Dl */
133 {mdoc_bd_pre, NULL}, /* Bd */
134 {NULL, NULL}, /* Ed */
135 {mdoc_bl_pre, NULL}, /* Bl */
136 {NULL, NULL}, /* El */
137 {mdoc_it_pre, NULL}, /* It */
138 {mdoc_ad_pre, NULL}, /* Ad */
139 {mdoc_an_pre, NULL}, /* An */
140 {mdoc_ar_pre, NULL}, /* Ar */
141 {mdoc_cd_pre, NULL}, /* Cd */
142 {mdoc_fl_pre, NULL}, /* Cm */
143 {mdoc_dv_pre, NULL}, /* Dv */
144 {mdoc_er_pre, NULL}, /* Er */
145 {mdoc_ev_pre, NULL}, /* Ev */
146 {mdoc_ex_pre, NULL}, /* Ex */
147 {mdoc_fa_pre, NULL}, /* Fa */
148 {mdoc_fd_pre, NULL}, /* Fd */
149 {mdoc_fl_pre, NULL}, /* Fl */
150 {mdoc_fn_pre, NULL}, /* Fn */
151 {mdoc_ft_pre, NULL}, /* Ft */
152 {mdoc_ic_pre, NULL}, /* Ic */
153 {mdoc_in_pre, NULL}, /* In */
154 {mdoc_li_pre, NULL}, /* Li */
155 {mdoc_nd_pre, NULL}, /* Nd */
156 {mdoc_nm_pre, NULL}, /* Nm */
157 {mdoc_quote_pre, mdoc_quote_post}, /* Op */
158 {NULL, NULL}, /* Ot */
159 {mdoc_pa_pre, NULL}, /* Pa */
160 {mdoc_rv_pre, NULL}, /* Rv */
161 {NULL, NULL}, /* St */
162 {mdoc_va_pre, NULL}, /* Va */
163 {mdoc_vt_pre, NULL}, /* Vt */
164 {mdoc_xr_pre, NULL}, /* Xr */
165 {mdoc__x_pre, mdoc__x_post}, /* %A */
166 {mdoc__x_pre, mdoc__x_post}, /* %B */
167 {mdoc__x_pre, mdoc__x_post}, /* %D */
168 {mdoc__x_pre, mdoc__x_post}, /* %I */
169 {mdoc__x_pre, mdoc__x_post}, /* %J */
170 {mdoc__x_pre, mdoc__x_post}, /* %N */
171 {mdoc__x_pre, mdoc__x_post}, /* %O */
172 {mdoc__x_pre, mdoc__x_post}, /* %P */
173 {mdoc__x_pre, mdoc__x_post}, /* %R */
174 {mdoc__x_pre, mdoc__x_post}, /* %T */
175 {mdoc__x_pre, mdoc__x_post}, /* %V */
176 {NULL, NULL}, /* Ac */
177 {mdoc_quote_pre, mdoc_quote_post}, /* Ao */
178 {mdoc_quote_pre, mdoc_quote_post}, /* Aq */
179 {NULL, NULL}, /* At */
180 {NULL, NULL}, /* Bc */
181 {mdoc_bf_pre, NULL}, /* Bf */
182 {mdoc_quote_pre, mdoc_quote_post}, /* Bo */
183 {mdoc_quote_pre, mdoc_quote_post}, /* Bq */
184 {mdoc_xx_pre, NULL}, /* Bsx */
185 {mdoc_bx_pre, NULL}, /* Bx */
186 {NULL, NULL}, /* Db */
187 {NULL, NULL}, /* Dc */
188 {mdoc_quote_pre, mdoc_quote_post}, /* Do */
189 {mdoc_quote_pre, mdoc_quote_post}, /* Dq */
190 {NULL, NULL}, /* Ec */ /* FIXME: no space */
191 {NULL, NULL}, /* Ef */
192 {mdoc_em_pre, NULL}, /* Em */
193 {NULL, NULL}, /* Eo */
194 {mdoc_xx_pre, NULL}, /* Fx */
195 {mdoc_ms_pre, NULL}, /* Ms */
196 {mdoc_igndelim_pre, NULL}, /* No */
197 {mdoc_ns_pre, NULL}, /* Ns */
198 {mdoc_xx_pre, NULL}, /* Nx */
199 {mdoc_xx_pre, NULL}, /* Ox */
200 {NULL, NULL}, /* Pc */
201 {mdoc_igndelim_pre, mdoc_pf_post}, /* Pf */
202 {mdoc_quote_pre, mdoc_quote_post}, /* Po */
203 {mdoc_quote_pre, mdoc_quote_post}, /* Pq */
204 {NULL, NULL}, /* Qc */
205 {mdoc_quote_pre, mdoc_quote_post}, /* Ql */
206 {mdoc_quote_pre, mdoc_quote_post}, /* Qo */
207 {mdoc_quote_pre, mdoc_quote_post}, /* Qq */
208 {NULL, NULL}, /* Re */
209 {mdoc_rs_pre, NULL}, /* Rs */
210 {NULL, NULL}, /* Sc */
211 {mdoc_quote_pre, mdoc_quote_post}, /* So */
212 {mdoc_quote_pre, mdoc_quote_post}, /* Sq */
213 {mdoc_sm_pre, NULL}, /* Sm */
214 {mdoc_sx_pre, NULL}, /* Sx */
215 {mdoc_sy_pre, NULL}, /* Sy */
216 {NULL, NULL}, /* Tn */
217 {mdoc_xx_pre, NULL}, /* Ux */
218 {NULL, NULL}, /* Xc */
219 {NULL, NULL}, /* Xo */
220 {mdoc_fo_pre, mdoc_fo_post}, /* Fo */
221 {NULL, NULL}, /* Fc */
222 {mdoc_quote_pre, mdoc_quote_post}, /* Oo */
223 {NULL, NULL}, /* Oc */
224 {mdoc_bk_pre, mdoc_bk_post}, /* Bk */
225 {NULL, NULL}, /* Ek */
226 {mdoc_bt_pre, NULL}, /* Bt */
227 {NULL, NULL}, /* Hf */
228 {NULL, NULL}, /* Fr */
229 {mdoc_ud_pre, NULL}, /* Ud */
230 {mdoc_lb_pre, NULL}, /* Lb */
231 {mdoc_pp_pre, NULL}, /* Lp */
232 {mdoc_lk_pre, NULL}, /* Lk */
233 {mdoc_mt_pre, NULL}, /* Mt */
234 {mdoc_quote_pre, mdoc_quote_post}, /* Brq */
235 {mdoc_quote_pre, mdoc_quote_post}, /* Bro */
236 {NULL, NULL}, /* Brc */
237 {mdoc__x_pre, mdoc__x_post}, /* %C */
238 {NULL, NULL}, /* Es */ /* TODO */
239 {NULL, NULL}, /* En */ /* TODO */
240 {mdoc_xx_pre, NULL}, /* Dx */
241 {mdoc__x_pre, mdoc__x_post}, /* %Q */
242 {mdoc_sp_pre, NULL}, /* br */
243 {mdoc_sp_pre, NULL}, /* sp */
244 {mdoc__x_pre, mdoc__x_post}, /* %U */
245 {NULL, NULL}, /* Ta */
248 static const char * const lists[LIST_MAX] = {
264 html_mdoc(void *arg, const struct mdoc *m)
269 h = (struct html *)arg;
272 t = print_otag(h, TAG_HTML, 0, NULL);
273 print_mdoc(mdoc_meta(m), mdoc_node(m), h);
281 * Calculate the scaling unit passed in a `-width' argument. This uses
282 * either a native scaling unit (e.g., 1i, 2m) or the string length of
286 a2width(const char *p, struct roffsu *su)
289 if ( ! a2roffsu(p, su, SCALE_MAX)) {
291 su->scale = (int)strlen(p);
297 * See the same function in mdoc_term.c for documentation.
300 synopsis_pre(struct html *h, const struct mdoc_node *n)
303 if (NULL == n->prev || ! (MDOC_SYNPRETTY & n->flags))
306 if (n->prev->tok == n->tok &&
310 print_otag(h, TAG_BR, 0, NULL);
314 switch (n->prev->tok) {
324 print_otag(h, TAG_P, 0, NULL);
327 if (MDOC_Fn != n->tok && MDOC_Fo != n->tok) {
328 print_otag(h, TAG_P, 0, NULL);
333 print_otag(h, TAG_BR, 0, NULL);
340 * Calculate the scaling unit passed in an `-offset' argument. This
341 * uses either a native scaling unit (e.g., 1i, 2m), one of a set of
342 * predefined strings (indent, etc.), or the string length of the value.
345 a2offs(const char *p, struct roffsu *su)
348 /* FIXME: "right"? */
350 if (0 == strcmp(p, "left"))
351 SCALE_HS_INIT(su, 0);
352 else if (0 == strcmp(p, "indent"))
353 SCALE_HS_INIT(su, INDENT);
354 else if (0 == strcmp(p, "indent-two"))
355 SCALE_HS_INIT(su, INDENT * 2);
356 else if ( ! a2roffsu(p, su, SCALE_MAX)) {
358 su->scale = (int)strlen(p);
364 print_mdoc(MDOC_ARGS)
368 t = print_otag(h, TAG_HEAD, 0, NULL);
369 print_mdoc_head(m, n, h);
372 t = print_otag(h, TAG_BODY, 0, NULL);
373 print_mdoc_nodelist(m, n, h);
380 print_mdoc_head(MDOC_ARGS)
385 buffmt(h, "%s(%s)", m->title, m->msec);
393 print_otag(h, TAG_TITLE, 0, NULL);
394 print_text(h, h->buf);
399 print_mdoc_nodelist(MDOC_ARGS)
402 print_mdoc_node(m, n, h);
404 print_mdoc_nodelist(m, n->next, h);
409 print_mdoc_node(MDOC_ARGS)
420 child = mdoc_root_pre(m, n, h);
423 print_text(h, n->string);
426 print_tbl(h, n->span);
429 if (mdocs[n->tok].pre && ENDBODY_NOT == n->end)
430 child = (*mdocs[n->tok].pre)(m, n, h);
434 if (HTML_KEEP & h->flags) {
435 if (n->prev && n->prev->line != n->line) {
436 h->flags &= ~HTML_KEEP;
437 h->flags |= HTML_PREKEEP;
438 } else if (NULL == n->prev) {
439 if (n->parent && n->parent->line != n->line) {
440 h->flags &= ~HTML_KEEP;
441 h->flags |= HTML_PREKEEP;
446 if (child && n->child)
447 print_mdoc_nodelist(m, n->child, h);
454 mdoc_root_post(m, n, h);
459 if (mdocs[n->tok].post && ENDBODY_NOT == n->end)
460 (*mdocs[n->tok].post)(m, n, h);
467 mdoc_root_post(MDOC_ARGS)
469 struct htmlpair tag[3];
473 time2a(m->date, b, DATESIZ);
475 PAIR_SUMMARY_INIT(&tag[0], "Document Footer");
476 PAIR_CLASS_INIT(&tag[1], "foot");
477 if (NULL == h->style) {
478 PAIR_INIT(&tag[2], ATTR_WIDTH, "100%");
479 t = print_otag(h, TAG_TABLE, 3, tag);
480 PAIR_INIT(&tag[0], ATTR_WIDTH, "50%");
481 print_otag(h, TAG_COL, 1, tag);
482 print_otag(h, TAG_COL, 1, tag);
484 t = print_otag(h, TAG_TABLE, 2, tag);
486 t = print_otag(h, TAG_TBODY, 0, NULL);
488 tt = print_otag(h, TAG_TR, 0, NULL);
490 PAIR_CLASS_INIT(&tag[0], "foot-date");
491 print_otag(h, TAG_TD, 1, tag);
496 PAIR_CLASS_INIT(&tag[0], "foot-os");
497 if (NULL == h->style) {
498 PAIR_INIT(&tag[1], ATTR_ALIGN, "right");
499 print_otag(h, TAG_TD, 2, tag);
501 print_otag(h, TAG_TD, 1, tag);
503 print_text(h, m->os);
510 mdoc_root_pre(MDOC_ARGS)
512 struct htmlpair tag[3];
514 char b[BUFSIZ], title[BUFSIZ];
516 strlcpy(b, m->vol, BUFSIZ);
519 strlcat(b, " (", BUFSIZ);
520 strlcat(b, m->arch, BUFSIZ);
521 strlcat(b, ")", BUFSIZ);
524 snprintf(title, BUFSIZ - 1, "%s(%s)", m->title, m->msec);
526 PAIR_SUMMARY_INIT(&tag[0], "Document Header");
527 PAIR_CLASS_INIT(&tag[1], "head");
528 if (NULL == h->style) {
529 PAIR_INIT(&tag[2], ATTR_WIDTH, "100%");
530 t = print_otag(h, TAG_TABLE, 3, tag);
531 PAIR_INIT(&tag[0], ATTR_WIDTH, "30%");
532 print_otag(h, TAG_COL, 1, tag);
533 print_otag(h, TAG_COL, 1, tag);
534 print_otag(h, TAG_COL, 1, tag);
536 t = print_otag(h, TAG_TABLE, 2, tag);
538 print_otag(h, TAG_TBODY, 0, NULL);
540 tt = print_otag(h, TAG_TR, 0, NULL);
542 PAIR_CLASS_INIT(&tag[0], "head-ltitle");
543 print_otag(h, TAG_TD, 1, tag);
545 print_text(h, title);
548 PAIR_CLASS_INIT(&tag[0], "head-vol");
549 if (NULL == h->style) {
550 PAIR_INIT(&tag[1], ATTR_ALIGN, "center");
551 print_otag(h, TAG_TD, 2, tag);
553 print_otag(h, TAG_TD, 1, tag);
558 PAIR_CLASS_INIT(&tag[0], "head-rtitle");
559 if (NULL == h->style) {
560 PAIR_INIT(&tag[1], ATTR_ALIGN, "right");
561 print_otag(h, TAG_TD, 2, tag);
563 print_otag(h, TAG_TD, 1, tag);
565 print_text(h, title);
573 mdoc_sh_pre(MDOC_ARGS)
578 if (MDOC_BLOCK == n->type) {
579 PAIR_CLASS_INIT(&tag, "section");
580 print_otag(h, TAG_DIV, 1, &tag);
582 } else if (MDOC_BODY == n->type)
586 for (n = n->child; n; n = n->next) {
587 html_idcat(buf, n->string, BUFSIZ);
589 html_idcat(buf, " ", BUFSIZ);
592 PAIR_ID_INIT(&tag, buf);
593 print_otag(h, TAG_H1, 1, &tag);
600 mdoc_ss_pre(MDOC_ARGS)
605 if (MDOC_BLOCK == n->type) {
606 PAIR_CLASS_INIT(&tag, "subsection");
607 print_otag(h, TAG_DIV, 1, &tag);
609 } else if (MDOC_BODY == n->type)
613 for (n = n->child; n; n = n->next) {
614 html_idcat(buf, n->string, BUFSIZ);
616 html_idcat(buf, " ", BUFSIZ);
619 PAIR_ID_INIT(&tag, buf);
620 print_otag(h, TAG_H2, 1, &tag);
627 mdoc_fl_pre(MDOC_ARGS)
631 PAIR_CLASS_INIT(&tag, "flag");
632 print_otag(h, TAG_B, 1, &tag);
634 /* `Cm' has no leading hyphen. */
636 if (MDOC_Cm == n->tok)
639 print_text(h, "\\-");
642 h->flags |= HTML_NOSPACE;
643 else if (n->next && n->next->line == n->line)
644 h->flags |= HTML_NOSPACE;
652 mdoc_nd_pre(MDOC_ARGS)
656 if (MDOC_BODY != n->type)
659 /* XXX: this tag in theory can contain block elements. */
661 print_text(h, "\\(em");
662 PAIR_CLASS_INIT(&tag, "desc");
663 print_otag(h, TAG_SPAN, 1, &tag);
669 mdoc_nm_pre(MDOC_ARGS)
678 PAIR_CLASS_INIT(&tag, "name");
679 print_otag(h, TAG_B, 1, &tag);
680 if (NULL == n->child && m->name)
681 print_text(h, m->name);
684 print_otag(h, TAG_TD, 0, NULL);
685 if (NULL == n->child && m->name)
686 print_text(h, m->name);
689 print_otag(h, TAG_TD, 0, NULL);
696 PAIR_CLASS_INIT(&tag, "synopsis");
697 print_otag(h, TAG_TABLE, 1, &tag);
699 for (len = 0, n = n->child; n; n = n->next)
700 if (MDOC_TEXT == n->type)
701 len += strlen(n->string);
703 if (0 == len && m->name)
704 len = strlen(m->name);
706 SCALE_HS_INIT(&su, (double)len);
707 bufcat_su(h, "width", &su);
708 PAIR_STYLE_INIT(&tag, h);
709 print_otag(h, TAG_COL, 1, &tag);
710 print_otag(h, TAG_COL, 0, NULL);
711 print_otag(h, TAG_TBODY, 0, NULL);
712 print_otag(h, TAG_TR, 0, NULL);
719 mdoc_xr_pre(MDOC_ARGS)
721 struct htmlpair tag[2];
722 const struct mdoc_node *nn;
724 if (NULL == n->child)
727 PAIR_CLASS_INIT(&tag[0], "link-man");
730 buffmt_man(h, n->child->string,
732 n->child->next->string : NULL);
733 PAIR_HREF_INIT(&tag[1], h->buf);
734 print_otag(h, TAG_A, 2, tag);
736 print_otag(h, TAG_A, 1, tag);
739 print_text(h, nn->string);
741 if (NULL == (nn = nn->next))
744 h->flags |= HTML_NOSPACE;
746 h->flags |= HTML_NOSPACE;
747 print_text(h, nn->string);
748 h->flags |= HTML_NOSPACE;
756 mdoc_ns_pre(MDOC_ARGS)
759 h->flags |= HTML_NOSPACE;
766 mdoc_ar_pre(MDOC_ARGS)
770 PAIR_CLASS_INIT(&tag, "arg");
771 print_otag(h, TAG_I, 1, &tag);
778 mdoc_xx_pre(MDOC_ARGS)
806 PAIR_CLASS_INIT(&tag, "unix");
807 print_otag(h, TAG_SPAN, 1, &tag);
815 mdoc_bx_pre(MDOC_ARGS)
817 const struct mdoc_node *nn;
820 PAIR_CLASS_INIT(&tag, "unix");
821 print_otag(h, TAG_SPAN, 1, &tag);
823 for (nn = n->child; nn; nn = nn->next)
824 print_mdoc_node(m, nn, h);
827 h->flags |= HTML_NOSPACE;
829 print_text(h, "BSD");
835 mdoc_it_pre(MDOC_ARGS)
839 struct htmlpair tag[2];
840 const struct mdoc_node *bl;
843 while (bl && MDOC_Bl != bl->tok)
848 type = bl->norm->Bl.type;
851 PAIR_CLASS_INIT(&tag[0], lists[type]);
853 if (MDOC_HEAD == n->type) {
874 SCALE_VS_INIT(&su, ! bl->norm->Bl.comp);
875 bufcat_su(h, "margin-top", &su);
876 PAIR_STYLE_INIT(&tag[1], h);
877 print_otag(h, TAG_DT, 2, tag);
878 if (LIST_diag != type)
880 PAIR_CLASS_INIT(&tag[0], "diag");
881 print_otag(h, TAG_B, 1, tag);
888 } else if (MDOC_BODY == n->type) {
899 SCALE_VS_INIT(&su, ! bl->norm->Bl.comp);
900 bufcat_su(h, "margin-top", &su);
901 PAIR_STYLE_INIT(&tag[1], h);
902 print_otag(h, TAG_LI, 2, tag);
913 if (NULL == bl->norm->Bl.width) {
914 print_otag(h, TAG_DD, 1, tag);
917 a2width(bl->norm->Bl.width, &su);
918 bufcat_su(h, "margin-left", &su);
919 PAIR_STYLE_INIT(&tag[1], h);
920 print_otag(h, TAG_DD, 2, tag);
923 SCALE_VS_INIT(&su, ! bl->norm->Bl.comp);
924 bufcat_su(h, "margin-top", &su);
925 PAIR_STYLE_INIT(&tag[1], h);
926 print_otag(h, TAG_TD, 2, tag);
934 print_otag(h, TAG_TR, 1, tag);
946 mdoc_bl_pre(MDOC_ARGS)
949 struct htmlpair tag[3];
953 if (MDOC_BODY == n->type) {
954 if (LIST_column == n->norm->Bl.type)
955 print_otag(h, TAG_TBODY, 0, NULL);
959 if (MDOC_HEAD == n->type) {
960 if (LIST_column != n->norm->Bl.type)
964 * For each column, print out the <COL> tag with our
965 * suggested width. The last column gets min-width, as
966 * in terminal mode it auto-sizes to the width of the
967 * screen and we want to preserve that behaviour.
970 for (i = 0; i < (int)n->norm->Bl.ncols; i++) {
971 a2width(n->norm->Bl.cols[i], &su);
973 if (i < (int)n->norm->Bl.ncols - 1)
974 bufcat_su(h, "width", &su);
976 bufcat_su(h, "min-width", &su);
977 PAIR_STYLE_INIT(&tag[0], h);
978 print_otag(h, TAG_COL, 1, tag);
984 SCALE_VS_INIT(&su, 0);
985 bufcat_su(h, "margin-top", &su);
986 bufcat_su(h, "margin-bottom", &su);
987 PAIR_STYLE_INIT(&tag[0], h);
989 assert(lists[n->norm->Bl.type]);
990 strlcpy(buf, "list ", BUFSIZ);
991 strlcat(buf, lists[n->norm->Bl.type], BUFSIZ);
992 PAIR_INIT(&tag[1], ATTR_CLASS, buf);
994 /* Set the block's left-hand margin. */
996 if (n->norm->Bl.offs) {
997 a2offs(n->norm->Bl.offs, &su);
998 bufcat_su(h, "margin-left", &su);
1001 switch (n->norm->Bl.type) {
1009 print_otag(h, TAG_UL, 2, tag);
1012 print_otag(h, TAG_OL, 2, tag);
1023 print_otag(h, TAG_DL, 2, tag);
1026 print_otag(h, TAG_TABLE, 2, tag);
1038 mdoc_ex_pre(MDOC_ARGS)
1040 const struct mdoc_node *nn;
1042 struct htmlpair tag;
1045 print_otag(h, TAG_BR, 0, NULL);
1047 PAIR_CLASS_INIT(&tag, "utility");
1049 print_text(h, "The");
1050 for (nn = n->child; nn; nn = nn->next) {
1051 t = print_otag(h, TAG_B, 1, &tag);
1052 print_text(h, nn->string);
1055 h->flags |= HTML_NOSPACE;
1057 if (nn->next && NULL == nn->next->next)
1058 print_text(h, ", and");
1062 h->flags &= ~HTML_NOSPACE;
1065 if (n->child && n->child->next)
1066 print_text(h, "utilities exit");
1068 print_text(h, "utility exits");
1070 print_text(h, "0 on success, and >0 if an error occurs.");
1077 mdoc_em_pre(MDOC_ARGS)
1079 struct htmlpair tag;
1081 PAIR_CLASS_INIT(&tag, "emph");
1082 print_otag(h, TAG_SPAN, 1, &tag);
1089 mdoc_d1_pre(MDOC_ARGS)
1091 struct htmlpair tag[2];
1094 if (MDOC_BLOCK != n->type)
1097 SCALE_VS_INIT(&su, 0);
1098 bufcat_su(h, "margin-top", &su);
1099 bufcat_su(h, "margin-bottom", &su);
1100 PAIR_STYLE_INIT(&tag[0], h);
1101 print_otag(h, TAG_BLOCKQUOTE, 1, tag);
1103 /* BLOCKQUOTE needs a block body. */
1105 PAIR_CLASS_INIT(&tag[0], "display");
1106 print_otag(h, TAG_DIV, 1, tag);
1108 if (MDOC_Dl == n->tok) {
1109 PAIR_CLASS_INIT(&tag[0], "lit");
1110 print_otag(h, TAG_CODE, 1, tag);
1119 mdoc_sx_pre(MDOC_ARGS)
1121 struct htmlpair tag[2];
1122 const struct mdoc_node *nn;
1125 strlcpy(buf, "#", BUFSIZ);
1126 for (nn = n->child; nn; nn = nn->next) {
1127 html_idcat(buf, nn->string, BUFSIZ);
1129 html_idcat(buf, " ", BUFSIZ);
1132 PAIR_CLASS_INIT(&tag[0], "link-sec");
1133 PAIR_HREF_INIT(&tag[1], buf);
1135 print_otag(h, TAG_I, 1, tag);
1136 print_otag(h, TAG_A, 2, tag);
1143 mdoc_bd_pre(MDOC_ARGS)
1145 struct htmlpair tag[2];
1147 const struct mdoc_node *nn;
1150 if (MDOC_HEAD == n->type)
1153 if (MDOC_BLOCK == n->type) {
1154 comp = n->norm->Bd.comp;
1155 for (nn = n; nn && ! comp; nn = nn->parent) {
1156 if (MDOC_BLOCK != nn->type)
1158 if (MDOC_Ss == nn->tok || MDOC_Sh == nn->tok)
1164 print_otag(h, TAG_P, 0, NULL);
1168 SCALE_HS_INIT(&su, 0);
1169 if (n->norm->Bd.offs)
1170 a2offs(n->norm->Bd.offs, &su);
1172 bufcat_su(h, "margin-left", &su);
1173 PAIR_STYLE_INIT(&tag[0], h);
1175 if (DISP_unfilled != n->norm->Bd.type &&
1176 DISP_literal != n->norm->Bd.type) {
1177 PAIR_CLASS_INIT(&tag[1], "display");
1178 print_otag(h, TAG_DIV, 2, tag);
1182 PAIR_CLASS_INIT(&tag[1], "lit display");
1183 print_otag(h, TAG_PRE, 2, tag);
1185 for (nn = n->child; nn; nn = nn->next) {
1186 print_mdoc_node(m, nn, h);
1188 * If the printed node flushes its own line, then we
1189 * needn't do it here as well. This is hacky, but the
1190 * notion of selective eoln whitespace is pretty dumb
1191 * anyway, so don't sweat it.
1213 if (nn->next && nn->next->line == nn->line)
1216 print_text(h, "\n");
1218 h->flags |= HTML_NOSPACE;
1227 mdoc_pa_pre(MDOC_ARGS)
1229 struct htmlpair tag;
1231 PAIR_CLASS_INIT(&tag, "file");
1232 print_otag(h, TAG_I, 1, &tag);
1239 mdoc_ad_pre(MDOC_ARGS)
1241 struct htmlpair tag;
1243 PAIR_CLASS_INIT(&tag, "addr");
1244 print_otag(h, TAG_I, 1, &tag);
1251 mdoc_an_pre(MDOC_ARGS)
1253 struct htmlpair tag;
1255 /* TODO: -split and -nosplit (see termp_an_pre()). */
1257 PAIR_CLASS_INIT(&tag, "author");
1258 print_otag(h, TAG_SPAN, 1, &tag);
1265 mdoc_cd_pre(MDOC_ARGS)
1267 struct htmlpair tag;
1270 PAIR_CLASS_INIT(&tag, "config");
1271 print_otag(h, TAG_B, 1, &tag);
1278 mdoc_dv_pre(MDOC_ARGS)
1280 struct htmlpair tag;
1282 PAIR_CLASS_INIT(&tag, "define");
1283 print_otag(h, TAG_SPAN, 1, &tag);
1290 mdoc_ev_pre(MDOC_ARGS)
1292 struct htmlpair tag;
1294 PAIR_CLASS_INIT(&tag, "env");
1295 print_otag(h, TAG_SPAN, 1, &tag);
1302 mdoc_er_pre(MDOC_ARGS)
1304 struct htmlpair tag;
1306 PAIR_CLASS_INIT(&tag, "errno");
1307 print_otag(h, TAG_SPAN, 1, &tag);
1314 mdoc_fa_pre(MDOC_ARGS)
1316 const struct mdoc_node *nn;
1317 struct htmlpair tag;
1320 PAIR_CLASS_INIT(&tag, "farg");
1321 if (n->parent->tok != MDOC_Fo) {
1322 print_otag(h, TAG_I, 1, &tag);
1326 for (nn = n->child; nn; nn = nn->next) {
1327 t = print_otag(h, TAG_I, 1, &tag);
1328 print_text(h, nn->string);
1334 if (n->child && n->next && n->next->tok == MDOC_Fa)
1343 mdoc_fd_pre(MDOC_ARGS)
1345 struct htmlpair tag;
1349 PAIR_CLASS_INIT(&tag, "macro");
1350 print_otag(h, TAG_B, 1, &tag);
1357 mdoc_vt_pre(MDOC_ARGS)
1359 struct htmlpair tag;
1361 if (MDOC_BLOCK == n->type) {
1364 } else if (MDOC_ELEM == n->type) {
1366 } else if (MDOC_HEAD == n->type)
1369 PAIR_CLASS_INIT(&tag, "type");
1370 print_otag(h, TAG_SPAN, 1, &tag);
1377 mdoc_ft_pre(MDOC_ARGS)
1379 struct htmlpair tag;
1382 PAIR_CLASS_INIT(&tag, "ftype");
1383 print_otag(h, TAG_I, 1, &tag);
1390 mdoc_fn_pre(MDOC_ARGS)
1393 struct htmlpair tag[2];
1394 const struct mdoc_node *nn;
1396 const char *sp, *ep;
1401 /* Split apart into type and name. */
1402 assert(n->child->string);
1403 sp = n->child->string;
1405 ep = strchr(sp, ' ');
1407 PAIR_CLASS_INIT(&tag[0], "ftype");
1408 t = print_otag(h, TAG_I, 1, tag);
1411 sz = MIN((int)(ep - sp), BUFSIZ - 1);
1412 (void)memcpy(nbuf, sp, (size_t)sz);
1414 print_text(h, nbuf);
1416 ep = strchr(sp, ' ');
1421 PAIR_CLASS_INIT(&tag[0], "fname");
1424 * FIXME: only refer to IDs that we know exist.
1428 if (MDOC_SYNPRETTY & n->flags) {
1430 html_idcat(nbuf, sp, BUFSIZ);
1431 PAIR_ID_INIT(&tag[1], nbuf);
1433 strlcpy(nbuf, "#", BUFSIZ);
1434 html_idcat(nbuf, sp, BUFSIZ);
1435 PAIR_HREF_INIT(&tag[1], nbuf);
1439 t = print_otag(h, TAG_B, 1, tag);
1442 strlcpy(nbuf, sp, BUFSIZ);
1443 print_text(h, nbuf);
1448 h->flags |= HTML_NOSPACE;
1452 PAIR_CLASS_INIT(&tag[0], "farg");
1453 bufcat_style(h, "white-space", "nowrap");
1454 PAIR_STYLE_INIT(&tag[1], h);
1456 for (nn = n->child->next; nn; nn = nn->next) {
1458 if (MDOC_SYNPRETTY & n->flags)
1460 t = print_otag(h, TAG_I, i, tag);
1461 print_text(h, nn->string);
1468 if (MDOC_SYNPRETTY & n->flags)
1477 mdoc_sm_pre(MDOC_ARGS)
1480 assert(n->child && MDOC_TEXT == n->child->type);
1481 if (0 == strcmp("on", n->child->string)) {
1483 * FIXME: no p->col to check. Thus, if we have
1490 * the "3" is preceded by a space.
1492 h->flags &= ~HTML_NOSPACE;
1493 h->flags &= ~HTML_NONOSPACE;
1495 h->flags |= HTML_NONOSPACE;
1502 mdoc_pp_pre(MDOC_ARGS)
1505 print_otag(h, TAG_P, 0, NULL);
1512 mdoc_sp_pre(MDOC_ARGS)
1515 struct htmlpair tag;
1517 SCALE_VS_INIT(&su, 1);
1519 if (MDOC_sp == n->tok) {
1521 a2roffsu(n->child->string, &su, SCALE_VS);
1525 bufcat_su(h, "height", &su);
1526 PAIR_STYLE_INIT(&tag, h);
1527 print_otag(h, TAG_DIV, 1, &tag);
1529 /* So the div isn't empty: */
1530 print_text(h, "\\~");
1538 mdoc_lk_pre(MDOC_ARGS)
1540 const struct mdoc_node *nn;
1541 struct htmlpair tag[2];
1545 PAIR_CLASS_INIT(&tag[0], "link-ext");
1546 PAIR_HREF_INIT(&tag[1], nn->string);
1547 print_otag(h, TAG_A, 2, tag);
1549 if (NULL == nn || NULL == nn->next)
1552 for (nn = nn->next; nn; nn = nn->next)
1553 print_text(h, nn->string);
1561 mdoc_mt_pre(MDOC_ARGS)
1563 struct htmlpair tag[2];
1565 const struct mdoc_node *nn;
1567 PAIR_CLASS_INIT(&tag[0], "link-mail");
1569 for (nn = n->child; nn; nn = nn->next) {
1571 bufcat(h, "mailto:");
1572 bufcat(h, nn->string);
1573 PAIR_HREF_INIT(&tag[1], h->buf);
1574 t = print_otag(h, TAG_A, 2, tag);
1575 print_text(h, nn->string);
1585 mdoc_fo_pre(MDOC_ARGS)
1587 struct htmlpair tag;
1590 if (MDOC_BODY == n->type) {
1591 h->flags |= HTML_NOSPACE;
1593 h->flags |= HTML_NOSPACE;
1595 } else if (MDOC_BLOCK == n->type) {
1600 /* XXX: we drop non-initial arguments as per groff. */
1603 assert(n->child->string);
1605 PAIR_CLASS_INIT(&tag, "fname");
1606 t = print_otag(h, TAG_B, 1, &tag);
1607 print_text(h, n->child->string);
1615 mdoc_fo_post(MDOC_ARGS)
1618 if (MDOC_BODY != n->type)
1627 mdoc_in_pre(MDOC_ARGS)
1629 const struct mdoc_node *nn;
1631 struct htmlpair tag[2];
1636 PAIR_CLASS_INIT(&tag[0], "includes");
1637 print_otag(h, TAG_B, 1, tag);
1639 if (MDOC_SYNPRETTY & n->flags && MDOC_LINE & n->flags)
1640 print_text(h, "#include");
1643 h->flags |= HTML_NOSPACE;
1645 for (nn = n->child; nn; nn = nn->next) {
1646 PAIR_CLASS_INIT(&tag[0], "link-includes");
1649 if (h->base_includes) {
1650 buffmt_includes(h, nn->string);
1651 PAIR_HREF_INIT(&tag[i], h->buf);
1654 t = print_otag(h, TAG_A, i, tag);
1655 print_mdoc_node(m, nn, h);
1659 h->flags |= HTML_NOSPACE;
1668 mdoc_ic_pre(MDOC_ARGS)
1670 struct htmlpair tag;
1672 PAIR_CLASS_INIT(&tag, "cmd");
1673 print_otag(h, TAG_B, 1, &tag);
1680 mdoc_rv_pre(MDOC_ARGS)
1682 const struct mdoc_node *nn;
1683 struct htmlpair tag;
1687 print_otag(h, TAG_BR, 0, NULL);
1689 print_text(h, "The");
1691 for (nn = n->child; nn; nn = nn->next) {
1692 PAIR_CLASS_INIT(&tag, "fname");
1693 t = print_otag(h, TAG_B, 1, &tag);
1694 print_text(h, nn->string);
1697 h->flags |= HTML_NOSPACE;
1698 if (nn->next && NULL == nn->next->next)
1699 print_text(h, "(), and");
1701 print_text(h, "(),");
1703 print_text(h, "()");
1706 if (n->child && n->child->next)
1707 print_text(h, "functions return");
1709 print_text(h, "function returns");
1711 print_text(h, "the value 0 if successful; otherwise the value "
1712 "-1 is returned and the global variable");
1714 PAIR_CLASS_INIT(&tag, "var");
1715 t = print_otag(h, TAG_B, 1, &tag);
1716 print_text(h, "errno");
1718 print_text(h, "is set to indicate the error.");
1725 mdoc_va_pre(MDOC_ARGS)
1727 struct htmlpair tag;
1729 PAIR_CLASS_INIT(&tag, "var");
1730 print_otag(h, TAG_B, 1, &tag);
1737 mdoc_ap_pre(MDOC_ARGS)
1740 h->flags |= HTML_NOSPACE;
1741 print_text(h, "\\(aq");
1742 h->flags |= HTML_NOSPACE;
1749 mdoc_bf_pre(MDOC_ARGS)
1751 struct htmlpair tag[2];
1754 if (MDOC_HEAD == n->type)
1756 else if (MDOC_BODY != n->type)
1759 if (FONT_Em == n->norm->Bf.font)
1760 PAIR_CLASS_INIT(&tag[0], "emph");
1761 else if (FONT_Sy == n->norm->Bf.font)
1762 PAIR_CLASS_INIT(&tag[0], "symb");
1763 else if (FONT_Li == n->norm->Bf.font)
1764 PAIR_CLASS_INIT(&tag[0], "lit");
1766 PAIR_CLASS_INIT(&tag[0], "none");
1769 * We want this to be inline-formatted, but needs to be div to
1770 * accept block children.
1772 bufcat_style(h, "display", "inline");
1773 SCALE_HS_INIT(&su, 1);
1774 /* Needs a left-margin for spacing. */
1775 bufcat_su(h, "margin-left", &su);
1776 PAIR_STYLE_INIT(&tag[1], h);
1777 print_otag(h, TAG_DIV, 2, tag);
1784 mdoc_ms_pre(MDOC_ARGS)
1786 struct htmlpair tag;
1788 PAIR_CLASS_INIT(&tag, "symb");
1789 print_otag(h, TAG_SPAN, 1, &tag);
1796 mdoc_igndelim_pre(MDOC_ARGS)
1799 h->flags |= HTML_IGNDELIM;
1806 mdoc_pf_post(MDOC_ARGS)
1809 h->flags |= HTML_NOSPACE;
1815 mdoc_rs_pre(MDOC_ARGS)
1817 struct htmlpair tag;
1819 if (MDOC_BLOCK != n->type)
1822 if (n->prev && SEC_SEE_ALSO == n->sec)
1823 print_otag(h, TAG_P, 0, NULL);
1825 PAIR_CLASS_INIT(&tag, "ref");
1826 print_otag(h, TAG_SPAN, 1, &tag);
1834 mdoc_li_pre(MDOC_ARGS)
1836 struct htmlpair tag;
1838 PAIR_CLASS_INIT(&tag, "lit");
1839 print_otag(h, TAG_SPAN, 1, &tag);
1846 mdoc_sy_pre(MDOC_ARGS)
1848 struct htmlpair tag;
1850 PAIR_CLASS_INIT(&tag, "symb");
1851 print_otag(h, TAG_SPAN, 1, &tag);
1858 mdoc_bt_pre(MDOC_ARGS)
1861 print_text(h, "is currently in beta test.");
1868 mdoc_ud_pre(MDOC_ARGS)
1871 print_text(h, "currently under development.");
1878 mdoc_lb_pre(MDOC_ARGS)
1880 struct htmlpair tag;
1882 if (SEC_LIBRARY == n->sec && MDOC_LINE & n->flags && n->prev)
1883 print_otag(h, TAG_BR, 0, NULL);
1885 PAIR_CLASS_INIT(&tag, "lib");
1886 print_otag(h, TAG_SPAN, 1, &tag);
1893 mdoc__x_pre(MDOC_ARGS)
1895 struct htmlpair tag[2];
1902 PAIR_CLASS_INIT(&tag[0], "ref-auth");
1903 if (n->prev && MDOC__A == n->prev->tok)
1904 if (NULL == n->next || MDOC__A != n->next->tok)
1905 print_text(h, "and");
1908 PAIR_CLASS_INIT(&tag[0], "ref-book");
1912 PAIR_CLASS_INIT(&tag[0], "ref-city");
1915 PAIR_CLASS_INIT(&tag[0], "ref-date");
1918 PAIR_CLASS_INIT(&tag[0], "ref-issue");
1922 PAIR_CLASS_INIT(&tag[0], "ref-jrnl");
1926 PAIR_CLASS_INIT(&tag[0], "ref-num");
1929 PAIR_CLASS_INIT(&tag[0], "ref-opt");
1932 PAIR_CLASS_INIT(&tag[0], "ref-page");
1935 PAIR_CLASS_INIT(&tag[0], "ref-corp");
1938 PAIR_CLASS_INIT(&tag[0], "ref-rep");
1941 PAIR_CLASS_INIT(&tag[0], "ref-title");
1944 PAIR_CLASS_INIT(&tag[0], "link-ref");
1947 PAIR_CLASS_INIT(&tag[0], "ref-vol");
1954 if (MDOC__U != n->tok) {
1955 print_otag(h, t, 1, tag);
1959 PAIR_HREF_INIT(&tag[1], n->child->string);
1960 print_otag(h, TAG_A, 2, tag);
1968 mdoc__x_post(MDOC_ARGS)
1971 if (MDOC__A == n->tok && n->next && MDOC__A == n->next->tok)
1972 if (NULL == n->next->next || MDOC__A != n->next->next->tok)
1973 if (NULL == n->prev || MDOC__A != n->prev->tok)
1978 if (NULL == n->parent || MDOC_Rs != n->parent->tok)
1981 print_text(h, n->next ? "," : ".");
1987 mdoc_bk_pre(MDOC_ARGS)
1996 if (n->parent->args || 0 == n->prev->nchild)
1997 h->flags |= HTML_PREKEEP;
2010 mdoc_bk_post(MDOC_ARGS)
2013 if (MDOC_BODY == n->type)
2014 h->flags &= ~(HTML_KEEP | HTML_PREKEEP);
2020 mdoc_quote_pre(MDOC_ARGS)
2022 struct htmlpair tag;
2024 if (MDOC_BODY != n->type)
2031 print_text(h, "\\(la");
2036 print_text(h, "\\(lC");
2041 print_text(h, "\\(lB");
2046 print_text(h, "\\(lB");
2047 h->flags |= HTML_NOSPACE;
2048 PAIR_CLASS_INIT(&tag, "opt");
2049 print_otag(h, TAG_SPAN, 1, &tag);
2058 print_text(h, "\\(lq");
2070 print_text(h, "\\(oq");
2077 h->flags |= HTML_NOSPACE;
2084 mdoc_quote_post(MDOC_ARGS)
2087 if (MDOC_BODY != n->type)
2090 h->flags |= HTML_NOSPACE;
2096 print_text(h, "\\(ra");
2101 print_text(h, "\\(rC");
2110 print_text(h, "\\(rB");
2119 print_text(h, "\\(rq");
2131 print_text(h, "\\(aq");