]>
git.cameronkatri.com Git - mandoc.git/blob - man_html.c
1 /* $Id: man_html.c,v 1.6 2009/10/04 15:24:54 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
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.
17 #include <sys/types.h>
18 #include <sys/queue.h>
30 /* TODO: preserve ident widths. */
35 #define MAN_ARGS const struct man_meta *m, \
36 const struct man_node *n, \
41 int (*post
)(MAN_ARGS
);
44 static void print_man(MAN_ARGS
);
45 static void print_man_head(MAN_ARGS
);
46 static void print_man_nodelist(MAN_ARGS
);
47 static void print_man_node(MAN_ARGS
);
49 static int a2width(const struct man_node
*);
51 static int man_br_pre(MAN_ARGS
);
52 static int man_HP_pre(MAN_ARGS
);
53 static int man_IP_pre(MAN_ARGS
);
54 static int man_PP_pre(MAN_ARGS
);
55 static void man_root_post(MAN_ARGS
);
56 static int man_root_pre(MAN_ARGS
);
57 static int man_SH_pre(MAN_ARGS
);
58 static int man_SS_pre(MAN_ARGS
);
61 extern size_t strlcpy(char *, const char *, size_t);
62 extern size_t strlcat(char *, const char *, size_t);
65 static const struct htmlman mans
[MAN_MAX
] = {
66 { man_br_pre
, NULL
}, /* br */
67 { NULL
, NULL
}, /* TH */
68 { man_SH_pre
, NULL
}, /* SH */
69 { man_SS_pre
, NULL
}, /* SS */
70 { man_IP_pre
, NULL
}, /* TP */
71 { man_PP_pre
, NULL
}, /* LP */
72 { man_PP_pre
, NULL
}, /* PP */
73 { man_PP_pre
, NULL
}, /* P */
74 { man_IP_pre
, NULL
}, /* IP */
75 { man_HP_pre
, NULL
}, /* HP */
76 { NULL
, NULL
}, /* SM */
77 { NULL
, NULL
}, /* SB */
78 { NULL
, NULL
}, /* BI */
79 { NULL
, NULL
}, /* IB */
80 { NULL
, NULL
}, /* BR */
81 { NULL
, NULL
}, /* RB */
82 { NULL
, NULL
}, /* R */
83 { NULL
, NULL
}, /* B */
84 { NULL
, NULL
}, /* I */
85 { NULL
, NULL
}, /* IR */
86 { NULL
, NULL
}, /* RI */
87 { NULL
, NULL
}, /* na */
88 { NULL
, NULL
}, /* i */
89 { man_br_pre
, NULL
}, /* sp */
90 { NULL
, NULL
}, /* nf */
91 { NULL
, NULL
}, /* fi */
92 { NULL
, NULL
}, /* r */
93 { NULL
, NULL
}, /* RE */
94 { NULL
, NULL
}, /* RS */
95 { NULL
, NULL
}, /* DT */
96 { NULL
, NULL
}, /* UC */
101 html_man(void *arg
, const struct man
*m
)
106 h
= (struct html
*)arg
;
108 print_gen_doctype(h
);
110 t
= print_otag(h
, TAG_HTML
, 0, NULL
);
111 print_man(man_meta(m
), man_node(m
), h
);
124 t
= print_otag(h
, TAG_HEAD
, 0, NULL
);
126 print_man_head(m
, n
, h
);
128 t
= print_otag(h
, TAG_BODY
, 0, NULL
);
130 tag
.key
= ATTR_CLASS
;
132 print_otag(h
, TAG_DIV
, 1, &tag
);
134 print_man_nodelist(m
, n
, h
);
142 print_man_head(MAN_ARGS
)
147 buffmt(h
, "%s(%d)", m
->title
, m
->msec
);
149 print_otag(h
, TAG_TITLE
, 0, NULL
);
150 print_text(h
, h
->buf
);
155 print_man_nodelist(MAN_ARGS
)
158 print_man_node(m
, n
, h
);
160 print_man_nodelist(m
, n
->next
, h
);
165 print_man_node(MAN_ARGS
)
171 t
= SLIST_FIRST(&h
->tags
);
177 child
= man_root_pre(m
, n
, h
);
180 print_text(h
, n
->string
);
183 if (mans
[n
->tok
].pre
)
184 child
= (*mans
[n
->tok
].pre
)(m
, n
, h
);
188 if (child
&& n
->child
)
189 print_man_nodelist(m
, n
->child
, h
);
197 man_root_post(m
, n
, h
);
202 if (mans
[n
->tok
].post
)
203 (*mans
[n
->tok
].post
)(m
, n
, h
);
210 a2width(const struct man_node
*n
)
215 if (MAN_TEXT
!= n
->type
)
220 if (0 == (len
= (int)strlen(p
)))
223 for (i
= 0; i
< len
; i
++)
224 if ( ! isdigit((u_char
)p
[i
]))
228 if ('n' == p
[len
- 1] || 'm' == p
[len
- 1])
239 man_root_pre(MAN_ARGS
)
241 struct htmlpair tag
[2];
243 char b
[BUFSIZ
], title
[BUFSIZ
];
247 (void)strlcat(b
, m
->vol
, BUFSIZ
);
249 (void)snprintf(title
, BUFSIZ
- 1,
250 "%s(%d)", m
->title
, m
->msec
);
252 tag
[0].key
= ATTR_CLASS
;
253 tag
[0].val
= "header";
254 tag
[1].key
= ATTR_STYLE
;
255 tag
[1].val
= "width: 100%;";
256 t
= print_otag(h
, TAG_TABLE
, 2, tag
);
257 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
259 tag
[0].key
= ATTR_STYLE
;
260 tag
[0].val
= "width: 10%;";
261 print_otag(h
, TAG_TD
, 1, tag
);
262 print_text(h
, title
);
265 tag
[0].key
= ATTR_STYLE
;
266 tag
[0].val
= "width: 80%; white-space: nowrap; text-align: center;";
267 print_otag(h
, TAG_TD
, 1, tag
);
271 tag
[0].key
= ATTR_STYLE
;
272 tag
[0].val
= "width: 10%; text-align: right;";
273 print_otag(h
, TAG_TD
, 1, tag
);
274 print_text(h
, title
);
283 man_root_post(MAN_ARGS
)
286 struct htmlpair tag
[2];
290 (void)localtime_r(&m
->date
, &tm
);
292 if (0 == strftime(b
, BUFSIZ
- 1, "%B %e, %Y", &tm
))
293 err(EXIT_FAILURE
, "strftime");
295 tag
[0].key
= ATTR_CLASS
;
296 tag
[0].val
= "footer";
297 tag
[1].key
= ATTR_STYLE
;
298 tag
[1].val
= "width: 100%;";
299 t
= print_otag(h
, TAG_TABLE
, 2, tag
);
300 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
302 tag
[0].key
= ATTR_STYLE
;
303 tag
[0].val
= "width: 50%;";
304 print_otag(h
, TAG_TD
, 1, tag
);
308 tag
[0].key
= ATTR_STYLE
;
309 tag
[0].val
= "width: 50%; text-align: right;";
310 print_otag(h
, TAG_TD
, 1, tag
);
312 print_text(h
, m
->source
);
327 len
= n
->child
? atoi(n
->child
->string
) : 1;
337 buffmt(h
, "height: %dem;", len
);
338 tag
.key
= ATTR_STYLE
;
340 print_otag(h
, TAG_DIV
, 1, &tag
);
349 struct htmlpair tag
[2];
351 if (MAN_BODY
== n
->type
) {
352 buffmt(h
, "margin-left: %dem;", INDENT
);
354 tag
[0].key
= ATTR_CLASS
;
355 tag
[0].val
= "sec-body";
356 tag
[1].key
= ATTR_STYLE
;
359 print_otag(h
, TAG_DIV
, 2, tag
);
361 } else if (MAN_BLOCK
== n
->type
) {
362 tag
[0].key
= ATTR_CLASS
;
363 tag
[0].val
= "sec-block";
365 if (n
->prev
&& MAN_SH
== n
->prev
->tok
)
366 if (NULL
== n
->prev
->body
->child
) {
367 print_otag(h
, TAG_DIV
, 1, tag
);
371 bufcat(h
, "margin-top: 1em;");
373 bufcat(h
, "margin-bottom: 1em;");
375 tag
[1].key
= ATTR_STYLE
;
378 print_otag(h
, TAG_DIV
, 2, tag
);
382 tag
[0].key
= ATTR_CLASS
;
383 tag
[0].val
= "sec-head";
385 print_otag(h
, TAG_DIV
, 1, tag
);
394 struct htmlpair tag
[3];
399 if (MAN_BODY
== n
->type
) {
400 tag
[i
].key
= ATTR_CLASS
;
401 tag
[i
++].val
= "ssec-body";
403 if (n
->parent
->next
&& n
->child
) {
404 bufcat(h
, "margin-bottom: 1em;");
405 tag
[i
].key
= ATTR_STYLE
;
406 tag
[i
++].val
= h
->buf
;
409 print_otag(h
, TAG_DIV
, i
, tag
);
411 } else if (MAN_BLOCK
== n
->type
) {
412 tag
[i
].key
= ATTR_CLASS
;
413 tag
[i
++].val
= "ssec-block";
415 if (n
->prev
&& MAN_SS
== n
->prev
->tok
)
416 if (n
->prev
->body
->child
) {
417 bufcat(h
, "margin-top: 1em;");
418 tag
[i
].key
= ATTR_STYLE
;
419 tag
[i
++].val
= h
->buf
;
422 print_otag(h
, TAG_DIV
, i
, tag
);
426 buffmt(h
, "margin-left: -%dem;", INDENT
- HALFINDENT
);
428 tag
[0].key
= ATTR_CLASS
;
429 tag
[0].val
= "ssec-head";
430 tag
[1].key
= ATTR_STYLE
;
433 print_otag(h
, TAG_DIV
, 2, tag
);
445 if (MAN_BLOCK
!= n
->type
)
450 if (MAN_ROOT
== n
->parent
->tok
) {
451 buffmt(h
, "margin-left: %dem;", INDENT
);
454 if (n
->next
&& n
->next
->child
) {
456 bufcat(h
, "margin-bottom: 1em;");
459 tag
.key
= ATTR_STYLE
;
461 print_otag(h
, TAG_DIV
, i
, &tag
);
472 const struct man_node
*nn
;
474 if (MAN_BODY
== n
->type
) {
475 print_otag(h
, TAG_DIV
, 0, NULL
);
479 nn
= MAN_BLOCK
== n
->type
?
480 n
->head
->child
: n
->parent
->head
->child
;
484 /* Calculate the indentation length. */
487 if (NULL
!= (nn
= nn
->next
)) {
488 for ( ; nn
->next
; nn
= nn
->next
)
490 if ((ival
= a2width(nn
)) >= 0)
494 if (MAN_BLOCK
== n
->type
) {
495 buffmt(h
, "clear: both; margin-left: %dem;", len
);
496 tag
.key
= ATTR_STYLE
;
498 print_otag(h
, TAG_DIV
, 1, &tag
);
502 /* If there's an indent string, print it out. */
504 buffmt(h
, "margin-left: -%dem; min-width: %dem;",
506 bufcat(h
, "clear: left; padding-right: 1em;");
508 if (n
->next
&& n
->next
->child
)
509 bufcat(h
, "float: left;");
511 tag
.key
= ATTR_STYLE
;
513 print_otag(h
, TAG_DIV
, 1, &tag
);
518 /* With a length string, omit the last child. */
520 for (nn
= n
->child
; nn
->next
; nn
= nn
->next
)
521 print_man_node(m
, nn
, h
);
532 const struct man_node
*nn
;
535 if (MAN_HEAD
== n
->type
)
538 nn
= MAN_BLOCK
== n
->type
?
539 n
->head
->child
: n
->parent
->head
->child
;
544 if ((ival
= a2width(nn
)) >= 0)
547 if (MAN_BLOCK
== n
->type
) {
548 buffmt(h
, "clear: both; margin-left: %dem;", len
);
549 tag
.key
= ATTR_STYLE
;
551 print_otag(h
, TAG_DIV
, 1, &tag
);
555 buffmt(h
, "text-indent: -%dem;", len
);
557 tag
.key
= ATTR_STYLE
;
559 print_otag(h
, TAG_DIV
, 1, &tag
);