]>
git.cameronkatri.com Git - mandoc.git/blob - man_html.c
1 /* $Id: man_html.c,v 1.5 2009/10/04 10:24:31 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>
33 #define MAN_ARGS const struct man_meta *m, \
34 const struct man_node *n, \
39 int (*post
)(MAN_ARGS
);
42 static void print_man(MAN_ARGS
);
43 static void print_man_head(MAN_ARGS
);
44 static void print_man_nodelist(MAN_ARGS
);
45 static void print_man_node(MAN_ARGS
);
47 static int a2width(const struct man_node
*);
49 static int man_br_pre(MAN_ARGS
);
50 static int man_IP_pre(MAN_ARGS
);
51 static int man_PP_pre(MAN_ARGS
);
52 static void man_root_post(MAN_ARGS
);
53 static int man_root_pre(MAN_ARGS
);
54 static int man_SH_pre(MAN_ARGS
);
55 static int man_SS_pre(MAN_ARGS
);
58 extern size_t strlcpy(char *, const char *, size_t);
59 extern size_t strlcat(char *, const char *, size_t);
62 static const struct htmlman mans
[MAN_MAX
] = {
63 { man_br_pre
, NULL
}, /* br */
64 { NULL
, NULL
}, /* TH */
65 { man_SH_pre
, NULL
}, /* SH */
66 { man_SS_pre
, NULL
}, /* SS */
67 { NULL
, NULL
}, /* TP */
68 { man_PP_pre
, NULL
}, /* LP */
69 { man_PP_pre
, NULL
}, /* PP */
70 { man_PP_pre
, NULL
}, /* P */
71 { man_IP_pre
, NULL
}, /* IP */
72 { NULL
, NULL
}, /* HP */
73 { NULL
, NULL
}, /* SM */
74 { NULL
, NULL
}, /* SB */
75 { NULL
, NULL
}, /* BI */
76 { NULL
, NULL
}, /* IB */
77 { NULL
, NULL
}, /* BR */
78 { NULL
, NULL
}, /* RB */
79 { NULL
, NULL
}, /* R */
80 { NULL
, NULL
}, /* B */
81 { NULL
, NULL
}, /* I */
82 { NULL
, NULL
}, /* IR */
83 { NULL
, NULL
}, /* RI */
84 { NULL
, NULL
}, /* na */
85 { NULL
, NULL
}, /* i */
86 { man_br_pre
, NULL
}, /* sp */
87 { NULL
, NULL
}, /* nf */
88 { NULL
, NULL
}, /* fi */
89 { NULL
, NULL
}, /* r */
90 { NULL
, NULL
}, /* RE */
91 { NULL
, NULL
}, /* RS */
92 { NULL
, NULL
}, /* DT */
93 { NULL
, NULL
}, /* UC */
98 html_man(void *arg
, const struct man
*m
)
103 h
= (struct html
*)arg
;
105 print_gen_doctype(h
);
107 t
= print_otag(h
, TAG_HTML
, 0, NULL
);
108 print_man(man_meta(m
), man_node(m
), h
);
121 t
= print_otag(h
, TAG_HEAD
, 0, NULL
);
123 print_man_head(m
, n
, h
);
125 t
= print_otag(h
, TAG_BODY
, 0, NULL
);
127 tag
.key
= ATTR_CLASS
;
129 print_otag(h
, TAG_DIV
, 1, &tag
);
131 print_man_nodelist(m
, n
, h
);
139 print_man_head(MAN_ARGS
)
144 buffmt(h
, "%s(%d)", m
->title
, m
->msec
);
146 print_otag(h
, TAG_TITLE
, 0, NULL
);
147 print_text(h
, h
->buf
);
152 print_man_nodelist(MAN_ARGS
)
155 print_man_node(m
, n
, h
);
157 print_man_nodelist(m
, n
->next
, h
);
162 print_man_node(MAN_ARGS
)
168 t
= SLIST_FIRST(&h
->tags
);
174 child
= man_root_pre(m
, n
, h
);
177 print_text(h
, n
->string
);
180 if (mans
[n
->tok
].pre
)
181 child
= (*mans
[n
->tok
].pre
)(m
, n
, h
);
185 if (child
&& n
->child
)
186 print_man_nodelist(m
, n
->child
, h
);
194 man_root_post(m
, n
, h
);
199 if (mans
[n
->tok
].post
)
200 (*mans
[n
->tok
].post
)(m
, n
, h
);
207 a2width(const struct man_node
*n
)
212 assert(MAN_TEXT
== n
->type
);
217 if (0 == (len
= (int)strlen(p
)))
220 for (i
= 0; i
< len
; i
++)
221 if ( ! isdigit((u_char
)p
[i
]))
225 if ('n' == p
[len
- 1] || 'm' == p
[len
- 1])
236 man_root_pre(MAN_ARGS
)
238 struct htmlpair tag
[2];
240 char b
[BUFSIZ
], title
[BUFSIZ
];
244 (void)strlcat(b
, m
->vol
, BUFSIZ
);
246 (void)snprintf(title
, BUFSIZ
- 1,
247 "%s(%d)", m
->title
, m
->msec
);
249 tag
[0].key
= ATTR_CLASS
;
250 tag
[0].val
= "header";
251 tag
[1].key
= ATTR_STYLE
;
252 tag
[1].val
= "width: 100%;";
253 t
= print_otag(h
, TAG_TABLE
, 2, tag
);
254 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
256 tag
[0].key
= ATTR_STYLE
;
257 tag
[0].val
= "width: 10%;";
258 print_otag(h
, TAG_TD
, 1, tag
);
259 print_text(h
, title
);
262 tag
[0].key
= ATTR_STYLE
;
263 tag
[0].val
= "width: 80%; white-space: nowrap; text-align: center;";
264 print_otag(h
, TAG_TD
, 1, tag
);
268 tag
[0].key
= ATTR_STYLE
;
269 tag
[0].val
= "width: 10%; text-align: right;";
270 print_otag(h
, TAG_TD
, 1, tag
);
271 print_text(h
, title
);
280 man_root_post(MAN_ARGS
)
283 struct htmlpair tag
[2];
287 (void)localtime_r(&m
->date
, &tm
);
289 if (0 == strftime(b
, BUFSIZ
- 1, "%B %e, %Y", &tm
))
290 err(EXIT_FAILURE
, "strftime");
292 tag
[0].key
= ATTR_CLASS
;
293 tag
[0].val
= "footer";
294 tag
[1].key
= ATTR_STYLE
;
295 tag
[1].val
= "width: 100%;";
296 t
= print_otag(h
, TAG_TABLE
, 2, tag
);
297 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
299 tag
[0].key
= ATTR_STYLE
;
300 tag
[0].val
= "width: 50%;";
301 print_otag(h
, TAG_TD
, 1, tag
);
305 tag
[0].key
= ATTR_STYLE
;
306 tag
[0].val
= "width: 50%; text-align: right;";
307 print_otag(h
, TAG_TD
, 1, tag
);
309 print_text(h
, m
->source
);
324 len
= n
->child
? atoi(n
->child
->string
) : 1;
334 buffmt(h
, "height: %dem;", len
);
335 tag
.key
= ATTR_STYLE
;
337 print_otag(h
, TAG_DIV
, 1, &tag
);
346 struct htmlpair tag
[2];
348 if (MAN_BODY
== n
->type
) {
349 buffmt(h
, "margin-left: %dem;", INDENT
);
351 tag
[0].key
= ATTR_CLASS
;
352 tag
[0].val
= "sec-body";
353 tag
[1].key
= ATTR_STYLE
;
356 print_otag(h
, TAG_DIV
, 2, tag
);
358 } else if (MAN_BLOCK
== n
->type
) {
359 tag
[0].key
= ATTR_CLASS
;
360 tag
[0].val
= "sec-block";
362 if (n
->prev
&& MAN_SH
== n
->prev
->tok
)
363 if (NULL
== n
->prev
->body
->child
) {
364 print_otag(h
, TAG_DIV
, 1, tag
);
368 bufcat(h
, "margin-top: 1em;");
370 bufcat(h
, "margin-bottom: 1em;");
372 tag
[1].key
= ATTR_STYLE
;
375 print_otag(h
, TAG_DIV
, 2, tag
);
379 tag
[0].key
= ATTR_CLASS
;
380 tag
[0].val
= "sec-head";
382 print_otag(h
, TAG_DIV
, 1, tag
);
391 struct htmlpair tag
[3];
396 if (MAN_BODY
== n
->type
) {
397 tag
[i
].key
= ATTR_CLASS
;
398 tag
[i
++].val
= "ssec-body";
400 if (n
->parent
->next
&& n
->child
) {
401 bufcat(h
, "margin-bottom: 1em;");
402 tag
[i
].key
= ATTR_STYLE
;
403 tag
[i
++].val
= h
->buf
;
406 print_otag(h
, TAG_DIV
, i
, tag
);
408 } else if (MAN_BLOCK
== n
->type
) {
409 tag
[i
].key
= ATTR_CLASS
;
410 tag
[i
++].val
= "ssec-block";
412 if (n
->prev
&& MAN_SS
== n
->prev
->tok
)
413 if (n
->prev
->body
->child
) {
414 bufcat(h
, "margin-top: 1em;");
415 tag
[i
].key
= ATTR_STYLE
;
416 tag
[i
++].val
= h
->buf
;
419 print_otag(h
, TAG_DIV
, i
, tag
);
423 buffmt(h
, "margin-left: -%dem;", INDENT
- HALFINDENT
);
425 tag
[0].key
= ATTR_CLASS
;
426 tag
[0].val
= "ssec-head";
427 tag
[1].key
= ATTR_STYLE
;
430 print_otag(h
, TAG_DIV
, 2, tag
);
442 if (MAN_BLOCK
!= n
->type
)
447 if (MAN_ROOT
== n
->parent
->tok
) {
448 buffmt(h
, "margin-left: %dem;", INDENT
);
451 if (n
->next
&& n
->next
->child
) {
453 bufcat(h
, "margin-bottom: 1em;");
456 tag
.key
= ATTR_STYLE
;
458 print_otag(h
, TAG_DIV
, i
, &tag
);
469 const struct man_node
*nn
;
472 if (NULL
!= (nn
= n
->parent
->head
->child
))
473 if (NULL
!= (nn
= nn
->next
)) {
474 for ( ; nn
->next
; nn
= nn
->next
)
476 if ((ival
= a2width(nn
)) >= 0)
480 if (MAN_BLOCK
== n
->type
) {
481 buffmt(h
, "clear: both; margin-left: %dem;", len
);
482 tag
.key
= ATTR_STYLE
;
484 print_otag(h
, TAG_DIV
, 1, &tag
);
486 } else if (MAN_HEAD
== n
->type
) {
487 buffmt(h
, "margin-left: -%dem; min-width: %dem;",
489 bufcat(h
, "clear: left;");
490 bufcat(h
, "padding-right: 1em;");
491 if (n
->next
&& n
->next
->child
)
492 bufcat(h
, "float: left;");
493 tag
.key
= ATTR_STYLE
;
495 print_otag(h
, TAG_DIV
, 1, &tag
);
497 /* Don't print the length value. */
499 for (nn
= n
->child
; nn
->next
; nn
= nn
->next
)
500 print_man_node(m
, nn
, h
);
504 print_otag(h
, TAG_DIV
, 0, &tag
);