]>
git.cameronkatri.com Git - mandoc.git/blob - man_html.c
1 /* $Id: man_html.c,v 1.99 2014/09/27 09:05:57 kristaps 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.
20 #include <sys/types.h>
29 #include "mandoc_aux.h"
35 /* TODO: preserve ident widths. */
36 /* FIXME: have PD set the default vspace width. */
40 #define MAN_ARGS const struct man_meta *man, \
41 const struct man_node *n, \
47 #define MANH_LITERAL (1 << 0) /* literal context */
52 int (*post
)(MAN_ARGS
);
55 static void print_bvspace(struct html
*,
56 const struct man_node
*);
57 static void print_man(MAN_ARGS
);
58 static void print_man_head(MAN_ARGS
);
59 static void print_man_nodelist(MAN_ARGS
);
60 static void print_man_node(MAN_ARGS
);
61 static int a2width(const struct man_node
*,
63 static int man_B_pre(MAN_ARGS
);
64 static int man_HP_pre(MAN_ARGS
);
65 static int man_IP_pre(MAN_ARGS
);
66 static int man_I_pre(MAN_ARGS
);
67 static int man_OP_pre(MAN_ARGS
);
68 static int man_PP_pre(MAN_ARGS
);
69 static int man_RS_pre(MAN_ARGS
);
70 static int man_SH_pre(MAN_ARGS
);
71 static int man_SM_pre(MAN_ARGS
);
72 static int man_SS_pre(MAN_ARGS
);
73 static int man_UR_pre(MAN_ARGS
);
74 static int man_alt_pre(MAN_ARGS
);
75 static int man_br_pre(MAN_ARGS
);
76 static int man_ign_pre(MAN_ARGS
);
77 static int man_in_pre(MAN_ARGS
);
78 static int man_literal_pre(MAN_ARGS
);
79 static void man_root_post(MAN_ARGS
);
80 static void man_root_pre(MAN_ARGS
);
82 static const struct htmlman mans
[MAN_MAX
] = {
83 { man_br_pre
, NULL
}, /* br */
84 { NULL
, NULL
}, /* TH */
85 { man_SH_pre
, NULL
}, /* SH */
86 { man_SS_pre
, NULL
}, /* SS */
87 { man_IP_pre
, NULL
}, /* TP */
88 { man_PP_pre
, NULL
}, /* LP */
89 { man_PP_pre
, NULL
}, /* PP */
90 { man_PP_pre
, NULL
}, /* P */
91 { man_IP_pre
, NULL
}, /* IP */
92 { man_HP_pre
, NULL
}, /* HP */
93 { man_SM_pre
, NULL
}, /* SM */
94 { man_SM_pre
, NULL
}, /* SB */
95 { man_alt_pre
, NULL
}, /* BI */
96 { man_alt_pre
, NULL
}, /* IB */
97 { man_alt_pre
, NULL
}, /* BR */
98 { man_alt_pre
, NULL
}, /* RB */
99 { NULL
, NULL
}, /* R */
100 { man_B_pre
, NULL
}, /* B */
101 { man_I_pre
, NULL
}, /* I */
102 { man_alt_pre
, NULL
}, /* IR */
103 { man_alt_pre
, NULL
}, /* RI */
104 { man_ign_pre
, NULL
}, /* na */
105 { man_br_pre
, NULL
}, /* sp */
106 { man_literal_pre
, NULL
}, /* nf */
107 { man_literal_pre
, NULL
}, /* fi */
108 { NULL
, NULL
}, /* RE */
109 { man_RS_pre
, NULL
}, /* RS */
110 { man_ign_pre
, NULL
}, /* DT */
111 { man_ign_pre
, NULL
}, /* UC */
112 { man_ign_pre
, NULL
}, /* PD */
113 { man_ign_pre
, NULL
}, /* AT */
114 { man_in_pre
, NULL
}, /* in */
115 { man_ign_pre
, NULL
}, /* ft */
116 { man_OP_pre
, NULL
}, /* OP */
117 { man_literal_pre
, NULL
}, /* EX */
118 { man_literal_pre
, NULL
}, /* EE */
119 { man_UR_pre
, NULL
}, /* UR */
120 { NULL
, NULL
}, /* UE */
121 { 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
);
182 print_man_head(MAN_ARGS
)
188 bufcat_fmt(h
, "%s(%s)", man
->title
, man
->msec
);
189 print_otag(h
, TAG_TITLE
, 0, NULL
);
190 print_text(h
, h
->buf
);
194 print_man_nodelist(MAN_ARGS
)
197 print_man_node(man
, n
, mh
, h
);
199 print_man_nodelist(man
, n
->next
, mh
, h
);
203 print_man_node(MAN_ARGS
)
213 man_root_pre(man
, n
, mh
, h
);
217 * If we have a blank line, output a vertical space.
218 * If we have a space as the first character, break
219 * before printing the line's data.
221 if ('\0' == *n
->string
) {
222 print_otag(h
, TAG_P
, 0, NULL
);
226 if (' ' == *n
->string
&& MAN_LINE
& n
->flags
)
227 print_otag(h
, TAG_BR
, 0, NULL
);
228 else if (MANH_LITERAL
& mh
->fl
&& n
->prev
)
229 print_otag(h
, TAG_BR
, 0, NULL
);
231 print_text(h
, n
->string
);
234 print_eqn(h
, n
->eqn
);
238 * This will take care of initialising all of the table
239 * state data for the first table, then tearing it down
242 print_tbl(h
, n
->span
);
246 * Close out scope of font prior to opening a macro
249 if (HTMLFONT_NONE
!= h
->metac
) {
251 h
->metac
= HTMLFONT_NONE
;
255 * Close out the current table, if it's open, and unset
256 * the "meta" table state. This will be reopened on the
257 * next table element.
263 if (mans
[n
->tok
].pre
)
264 child
= (*mans
[n
->tok
].pre
)(man
, n
, mh
, h
);
268 if (child
&& n
->child
)
269 print_man_nodelist(man
, n
->child
, mh
, h
);
271 /* This will automatically close out any font scope. */
276 man_root_post(man
, n
, mh
, h
);
281 if (mans
[n
->tok
].post
)
282 (*mans
[n
->tok
].post
)(man
, n
, mh
, h
);
288 a2width(const struct man_node
*n
, struct roffsu
*su
)
291 if (MAN_TEXT
!= n
->type
)
293 if (a2roffsu(n
->string
, su
, SCALE_BU
))
300 man_root_pre(MAN_ARGS
)
302 struct htmlpair tag
[2];
308 mandoc_asprintf(&title
, "%s(%s)", man
->title
, man
->msec
);
310 PAIR_CLASS_INIT(&tag
[0], "head");
311 t
= print_otag(h
, TAG_TABLE
, 1, tag
);
312 PAIR_INIT(&tag
[0], ATTR_WIDTH
, "30%");
313 print_otag(h
, TAG_COL
, 1, tag
);
314 print_otag(h
, TAG_COL
, 1, tag
);
315 print_otag(h
, TAG_COL
, 1, tag
);
317 print_otag(h
, TAG_TBODY
, 0, NULL
);
319 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
321 PAIR_CLASS_INIT(&tag
[0], "head-ltitle");
322 print_otag(h
, TAG_TD
, 1, tag
);
323 print_text(h
, title
);
326 PAIR_CLASS_INIT(&tag
[0], "head-vol");
327 PAIR_INIT(&tag
[1], ATTR_ALIGN
, "center");
328 print_otag(h
, TAG_TD
, 2, tag
);
329 if (NULL
!= man
->vol
)
330 print_text(h
, man
->vol
);
333 PAIR_CLASS_INIT(&tag
[0], "head-rtitle");
334 PAIR_INIT(&tag
[1], ATTR_ALIGN
, "right");
335 print_otag(h
, TAG_TD
, 2, tag
);
336 print_text(h
, title
);
342 man_root_post(MAN_ARGS
)
344 struct htmlpair tag
[2];
347 PAIR_CLASS_INIT(&tag
[0], "foot");
348 t
= print_otag(h
, TAG_TABLE
, 1, tag
);
349 PAIR_INIT(&tag
[0], ATTR_WIDTH
, "50%");
350 print_otag(h
, TAG_COL
, 1, tag
);
351 print_otag(h
, TAG_COL
, 1, tag
);
353 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
355 PAIR_CLASS_INIT(&tag
[0], "foot-date");
356 print_otag(h
, TAG_TD
, 1, tag
);
359 print_text(h
, man
->date
);
362 PAIR_CLASS_INIT(&tag
[0], "foot-os");
363 PAIR_INIT(&tag
[1], ATTR_ALIGN
, "right");
364 print_otag(h
, TAG_TD
, 2, tag
);
367 print_text(h
, man
->source
);
378 SCALE_VS_INIT(&su
, 1);
380 if (MAN_sp
== n
->tok
) {
381 if (NULL
!= (n
= n
->child
))
382 if ( ! a2roffsu(n
->string
, &su
, SCALE_VS
))
383 SCALE_VS_INIT(&su
, atoi(n
->string
));
388 bufcat_su(h
, "height", &su
);
389 PAIR_STYLE_INIT(&tag
, h
);
390 print_otag(h
, TAG_DIV
, 1, &tag
);
392 /* So the div isn't empty: */
393 print_text(h
, "\\~");
403 if (MAN_BLOCK
== n
->type
) {
404 mh
->fl
&= ~MANH_LITERAL
;
405 PAIR_CLASS_INIT(&tag
, "section");
406 print_otag(h
, TAG_DIV
, 1, &tag
);
408 } else if (MAN_BODY
== n
->type
)
411 print_otag(h
, TAG_H1
, 0, NULL
);
416 man_alt_pre(MAN_ARGS
)
418 const struct man_node
*nn
;
423 if ((savelit
= mh
->fl
& MANH_LITERAL
))
424 print_otag(h
, TAG_BR
, 0, NULL
);
426 mh
->fl
&= ~MANH_LITERAL
;
428 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
432 fp
= i
% 2 ? TAG_I
: TAG_B
;
435 fp
= i
% 2 ? TAG_B
: TAG_I
;
438 fp
= i
% 2 ? TAG_I
: TAG_MAX
;
441 fp
= i
% 2 ? TAG_MAX
: TAG_I
;
444 fp
= i
% 2 ? TAG_MAX
: TAG_B
;
447 fp
= i
% 2 ? TAG_B
: TAG_MAX
;
455 h
->flags
|= HTML_NOSPACE
;
458 t
= print_otag(h
, fp
, 0, NULL
);
460 print_man_node(man
, nn
, mh
, h
);
467 mh
->fl
|= MANH_LITERAL
;
476 print_otag(h
, TAG_SMALL
, 0, NULL
);
477 if (MAN_SB
== n
->tok
)
478 print_otag(h
, TAG_B
, 0, NULL
);
487 if (MAN_BLOCK
== n
->type
) {
488 mh
->fl
&= ~MANH_LITERAL
;
489 PAIR_CLASS_INIT(&tag
, "subsection");
490 print_otag(h
, TAG_DIV
, 1, &tag
);
492 } else if (MAN_BODY
== n
->type
)
495 print_otag(h
, TAG_H2
, 0, NULL
);
503 if (MAN_HEAD
== n
->type
)
505 else if (MAN_BLOCK
== n
->type
)
514 const struct man_node
*nn
;
516 if (MAN_BODY
== n
->type
) {
517 print_otag(h
, TAG_DD
, 0, NULL
);
519 } else if (MAN_HEAD
!= n
->type
) {
520 print_otag(h
, TAG_DL
, 0, NULL
);
524 /* FIXME: width specification. */
526 print_otag(h
, TAG_DT
, 0, NULL
);
528 /* For IP, only print the first header element. */
530 if (MAN_IP
== n
->tok
&& n
->child
)
531 print_man_node(man
, n
->child
, mh
, h
);
533 /* For TP, only print next-line header elements. */
535 if (MAN_TP
== n
->tok
) {
537 while (NULL
!= nn
&& 0 == (MAN_LINE
& nn
->flags
))
540 print_man_node(man
, nn
, mh
, h
);
553 const struct man_node
*np
;
555 if (MAN_HEAD
== n
->type
)
557 else if (MAN_BLOCK
!= n
->type
)
562 if (NULL
== np
|| ! a2width(np
, &su
))
563 SCALE_HS_INIT(&su
, INDENT
);
568 bufcat_su(h
, "margin-left", &su
);
569 su
.scale
= -su
.scale
;
570 bufcat_su(h
, "text-indent", &su
);
571 PAIR_STYLE_INIT(&tag
, h
);
572 print_otag(h
, TAG_P
, 1, &tag
);
583 h
->flags
|= HTML_NOSPACE
;
584 PAIR_CLASS_INIT(&tag
, "opt");
585 tt
= print_otag(h
, TAG_SPAN
, 1, &tag
);
587 if (NULL
!= (n
= n
->child
)) {
588 print_otag(h
, TAG_B
, 0, NULL
);
589 print_text(h
, n
->string
);
594 if (NULL
!= n
&& NULL
!= n
->next
) {
595 print_otag(h
, TAG_I
, 0, NULL
);
596 print_text(h
, n
->next
->string
);
600 h
->flags
|= HTML_NOSPACE
;
609 print_otag(h
, TAG_B
, 0, NULL
);
617 print_otag(h
, TAG_I
, 0, NULL
);
622 man_literal_pre(MAN_ARGS
)
625 if (MAN_fi
== n
->tok
|| MAN_EE
== n
->tok
) {
626 print_otag(h
, TAG_BR
, 0, NULL
);
627 mh
->fl
&= ~MANH_LITERAL
;
629 mh
->fl
|= MANH_LITERAL
;
638 print_otag(h
, TAG_BR
, 0, NULL
);
643 man_ign_pre(MAN_ARGS
)
655 if (MAN_HEAD
== n
->type
)
657 else if (MAN_BODY
== n
->type
)
660 SCALE_HS_INIT(&su
, INDENT
);
662 a2width(n
->head
->child
, &su
);
665 bufcat_su(h
, "margin-left", &su
);
666 PAIR_STYLE_INIT(&tag
, h
);
667 print_otag(h
, TAG_DIV
, 1, &tag
);
674 struct htmlpair tag
[2];
677 assert(MAN_HEAD
== n
->type
);
679 assert(MAN_TEXT
== n
->child
->type
);
680 PAIR_CLASS_INIT(&tag
[0], "link-ext");
681 PAIR_HREF_INIT(&tag
[1], n
->child
->string
);
682 print_otag(h
, TAG_A
, 2, tag
);
685 assert(MAN_BODY
== n
->next
->type
);
689 print_man_nodelist(man
, n
->child
, mh
, h
);