]>
git.cameronkatri.com Git - mandoc.git/blob - man_html.c
1 /* $Id: man_html.c,v 1.92 2014/03/30 19:47:48 schwarze Exp $ */
3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2013, 2014 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 AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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.
22 #include <sys/types.h>
36 /* TODO: preserve ident widths. */
37 /* FIXME: have PD set the default vspace width. */
41 #define MAN_ARGS const struct man_meta *man, \
42 const struct man_node *n, \
48 #define MANH_LITERAL (1 << 0) /* literal context */
53 int (*post
)(MAN_ARGS
);
56 static void print_bvspace(struct html
*,
57 const struct man_node
*);
58 static void print_man(MAN_ARGS
);
59 static void print_man_head(MAN_ARGS
);
60 static void print_man_nodelist(MAN_ARGS
);
61 static void print_man_node(MAN_ARGS
);
62 static int a2width(const struct man_node
*,
64 static int man_B_pre(MAN_ARGS
);
65 static int man_HP_pre(MAN_ARGS
);
66 static int man_IP_pre(MAN_ARGS
);
67 static int man_I_pre(MAN_ARGS
);
68 static int man_OP_pre(MAN_ARGS
);
69 static int man_PP_pre(MAN_ARGS
);
70 static int man_RS_pre(MAN_ARGS
);
71 static int man_SH_pre(MAN_ARGS
);
72 static int man_SM_pre(MAN_ARGS
);
73 static int man_SS_pre(MAN_ARGS
);
74 static int man_UR_pre(MAN_ARGS
);
75 static int man_alt_pre(MAN_ARGS
);
76 static int man_br_pre(MAN_ARGS
);
77 static int man_ign_pre(MAN_ARGS
);
78 static int man_in_pre(MAN_ARGS
);
79 static int man_literal_pre(MAN_ARGS
);
80 static void man_root_post(MAN_ARGS
);
81 static void man_root_pre(MAN_ARGS
);
83 static const struct htmlman mans
[MAN_MAX
] = {
84 { man_br_pre
, NULL
}, /* br */
85 { NULL
, NULL
}, /* TH */
86 { man_SH_pre
, NULL
}, /* SH */
87 { man_SS_pre
, NULL
}, /* SS */
88 { man_IP_pre
, NULL
}, /* TP */
89 { man_PP_pre
, NULL
}, /* LP */
90 { man_PP_pre
, NULL
}, /* PP */
91 { man_PP_pre
, NULL
}, /* P */
92 { man_IP_pre
, NULL
}, /* IP */
93 { man_HP_pre
, NULL
}, /* HP */
94 { man_SM_pre
, NULL
}, /* SM */
95 { man_SM_pre
, NULL
}, /* SB */
96 { man_alt_pre
, NULL
}, /* BI */
97 { man_alt_pre
, NULL
}, /* IB */
98 { man_alt_pre
, NULL
}, /* BR */
99 { man_alt_pre
, NULL
}, /* RB */
100 { NULL
, NULL
}, /* R */
101 { man_B_pre
, NULL
}, /* B */
102 { man_I_pre
, NULL
}, /* I */
103 { man_alt_pre
, NULL
}, /* IR */
104 { man_alt_pre
, NULL
}, /* RI */
105 { man_ign_pre
, NULL
}, /* na */
106 { man_br_pre
, NULL
}, /* sp */
107 { man_literal_pre
, NULL
}, /* nf */
108 { man_literal_pre
, NULL
}, /* fi */
109 { NULL
, NULL
}, /* RE */
110 { man_RS_pre
, NULL
}, /* RS */
111 { man_ign_pre
, NULL
}, /* DT */
112 { man_ign_pre
, NULL
}, /* UC */
113 { man_ign_pre
, NULL
}, /* PD */
114 { man_ign_pre
, NULL
}, /* AT */
115 { man_in_pre
, NULL
}, /* in */
116 { man_ign_pre
, NULL
}, /* ft */
117 { man_OP_pre
, NULL
}, /* OP */
118 { man_literal_pre
, NULL
}, /* EX */
119 { man_literal_pre
, NULL
}, /* EE */
120 { man_UR_pre
, NULL
}, /* UR */
121 { NULL
, NULL
}, /* UE */
122 { man_ign_pre
, NULL
}, /* ll */
126 * Printing leading vertical space before a block.
127 * This is used for the paragraph macros.
128 * The rules are pretty simple, since there's very little nesting going
129 * on here. Basically, if we're the first within another block (SS/SH),
130 * then don't emit vertical space. If we are (RS), then do. If not the
134 print_bvspace(struct html
*h
, const struct man_node
*n
)
137 if (n
->body
&& n
->body
->child
)
138 if (MAN_TBL
== n
->body
->child
->type
)
141 if (MAN_ROOT
== n
->parent
->type
|| MAN_RS
!= n
->parent
->tok
)
145 print_otag(h
, TAG_P
, 0, NULL
);
149 html_man(void *arg
, const struct man
*man
)
153 memset(&mh
, 0, sizeof(struct mhtml
));
154 print_man(man_meta(man
), man_node(man
), &mh
, (struct html
*)arg
);
164 PAIR_CLASS_INIT(&tag
, "mandoc");
166 if ( ! (HTML_FRAGMENT
& h
->oflags
)) {
168 t
= print_otag(h
, TAG_HTML
, 0, NULL
);
169 tt
= print_otag(h
, TAG_HEAD
, 0, NULL
);
170 print_man_head(man
, n
, mh
, h
);
172 print_otag(h
, TAG_BODY
, 0, NULL
);
173 print_otag(h
, TAG_DIV
, 1, &tag
);
175 t
= print_otag(h
, TAG_DIV
, 1, &tag
);
177 print_man_nodelist(man
, n
, mh
, h
);
184 print_man_head(MAN_ARGS
)
190 bufcat_fmt(h
, "%s(%s)", man
->title
, man
->msec
);
191 print_otag(h
, TAG_TITLE
, 0, NULL
);
192 print_text(h
, h
->buf
);
197 print_man_nodelist(MAN_ARGS
)
200 print_man_node(man
, n
, mh
, h
);
202 print_man_nodelist(man
, n
->next
, mh
, h
);
207 print_man_node(MAN_ARGS
)
217 man_root_pre(man
, n
, mh
, h
);
221 * If we have a blank line, output a vertical space.
222 * If we have a space as the first character, break
223 * before printing the line's data.
225 if ('\0' == *n
->string
) {
226 print_otag(h
, TAG_P
, 0, NULL
);
230 if (' ' == *n
->string
&& MAN_LINE
& n
->flags
)
231 print_otag(h
, TAG_BR
, 0, NULL
);
232 else if (MANH_LITERAL
& mh
->fl
&& n
->prev
)
233 print_otag(h
, TAG_BR
, 0, NULL
);
235 print_text(h
, n
->string
);
238 print_eqn(h
, n
->eqn
);
242 * This will take care of initialising all of the table
243 * state data for the first table, then tearing it down
246 print_tbl(h
, n
->span
);
250 * Close out scope of font prior to opening a macro
253 if (HTMLFONT_NONE
!= h
->metac
) {
255 h
->metac
= HTMLFONT_NONE
;
259 * Close out the current table, if it's open, and unset
260 * the "meta" table state. This will be reopened on the
261 * next table element.
267 if (mans
[n
->tok
].pre
)
268 child
= (*mans
[n
->tok
].pre
)(man
, n
, mh
, h
);
272 if (child
&& n
->child
)
273 print_man_nodelist(man
, n
->child
, mh
, h
);
275 /* This will automatically close out any font scope. */
280 man_root_post(man
, n
, mh
, h
);
285 if (mans
[n
->tok
].post
)
286 (*mans
[n
->tok
].post
)(man
, n
, mh
, h
);
293 a2width(const struct man_node
*n
, struct roffsu
*su
)
296 if (MAN_TEXT
!= n
->type
)
298 if (a2roffsu(n
->string
, su
, SCALE_BU
))
307 man_root_pre(MAN_ARGS
)
309 struct htmlpair tag
[3];
311 char b
[BUFSIZ
], title
[BUFSIZ
];
315 (void)strlcat(b
, man
->vol
, BUFSIZ
);
319 snprintf(title
, BUFSIZ
- 1, "%s(%s)", man
->title
, man
->msec
);
321 PAIR_SUMMARY_INIT(&tag
[0], "Document Header");
322 PAIR_CLASS_INIT(&tag
[1], "head");
323 PAIR_INIT(&tag
[2], ATTR_WIDTH
, "100%");
324 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
325 PAIR_INIT(&tag
[0], ATTR_WIDTH
, "30%");
326 print_otag(h
, TAG_COL
, 1, tag
);
327 print_otag(h
, TAG_COL
, 1, tag
);
328 print_otag(h
, TAG_COL
, 1, tag
);
330 print_otag(h
, TAG_TBODY
, 0, NULL
);
332 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
334 PAIR_CLASS_INIT(&tag
[0], "head-ltitle");
335 print_otag(h
, TAG_TD
, 1, tag
);
336 print_text(h
, title
);
339 PAIR_CLASS_INIT(&tag
[0], "head-vol");
340 PAIR_INIT(&tag
[1], ATTR_ALIGN
, "center");
341 print_otag(h
, TAG_TD
, 2, tag
);
345 PAIR_CLASS_INIT(&tag
[0], "head-rtitle");
346 PAIR_INIT(&tag
[1], ATTR_ALIGN
, "right");
347 print_otag(h
, TAG_TD
, 2, tag
);
348 print_text(h
, title
);
355 man_root_post(MAN_ARGS
)
357 struct htmlpair tag
[3];
360 PAIR_SUMMARY_INIT(&tag
[0], "Document Footer");
361 PAIR_CLASS_INIT(&tag
[1], "foot");
362 PAIR_INIT(&tag
[2], ATTR_WIDTH
, "100%");
363 t
= print_otag(h
, TAG_TABLE
, 3, tag
);
364 PAIR_INIT(&tag
[0], ATTR_WIDTH
, "50%");
365 print_otag(h
, TAG_COL
, 1, tag
);
366 print_otag(h
, TAG_COL
, 1, tag
);
368 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
370 PAIR_CLASS_INIT(&tag
[0], "foot-date");
371 print_otag(h
, TAG_TD
, 1, tag
);
374 print_text(h
, man
->date
);
377 PAIR_CLASS_INIT(&tag
[0], "foot-os");
378 PAIR_INIT(&tag
[1], ATTR_ALIGN
, "right");
379 print_otag(h
, TAG_TD
, 2, tag
);
382 print_text(h
, man
->source
);
394 SCALE_VS_INIT(&su
, 1);
396 if (MAN_sp
== n
->tok
) {
397 if (NULL
!= (n
= n
->child
))
398 if ( ! a2roffsu(n
->string
, &su
, SCALE_VS
))
399 SCALE_VS_INIT(&su
, atoi(n
->string
));
404 bufcat_su(h
, "height", &su
);
405 PAIR_STYLE_INIT(&tag
, h
);
406 print_otag(h
, TAG_DIV
, 1, &tag
);
408 /* So the div isn't empty: */
409 print_text(h
, "\\~");
420 if (MAN_BLOCK
== n
->type
) {
421 mh
->fl
&= ~MANH_LITERAL
;
422 PAIR_CLASS_INIT(&tag
, "section");
423 print_otag(h
, TAG_DIV
, 1, &tag
);
425 } else if (MAN_BODY
== n
->type
)
428 print_otag(h
, TAG_H1
, 0, NULL
);
434 man_alt_pre(MAN_ARGS
)
436 const struct man_node
*nn
;
441 if ((savelit
= mh
->fl
& MANH_LITERAL
))
442 print_otag(h
, TAG_BR
, 0, NULL
);
444 mh
->fl
&= ~MANH_LITERAL
;
446 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
450 fp
= i
% 2 ? TAG_I
: TAG_B
;
453 fp
= i
% 2 ? TAG_B
: TAG_I
;
456 fp
= i
% 2 ? TAG_I
: TAG_MAX
;
459 fp
= i
% 2 ? TAG_MAX
: TAG_I
;
462 fp
= i
% 2 ? TAG_MAX
: TAG_B
;
465 fp
= i
% 2 ? TAG_B
: TAG_MAX
;
473 h
->flags
|= HTML_NOSPACE
;
476 t
= print_otag(h
, fp
, 0, NULL
);
478 print_man_node(man
, nn
, mh
, h
);
485 mh
->fl
|= MANH_LITERAL
;
495 print_otag(h
, TAG_SMALL
, 0, NULL
);
496 if (MAN_SB
== n
->tok
)
497 print_otag(h
, TAG_B
, 0, NULL
);
507 if (MAN_BLOCK
== n
->type
) {
508 mh
->fl
&= ~MANH_LITERAL
;
509 PAIR_CLASS_INIT(&tag
, "subsection");
510 print_otag(h
, TAG_DIV
, 1, &tag
);
512 } else if (MAN_BODY
== n
->type
)
515 print_otag(h
, TAG_H2
, 0, NULL
);
524 if (MAN_HEAD
== n
->type
)
526 else if (MAN_BLOCK
== n
->type
)
536 const struct man_node
*nn
;
538 if (MAN_BODY
== n
->type
) {
539 print_otag(h
, TAG_DD
, 0, NULL
);
541 } else if (MAN_HEAD
!= n
->type
) {
542 print_otag(h
, TAG_DL
, 0, NULL
);
546 /* FIXME: width specification. */
548 print_otag(h
, TAG_DT
, 0, NULL
);
550 /* For IP, only print the first header element. */
552 if (MAN_IP
== n
->tok
&& n
->child
)
553 print_man_node(man
, n
->child
, mh
, h
);
555 /* For TP, only print next-line header elements. */
557 if (MAN_TP
== n
->tok
) {
559 while (NULL
!= nn
&& 0 == (MAN_LINE
& nn
->flags
))
562 print_man_node(man
, nn
, mh
, h
);
576 const struct man_node
*np
;
578 if (MAN_HEAD
== n
->type
)
580 else if (MAN_BLOCK
!= n
->type
)
585 if (NULL
== np
|| ! a2width(np
, &su
))
586 SCALE_HS_INIT(&su
, INDENT
);
591 bufcat_su(h
, "margin-left", &su
);
592 su
.scale
= -su
.scale
;
593 bufcat_su(h
, "text-indent", &su
);
594 PAIR_STYLE_INIT(&tag
, h
);
595 print_otag(h
, TAG_P
, 1, &tag
);
607 h
->flags
|= HTML_NOSPACE
;
608 PAIR_CLASS_INIT(&tag
, "opt");
609 tt
= print_otag(h
, TAG_SPAN
, 1, &tag
);
611 if (NULL
!= (n
= n
->child
)) {
612 print_otag(h
, TAG_B
, 0, NULL
);
613 print_text(h
, n
->string
);
618 if (NULL
!= n
&& NULL
!= n
->next
) {
619 print_otag(h
, TAG_I
, 0, NULL
);
620 print_text(h
, n
->next
->string
);
624 h
->flags
|= HTML_NOSPACE
;
635 print_otag(h
, TAG_B
, 0, NULL
);
644 print_otag(h
, TAG_I
, 0, NULL
);
650 man_literal_pre(MAN_ARGS
)
653 if (MAN_fi
== n
->tok
|| MAN_EE
== n
->tok
) {
654 print_otag(h
, TAG_BR
, 0, NULL
);
655 mh
->fl
&= ~MANH_LITERAL
;
657 mh
->fl
|= MANH_LITERAL
;
667 print_otag(h
, TAG_BR
, 0, NULL
);
673 man_ign_pre(MAN_ARGS
)
686 if (MAN_HEAD
== n
->type
)
688 else if (MAN_BODY
== n
->type
)
691 SCALE_HS_INIT(&su
, INDENT
);
693 a2width(n
->head
->child
, &su
);
696 bufcat_su(h
, "margin-left", &su
);
697 PAIR_STYLE_INIT(&tag
, h
);
698 print_otag(h
, TAG_DIV
, 1, &tag
);
706 struct htmlpair tag
[2];
709 assert(MAN_HEAD
== n
->type
);
711 assert(MAN_TEXT
== n
->child
->type
);
712 PAIR_CLASS_INIT(&tag
[0], "link-ext");
713 PAIR_HREF_INIT(&tag
[1], n
->child
->string
);
714 print_otag(h
, TAG_A
, 2, tag
);
717 assert(MAN_BODY
== n
->next
->type
);
721 print_man_nodelist(man
, n
->child
, mh
, h
);