]>
git.cameronkatri.com Git - mandoc.git/blob - man_html.c
1 /* $Id: man_html.c,v 1.149 2018/05/08 21:42:34 schwarze Exp $ */
3 * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2013,2014,2015,2017,2018 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>
28 #include "mandoc_aux.h"
36 /* FIXME: have PD set the default vspace width. */
40 #define MAN_ARGS const struct roff_meta *man, \
41 const struct roff_node *n, \
46 int (*post
)(MAN_ARGS
);
49 static void print_bvspace(struct html
*,
50 const struct roff_node
*);
51 static void print_man_head(const struct roff_meta
*,
53 static void print_man_nodelist(MAN_ARGS
);
54 static void print_man_node(MAN_ARGS
);
55 static int fillmode(struct html
*, int);
56 static int a2width(const struct roff_node
*,
58 static int man_B_pre(MAN_ARGS
);
59 static int man_HP_pre(MAN_ARGS
);
60 static int man_IP_pre(MAN_ARGS
);
61 static int man_I_pre(MAN_ARGS
);
62 static int man_OP_pre(MAN_ARGS
);
63 static int man_PP_pre(MAN_ARGS
);
64 static int man_RS_pre(MAN_ARGS
);
65 static int man_SH_pre(MAN_ARGS
);
66 static int man_SM_pre(MAN_ARGS
);
67 static int man_SS_pre(MAN_ARGS
);
68 static int man_UR_pre(MAN_ARGS
);
69 static int man_alt_pre(MAN_ARGS
);
70 static int man_ign_pre(MAN_ARGS
);
71 static int man_in_pre(MAN_ARGS
);
72 static void man_root_post(const struct roff_meta
*,
74 static void man_root_pre(const struct roff_meta
*,
77 static const struct htmlman __mans
[MAN_MAX
- MAN_TH
] = {
78 { NULL
, NULL
}, /* TH */
79 { man_SH_pre
, NULL
}, /* SH */
80 { man_SS_pre
, NULL
}, /* SS */
81 { man_IP_pre
, NULL
}, /* TP */
82 { man_PP_pre
, NULL
}, /* LP */
83 { man_PP_pre
, NULL
}, /* PP */
84 { man_PP_pre
, NULL
}, /* P */
85 { man_IP_pre
, NULL
}, /* IP */
86 { man_HP_pre
, NULL
}, /* HP */
87 { man_SM_pre
, NULL
}, /* SM */
88 { man_SM_pre
, NULL
}, /* SB */
89 { man_alt_pre
, NULL
}, /* BI */
90 { man_alt_pre
, NULL
}, /* IB */
91 { man_alt_pre
, NULL
}, /* BR */
92 { man_alt_pre
, NULL
}, /* RB */
93 { NULL
, NULL
}, /* R */
94 { man_B_pre
, NULL
}, /* B */
95 { man_I_pre
, NULL
}, /* I */
96 { man_alt_pre
, NULL
}, /* IR */
97 { man_alt_pre
, NULL
}, /* RI */
98 { NULL
, NULL
}, /* nf */
99 { NULL
, NULL
}, /* fi */
100 { NULL
, NULL
}, /* RE */
101 { man_RS_pre
, NULL
}, /* RS */
102 { man_ign_pre
, NULL
}, /* DT */
103 { man_ign_pre
, NULL
}, /* UC */
104 { man_ign_pre
, NULL
}, /* PD */
105 { man_ign_pre
, NULL
}, /* AT */
106 { man_in_pre
, NULL
}, /* in */
107 { man_OP_pre
, NULL
}, /* OP */
108 { NULL
, NULL
}, /* EX */
109 { NULL
, NULL
}, /* EE */
110 { man_UR_pre
, NULL
}, /* UR */
111 { NULL
, NULL
}, /* UE */
112 { man_UR_pre
, NULL
}, /* MT */
113 { NULL
, NULL
}, /* ME */
115 static const struct htmlman
*const mans
= __mans
- MAN_TH
;
119 * Printing leading vertical space before a block.
120 * This is used for the paragraph macros.
121 * The rules are pretty simple, since there's very little nesting going
122 * on here. Basically, if we're the first within another block (SS/SH),
123 * then don't emit vertical space. If we are (RS), then do. If not the
127 print_bvspace(struct html
*h
, const struct roff_node
*n
)
130 if (n
->body
&& n
->body
->child
)
131 if (n
->body
->child
->type
== ROFFT_TBL
)
134 if (n
->parent
->type
== ROFFT_ROOT
|| n
->parent
->tok
!= MAN_RS
)
142 html_man(void *arg
, const struct roff_man
*man
)
148 h
= (struct html
*)arg
;
149 n
= man
->first
->child
;
151 if ((h
->oflags
& HTML_FRAGMENT
) == 0) {
153 print_otag(h
, TAG_HTML
, "");
154 if (n
->type
== ROFFT_COMMENT
)
155 print_gen_comment(h
, n
);
156 t
= print_otag(h
, TAG_HEAD
, "");
157 print_man_head(&man
->meta
, h
);
159 print_otag(h
, TAG_BODY
, "");
162 man_root_pre(&man
->meta
, h
);
163 t
= print_otag(h
, TAG_DIV
, "c", "manual-text");
164 print_man_nodelist(&man
->meta
, n
, h
);
166 man_root_post(&man
->meta
, h
);
171 print_man_head(const struct roff_meta
*man
, struct html
*h
)
176 mandoc_asprintf(&cp
, "%s(%s)", man
->title
, man
->msec
);
177 print_otag(h
, TAG_TITLE
, "");
183 print_man_nodelist(MAN_ARGS
)
187 print_man_node(man
, n
, h
);
193 print_man_node(MAN_ARGS
)
195 static int want_fillmode
= MAN_fi
;
196 static int save_fillmode
;
202 * Handle fill mode switch requests up front,
203 * they would just cause trouble in the subsequent code.
209 want_fillmode
= MAN_nf
;
213 want_fillmode
= MAN_fi
;
214 if (fillmode(h
, 0) == MAN_fi
)
215 print_otag(h
, TAG_BR
, "");
221 /* Set up fill mode for the upcoming node. */
226 /* Some block macros suspend or cancel .nf. */
228 case MAN_TP
: /* Tagged paragraphs */
229 case MAN_IP
: /* temporarily disable .nf */
230 case MAN_HP
: /* for the head. */
231 save_fillmode
= want_fillmode
;
233 case MAN_SH
: /* Section headers */
234 case MAN_SS
: /* permanently cancel .nf. */
235 want_fillmode
= MAN_fi
;
237 case MAN_PP
: /* These have no head. */
238 case MAN_LP
: /* They will simply */
239 case MAN_P
: /* reopen .nf in the body. */
254 * Some in-line macros produce tags and/or text
255 * in the handler, so they require fill mode to be
256 * configured up front just like for text nodes.
257 * For the others, keep the traditional approach
258 * of doing the same, for now.
260 fillmode(h
, want_fillmode
);
263 if (fillmode(h
, want_fillmode
) == MAN_fi
&&
264 want_fillmode
== MAN_fi
&&
265 n
->flags
& NODE_LINE
&& *n
->string
== ' ' &&
266 (h
->flags
& HTML_NONEWLINE
) == 0)
267 print_otag(h
, TAG_BR
, "");
268 if (*n
->string
!= '\0')
278 /* Produce output for this node. */
284 print_text(h
, n
->string
);
288 print_eqn(h
, n
->eqn
);
292 * This will take care of initialising all of the table
293 * state data for the first table, then tearing it down
296 print_tbl(h
, n
->span
);
300 * Close out scope of font prior to opening a macro
303 if (HTMLFONT_NONE
!= h
->metac
) {
305 h
->metac
= HTMLFONT_NONE
;
309 * Close out the current table, if it's open, and unset
310 * the "meta" table state. This will be reopened on the
311 * next table element.
317 if (n
->tok
< ROFF_MAX
) {
323 assert(n
->tok
>= MAN_TH
&& n
->tok
< MAN_MAX
);
324 if (mans
[n
->tok
].pre
)
325 child
= (*mans
[n
->tok
].pre
)(man
, n
, h
);
327 /* Some block macros resume .nf in the body. */
328 if (save_fillmode
&& n
->type
== ROFFT_BODY
)
329 want_fillmode
= save_fillmode
;
334 if (child
&& n
->child
)
335 print_man_nodelist(man
, n
->child
, h
);
337 /* This will automatically close out any font scope. */
340 if (fillmode(h
, 0) == MAN_nf
&&
341 n
->next
!= NULL
&& n
->next
->flags
& NODE_LINE
)
346 * MAN_nf switches to no-fill mode, MAN_fi to fill mode.
347 * Other arguments do not switch.
348 * The old mode is returned.
351 fillmode(struct html
*h
, int want
)
356 for (pre
= h
->tag
; pre
!= NULL
; pre
= pre
->next
)
357 if (pre
->tag
== TAG_PRE
)
360 had
= pre
== NULL
? MAN_fi
: MAN_nf
;
362 if (want
&& want
!= had
) {
364 print_otag(h
, TAG_PRE
, "");
372 a2width(const struct roff_node
*n
, struct roffsu
*su
)
374 if (n
->type
!= ROFFT_TEXT
)
376 return a2roffsu(n
->string
, su
, SCALE_EN
) != NULL
;
380 man_root_pre(const struct roff_meta
*man
, struct html
*h
)
387 mandoc_asprintf(&title
, "%s(%s)", man
->title
, man
->msec
);
389 t
= print_otag(h
, TAG_TABLE
, "c", "head");
390 tt
= print_otag(h
, TAG_TR
, "");
392 print_otag(h
, TAG_TD
, "c", "head-ltitle");
393 print_text(h
, title
);
396 print_otag(h
, TAG_TD
, "c", "head-vol");
397 if (NULL
!= man
->vol
)
398 print_text(h
, man
->vol
);
401 print_otag(h
, TAG_TD
, "c", "head-rtitle");
402 print_text(h
, title
);
408 man_root_post(const struct roff_meta
*man
, struct html
*h
)
412 t
= print_otag(h
, TAG_TABLE
, "c", "foot");
413 tt
= print_otag(h
, TAG_TR
, "");
415 print_otag(h
, TAG_TD
, "c", "foot-date");
416 print_text(h
, man
->date
);
419 print_otag(h
, TAG_TD
, "c", "foot-os");
421 print_text(h
, man
->os
);
430 if (n
->type
== ROFFT_HEAD
) {
431 id
= html_make_id(n
);
432 print_otag(h
, TAG_H1
, "cTi", "Sh", id
);
434 print_otag(h
, TAG_A
, "chR", "permalink", id
);
441 man_alt_pre(MAN_ARGS
)
443 const struct roff_node
*nn
;
448 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
451 fp
= i
% 2 ? TAG_I
: TAG_B
;
454 fp
= i
% 2 ? TAG_B
: TAG_I
;
457 fp
= i
% 2 ? TAG_I
: TAG_MAX
;
460 fp
= i
% 2 ? TAG_MAX
: TAG_I
;
463 fp
= i
% 2 ? TAG_MAX
: TAG_B
;
466 fp
= i
% 2 ? TAG_B
: TAG_MAX
;
473 h
->flags
|= HTML_NOSPACE
;
476 t
= print_otag(h
, fp
, "");
478 print_text(h
, nn
->string
);
489 print_otag(h
, TAG_SMALL
, "");
490 if (MAN_SB
== n
->tok
)
491 print_otag(h
, TAG_B
, "");
500 if (n
->type
== ROFFT_HEAD
) {
501 id
= html_make_id(n
);
502 print_otag(h
, TAG_H2
, "cTi", "Ss", id
);
504 print_otag(h
, TAG_A
, "chR", "permalink", id
);
514 if (n
->type
== ROFFT_HEAD
)
516 else if (n
->type
== ROFFT_BLOCK
)
525 const struct roff_node
*nn
;
527 if (n
->type
== ROFFT_BODY
) {
528 print_otag(h
, TAG_DD
, "");
530 } else if (n
->type
!= ROFFT_HEAD
) {
531 print_otag(h
, TAG_DL
, "c", "Bl-tag");
535 /* FIXME: width specification. */
537 print_otag(h
, TAG_DT
, "");
539 /* For IP, only print the first header element. */
541 if (MAN_IP
== n
->tok
&& n
->child
)
542 print_man_node(man
, n
->child
, h
);
544 /* For TP, only print next-line header elements. */
546 if (MAN_TP
== n
->tok
) {
548 while (NULL
!= nn
&& 0 == (NODE_LINE
& nn
->flags
))
551 print_man_node(man
, nn
, h
);
562 struct roffsu sum
, sui
;
563 const struct roff_node
*np
;
565 if (n
->type
== ROFFT_HEAD
)
567 else if (n
->type
!= ROFFT_BLOCK
)
572 if (np
== NULL
|| !a2width(np
, &sum
))
573 SCALE_HS_INIT(&sum
, INDENT
);
576 sui
.scale
= -sum
.scale
;
579 print_otag(h
, TAG_DIV
, "csului", "Pp", &sum
, &sui
);
589 h
->flags
|= HTML_NOSPACE
;
590 tt
= print_otag(h
, TAG_SPAN
, "c", "Op");
592 if (NULL
!= (n
= n
->child
)) {
593 print_otag(h
, TAG_B
, "");
594 print_text(h
, n
->string
);
599 if (NULL
!= n
&& NULL
!= n
->next
) {
600 print_otag(h
, TAG_I
, "");
601 print_text(h
, n
->next
->string
);
605 h
->flags
|= HTML_NOSPACE
;
613 print_otag(h
, TAG_B
, "");
620 print_otag(h
, TAG_I
, "");
627 print_otag(h
, TAG_BR
, "");
632 man_ign_pre(MAN_ARGS
)
643 if (n
->type
== ROFFT_HEAD
)
645 else if (n
->type
== ROFFT_BODY
)
648 SCALE_HS_INIT(&su
, INDENT
);
650 a2width(n
->head
->child
, &su
);
652 print_otag(h
, TAG_DIV
, "sul", &su
);
661 assert(n
->type
== ROFFT_HEAD
);
662 if (n
->child
!= NULL
) {
663 assert(n
->child
->type
== ROFFT_TEXT
);
664 if (n
->tok
== MAN_MT
) {
665 mandoc_asprintf(&cp
, "mailto:%s", n
->child
->string
);
666 print_otag(h
, TAG_A
, "cTh", "Mt", cp
);
669 print_otag(h
, TAG_A
, "cTh", "Lk", n
->child
->string
);
672 assert(n
->next
->type
== ROFFT_BODY
);
673 if (n
->next
->child
!= NULL
)
676 print_man_nodelist(man
, n
->child
, h
);