]>
git.cameronkatri.com Git - mandoc.git/blob - man_html.c
1 /* $Id: man_html.c,v 1.4 2009/10/04 09:35:26 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>
31 #define MAN_ARGS const struct man_meta *m, \
32 const struct man_node *n, \
37 int (*post
)(MAN_ARGS
);
40 static void print_man(MAN_ARGS
);
41 static void print_man_head(MAN_ARGS
);
42 static void print_man_nodelist(MAN_ARGS
);
43 static void print_man_node(MAN_ARGS
);
45 static int man_br_pre(MAN_ARGS
);
46 static int man_PP_pre(MAN_ARGS
);
47 static void man_root_post(MAN_ARGS
);
48 static int man_root_pre(MAN_ARGS
);
49 static int man_SH_pre(MAN_ARGS
);
50 static int man_SS_pre(MAN_ARGS
);
53 extern size_t strlcpy(char *, const char *, size_t);
54 extern size_t strlcat(char *, const char *, size_t);
57 static const struct htmlman mans
[MAN_MAX
] = {
58 { man_br_pre
, NULL
}, /* br */
59 { NULL
, NULL
}, /* TH */
60 { man_SH_pre
, NULL
}, /* SH */
61 { man_SS_pre
, NULL
}, /* SS */
62 { NULL
, NULL
}, /* TP */
63 { man_PP_pre
, NULL
}, /* LP */
64 { man_PP_pre
, NULL
}, /* PP */
65 { man_PP_pre
, NULL
}, /* P */
66 { NULL
, NULL
}, /* IP */
67 { NULL
, NULL
}, /* HP */
68 { NULL
, NULL
}, /* SM */
69 { NULL
, NULL
}, /* SB */
70 { NULL
, NULL
}, /* BI */
71 { NULL
, NULL
}, /* IB */
72 { NULL
, NULL
}, /* BR */
73 { NULL
, NULL
}, /* RB */
74 { NULL
, NULL
}, /* R */
75 { NULL
, NULL
}, /* B */
76 { NULL
, NULL
}, /* I */
77 { NULL
, NULL
}, /* IR */
78 { NULL
, NULL
}, /* RI */
79 { NULL
, NULL
}, /* na */
80 { NULL
, NULL
}, /* i */
81 { man_br_pre
, NULL
}, /* sp */
82 { NULL
, NULL
}, /* nf */
83 { NULL
, NULL
}, /* fi */
84 { NULL
, NULL
}, /* r */
85 { NULL
, NULL
}, /* RE */
86 { NULL
, NULL
}, /* RS */
87 { NULL
, NULL
}, /* DT */
88 { NULL
, NULL
}, /* UC */
93 html_man(void *arg
, const struct man
*m
)
98 h
= (struct html
*)arg
;
100 print_gen_doctype(h
);
102 t
= print_otag(h
, TAG_HTML
, 0, NULL
);
103 print_man(man_meta(m
), man_node(m
), h
);
116 t
= print_otag(h
, TAG_HEAD
, 0, NULL
);
118 print_man_head(m
, n
, h
);
120 t
= print_otag(h
, TAG_BODY
, 0, NULL
);
122 tag
.key
= ATTR_CLASS
;
124 print_otag(h
, TAG_DIV
, 1, &tag
);
126 print_man_nodelist(m
, n
, h
);
134 print_man_head(MAN_ARGS
)
139 buffmt(h
, "%s(%d)", m
->title
, m
->msec
);
141 print_otag(h
, TAG_TITLE
, 0, NULL
);
142 print_text(h
, h
->buf
);
147 print_man_nodelist(MAN_ARGS
)
150 print_man_node(m
, n
, h
);
152 print_man_nodelist(m
, n
->next
, h
);
157 print_man_node(MAN_ARGS
)
163 t
= SLIST_FIRST(&h
->tags
);
169 child
= man_root_pre(m
, n
, h
);
172 print_text(h
, n
->string
);
175 if (mans
[n
->tok
].pre
)
176 child
= (*mans
[n
->tok
].pre
)(m
, n
, h
);
180 if (child
&& n
->child
)
181 print_man_nodelist(m
, n
->child
, h
);
189 man_root_post(m
, n
, h
);
194 if (mans
[n
->tok
].post
)
195 (*mans
[n
->tok
].post
)(m
, n
, h
);
203 man_root_pre(MAN_ARGS
)
205 struct htmlpair tag
[2];
207 char b
[BUFSIZ
], title
[BUFSIZ
];
211 (void)strlcat(b
, m
->vol
, BUFSIZ
);
213 (void)snprintf(title
, BUFSIZ
- 1,
214 "%s(%d)", m
->title
, m
->msec
);
216 tag
[0].key
= ATTR_CLASS
;
217 tag
[0].val
= "header";
218 tag
[1].key
= ATTR_STYLE
;
219 tag
[1].val
= "width: 100%;";
220 t
= print_otag(h
, TAG_TABLE
, 2, tag
);
221 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
223 tag
[0].key
= ATTR_STYLE
;
224 tag
[0].val
= "width: 10%;";
225 print_otag(h
, TAG_TD
, 1, tag
);
226 print_text(h
, title
);
229 tag
[0].key
= ATTR_STYLE
;
230 tag
[0].val
= "width: 80%; white-space: nowrap; text-align: center;";
231 print_otag(h
, TAG_TD
, 1, tag
);
235 tag
[0].key
= ATTR_STYLE
;
236 tag
[0].val
= "width: 10%; text-align: right;";
237 print_otag(h
, TAG_TD
, 1, tag
);
238 print_text(h
, title
);
247 man_root_post(MAN_ARGS
)
250 struct htmlpair tag
[2];
254 (void)localtime_r(&m
->date
, &tm
);
256 if (0 == strftime(b
, BUFSIZ
- 1, "%B %e, %Y", &tm
))
257 err(EXIT_FAILURE
, "strftime");
259 tag
[0].key
= ATTR_CLASS
;
260 tag
[0].val
= "footer";
261 tag
[1].key
= ATTR_STYLE
;
262 tag
[1].val
= "width: 100%;";
263 t
= print_otag(h
, TAG_TABLE
, 2, tag
);
264 tt
= print_otag(h
, TAG_TR
, 0, NULL
);
266 tag
[0].key
= ATTR_STYLE
;
267 tag
[0].val
= "width: 50%;";
268 print_otag(h
, TAG_TD
, 1, tag
);
272 tag
[0].key
= ATTR_STYLE
;
273 tag
[0].val
= "width: 50%; text-align: right;";
274 print_otag(h
, TAG_TD
, 1, tag
);
276 print_text(h
, m
->source
);
291 len
= n
->child
? atoi(n
->child
->string
) : 1;
301 buffmt(h
, "height: %dem;", len
);
302 tag
.key
= ATTR_STYLE
;
304 print_otag(h
, TAG_DIV
, 1, &tag
);
313 struct htmlpair tag
[2];
315 if (MAN_BODY
== n
->type
) {
316 buffmt(h
, "margin-left: %dem;", INDENT
);
318 tag
[0].key
= ATTR_CLASS
;
319 tag
[0].val
= "sec-body";
320 tag
[1].key
= ATTR_STYLE
;
323 print_otag(h
, TAG_DIV
, 2, tag
);
325 } else if (MAN_BLOCK
== n
->type
) {
326 tag
[0].key
= ATTR_CLASS
;
327 tag
[0].val
= "sec-block";
329 if (n
->prev
&& MAN_SH
== n
->prev
->tok
)
330 if (NULL
== n
->prev
->body
->child
) {
331 print_otag(h
, TAG_DIV
, 1, tag
);
335 bufcat(h
, "margin-top: 1em;");
337 bufcat(h
, "margin-bottom: 1em;");
339 tag
[1].key
= ATTR_STYLE
;
342 print_otag(h
, TAG_DIV
, 2, tag
);
346 tag
[0].key
= ATTR_CLASS
;
347 tag
[0].val
= "sec-head";
349 print_otag(h
, TAG_DIV
, 1, tag
);
358 struct htmlpair tag
[3];
363 if (MAN_BODY
== n
->type
) {
364 tag
[i
].key
= ATTR_CLASS
;
365 tag
[i
++].val
= "ssec-body";
367 if (n
->parent
->next
&& n
->child
) {
368 bufcat(h
, "margin-bottom: 1em;");
369 tag
[i
].key
= ATTR_STYLE
;
370 tag
[i
++].val
= h
->buf
;
373 print_otag(h
, TAG_DIV
, i
, tag
);
375 } else if (MAN_BLOCK
== n
->type
) {
376 tag
[i
].key
= ATTR_CLASS
;
377 tag
[i
++].val
= "ssec-block";
379 if (n
->prev
&& MAN_SS
== n
->prev
->tok
)
380 if (n
->prev
->body
->child
) {
381 bufcat(h
, "margin-top: 1em;");
382 tag
[i
].key
= ATTR_STYLE
;
383 tag
[i
++].val
= h
->buf
;
386 print_otag(h
, TAG_DIV
, i
, tag
);
390 buffmt(h
, "margin-left: -%dem;", INDENT
- HALFINDENT
);
392 tag
[0].key
= ATTR_CLASS
;
393 tag
[0].val
= "ssec-head";
394 tag
[1].key
= ATTR_STYLE
;
397 print_otag(h
, TAG_DIV
, 2, tag
);
408 if (MAN_BLOCK
!= n
->type
)
411 buffmt(h
, "margin-left: %dem;", INDENT
);
412 if (n
->next
&& n
->next
->child
)
413 bufcat(h
, "margin-bottom: 1em;");
415 tag
.key
= ATTR_STYLE
;
417 print_otag(h
, TAG_DIV
, 1, &tag
);