1 /* $Id: mdoc_html.c,v 1.294 2017/07/15 17:57:51 schwarze Exp $ */
3 * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2014, 2015, 2016, 2017 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/types.h>
29 #include "mandoc_aux.h"
39 #define MDOC_ARGS const struct roff_meta *meta, \
40 struct roff_node *n, \
44 #define MIN(a,b) ((/*CONSTCOND*/(a)<(b))?(a):(b))
48 int (*pre)(MDOC_ARGS);
49 void (*post)(MDOC_ARGS);
52 static char *cond_id(const struct roff_node *);
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 roff_node *);
59 static void mdoc_root_post(MDOC_ARGS);
60 static int mdoc_root_pre(MDOC_ARGS);
62 static void mdoc__x_post(MDOC_ARGS);
63 static int mdoc__x_pre(MDOC_ARGS);
64 static int mdoc_ad_pre(MDOC_ARGS);
65 static int mdoc_an_pre(MDOC_ARGS);
66 static int mdoc_ap_pre(MDOC_ARGS);
67 static int mdoc_ar_pre(MDOC_ARGS);
68 static int mdoc_bd_pre(MDOC_ARGS);
69 static int mdoc_bf_pre(MDOC_ARGS);
70 static void mdoc_bk_post(MDOC_ARGS);
71 static int mdoc_bk_pre(MDOC_ARGS);
72 static int mdoc_bl_pre(MDOC_ARGS);
73 static int mdoc_cd_pre(MDOC_ARGS);
74 static int mdoc_cm_pre(MDOC_ARGS);
75 static int mdoc_d1_pre(MDOC_ARGS);
76 static int mdoc_dv_pre(MDOC_ARGS);
77 static int mdoc_fa_pre(MDOC_ARGS);
78 static int mdoc_fd_pre(MDOC_ARGS);
79 static int mdoc_fl_pre(MDOC_ARGS);
80 static int mdoc_fn_pre(MDOC_ARGS);
81 static int mdoc_ft_pre(MDOC_ARGS);
82 static int mdoc_em_pre(MDOC_ARGS);
83 static void mdoc_eo_post(MDOC_ARGS);
84 static int mdoc_eo_pre(MDOC_ARGS);
85 static int mdoc_er_pre(MDOC_ARGS);
86 static int mdoc_ev_pre(MDOC_ARGS);
87 static int mdoc_ex_pre(MDOC_ARGS);
88 static void mdoc_fo_post(MDOC_ARGS);
89 static int mdoc_fo_pre(MDOC_ARGS);
90 static int mdoc_ic_pre(MDOC_ARGS);
91 static int mdoc_igndelim_pre(MDOC_ARGS);
92 static int mdoc_in_pre(MDOC_ARGS);
93 static int mdoc_it_pre(MDOC_ARGS);
94 static int mdoc_lb_pre(MDOC_ARGS);
95 static int mdoc_li_pre(MDOC_ARGS);
96 static int mdoc_lk_pre(MDOC_ARGS);
97 static int mdoc_mt_pre(MDOC_ARGS);
98 static int mdoc_ms_pre(MDOC_ARGS);
99 static int mdoc_nd_pre(MDOC_ARGS);
100 static int mdoc_nm_pre(MDOC_ARGS);
101 static int mdoc_no_pre(MDOC_ARGS);
102 static int mdoc_ns_pre(MDOC_ARGS);
103 static int mdoc_pa_pre(MDOC_ARGS);
104 static void mdoc_pf_post(MDOC_ARGS);
105 static int mdoc_pp_pre(MDOC_ARGS);
106 static void mdoc_quote_post(MDOC_ARGS);
107 static int mdoc_quote_pre(MDOC_ARGS);
108 static int mdoc_rs_pre(MDOC_ARGS);
109 static int mdoc_sh_pre(MDOC_ARGS);
110 static int mdoc_skip_pre(MDOC_ARGS);
111 static int mdoc_sm_pre(MDOC_ARGS);
112 static int mdoc_ss_pre(MDOC_ARGS);
113 static int mdoc_st_pre(MDOC_ARGS);
114 static int mdoc_sx_pre(MDOC_ARGS);
115 static int mdoc_sy_pre(MDOC_ARGS);
116 static int mdoc_va_pre(MDOC_ARGS);
117 static int mdoc_vt_pre(MDOC_ARGS);
118 static int mdoc_xr_pre(MDOC_ARGS);
119 static int mdoc_xx_pre(MDOC_ARGS);
121 static const struct htmlmdoc __mdocs[MDOC_MAX - MDOC_Dd] = {
122 {NULL, NULL}, /* Dd */
123 {NULL, NULL}, /* Dt */
124 {NULL, NULL}, /* Os */
125 {mdoc_sh_pre, NULL }, /* Sh */
126 {mdoc_ss_pre, NULL }, /* Ss */
127 {mdoc_pp_pre, NULL}, /* Pp */
128 {mdoc_d1_pre, NULL}, /* D1 */
129 {mdoc_d1_pre, NULL}, /* Dl */
130 {mdoc_bd_pre, NULL}, /* Bd */
131 {NULL, NULL}, /* Ed */
132 {mdoc_bl_pre, NULL}, /* Bl */
133 {NULL, NULL}, /* El */
134 {mdoc_it_pre, NULL}, /* It */
135 {mdoc_ad_pre, NULL}, /* Ad */
136 {mdoc_an_pre, NULL}, /* An */
137 {mdoc_ap_pre, NULL}, /* Ap */
138 {mdoc_ar_pre, NULL}, /* Ar */
139 {mdoc_cd_pre, NULL}, /* Cd */
140 {mdoc_cm_pre, NULL}, /* Cm */
141 {mdoc_dv_pre, NULL}, /* Dv */
142 {mdoc_er_pre, NULL}, /* Er */
143 {mdoc_ev_pre, NULL}, /* Ev */
144 {mdoc_ex_pre, NULL}, /* Ex */
145 {mdoc_fa_pre, NULL}, /* Fa */
146 {mdoc_fd_pre, NULL}, /* Fd */
147 {mdoc_fl_pre, NULL}, /* Fl */
148 {mdoc_fn_pre, NULL}, /* Fn */
149 {mdoc_ft_pre, NULL}, /* Ft */
150 {mdoc_ic_pre, NULL}, /* Ic */
151 {mdoc_in_pre, NULL}, /* In */
152 {mdoc_li_pre, NULL}, /* Li */
153 {mdoc_nd_pre, NULL}, /* Nd */
154 {mdoc_nm_pre, NULL}, /* Nm */
155 {mdoc_quote_pre, mdoc_quote_post}, /* Op */
156 {mdoc_ft_pre, NULL}, /* Ot */
157 {mdoc_pa_pre, NULL}, /* Pa */
158 {mdoc_ex_pre, NULL}, /* Rv */
159 {mdoc_st_pre, NULL}, /* St */
160 {mdoc_va_pre, NULL}, /* Va */
161 {mdoc_vt_pre, NULL}, /* Vt */
162 {mdoc_xr_pre, NULL}, /* Xr */
163 {mdoc__x_pre, mdoc__x_post}, /* %A */
164 {mdoc__x_pre, mdoc__x_post}, /* %B */
165 {mdoc__x_pre, mdoc__x_post}, /* %D */
166 {mdoc__x_pre, mdoc__x_post}, /* %I */
167 {mdoc__x_pre, mdoc__x_post}, /* %J */
168 {mdoc__x_pre, mdoc__x_post}, /* %N */
169 {mdoc__x_pre, mdoc__x_post}, /* %O */
170 {mdoc__x_pre, mdoc__x_post}, /* %P */
171 {mdoc__x_pre, mdoc__x_post}, /* %R */
172 {mdoc__x_pre, mdoc__x_post}, /* %T */
173 {mdoc__x_pre, mdoc__x_post}, /* %V */
174 {NULL, NULL}, /* Ac */
175 {mdoc_quote_pre, mdoc_quote_post}, /* Ao */
176 {mdoc_quote_pre, mdoc_quote_post}, /* Aq */
177 {mdoc_xx_pre, NULL}, /* At */
178 {NULL, NULL}, /* Bc */
179 {mdoc_bf_pre, NULL}, /* Bf */
180 {mdoc_quote_pre, mdoc_quote_post}, /* Bo */
181 {mdoc_quote_pre, mdoc_quote_post}, /* Bq */
182 {mdoc_xx_pre, NULL}, /* Bsx */
183 {mdoc_xx_pre, NULL}, /* Bx */
184 {mdoc_skip_pre, NULL}, /* Db */
185 {NULL, NULL}, /* Dc */
186 {mdoc_quote_pre, mdoc_quote_post}, /* Do */
187 {mdoc_quote_pre, mdoc_quote_post}, /* Dq */
188 {NULL, NULL}, /* Ec */ /* FIXME: no space */
189 {NULL, NULL}, /* Ef */
190 {mdoc_em_pre, NULL}, /* Em */
191 {mdoc_eo_pre, mdoc_eo_post}, /* Eo */
192 {mdoc_xx_pre, NULL}, /* Fx */
193 {mdoc_ms_pre, NULL}, /* Ms */
194 {mdoc_no_pre, NULL}, /* No */
195 {mdoc_ns_pre, NULL}, /* Ns */
196 {mdoc_xx_pre, NULL}, /* Nx */
197 {mdoc_xx_pre, NULL}, /* Ox */
198 {NULL, NULL}, /* Pc */
199 {mdoc_igndelim_pre, mdoc_pf_post}, /* Pf */
200 {mdoc_quote_pre, mdoc_quote_post}, /* Po */
201 {mdoc_quote_pre, mdoc_quote_post}, /* Pq */
202 {NULL, NULL}, /* Qc */
203 {mdoc_quote_pre, mdoc_quote_post}, /* Ql */
204 {mdoc_quote_pre, mdoc_quote_post}, /* Qo */
205 {mdoc_quote_pre, mdoc_quote_post}, /* Qq */
206 {NULL, NULL}, /* Re */
207 {mdoc_rs_pre, NULL}, /* Rs */
208 {NULL, NULL}, /* Sc */
209 {mdoc_quote_pre, mdoc_quote_post}, /* So */
210 {mdoc_quote_pre, mdoc_quote_post}, /* Sq */
211 {mdoc_sm_pre, NULL}, /* Sm */
212 {mdoc_sx_pre, NULL}, /* Sx */
213 {mdoc_sy_pre, NULL}, /* Sy */
214 {NULL, NULL}, /* Tn */
215 {mdoc_xx_pre, NULL}, /* Ux */
216 {NULL, NULL}, /* Xc */
217 {NULL, NULL}, /* Xo */
218 {mdoc_fo_pre, mdoc_fo_post}, /* Fo */
219 {NULL, NULL}, /* Fc */
220 {mdoc_quote_pre, mdoc_quote_post}, /* Oo */
221 {NULL, NULL}, /* Oc */
222 {mdoc_bk_pre, mdoc_bk_post}, /* Bk */
223 {NULL, NULL}, /* Ek */
224 {NULL, NULL}, /* Bt */
225 {NULL, NULL}, /* Hf */
226 {mdoc_em_pre, NULL}, /* Fr */
227 {NULL, NULL}, /* Ud */
228 {mdoc_lb_pre, NULL}, /* Lb */
229 {mdoc_pp_pre, NULL}, /* Lp */
230 {mdoc_lk_pre, NULL}, /* Lk */
231 {mdoc_mt_pre, NULL}, /* Mt */
232 {mdoc_quote_pre, mdoc_quote_post}, /* Brq */
233 {mdoc_quote_pre, mdoc_quote_post}, /* Bro */
234 {NULL, NULL}, /* Brc */
235 {mdoc__x_pre, mdoc__x_post}, /* %C */
236 {mdoc_skip_pre, NULL}, /* Es */
237 {mdoc_quote_pre, mdoc_quote_post}, /* En */
238 {mdoc_xx_pre, NULL}, /* Dx */
239 {mdoc__x_pre, mdoc__x_post}, /* %Q */
240 {mdoc__x_pre, mdoc__x_post}, /* %U */
241 {NULL, NULL}, /* Ta */
243 static const struct htmlmdoc *const mdocs = __mdocs - MDOC_Dd;
247 * See the same function in mdoc_term.c for documentation.
250 synopsis_pre(struct html *h, const struct roff_node *n)
253 if (NULL == n->prev || ! (NODE_SYNPRETTY & n->flags))
256 if (n->prev->tok == n->tok &&
260 print_otag(h, TAG_BR, "");
264 switch (n->prev->tok) {
273 if (MDOC_Fn != n->tok && MDOC_Fo != n->tok) {
279 print_otag(h, TAG_BR, "");
285 html_mdoc(void *arg, const struct roff_man *mdoc)
290 h = (struct html *)arg;
292 if ((h->oflags & HTML_FRAGMENT) == 0) {
294 print_otag(h, TAG_HTML, "");
295 t = print_otag(h, TAG_HEAD, "");
296 print_mdoc_head(&mdoc->meta, mdoc->first->child, h);
298 print_otag(h, TAG_BODY, "");
301 mdoc_root_pre(&mdoc->meta, mdoc->first->child, h);
302 t = print_otag(h, TAG_DIV, "c", "manual-text");
303 print_mdoc_nodelist(&mdoc->meta, mdoc->first->child, h);
305 mdoc_root_post(&mdoc->meta, mdoc->first->child, h);
310 print_mdoc_head(MDOC_ARGS)
316 if (meta->arch != NULL && meta->msec != NULL)
317 mandoc_asprintf(&cp, "%s(%s) (%s)", meta->title,
318 meta->msec, meta->arch);
319 else if (meta->msec != NULL)
320 mandoc_asprintf(&cp, "%s(%s)", meta->title, meta->msec);
321 else if (meta->arch != NULL)
322 mandoc_asprintf(&cp, "%s (%s)", meta->title, meta->arch);
324 cp = mandoc_strdup(meta->title);
326 print_otag(h, TAG_TITLE, "");
332 print_mdoc_nodelist(MDOC_ARGS)
336 print_mdoc_node(meta, n, h);
342 print_mdoc_node(MDOC_ARGS)
347 if (n->flags & NODE_NOPRT)
352 n->flags &= ~NODE_ENDED;
356 /* No tables in this mode... */
357 assert(NULL == h->tblt);
360 * Make sure that if we're in a literal mode already
361 * (i.e., within a <PRE>) don't print the newline.
363 if (*n->string == ' ' && n->flags & NODE_LINE &&
364 (h->flags & (HTML_LITERAL | HTML_NONEWLINE)) == 0)
365 print_otag(h, TAG_BR, "");
366 if (NODE_DELIMC & n->flags)
367 h->flags |= HTML_NOSPACE;
368 print_text(h, n->string);
369 if (NODE_DELIMO & n->flags)
370 h->flags |= HTML_NOSPACE;
373 print_eqn(h, n->eqn);
377 * This will take care of initialising all of the table
378 * state data for the first table, then tearing it down
381 print_tbl(h, n->span);
385 * Close out the current table, if it's open, and unset
386 * the "meta" table state. This will be reopened on the
387 * next table element.
389 if (h->tblt != NULL) {
393 assert(h->tblt == NULL);
394 if (n->tok < ROFF_MAX) {
399 assert(n->tok >= MDOC_Dd && n->tok < MDOC_MAX);
400 if (mdocs[n->tok].pre != NULL &&
401 (n->end == ENDBODY_NOT || n->child != NULL))
402 child = (*mdocs[n->tok].pre)(meta, n, h);
406 if (h->flags & HTML_KEEP && n->flags & NODE_LINE) {
407 h->flags &= ~HTML_KEEP;
408 h->flags |= HTML_PREKEEP;
411 if (child && n->child)
412 print_mdoc_nodelist(meta, n->child, h);
420 if (n->tok < ROFF_MAX ||
421 mdocs[n->tok].post == NULL ||
422 n->flags & NODE_ENDED)
424 (*mdocs[n->tok].post)(meta, n, h);
425 if (n->end != ENDBODY_NOT)
426 n->body->flags |= NODE_ENDED;
432 mdoc_root_post(MDOC_ARGS)
436 t = print_otag(h, TAG_TABLE, "c", "foot");
437 tt = print_otag(h, TAG_TR, "");
439 print_otag(h, TAG_TD, "c", "foot-date");
440 print_text(h, meta->date);
443 print_otag(h, TAG_TD, "c", "foot-os");
444 print_text(h, meta->os);
449 mdoc_root_pre(MDOC_ARGS)
452 char *volume, *title;
454 if (NULL == meta->arch)
455 volume = mandoc_strdup(meta->vol);
457 mandoc_asprintf(&volume, "%s (%s)",
458 meta->vol, meta->arch);
460 if (NULL == meta->msec)
461 title = mandoc_strdup(meta->title);
463 mandoc_asprintf(&title, "%s(%s)",
464 meta->title, meta->msec);
466 t = print_otag(h, TAG_TABLE, "c", "head");
467 tt = print_otag(h, TAG_TR, "");
469 print_otag(h, TAG_TD, "c", "head-ltitle");
470 print_text(h, title);
473 print_otag(h, TAG_TD, "c", "head-vol");
474 print_text(h, volume);
477 print_otag(h, TAG_TD, "c", "head-rtitle");
478 print_text(h, title);
487 cond_id(const struct roff_node *n)
489 if (n->child != NULL &&
490 n->child->type == ROFFT_TEXT &&
492 (n->prev->type == ROFFT_TEXT &&
493 strcmp(n->prev->string, "|") == 0)) &&
494 (n->parent->tok == MDOC_It ||
495 (n->parent->tok == MDOC_Xo &&
496 n->parent->parent->prev == NULL &&
497 n->parent->parent->parent->tok == MDOC_It)))
498 return html_make_id(n);
503 mdoc_sh_pre(MDOC_ARGS)
509 id = html_make_id(n);
510 print_otag(h, TAG_H1, "cTi", "Sh", id);
512 print_otag(h, TAG_A, "chR", "selflink", id);
516 if (n->sec == SEC_AUTHORS)
517 h->flags &= ~(HTML_SPLIT|HTML_NOSPLIT);
526 mdoc_ss_pre(MDOC_ARGS)
530 if (n->type != ROFFT_HEAD)
533 id = html_make_id(n);
534 print_otag(h, TAG_H2, "cTi", "Ss", id);
536 print_otag(h, TAG_A, "chR", "selflink", id);
542 mdoc_fl_pre(MDOC_ARGS)
546 if ((id = cond_id(n)) != NULL)
547 print_otag(h, TAG_A, "chR", "selflink", id);
548 print_otag(h, TAG_B, "cTi", "Fl", id);
551 print_text(h, "\\-");
552 if (!(n->child == NULL &&
554 n->next->type == ROFFT_TEXT ||
555 n->next->flags & NODE_LINE)))
556 h->flags |= HTML_NOSPACE;
562 mdoc_cm_pre(MDOC_ARGS)
566 if ((id = cond_id(n)) != NULL)
567 print_otag(h, TAG_A, "chR", "selflink", id);
568 print_otag(h, TAG_B, "cTi", "Cm", id);
574 mdoc_nd_pre(MDOC_ARGS)
576 if (n->type != ROFFT_BODY)
579 /* XXX: this tag in theory can contain block elements. */
581 print_text(h, "\\(em");
582 print_otag(h, TAG_SPAN, "cT", "Nd");
587 mdoc_nm_pre(MDOC_ARGS)
591 print_otag(h, TAG_TD, "");
594 print_otag(h, TAG_B, "cT", "Nm");
597 print_otag(h, TAG_TD, "");
603 print_otag(h, TAG_TABLE, "c", "Nm");
604 print_otag(h, TAG_TR, "");
609 mdoc_xr_pre(MDOC_ARGS)
611 if (NULL == n->child)
615 print_otag(h, TAG_A, "cThM", "Xr",
616 n->child->string, n->child->next == NULL ?
617 NULL : n->child->next->string);
619 print_otag(h, TAG_A, "cT", "Xr");
622 print_text(h, n->string);
624 if (NULL == (n = n->next))
627 h->flags |= HTML_NOSPACE;
629 h->flags |= HTML_NOSPACE;
630 print_text(h, n->string);
631 h->flags |= HTML_NOSPACE;
637 mdoc_ns_pre(MDOC_ARGS)
640 if ( ! (NODE_LINE & n->flags))
641 h->flags |= HTML_NOSPACE;
646 mdoc_ar_pre(MDOC_ARGS)
648 print_otag(h, TAG_VAR, "cT", "Ar");
653 mdoc_xx_pre(MDOC_ARGS)
655 print_otag(h, TAG_SPAN, "c", "Ux");
660 mdoc_it_pre(MDOC_ARGS)
662 const struct roff_node *bl;
668 while (bl->tok != MDOC_Bl)
670 type = bl->norm->Bl.type;
718 print_otag(h, TAG_LI, "c", cattr);
730 print_otag(h, TAG_DT, "c", cattr);
731 if (type == LIST_diag)
732 print_otag(h, TAG_B, "c", cattr);
735 print_otag(h, TAG_DD, "csw*+l", cattr,
745 if (h->style != NULL && !bl->norm->Bl.comp &&
746 (n->parent->prev == NULL ||
747 n->parent->prev->body == NULL ||
748 n->parent->prev->body->child != NULL)) {
749 t = print_otag(h, TAG_DT, "csw*+-l",
750 cattr, bl->norm->Bl.width);
751 print_text(h, "\\ ");
753 t = print_otag(h, TAG_DD, "c", cattr);
754 print_text(h, "\\ ");
757 print_otag(h, TAG_DT, "csw*+-l", cattr,
761 if (n->child == NULL) {
762 print_otag(h, TAG_DD, "css?", cattr,
764 print_text(h, "\\ ");
766 print_otag(h, TAG_DD, "c", cattr);
777 print_otag(h, TAG_TD, "c", cattr);
780 print_otag(h, TAG_TR, "c", cattr);
790 mdoc_bl_pre(MDOC_ARGS)
796 enum htmltag elemtype;
805 if (bl->type != LIST_column || bl->ncols == 0)
809 * For each column, print out the <COL> tag with our
810 * suggested width. The last column gets min-width, as
811 * in terminal mode it auto-sizes to the width of the
812 * screen and we want to preserve that behaviour.
815 t = print_otag(h, TAG_COLGROUP, "");
816 for (i = 0; i < bl->ncols - 1; i++)
817 print_otag(h, TAG_COL, "sw+w", bl->cols[i]);
818 print_otag(h, TAG_COL, "swW", bl->cols[i]);
829 (void)strlcpy(cattr, "Bl-bullet", sizeof(cattr));
834 (void)strlcpy(cattr, "Bl-dash", sizeof(cattr));
838 (void)strlcpy(cattr, "Bl-item", sizeof(cattr));
842 (void)strlcpy(cattr, "Bl-enum", sizeof(cattr));
846 (void)strlcpy(cattr, "Bl-diag", sizeof(cattr));
850 (void)strlcpy(cattr, "Bl-hang", sizeof(cattr));
854 (void)strlcpy(cattr, "Bl-inset", sizeof(cattr));
858 (void)strlcpy(cattr, "Bl-ohang", sizeof(cattr));
862 print_otag(h, TAG_DIV, "cswl", "Bl-tag", bl->offs);
863 print_otag(h, TAG_DL, "csw*+l", bl->comp ?
864 "Bl-tag Bl-compact" : "Bl-tag", bl->width);
867 elemtype = TAG_TABLE;
868 (void)strlcpy(cattr, "Bl-column", sizeof(cattr));
874 (void)strlcat(cattr, " Bl-compact", sizeof(cattr));
875 print_otag(h, elemtype, "cswl", cattr, bl->offs);
880 mdoc_ex_pre(MDOC_ARGS)
883 print_otag(h, TAG_BR, "");
888 mdoc_st_pre(MDOC_ARGS)
890 print_otag(h, TAG_SPAN, "cT", "St");
895 mdoc_em_pre(MDOC_ARGS)
897 print_otag(h, TAG_I, "cT", "Em");
902 mdoc_d1_pre(MDOC_ARGS)
904 if (n->type != ROFFT_BLOCK)
907 print_otag(h, TAG_DIV, "c", "D1");
909 if (n->tok == MDOC_Dl)
910 print_otag(h, TAG_CODE, "c", "Li");
916 mdoc_sx_pre(MDOC_ARGS)
920 id = html_make_id(n);
921 print_otag(h, TAG_A, "cThR", "Sx", id);
927 mdoc_bd_pre(MDOC_ARGS)
930 struct roff_node *nn;
932 if (n->type == ROFFT_HEAD)
935 if (n->type == ROFFT_BLOCK) {
936 comp = n->norm->Bd.comp;
937 for (nn = n; nn && ! comp; nn = nn->parent) {
938 if (nn->type != ROFFT_BLOCK)
940 if (MDOC_Ss == nn->tok || MDOC_Sh == nn->tok)
950 /* Handle the -offset argument. */
952 if (n->norm->Bd.offs == NULL ||
953 ! strcmp(n->norm->Bd.offs, "left"))
955 else if ( ! strcmp(n->norm->Bd.offs, "indent"))
957 else if ( ! strcmp(n->norm->Bd.offs, "indent-two"))
963 print_otag(h, TAG_DIV, "cswl", "Bd", n->norm->Bd.offs);
965 print_otag(h, TAG_DIV, "cshl", "Bd", offs);
967 if (n->norm->Bd.type != DISP_unfilled &&
968 n->norm->Bd.type != DISP_literal)
971 print_otag(h, TAG_PRE, "c", "Li");
973 /* This can be recursive: save & set our literal state. */
975 sv = h->flags & HTML_LITERAL;
976 h->flags |= HTML_LITERAL;
978 for (nn = n->child; nn; nn = nn->next) {
979 print_mdoc_node(meta, nn, h);
981 * If the printed node flushes its own line, then we
982 * needn't do it here as well. This is hacky, but the
983 * notion of selective eoln whitespace is pretty dumb
984 * anyway, so don't sweat it.
999 if (h->flags & HTML_NONEWLINE ||
1000 (nn->next && ! (nn->next->flags & NODE_LINE)))
1003 print_text(h, "\n");
1005 h->flags |= HTML_NOSPACE;
1009 h->flags &= ~HTML_LITERAL;
1015 mdoc_pa_pre(MDOC_ARGS)
1017 print_otag(h, TAG_I, "cT", "Pa");
1022 mdoc_ad_pre(MDOC_ARGS)
1024 print_otag(h, TAG_I, "c", "Ad");
1029 mdoc_an_pre(MDOC_ARGS)
1031 if (n->norm->An.auth == AUTH_split) {
1032 h->flags &= ~HTML_NOSPLIT;
1033 h->flags |= HTML_SPLIT;
1036 if (n->norm->An.auth == AUTH_nosplit) {
1037 h->flags &= ~HTML_SPLIT;
1038 h->flags |= HTML_NOSPLIT;
1042 if (h->flags & HTML_SPLIT)
1043 print_otag(h, TAG_BR, "");
1045 if (n->sec == SEC_AUTHORS && ! (h->flags & HTML_NOSPLIT))
1046 h->flags |= HTML_SPLIT;
1048 print_otag(h, TAG_SPAN, "cT", "An");
1053 mdoc_cd_pre(MDOC_ARGS)
1056 print_otag(h, TAG_B, "cT", "Cd");
1061 mdoc_dv_pre(MDOC_ARGS)
1065 if ((id = cond_id(n)) != NULL)
1066 print_otag(h, TAG_A, "chR", "selflink", id);
1067 print_otag(h, TAG_CODE, "cTi", "Dv", id);
1073 mdoc_ev_pre(MDOC_ARGS)
1077 if ((id = cond_id(n)) != NULL)
1078 print_otag(h, TAG_A, "chR", "selflink", id);
1079 print_otag(h, TAG_CODE, "cTi", "Ev", id);
1085 mdoc_er_pre(MDOC_ARGS)
1089 id = n->sec == SEC_ERRORS &&
1090 (n->parent->tok == MDOC_It ||
1091 (n->parent->tok == MDOC_Bq &&
1092 n->parent->parent->parent->tok == MDOC_It)) ?
1093 html_make_id(n) : NULL;
1096 print_otag(h, TAG_A, "chR", "selflink", id);
1097 print_otag(h, TAG_CODE, "cTi", "Er", id);
1103 mdoc_fa_pre(MDOC_ARGS)
1105 const struct roff_node *nn;
1108 if (n->parent->tok != MDOC_Fo) {
1109 print_otag(h, TAG_VAR, "cT", "Fa");
1113 for (nn = n->child; nn; nn = nn->next) {
1114 t = print_otag(h, TAG_VAR, "cT", "Fa");
1115 print_text(h, nn->string);
1118 h->flags |= HTML_NOSPACE;
1123 if (n->child && n->next && n->next->tok == MDOC_Fa) {
1124 h->flags |= HTML_NOSPACE;
1132 mdoc_fd_pre(MDOC_ARGS)
1139 if (NULL == (n = n->child))
1142 assert(n->type == ROFFT_TEXT);
1144 if (strcmp(n->string, "#include")) {
1145 print_otag(h, TAG_B, "cT", "Fd");
1149 print_otag(h, TAG_B, "cT", "In");
1150 print_text(h, n->string);
1152 if (NULL != (n = n->next)) {
1153 assert(n->type == ROFFT_TEXT);
1155 if (h->base_includes) {
1157 if (*cp == '<' || *cp == '"')
1159 buf = mandoc_strdup(cp);
1160 cp = strchr(buf, '\0') - 1;
1161 if (cp >= buf && (*cp == '>' || *cp == '"'))
1163 t = print_otag(h, TAG_A, "cThI", "In", buf);
1166 t = print_otag(h, TAG_A, "cT", "In");
1168 print_text(h, n->string);
1174 for ( ; n; n = n->next) {
1175 assert(n->type == ROFFT_TEXT);
1176 print_text(h, n->string);
1183 mdoc_vt_pre(MDOC_ARGS)
1185 if (n->type == ROFFT_BLOCK) {
1188 } else if (n->type == ROFFT_ELEM) {
1190 } else if (n->type == ROFFT_HEAD)
1193 print_otag(h, TAG_VAR, "cT", "Vt");
1198 mdoc_ft_pre(MDOC_ARGS)
1201 print_otag(h, TAG_VAR, "cT", "Ft");
1206 mdoc_fn_pre(MDOC_ARGS)
1210 const char *sp, *ep;
1213 pretty = NODE_SYNPRETTY & n->flags;
1216 /* Split apart into type and name. */
1217 assert(n->child->string);
1218 sp = n->child->string;
1220 ep = strchr(sp, ' ');
1222 t = print_otag(h, TAG_VAR, "cT", "Ft");
1225 sz = MIN((int)(ep - sp), BUFSIZ - 1);
1226 (void)memcpy(nbuf, sp, (size_t)sz);
1228 print_text(h, nbuf);
1230 ep = strchr(sp, ' ');
1235 t = print_otag(h, TAG_B, "cT", "Fn");
1242 h->flags |= HTML_NOSPACE;
1244 h->flags |= HTML_NOSPACE;
1246 for (n = n->child->next; n; n = n->next) {
1247 if (NODE_SYNPRETTY & n->flags)
1248 t = print_otag(h, TAG_VAR, "cTss?", "Fa",
1249 "white-space", "nowrap");
1251 t = print_otag(h, TAG_VAR, "cT", "Fa");
1252 print_text(h, n->string);
1255 h->flags |= HTML_NOSPACE;
1260 h->flags |= HTML_NOSPACE;
1264 h->flags |= HTML_NOSPACE;
1272 mdoc_sm_pre(MDOC_ARGS)
1275 if (NULL == n->child)
1276 h->flags ^= HTML_NONOSPACE;
1277 else if (0 == strcmp("on", n->child->string))
1278 h->flags &= ~HTML_NONOSPACE;
1280 h->flags |= HTML_NONOSPACE;
1282 if ( ! (HTML_NONOSPACE & h->flags))
1283 h->flags &= ~HTML_NOSPACE;
1289 mdoc_skip_pre(MDOC_ARGS)
1296 mdoc_pp_pre(MDOC_ARGS)
1304 mdoc_lk_pre(MDOC_ARGS)
1306 const struct roff_node *link, *descr, *punct;
1309 if ((link = n->child) == NULL)
1312 /* Find beginning of trailing punctuation. */
1314 while (punct != link && punct->flags & NODE_DELIMC)
1315 punct = punct->prev;
1316 punct = punct->next;
1318 /* Link target and link text. */
1321 descr = link; /* no text */
1322 t = print_otag(h, TAG_A, "cTh", "Lk", link->string);
1324 if (descr->flags & (NODE_DELIMC | NODE_DELIMO))
1325 h->flags |= HTML_NOSPACE;
1326 print_text(h, descr->string);
1327 descr = descr->next;
1328 } while (descr != punct);
1331 /* Trailing punctuation. */
1332 while (punct != NULL) {
1333 h->flags |= HTML_NOSPACE;
1334 print_text(h, punct->string);
1335 punct = punct->next;
1341 mdoc_mt_pre(MDOC_ARGS)
1346 for (n = n->child; n; n = n->next) {
1347 assert(n->type == ROFFT_TEXT);
1349 mandoc_asprintf(&cp, "mailto:%s", n->string);
1350 t = print_otag(h, TAG_A, "cTh", "Mt", cp);
1351 print_text(h, n->string);
1360 mdoc_fo_pre(MDOC_ARGS)
1364 if (n->type == ROFFT_BODY) {
1365 h->flags |= HTML_NOSPACE;
1367 h->flags |= HTML_NOSPACE;
1369 } else if (n->type == ROFFT_BLOCK) {
1374 if (n->child == NULL)
1377 assert(n->child->string);
1378 t = print_otag(h, TAG_B, "cT", "Fn");
1379 print_text(h, n->child->string);
1385 mdoc_fo_post(MDOC_ARGS)
1388 if (n->type != ROFFT_BODY)
1390 h->flags |= HTML_NOSPACE;
1392 h->flags |= HTML_NOSPACE;
1397 mdoc_in_pre(MDOC_ARGS)
1402 print_otag(h, TAG_B, "cT", "In");
1405 * The first argument of the `In' gets special treatment as
1406 * being a linked value. Subsequent values are printed
1407 * afterward. groff does similarly. This also handles the case
1411 if (NODE_SYNPRETTY & n->flags && NODE_LINE & n->flags)
1412 print_text(h, "#include");
1415 h->flags |= HTML_NOSPACE;
1417 if (NULL != (n = n->child)) {
1418 assert(n->type == ROFFT_TEXT);
1420 if (h->base_includes)
1421 t = print_otag(h, TAG_A, "cThI", "In", n->string);
1423 t = print_otag(h, TAG_A, "cT", "In");
1424 print_text(h, n->string);
1430 h->flags |= HTML_NOSPACE;
1433 for ( ; n; n = n->next) {
1434 assert(n->type == ROFFT_TEXT);
1435 print_text(h, n->string);
1442 mdoc_ic_pre(MDOC_ARGS)
1446 if ((id = cond_id(n)) != NULL)
1447 print_otag(h, TAG_A, "chR", "selflink", id);
1448 print_otag(h, TAG_B, "cTi", "Ic", id);
1454 mdoc_va_pre(MDOC_ARGS)
1456 print_otag(h, TAG_VAR, "cT", "Va");
1461 mdoc_ap_pre(MDOC_ARGS)
1464 h->flags |= HTML_NOSPACE;
1465 print_text(h, "\\(aq");
1466 h->flags |= HTML_NOSPACE;
1471 mdoc_bf_pre(MDOC_ARGS)
1475 if (n->type == ROFFT_HEAD)
1477 else if (n->type != ROFFT_BODY)
1480 if (FONT_Em == n->norm->Bf.font)
1482 else if (FONT_Sy == n->norm->Bf.font)
1484 else if (FONT_Li == n->norm->Bf.font)
1490 * We want this to be inline-formatted, but needs to be div to
1491 * accept block children.
1494 print_otag(h, TAG_DIV, "css?hl", cattr, "display", "inline", 1);
1499 mdoc_ms_pre(MDOC_ARGS)
1503 if ((id = cond_id(n)) != NULL)
1504 print_otag(h, TAG_A, "chR", "selflink", id);
1505 print_otag(h, TAG_B, "cTi", "Ms", id);
1511 mdoc_igndelim_pre(MDOC_ARGS)
1514 h->flags |= HTML_IGNDELIM;
1519 mdoc_pf_post(MDOC_ARGS)
1522 if ( ! (n->next == NULL || n->next->flags & NODE_LINE))
1523 h->flags |= HTML_NOSPACE;
1527 mdoc_rs_pre(MDOC_ARGS)
1529 if (n->type != ROFFT_BLOCK)
1532 if (n->prev && SEC_SEE_ALSO == n->sec)
1535 print_otag(h, TAG_CITE, "cT", "Rs");
1540 mdoc_no_pre(MDOC_ARGS)
1544 if ((id = cond_id(n)) != NULL)
1545 print_otag(h, TAG_A, "chR", "selflink", id);
1546 print_otag(h, TAG_SPAN, "ci", "No", id);
1552 mdoc_li_pre(MDOC_ARGS)
1556 if ((id = cond_id(n)) != NULL)
1557 print_otag(h, TAG_A, "chR", "selflink", id);
1558 print_otag(h, TAG_CODE, "ci", "Li", id);
1564 mdoc_sy_pre(MDOC_ARGS)
1566 print_otag(h, TAG_B, "cT", "Sy");
1571 mdoc_lb_pre(MDOC_ARGS)
1573 if (SEC_LIBRARY == n->sec && NODE_LINE & n->flags && n->prev)
1574 print_otag(h, TAG_BR, "");
1576 print_otag(h, TAG_SPAN, "cT", "Lb");
1581 mdoc__x_pre(MDOC_ARGS)
1591 if (n->prev && MDOC__A == n->prev->tok)
1592 if (NULL == n->next || MDOC__A != n->next->tok)
1593 print_text(h, "and");
1632 print_otag(h, TAG_A, "ch", "RsU", n->child->string);
1641 print_otag(h, t, "c", cattr);
1646 mdoc__x_post(MDOC_ARGS)
1649 if (MDOC__A == n->tok && n->next && MDOC__A == n->next->tok)
1650 if (NULL == n->next->next || MDOC__A != n->next->next->tok)
1651 if (NULL == n->prev || MDOC__A != n->prev->tok)
1656 if (NULL == n->parent || MDOC_Rs != n->parent->tok)
1659 h->flags |= HTML_NOSPACE;
1660 print_text(h, n->next ? "," : ".");
1664 mdoc_bk_pre(MDOC_ARGS)
1673 if (n->parent->args != NULL || n->prev->child == NULL)
1674 h->flags |= HTML_PREKEEP;
1684 mdoc_bk_post(MDOC_ARGS)
1687 if (n->type == ROFFT_BODY)
1688 h->flags &= ~(HTML_KEEP | HTML_PREKEEP);
1692 mdoc_quote_pre(MDOC_ARGS)
1694 if (n->type != ROFFT_BODY)
1700 print_text(h, n->child != NULL && n->child->next == NULL &&
1701 n->child->tok == MDOC_Mt ? "<" : "\\(la");
1705 print_text(h, "\\(lC");
1709 print_text(h, "\\(lB");
1713 print_text(h, "\\(lB");
1714 h->flags |= HTML_NOSPACE;
1715 print_otag(h, TAG_SPAN, "c", "Op");
1718 if (NULL == n->norm->Es ||
1719 NULL == n->norm->Es->child)
1721 print_text(h, n->norm->Es->child->string);
1727 print_text(h, "\\(lq");
1734 print_text(h, "\\(oq");
1735 h->flags |= HTML_NOSPACE;
1736 print_otag(h, TAG_CODE, "c", "Li");
1740 print_text(h, "\\(oq");
1746 h->flags |= HTML_NOSPACE;
1751 mdoc_quote_post(MDOC_ARGS)
1754 if (n->type != ROFFT_BODY && n->type != ROFFT_ELEM)
1757 h->flags |= HTML_NOSPACE;
1762 print_text(h, n->child != NULL && n->child->next == NULL &&
1763 n->child->tok == MDOC_Mt ? ">" : "\\(ra");
1767 print_text(h, "\\(rC");
1773 print_text(h, "\\(rB");
1776 if (n->norm->Es == NULL ||
1777 n->norm->Es->child == NULL ||
1778 n->norm->Es->child->next == NULL)
1779 h->flags &= ~HTML_NOSPACE;
1781 print_text(h, n->norm->Es->child->next->string);
1787 print_text(h, "\\(rq");
1796 print_text(h, "\\(cq");
1804 mdoc_eo_pre(MDOC_ARGS)
1807 if (n->type != ROFFT_BODY)
1810 if (n->end == ENDBODY_NOT &&
1811 n->parent->head->child == NULL &&
1813 n->child->end != ENDBODY_NOT)
1814 print_text(h, "\\&");
1815 else if (n->end != ENDBODY_NOT ? n->child != NULL :
1816 n->parent->head->child != NULL && (n->child != NULL ||
1817 (n->parent->tail != NULL && n->parent->tail->child != NULL)))
1818 h->flags |= HTML_NOSPACE;
1823 mdoc_eo_post(MDOC_ARGS)
1827 if (n->type != ROFFT_BODY)
1830 if (n->end != ENDBODY_NOT) {
1831 h->flags &= ~HTML_NOSPACE;
1835 body = n->child != NULL || n->parent->head->child != NULL;
1836 tail = n->parent->tail != NULL && n->parent->tail->child != NULL;
1839 h->flags |= HTML_NOSPACE;
1841 h->flags &= ~HTML_NOSPACE;