Annotation of mandoc/html.c, Revision 1.40
1.40 ! kristaps 1: /* $Id: html.c,v 1.39 2009/09/20 19:25:06 kristaps Exp $ */
1.1 kristaps 2: /*
1.29 kristaps 3: * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1 kristaps 4: *
5: * Permission to use, copy, modify, and distribute this software for any
1.29 kristaps 6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 8: *
1.29 kristaps 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.
1.1 kristaps 16: */
1.30 kristaps 17: #include <sys/queue.h>
18:
1.1 kristaps 19: #include <assert.h>
1.33 kristaps 20: #include <ctype.h>
1.4 kristaps 21: #include <err.h>
1.29 kristaps 22: #include <stdio.h>
1.1 kristaps 23: #include <stdlib.h>
1.33 kristaps 24: #include <string.h>
1.1 kristaps 25:
1.32 kristaps 26: #include "chars.h"
1.29 kristaps 27: #include "mdoc.h"
28: #include "man.h"
1.2 kristaps 29:
1.29 kristaps 30: #define DOCTYPE "-//W3C//DTD HTML 4.01//EN"
31: #define DTD "http://www.w3.org/TR/html4/strict.dtd"
1.8 kristaps 32:
1.33 kristaps 33: #define INDENT 5
34: #define HALFINDENT 3
1.34 kristaps 35: #define PX_MULT 8
1.33 kristaps 36:
1.29 kristaps 37: enum htmltag {
38: TAG_HTML,
39: TAG_HEAD,
40: TAG_BODY,
41: TAG_META,
42: TAG_TITLE,
43: TAG_DIV,
44: TAG_H1,
45: TAG_H2,
46: TAG_P,
47: TAG_SPAN,
48: TAG_LINK,
1.30 kristaps 49: TAG_BR,
50: TAG_A,
1.33 kristaps 51: TAG_TABLE,
52: TAG_COL,
53: TAG_TR,
54: TAG_TD,
1.34 kristaps 55: TAG_LI,
56: TAG_UL,
57: TAG_OL,
1.29 kristaps 58: TAG_MAX
1.7 kristaps 59: };
60:
1.29 kristaps 61: enum htmlattr {
62: ATTR_HTTPEQUIV,
63: ATTR_CONTENT,
64: ATTR_NAME,
65: ATTR_REL,
66: ATTR_HREF,
67: ATTR_TYPE,
68: ATTR_MEDIA,
69: ATTR_CLASS,
1.33 kristaps 70: ATTR_STYLE,
71: ATTR_WIDTH,
72: ATTR_VALIGN,
1.29 kristaps 73: ATTR_MAX
1.7 kristaps 74: };
75:
1.29 kristaps 76: struct htmldata {
77: char *name;
78: int flags;
1.30 kristaps 79: #define HTML_CLRLINE (1 << 0)
80: #define HTML_NOSTACK (1 << 1)
1.29 kristaps 81: };
1.7 kristaps 82:
1.29 kristaps 83: static const struct htmldata htmltags[TAG_MAX] = {
1.30 kristaps 84: {"html", HTML_CLRLINE}, /* TAG_HTML */
85: {"head", HTML_CLRLINE}, /* TAG_HEAD */
86: {"body", HTML_CLRLINE}, /* TAG_BODY */
87: {"meta", HTML_CLRLINE | HTML_NOSTACK}, /* TAG_META */
1.33 kristaps 88: {"title", HTML_CLRLINE}, /* TAG_TITLE */
1.30 kristaps 89: {"div", HTML_CLRLINE}, /* TAG_DIV */
1.29 kristaps 90: {"h1", 0}, /* TAG_H1 */
91: {"h2", 0}, /* TAG_H2 */
1.30 kristaps 92: {"p", HTML_CLRLINE}, /* TAG_P */
1.29 kristaps 93: {"span", 0}, /* TAG_SPAN */
1.30 kristaps 94: {"link", HTML_CLRLINE | HTML_NOSTACK}, /* TAG_LINK */
95: {"br", HTML_CLRLINE | HTML_NOSTACK}, /* TAG_LINK */
96: {"a", 0}, /* TAG_A */
1.33 kristaps 97: {"table", HTML_CLRLINE}, /* TAG_TABLE */
98: {"col", HTML_CLRLINE | HTML_NOSTACK}, /* TAG_COL */
99: {"tr", HTML_CLRLINE}, /* TAG_TR */
100: {"td", HTML_CLRLINE}, /* TAG_TD */
1.34 kristaps 101: {"li", HTML_CLRLINE}, /* TAG_LI */
102: {"ul", HTML_CLRLINE}, /* TAG_UL */
103: {"ol", HTML_CLRLINE}, /* TAG_OL */
1.29 kristaps 104: };
1.10 kristaps 105:
1.29 kristaps 106: static const char *const htmlattrs[ATTR_MAX] = {
107: "http-equiv",
108: "content",
109: "name",
110: "rel",
111: "href",
112: "type",
113: "media",
1.33 kristaps 114: "class",
115: "style",
116: "width",
117: "valign",
1.29 kristaps 118: };
1.10 kristaps 119:
1.29 kristaps 120: struct htmlpair {
121: enum htmlattr key;
122: char *val;
123: };
1.10 kristaps 124:
1.30 kristaps 125: struct tag {
126: enum htmltag tag;
127: SLIST_ENTRY(tag) entry;
128: };
129:
1.37 kristaps 130: struct ord {
131: int pos;
132: const void *cookie;
133: SLIST_ENTRY(ord) entry;
134: };
135:
1.30 kristaps 136: SLIST_HEAD(tagq, tag);
1.37 kristaps 137: SLIST_HEAD(ordq, ord);
1.30 kristaps 138:
1.29 kristaps 139: struct html {
140: int flags;
141: #define HTML_NOSPACE (1 << 0)
1.30 kristaps 142: #define HTML_NEWLINE (1 << 1)
1.37 kristaps 143: struct tagq tags;
144: struct ordq ords;
1.32 kristaps 145: void *symtab;
1.29 kristaps 146: };
1.10 kristaps 147:
1.29 kristaps 148: #define MDOC_ARGS const struct mdoc_meta *m, \
149: const struct mdoc_node *n, \
150: struct html *h
151: #define MAN_ARGS const struct man_meta *m, \
152: const struct man_node *n, \
153: struct html *h
154: struct htmlmdoc {
155: int (*pre)(MDOC_ARGS);
156: void (*post)(MDOC_ARGS);
157: };
1.13 kristaps 158:
1.29 kristaps 159: static void print_gen_doctype(struct html *);
160: static void print_gen_head(struct html *);
161: static void print_mdoc(MDOC_ARGS);
162: static void print_mdoc_head(MDOC_ARGS);
1.30 kristaps 163: static void print_mdoc_title(MDOC_ARGS);
1.29 kristaps 164: static void print_mdoc_node(MDOC_ARGS);
1.39 kristaps 165: static void print_mdoc_nodelist(MDOC_ARGS);
1.29 kristaps 166: static void print_man(MAN_ARGS);
167: static void print_man_head(MAN_ARGS);
168: static void print_man_body(MAN_ARGS);
1.30 kristaps 169: static struct tag *print_otag(struct html *, enum htmltag,
1.29 kristaps 170: int, const struct htmlpair *);
1.30 kristaps 171: static void print_tagq(struct html *, const struct tag *);
172: static void print_stagq(struct html *, const struct tag *);
1.29 kristaps 173: static void print_ctag(struct html *, enum htmltag);
1.32 kristaps 174: static void print_encode(struct html *, const char *);
175: static void print_escape(struct html *, const char **);
1.29 kristaps 176: static void print_text(struct html *, const char *);
1.32 kristaps 177: static void print_res(struct html *, const char *, int);
178: static void print_spec(struct html *, const char *, int);
1.33 kristaps 179:
180: static int a2width(const char *);
181: static int a2offs(const char *);
1.37 kristaps 182: static int a2list(const struct mdoc_node *);
1.33 kristaps 183:
1.39 kristaps 184: static void mdoc_root_post(MDOC_ARGS);
1.29 kristaps 185: static int mdoc_root_pre(MDOC_ARGS);
1.34 kristaps 186: static int mdoc_tbl_pre(MDOC_ARGS, int);
1.37 kristaps 187: static int mdoc_tbl_block_pre(MDOC_ARGS, int, int, int, int);
1.34 kristaps 188: static int mdoc_tbl_body_pre(MDOC_ARGS, int, int);
189: static int mdoc_tbl_head_pre(MDOC_ARGS, int, int);
1.29 kristaps 190:
1.37 kristaps 191: static void mdoc_aq_post(MDOC_ARGS);
192: static int mdoc_aq_pre(MDOC_ARGS);
1.31 kristaps 193: static int mdoc_ar_pre(MDOC_ARGS);
1.39 kristaps 194: static int mdoc_bd_pre(MDOC_ARGS);
1.37 kristaps 195: static void mdoc_bl_post(MDOC_ARGS);
1.33 kristaps 196: static int mdoc_bl_pre(MDOC_ARGS);
1.34 kristaps 197: static int mdoc_d1_pre(MDOC_ARGS);
198: static void mdoc_dq_post(MDOC_ARGS);
199: static int mdoc_dq_pre(MDOC_ARGS);
1.30 kristaps 200: static int mdoc_fl_pre(MDOC_ARGS);
1.34 kristaps 201: static int mdoc_em_pre(MDOC_ARGS);
202: static int mdoc_ex_pre(MDOC_ARGS);
1.33 kristaps 203: static int mdoc_it_pre(MDOC_ARGS);
1.29 kristaps 204: static int mdoc_nd_pre(MDOC_ARGS);
205: static int mdoc_nm_pre(MDOC_ARGS);
1.31 kristaps 206: static int mdoc_ns_pre(MDOC_ARGS);
1.34 kristaps 207: static void mdoc_op_post(MDOC_ARGS);
1.30 kristaps 208: static int mdoc_op_pre(MDOC_ARGS);
1.39 kristaps 209: static int mdoc_pa_pre(MDOC_ARGS);
1.29 kristaps 210: static int mdoc_pp_pre(MDOC_ARGS);
1.34 kristaps 211: static void mdoc_pq_post(MDOC_ARGS);
212: static int mdoc_pq_pre(MDOC_ARGS);
1.39 kristaps 213: static void mdoc_qq_post(MDOC_ARGS);
214: static int mdoc_qq_pre(MDOC_ARGS);
1.29 kristaps 215: static int mdoc_sh_pre(MDOC_ARGS);
1.34 kristaps 216: static void mdoc_sq_post(MDOC_ARGS);
217: static int mdoc_sq_pre(MDOC_ARGS);
1.29 kristaps 218: static int mdoc_ss_pre(MDOC_ARGS);
1.34 kristaps 219: static int mdoc_sx_pre(MDOC_ARGS);
1.30 kristaps 220: static int mdoc_xr_pre(MDOC_ARGS);
1.33 kristaps 221: static int mdoc_xx_pre(MDOC_ARGS);
222:
223: #ifdef __linux__
1.35 kristaps 224: extern size_t strlcpy(char *, const char *, size_t);
225: extern size_t strlcat(char *, const char *, size_t);
1.33 kristaps 226: #endif
1.29 kristaps 227:
228: static const struct htmlmdoc mdocs[MDOC_MAX] = {
1.39 kristaps 229: {mdoc_pp_pre, NULL}, /* Ap */
1.29 kristaps 230: {NULL, NULL}, /* Dd */
231: {NULL, NULL}, /* Dt */
232: {NULL, NULL}, /* Os */
1.30 kristaps 233: {mdoc_sh_pre, NULL }, /* Sh */
234: {mdoc_ss_pre, NULL }, /* Ss */
1.29 kristaps 235: {mdoc_pp_pre, NULL}, /* Pp */
1.34 kristaps 236: {mdoc_d1_pre, NULL}, /* D1 */
237: {mdoc_d1_pre, NULL}, /* Dl */
1.39 kristaps 238: {mdoc_bd_pre, NULL}, /* Bd */
1.29 kristaps 239: {NULL, NULL}, /* Ed */
1.37 kristaps 240: {mdoc_bl_pre, mdoc_bl_post}, /* Bl */
1.29 kristaps 241: {NULL, NULL}, /* El */
1.33 kristaps 242: {mdoc_it_pre, NULL}, /* It */
1.29 kristaps 243: {NULL, NULL}, /* Ad */
244: {NULL, NULL}, /* An */
1.31 kristaps 245: {mdoc_ar_pre, NULL}, /* Ar */
1.29 kristaps 246: {NULL, NULL}, /* Cd */
247: {NULL, NULL}, /* Cm */
248: {NULL, NULL}, /* Dv */
249: {NULL, NULL}, /* Er */
250: {NULL, NULL}, /* Ev */
1.34 kristaps 251: {mdoc_ex_pre, NULL}, /* Ex */
1.29 kristaps 252: {NULL, NULL}, /* Fa */
253: {NULL, NULL}, /* Fd */
1.30 kristaps 254: {mdoc_fl_pre, NULL}, /* Fl */
1.29 kristaps 255: {NULL, NULL}, /* Fn */
256: {NULL, NULL}, /* Ft */
257: {NULL, NULL}, /* Ic */
258: {NULL, NULL}, /* In */
259: {NULL, NULL}, /* Li */
260: {mdoc_nd_pre, NULL}, /* Nd */
1.30 kristaps 261: {mdoc_nm_pre, NULL}, /* Nm */
262: {mdoc_op_pre, mdoc_op_post}, /* Op */
1.29 kristaps 263: {NULL, NULL}, /* Ot */
1.39 kristaps 264: {mdoc_pa_pre, NULL}, /* Pa */
1.29 kristaps 265: {NULL, NULL}, /* Rv */
266: {NULL, NULL}, /* St */
267: {NULL, NULL}, /* Va */
268: {NULL, NULL}, /* Vt */
1.30 kristaps 269: {mdoc_xr_pre, NULL}, /* Xr */
1.29 kristaps 270: {NULL, NULL}, /* %A */
271: {NULL, NULL}, /* %B */
272: {NULL, NULL}, /* %D */
273: {NULL, NULL}, /* %I */
274: {NULL, NULL}, /* %J */
275: {NULL, NULL}, /* %N */
276: {NULL, NULL}, /* %O */
277: {NULL, NULL}, /* %P */
278: {NULL, NULL}, /* %R */
279: {NULL, NULL}, /* %T */
280: {NULL, NULL}, /* %V */
281: {NULL, NULL}, /* Ac */
1.37 kristaps 282: {mdoc_aq_pre, mdoc_aq_post}, /* Ao */
283: {mdoc_aq_pre, mdoc_aq_post}, /* Aq */
1.29 kristaps 284: {NULL, NULL}, /* At */
285: {NULL, NULL}, /* Bc */
286: {NULL, NULL}, /* Bf */
287: {NULL, NULL}, /* Bo */
288: {NULL, NULL}, /* Bq */
1.33 kristaps 289: {mdoc_xx_pre, NULL}, /* Bsx */
1.29 kristaps 290: {NULL, NULL}, /* Bx */
291: {NULL, NULL}, /* Db */
292: {NULL, NULL}, /* Dc */
293: {NULL, NULL}, /* Do */
1.34 kristaps 294: {mdoc_dq_pre, mdoc_dq_post}, /* Dq */
1.29 kristaps 295: {NULL, NULL}, /* Ec */
296: {NULL, NULL}, /* Ef */
1.34 kristaps 297: {mdoc_em_pre, NULL}, /* Em */
1.29 kristaps 298: {NULL, NULL}, /* Eo */
1.33 kristaps 299: {mdoc_xx_pre, NULL}, /* Fx */
1.29 kristaps 300: {NULL, NULL}, /* Ms */
301: {NULL, NULL}, /* No */
1.31 kristaps 302: {mdoc_ns_pre, NULL}, /* Ns */
1.33 kristaps 303: {mdoc_xx_pre, NULL}, /* Nx */
304: {mdoc_xx_pre, NULL}, /* Ox */
1.29 kristaps 305: {NULL, NULL}, /* Pc */
306: {NULL, NULL}, /* Pf */
1.34 kristaps 307: {mdoc_pq_pre, mdoc_pq_post}, /* Po */
308: {mdoc_pq_pre, mdoc_pq_post}, /* Pq */
1.29 kristaps 309: {NULL, NULL}, /* Qc */
310: {NULL, NULL}, /* Ql */
1.39 kristaps 311: {mdoc_qq_pre, mdoc_qq_post}, /* Qo */
312: {mdoc_qq_pre, mdoc_qq_post}, /* Qq */
1.29 kristaps 313: {NULL, NULL}, /* Re */
314: {NULL, NULL}, /* Rs */
315: {NULL, NULL}, /* Sc */
1.34 kristaps 316: {mdoc_sq_pre, mdoc_sq_post}, /* So */
317: {mdoc_sq_pre, mdoc_sq_post}, /* Sq */
1.29 kristaps 318: {NULL, NULL}, /* Sm */
1.34 kristaps 319: {mdoc_sx_pre, NULL}, /* Sx */
1.29 kristaps 320: {NULL, NULL}, /* Sy */
321: {NULL, NULL}, /* Tn */
1.33 kristaps 322: {mdoc_xx_pre, NULL}, /* Ux */
1.29 kristaps 323: {NULL, NULL}, /* Xc */
324: {NULL, NULL}, /* Xo */
325: {NULL, NULL}, /* Fo */
326: {NULL, NULL}, /* Fc */
327: {NULL, NULL}, /* Oo */
328: {NULL, NULL}, /* Oc */
329: {NULL, NULL}, /* Bk */
330: {NULL, NULL}, /* Ek */
331: {NULL, NULL}, /* Bt */
332: {NULL, NULL}, /* Hf */
333: {NULL, NULL}, /* Fr */
334: {NULL, NULL}, /* Ud */
335: {NULL, NULL}, /* Lb */
336: {NULL, NULL}, /* Lp */
337: {NULL, NULL}, /* Lk */
338: {NULL, NULL}, /* Mt */
339: {NULL, NULL}, /* Brq */
340: {NULL, NULL}, /* Bro */
341: {NULL, NULL}, /* Brc */
342: {NULL, NULL}, /* %C */
343: {NULL, NULL}, /* Es */
344: {NULL, NULL}, /* En */
1.33 kristaps 345: {mdoc_xx_pre, NULL}, /* Dx */
1.29 kristaps 346: {NULL, NULL}, /* %Q */
347: {NULL, NULL}, /* br */
348: {NULL, NULL}, /* sp */
349: };
1.10 kristaps 350:
1.35 kristaps 351: static char buf[BUFSIZ]; /* XXX */
352:
353: #define bufcat(x) (void)strlcat(buf, (x), BUFSIZ)
354: #define bufinit() buf[0] = 0
355: #define buffmt(...) (void)snprintf(buf, BUFSIZ - 1, __VA_ARGS__)
1.33 kristaps 356:
1.30 kristaps 357: void
1.29 kristaps 358: html_mdoc(void *arg, const struct mdoc *m)
1.10 kristaps 359: {
1.29 kristaps 360: struct html *h;
1.30 kristaps 361: struct tag *t;
1.10 kristaps 362:
1.29 kristaps 363: h = (struct html *)arg;
1.10 kristaps 364:
1.29 kristaps 365: print_gen_doctype(h);
1.30 kristaps 366: t = print_otag(h, TAG_HTML, 0, NULL);
1.29 kristaps 367: print_mdoc(mdoc_meta(m), mdoc_node(m), h);
1.30 kristaps 368: print_tagq(h, t);
369:
1.29 kristaps 370: printf("\n");
1.10 kristaps 371: }
372:
1.33 kristaps 373:
1.30 kristaps 374: void
1.29 kristaps 375: html_man(void *arg, const struct man *m)
1.10 kristaps 376: {
1.29 kristaps 377: struct html *h;
1.30 kristaps 378: struct tag *t;
1.10 kristaps 379:
1.29 kristaps 380: h = (struct html *)arg;
1.10 kristaps 381:
1.29 kristaps 382: print_gen_doctype(h);
1.30 kristaps 383: t = print_otag(h, TAG_HTML, 0, NULL);
1.29 kristaps 384: print_man(man_meta(m), man_node(m), h);
1.30 kristaps 385: print_tagq(h, t);
386:
1.29 kristaps 387: printf("\n");
1.10 kristaps 388: }
389:
1.33 kristaps 390:
1.29 kristaps 391: void *
392: html_alloc(void)
1.10 kristaps 393: {
1.30 kristaps 394: struct html *h;
395:
396: if (NULL == (h = calloc(1, sizeof(struct html))))
397: return(NULL);
1.10 kristaps 398:
1.37 kristaps 399: SLIST_INIT(&h->tags);
400: SLIST_INIT(&h->ords);
401:
1.32 kristaps 402: if (NULL == (h->symtab = chars_init(CHARS_HTML))) {
403: free(h);
404: return(NULL);
405: }
1.30 kristaps 406: return(h);
1.29 kristaps 407: }
1.10 kristaps 408:
1.33 kristaps 409:
1.29 kristaps 410: void
411: html_free(void *p)
412: {
1.30 kristaps 413: struct tag *tag;
1.37 kristaps 414: struct ord *ord;
1.30 kristaps 415: struct html *h;
416:
417: h = (struct html *)p;
1.10 kristaps 418:
1.37 kristaps 419: while ( ! SLIST_EMPTY(&h->ords)) {
420: ord = SLIST_FIRST(&h->ords);
421: SLIST_REMOVE_HEAD(&h->ords, entry);
422: free(ord);
423: }
424:
425: while ( ! SLIST_EMPTY(&h->tags)) {
426: tag = SLIST_FIRST(&h->tags);
427: SLIST_REMOVE_HEAD(&h->tags, entry);
1.30 kristaps 428: free(tag);
429: }
1.36 kristaps 430:
431: if (h->symtab)
432: chars_free(h->symtab);
1.30 kristaps 433: free(h);
1.10 kristaps 434: }
1.2 kristaps 435:
1.33 kristaps 436:
1.29 kristaps 437: static void
438: print_mdoc(MDOC_ARGS)
1.4 kristaps 439: {
1.30 kristaps 440: struct tag *t;
1.4 kristaps 441:
1.30 kristaps 442: t = print_otag(h, TAG_HEAD, 0, NULL);
1.29 kristaps 443: print_mdoc_head(m, n, h);
1.30 kristaps 444: print_tagq(h, t);
445:
446: t = print_otag(h, TAG_BODY, 0, NULL);
447: print_mdoc_title(m, n, h);
1.39 kristaps 448: print_mdoc_nodelist(m, n, h);
1.30 kristaps 449: print_tagq(h, t);
1.29 kristaps 450: }
1.4 kristaps 451:
1.33 kristaps 452:
1.29 kristaps 453: static void
454: print_gen_head(struct html *h)
455: {
456: struct htmlpair meta0[2];
457: struct htmlpair meta1[2];
458: struct htmlpair link[4];
459:
460: meta0[0].key = ATTR_HTTPEQUIV;
461: meta0[0].val = "Content-Type";
462: meta0[1].key = ATTR_CONTENT;
1.34 kristaps 463: meta0[1].val = "text/html; charset=utf-8";
1.29 kristaps 464:
465: meta1[0].key = ATTR_NAME;
466: meta1[0].val = "resource-type";
467: meta1[1].key = ATTR_CONTENT;
468: meta1[1].val = "document";
469:
470: link[0].key = ATTR_REL;
471: link[0].val = "stylesheet";
472: link[1].key = ATTR_HREF;
1.30 kristaps 473: link[1].val = "style.css"; /* XXX */
1.29 kristaps 474: link[2].key = ATTR_TYPE;
475: link[2].val = "text/css";
476: link[3].key = ATTR_MEDIA;
477: link[3].val = "all";
478:
479: print_otag(h, TAG_META, 2, meta0);
480: print_otag(h, TAG_META, 2, meta1);
481: print_otag(h, TAG_LINK, 4, link);
1.4 kristaps 482: }
483:
1.33 kristaps 484:
1.30 kristaps 485: /* ARGSUSED */
1.29 kristaps 486: static void
487: print_mdoc_head(MDOC_ARGS)
1.18 kristaps 488: {
489:
1.29 kristaps 490: print_gen_head(h);
491: print_otag(h, TAG_TITLE, 0, NULL);
1.32 kristaps 492: print_encode(h, m->title);
1.2 kristaps 493: }
494:
1.33 kristaps 495:
1.30 kristaps 496: /* ARGSUSED */
1.29 kristaps 497: static void
1.30 kristaps 498: print_mdoc_title(MDOC_ARGS)
1.2 kristaps 499: {
500:
1.30 kristaps 501: /* TODO */
1.2 kristaps 502: }
503:
1.33 kristaps 504:
1.29 kristaps 505: static void
1.39 kristaps 506: print_mdoc_nodelist(MDOC_ARGS)
507: {
508:
509: print_mdoc_node(m, n, h);
510: if (n->next)
511: print_mdoc_nodelist(m, n->next, h);
512: }
513:
514:
515: static void
1.29 kristaps 516: print_mdoc_node(MDOC_ARGS)
1.2 kristaps 517: {
1.29 kristaps 518: int child;
1.30 kristaps 519: struct tag *t;
1.8 kristaps 520:
1.29 kristaps 521: child = 1;
1.37 kristaps 522: t = SLIST_FIRST(&h->tags);
1.8 kristaps 523:
1.35 kristaps 524: bufinit();
525:
1.29 kristaps 526: switch (n->type) {
527: case (MDOC_ROOT):
528: child = mdoc_root_pre(m, n, h);
1.7 kristaps 529: break;
1.29 kristaps 530: case (MDOC_TEXT):
531: print_text(h, n->string);
1.7 kristaps 532: break;
1.3 kristaps 533: default:
1.29 kristaps 534: if (mdocs[n->tok].pre)
535: child = (*mdocs[n->tok].pre)(m, n, h);
1.3 kristaps 536: break;
1.2 kristaps 537: }
538:
1.29 kristaps 539: if (child && n->child)
1.39 kristaps 540: print_mdoc_nodelist(m, n->child, h);
1.8 kristaps 541:
1.30 kristaps 542: print_stagq(h, t);
543:
1.35 kristaps 544: bufinit();
545:
1.29 kristaps 546: switch (n->type) {
547: case (MDOC_ROOT):
1.39 kristaps 548: mdoc_root_post(m, n, h);
1.7 kristaps 549: break;
1.29 kristaps 550: case (MDOC_TEXT):
1.7 kristaps 551: break;
1.3 kristaps 552: default:
1.29 kristaps 553: if (mdocs[n->tok].post)
554: (*mdocs[n->tok].post)(m, n, h);
1.3 kristaps 555: break;
556: }
1.2 kristaps 557: }
558:
1.33 kristaps 559:
1.29 kristaps 560: static void
561: print_man(MAN_ARGS)
1.9 kristaps 562: {
1.30 kristaps 563: struct tag *t;
1.9 kristaps 564:
1.30 kristaps 565: t = print_otag(h, TAG_HEAD, 0, NULL);
1.29 kristaps 566: print_man_head(m, n, h);
1.30 kristaps 567: print_tagq(h, t);
568:
569: t = print_otag(h, TAG_BODY, 0, NULL);
1.29 kristaps 570: print_man_body(m, n, h);
1.30 kristaps 571: print_tagq(h, t);
1.9 kristaps 572: }
573:
1.33 kristaps 574:
1.30 kristaps 575: /* ARGSUSED */
1.9 kristaps 576: static void
1.29 kristaps 577: print_man_head(MAN_ARGS)
1.9 kristaps 578: {
579:
1.29 kristaps 580: print_gen_head(h);
581: print_otag(h, TAG_TITLE, 0, NULL);
1.32 kristaps 582: print_encode(h, m->title);
1.29 kristaps 583: }
1.9 kristaps 584:
1.33 kristaps 585:
1.30 kristaps 586: /* ARGSUSED */
1.29 kristaps 587: static void
588: print_man_body(MAN_ARGS)
589: {
1.30 kristaps 590:
591: /* TODO */
1.9 kristaps 592: }
593:
1.33 kristaps 594:
1.32 kristaps 595: static void
596: print_spec(struct html *h, const char *p, int len)
597: {
598: const char *rhs;
599: int i;
600: size_t sz;
601:
602: rhs = chars_a2ascii(h->symtab, p, (size_t)len, &sz);
603:
604: if (NULL == rhs)
605: return;
606: for (i = 0; i < (int)sz; i++)
607: putchar(rhs[i]);
608: }
609:
1.33 kristaps 610:
1.32 kristaps 611: static void
612: print_res(struct html *h, const char *p, int len)
613: {
614: const char *rhs;
615: int i;
616: size_t sz;
617:
618: rhs = chars_a2res(h->symtab, p, (size_t)len, &sz);
619:
620: if (NULL == rhs)
621: return;
622: for (i = 0; i < (int)sz; i++)
623: putchar(rhs[i]);
624: }
625:
1.33 kristaps 626:
1.32 kristaps 627: static void
628: print_escape(struct html *h, const char **p)
629: {
630: int j, type;
631: const char *wp;
632:
633: wp = *p;
634: type = 1;
635:
636: if (0 == *(++wp)) {
637: *p = wp;
638: return;
639: }
640:
641: if ('(' == *wp) {
642: wp++;
643: if (0 == *wp || 0 == *(wp + 1)) {
644: *p = 0 == *wp ? wp : wp + 1;
645: return;
646: }
647:
648: print_spec(h, wp, 2);
649: *p = ++wp;
650: return;
651:
652: } else if ('*' == *wp) {
653: if (0 == *(++wp)) {
654: *p = wp;
655: return;
656: }
657:
658: switch (*wp) {
659: case ('('):
660: wp++;
661: if (0 == *wp || 0 == *(wp + 1)) {
662: *p = 0 == *wp ? wp : wp + 1;
663: return;
664: }
665:
666: print_res(h, wp, 2);
667: *p = ++wp;
668: return;
669: case ('['):
670: type = 0;
671: break;
672: default:
673: print_res(h, wp, 1);
674: *p = wp;
675: return;
676: }
677:
678: } else if ('f' == *wp) {
679: if (0 == *(++wp)) {
680: *p = wp;
681: return;
682: }
683:
684: switch (*wp) {
685: case ('B'):
686: /* TODO */
687: break;
688: case ('I'):
689: /* TODO */
690: break;
691: case ('P'):
692: /* FALLTHROUGH */
693: case ('R'):
694: /* TODO */
695: break;
696: default:
697: break;
698: }
699:
700: *p = wp;
701: return;
702:
703: } else if ('[' != *wp) {
704: print_spec(h, wp, 1);
705: *p = wp;
706: return;
707: }
708:
709: wp++;
710: for (j = 0; *wp && ']' != *wp; wp++, j++)
711: /* Loop... */ ;
712:
713: if (0 == *wp) {
714: *p = wp;
715: return;
716: }
717:
718: if (type)
719: print_spec(h, wp - j, j);
720: else
721: print_res(h, wp - j, j);
722:
723: *p = wp;
724: }
725:
1.9 kristaps 726:
1.29 kristaps 727: static void
1.32 kristaps 728: print_encode(struct html *h, const char *p)
1.29 kristaps 729: {
1.14 kristaps 730:
1.32 kristaps 731: for (; *p; p++) {
1.34 kristaps 732: if ('\\' == *p) {
733: print_escape(h, &p);
734: continue;
735: }
736: switch (*p) {
737: case ('<'):
738: printf("<");
739: break;
740: case ('>'):
741: printf(">");
742: break;
743: case ('&'):
744: printf("&");
745: break;
746: default:
1.32 kristaps 747: putchar(*p);
1.34 kristaps 748: break;
1.32 kristaps 749: }
750: }
1.14 kristaps 751: }
752:
753:
1.30 kristaps 754: static struct tag *
1.29 kristaps 755: print_otag(struct html *h, enum htmltag tag,
756: int sz, const struct htmlpair *p)
1.14 kristaps 757: {
1.29 kristaps 758: int i;
1.30 kristaps 759: struct tag *t;
760:
761: if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
762: if (NULL == (t = malloc(sizeof(struct tag))))
763: err(EXIT_FAILURE, "malloc");
764: t->tag = tag;
1.37 kristaps 765: SLIST_INSERT_HEAD(&h->tags, t, entry);
1.30 kristaps 766: } else
767: t = NULL;
1.29 kristaps 768:
769: if ( ! (HTML_NOSPACE & h->flags))
1.30 kristaps 770: if ( ! (HTML_CLRLINE & htmltags[tag].flags))
1.29 kristaps 771: printf(" ");
772:
773: printf("<%s", htmltags[tag].name);
774: for (i = 0; i < sz; i++) {
775: printf(" %s=\"", htmlattrs[p[i].key]);
776: assert(p->val);
1.32 kristaps 777: print_encode(h, p[i].val);
1.29 kristaps 778: printf("\"");
779: }
780: printf(">");
1.14 kristaps 781:
1.29 kristaps 782: h->flags |= HTML_NOSPACE;
1.30 kristaps 783: if (HTML_CLRLINE & htmltags[tag].flags)
784: h->flags |= HTML_NEWLINE;
785: else
786: h->flags &= ~HTML_NEWLINE;
1.14 kristaps 787:
1.30 kristaps 788: return(t);
1.14 kristaps 789: }
790:
791:
792: /* ARGSUSED */
1.29 kristaps 793: static void
794: print_ctag(struct html *h, enum htmltag tag)
1.14 kristaps 795: {
796:
1.29 kristaps 797: printf("</%s>", htmltags[tag].name);
1.30 kristaps 798: if (HTML_CLRLINE & htmltags[tag].flags)
1.29 kristaps 799: h->flags |= HTML_NOSPACE;
1.30 kristaps 800: if (HTML_CLRLINE & htmltags[tag].flags)
801: h->flags |= HTML_NEWLINE;
802: else
803: h->flags &= ~HTML_NEWLINE;
1.14 kristaps 804: }
805:
806:
1.29 kristaps 807: /* ARGSUSED */
808: static void
809: print_gen_doctype(struct html *h)
1.1 kristaps 810: {
1.29 kristaps 811:
812: printf("<!DOCTYPE HTML PUBLIC \"%s\" \"%s\">\n", DOCTYPE, DTD);
1.1 kristaps 813: }
814:
815:
1.29 kristaps 816: static void
817: print_text(struct html *h, const char *p)
1.1 kristaps 818: {
819:
1.29 kristaps 820: if (*p && 0 == *(p + 1))
821: switch (*p) {
822: case('.'):
823: /* FALLTHROUGH */
824: case(','):
825: /* FALLTHROUGH */
826: case(';'):
827: /* FALLTHROUGH */
828: case(':'):
829: /* FALLTHROUGH */
830: case('?'):
831: /* FALLTHROUGH */
832: case('!'):
833: /* FALLTHROUGH */
834: case(')'):
835: /* FALLTHROUGH */
836: case(']'):
837: /* FALLTHROUGH */
838: case('}'):
839: h->flags |= HTML_NOSPACE;
1.30 kristaps 840: break;
1.29 kristaps 841: default:
842: break;
843: }
1.1 kristaps 844:
1.29 kristaps 845: if ( ! (h->flags & HTML_NOSPACE))
846: printf(" ");
1.30 kristaps 847:
1.29 kristaps 848: h->flags &= ~HTML_NOSPACE;
1.30 kristaps 849: h->flags &= ~HTML_NEWLINE;
1.1 kristaps 850:
1.29 kristaps 851: if (p)
1.32 kristaps 852: print_encode(h, p);
1.8 kristaps 853:
1.29 kristaps 854: if (*p && 0 == *(p + 1))
855: switch (*p) {
856: case('('):
857: /* FALLTHROUGH */
858: case('['):
859: /* FALLTHROUGH */
860: case('{'):
861: h->flags |= HTML_NOSPACE;
1.30 kristaps 862: break;
1.29 kristaps 863: default:
864: break;
865: }
1.1 kristaps 866: }
1.30 kristaps 867:
868:
869: static void
870: print_tagq(struct html *h, const struct tag *until)
871: {
872: struct tag *tag;
873:
1.37 kristaps 874: while ( ! SLIST_EMPTY(&h->tags)) {
875: tag = SLIST_FIRST(&h->tags);
1.30 kristaps 876: print_ctag(h, tag->tag);
1.37 kristaps 877: SLIST_REMOVE_HEAD(&h->tags, entry);
1.30 kristaps 878: free(tag);
879: if (until && tag == until)
880: return;
881: }
882: }
883:
884:
885: static void
886: print_stagq(struct html *h, const struct tag *suntil)
887: {
888: struct tag *tag;
889:
1.37 kristaps 890: while ( ! SLIST_EMPTY(&h->tags)) {
891: tag = SLIST_FIRST(&h->tags);
1.30 kristaps 892: if (suntil && tag == suntil)
893: return;
894: print_ctag(h, tag->tag);
1.37 kristaps 895: SLIST_REMOVE_HEAD(&h->tags, entry);
1.30 kristaps 896: free(tag);
897: }
898: }
899:
900:
1.35 kristaps 901: /* FIXME: put in utility file for front-ends. */
1.33 kristaps 902: static int
903: a2offs(const char *p)
904: {
905: int len, i;
906:
907: if (0 == strcmp(p, "left"))
908: return(0);
909: if (0 == strcmp(p, "indent"))
910: return(INDENT + 1);
911: if (0 == strcmp(p, "indent-two"))
912: return((INDENT + 1) * 2);
913:
914: if (0 == (len = (int)strlen(p)))
915: return(0);
916:
917: for (i = 0; i < len - 1; i++)
918: if ( ! isdigit((u_char)p[i]))
919: break;
920:
921: if (i == len - 1)
922: if ('n' == p[len - 1] || 'm' == p[len - 1])
923: return(atoi(p));
924:
925: return(len);
926: }
927:
928:
1.35 kristaps 929: /* FIXME: put in utility file for front-ends. */
1.33 kristaps 930: static int
1.37 kristaps 931: a2list(const struct mdoc_node *bl)
932: {
933: int i;
934:
935: assert(MDOC_BLOCK == bl->type && MDOC_Bl == bl->tok);
936: assert(bl->args);
937:
938: for (i = 0; i < (int)bl->args->argc; i++)
939: switch (bl->args->argv[i].arg) {
940: case (MDOC_Enum):
941: /* FALLTHROUGH */
942: case (MDOC_Dash):
943: /* FALLTHROUGH */
944: case (MDOC_Hyphen):
945: /* FALLTHROUGH */
946: case (MDOC_Bullet):
947: /* FALLTHROUGH */
948: case (MDOC_Tag):
949: /* FALLTHROUGH */
950: case (MDOC_Hang):
951: /* FALLTHROUGH */
952: case (MDOC_Inset):
953: /* FALLTHROUGH */
954: case (MDOC_Diag):
955: /* FALLTHROUGH */
956: case (MDOC_Item):
957: /* FALLTHROUGH */
958: case (MDOC_Column):
959: /* FALLTHROUGH */
960: case (MDOC_Ohang):
961: return(bl->args->argv[i].arg);
962: default:
963: break;
964: }
965:
966: abort();
967: /* NOTREACHED */
968: }
969:
970:
971: /* FIXME: put in utility file for front-ends. */
972: static int
1.33 kristaps 973: a2width(const char *p)
974: {
975: int i, len;
976:
977: if (0 == (len = (int)strlen(p)))
978: return(0);
979: for (i = 0; i < len - 1; i++)
980: if ( ! isdigit((u_char)p[i]))
981: break;
982:
983: if (i == len - 1)
984: if ('n' == p[len - 1] || 'm' == p[len - 1])
985: return(atoi(p) + 2);
986:
987: return(len + 2);
988: }
989:
990:
1.39 kristaps 991: /* FIXME: parts should be in a utility file for front-ends. */
992: /* ARGSUSED */
993: static void
994: mdoc_root_post(MDOC_ARGS)
995: {
996: struct tm *tm;
1.40 ! kristaps 997: struct htmlpair tag;
1.39 kristaps 998: struct tag *t;
999: char b[BUFSIZ], os[BUFSIZ];
1000:
1001: tm = localtime(&m->date);
1002:
1003: if (0 == strftime(b, BUFSIZ - 1, "%B %e, %Y", tm))
1004: err(EXIT_FAILURE, "strftime");
1.33 kristaps 1005:
1.40 ! kristaps 1006: strlcpy(os, m->os, BUFSIZ);
1.33 kristaps 1007:
1.40 ! kristaps 1008: tag.key = ATTR_CLASS;
! 1009: tag.val = "foot";
1.39 kristaps 1010:
1.40 ! kristaps 1011: t = print_otag(h, TAG_DIV, 1, &tag);
1.39 kristaps 1012:
1.40 ! kristaps 1013: tag.key = ATTR_STYLE;
! 1014: tag.val = "width: 49%; display: "
! 1015: "inline-block; text-align: left;";
! 1016: print_otag(h, TAG_SPAN, 1, &tag);
1.39 kristaps 1017: print_text(h, b);
1018: print_stagq(h, t);
1019:
1.40 ! kristaps 1020: tag.key = ATTR_STYLE;
! 1021: tag.val = "width: 49%; display: "
! 1022: "inline-block; text-align: right;";
! 1023: print_otag(h, TAG_SPAN, 1, &tag);
1.39 kristaps 1024: print_text(h, os);
1025: print_tagq(h, t);
1026:
1027: }
1028:
1029:
1030: /* FIXME: parts should be in a utility file for front-ends. */
1.30 kristaps 1031: /* ARGSUSED */
1032: static int
1033: mdoc_root_pre(MDOC_ARGS)
1034: {
1.40 ! kristaps 1035: struct htmlpair tag;
1.39 kristaps 1036: struct tag *t, *tt;
1037: char b[BUFSIZ], title[BUFSIZ];
1038:
1039: assert(m->vol);
1040: (void)strlcpy(b, m->vol, BUFSIZ);
1041:
1042: if (m->arch) {
1.40 ! kristaps 1043: strlcat(b, " (", BUFSIZ);
! 1044: strlcat(b, m->arch, BUFSIZ);
! 1045: strlcat(b, ")", BUFSIZ);
1.39 kristaps 1046: }
1047:
1.40 ! kristaps 1048: snprintf(title, BUFSIZ - 1, "%s(%d)", m->title, m->msec);
1.39 kristaps 1049:
1.40 ! kristaps 1050: tag.key = ATTR_CLASS;
! 1051: tag.val = "body";
1.39 kristaps 1052:
1.40 ! kristaps 1053: t = print_otag(h, TAG_DIV, 1, &tag);
1.39 kristaps 1054:
1.40 ! kristaps 1055: tag.key = ATTR_CLASS;
! 1056: tag.val = "head";
1.39 kristaps 1057:
1.40 ! kristaps 1058: tt = print_otag(h, TAG_DIV, 1, &tag);
1.39 kristaps 1059:
1.40 ! kristaps 1060: tag.key = ATTR_STYLE;
! 1061: tag.val = "width: 32%; text-align: left; "
! 1062: "display: inline-block;";
! 1063: print_otag(h, TAG_SPAN, 1, &tag);
1.39 kristaps 1064: print_text(h, b);
1065: print_stagq(h, tt);
1066:
1.40 ! kristaps 1067: tag.key = ATTR_STYLE;
! 1068: tag.val = "width: 32%; text-align: center; "
! 1069: "display: inline-block;";
! 1070: print_otag(h, TAG_SPAN, 1, &tag);
1.39 kristaps 1071: print_text(h, title);
1072: print_stagq(h, tt);
1073:
1.40 ! kristaps 1074: tag.key = ATTR_STYLE;
! 1075: tag.val = "width: 32%; text-align: right; "
! 1076: "display: inline-block;";
! 1077: print_otag(h, TAG_SPAN, 1, &tag);
1.39 kristaps 1078: print_text(h, b);
1079: print_stagq(h, t);
1.30 kristaps 1080:
1081: return(1);
1082: }
1083:
1084:
1085: /* ARGSUSED */
1086: static int
1.35 kristaps 1087: mdoc_sh_pre(MDOC_ARGS)
1.30 kristaps 1088: {
1.34 kristaps 1089: struct htmlpair tag[2];
1090:
1.35 kristaps 1091: if (MDOC_HEAD == n->type) {
1092: tag[0].key = ATTR_CLASS;
1093: tag[0].val = "sec-head";
1094: print_otag(h, TAG_DIV, 1, tag);
1095: print_otag(h, TAG_SPAN, 1, tag);
1096: return(1);
1097: } else if (MDOC_BLOCK == n->type) {
1098: tag[0].key = ATTR_CLASS;
1099: tag[0].val = "sec-block";
1.39 kristaps 1100:
1101: if (n->prev && NULL == n->prev->body->child) {
1102: print_otag(h, TAG_DIV, 1, tag);
1103: return(1);
1104: }
1105:
1.40 ! kristaps 1106: bufcat("margin-top: 1em;");
! 1107: if (NULL == n->next)
! 1108: bufcat("margin-bottom: 1em;");
! 1109:
1.39 kristaps 1110: tag[1].key = ATTR_STYLE;
1.40 ! kristaps 1111: tag[1].val = buf;
1.39 kristaps 1112:
1113: print_otag(h, TAG_DIV, 2, tag);
1.35 kristaps 1114: return(1);
1115: }
1116:
1117: buffmt("margin-left: %dem;", INDENT);
1118:
1.34 kristaps 1119: tag[0].key = ATTR_CLASS;
1.35 kristaps 1120: tag[0].val = "sec-body";
1121: tag[1].key = ATTR_STYLE;
1122: tag[1].val = buf;
1123:
1124: print_otag(h, TAG_DIV, 2, tag);
1125: return(1);
1126: }
1127:
1128:
1129: /* ARGSUSED */
1130: static int
1131: mdoc_ss_pre(MDOC_ARGS)
1132: {
1133: struct htmlpair tag[2];
1134: int i;
1135:
1136: i = 0;
1137:
1138: if (MDOC_BODY == n->type) {
1139: tag[i].key = ATTR_CLASS;
1140: tag[i++].val = "ssec-body";
1141: if (n->parent->next && n->child) {
1142: bufcat("margin-bottom: 1em;");
1143: tag[i].key = ATTR_STYLE;
1144: tag[i++].val = buf;
1145: }
1146: print_otag(h, TAG_DIV, i, tag);
1147: return(1);
1148: } else if (MDOC_BLOCK == n->type) {
1149: tag[i].key = ATTR_CLASS;
1150: tag[i++].val = "ssec-block";
1151: if (n->prev) {
1152: bufcat("margin-top: 1em;");
1153: tag[i].key = ATTR_STYLE;
1154: tag[i++].val = buf;
1155: }
1156: print_otag(h, TAG_DIV, i, tag);
1157: return(1);
1158: }
1159:
1160: buffmt("margin-left: -%dem;", INDENT - HALFINDENT);
1.34 kristaps 1161:
1.35 kristaps 1162: tag[0].key = ATTR_CLASS;
1163: tag[0].val = "ssec-head";
1.34 kristaps 1164: tag[1].key = ATTR_STYLE;
1.35 kristaps 1165: tag[1].val = buf;
1.30 kristaps 1166:
1.35 kristaps 1167: print_otag(h, TAG_DIV, 2, tag);
1168: print_otag(h, TAG_SPAN, 1, tag);
1.30 kristaps 1169: return(1);
1170: }
1171:
1172:
1173: /* ARGSUSED */
1174: static int
1175: mdoc_fl_pre(MDOC_ARGS)
1176: {
1177: struct htmlpair tag;
1178:
1179: tag.key = ATTR_CLASS;
1180: tag.val = "flag";
1181:
1182: print_otag(h, TAG_SPAN, 1, &tag);
1183: print_text(h, "\\-");
1184: h->flags |= HTML_NOSPACE;
1185: return(1);
1186: }
1187:
1188:
1189: /* ARGSUSED */
1190: static int
1191: mdoc_pp_pre(MDOC_ARGS)
1192: {
1.34 kristaps 1193: struct htmlpair tag;
1.30 kristaps 1194:
1.35 kristaps 1195: bufcat("clear: both;");
1196: bufcat("height: 1em;");
1197:
1.34 kristaps 1198: tag.key = ATTR_STYLE;
1.35 kristaps 1199: tag.val = buf;
1.34 kristaps 1200:
1.35 kristaps 1201: print_otag(h, TAG_DIV, 1, &tag);
1.30 kristaps 1202: return(0);
1203: }
1204:
1205:
1206: /* ARGSUSED */
1207: static int
1208: mdoc_nd_pre(MDOC_ARGS)
1209: {
1.35 kristaps 1210: struct htmlpair tag;
1211:
1212: if (MDOC_BODY != n->type)
1213: return(1);
1.30 kristaps 1214:
1.35 kristaps 1215: /* XXX - this can contain block elements! */
1216: print_text(h, "\\(em");
1217: tag.key = ATTR_CLASS;
1218: tag.val = "desc-body";
1219: print_otag(h, TAG_SPAN, 1, &tag);
1.30 kristaps 1220: return(1);
1221: }
1222:
1223:
1224: /* ARGSUSED */
1225: static int
1226: mdoc_op_pre(MDOC_ARGS)
1227: {
1.35 kristaps 1228: struct htmlpair tag;
1229:
1230: if (MDOC_BODY != n->type)
1231: return(1);
1.30 kristaps 1232:
1.35 kristaps 1233: /* XXX - this can contain block elements! */
1234: print_text(h, "\\(lB");
1235: tag.key = ATTR_CLASS;
1236: tag.val = "opt";
1237: print_otag(h, TAG_SPAN, 1, &tag);
1.30 kristaps 1238: return(1);
1239: }
1240:
1241:
1242: /* ARGSUSED */
1243: static void
1244: mdoc_op_post(MDOC_ARGS)
1245: {
1246:
1247: if (MDOC_BODY != n->type)
1248: return;
1249: h->flags |= HTML_NOSPACE;
1250: print_text(h, "\\(rB");
1251: }
1252:
1253:
1254: static int
1255: mdoc_nm_pre(MDOC_ARGS)
1256: {
1.35 kristaps 1257: struct htmlpair tag;
1.30 kristaps 1258:
1259: if ( ! (HTML_NEWLINE & h->flags))
1.35 kristaps 1260: if (SEC_SYNOPSIS == n->sec) {
1261: tag.key = ATTR_STYLE;
1262: tag.val = "clear: both;";
1263: print_otag(h, TAG_BR, 1, &tag);
1264: }
1.30 kristaps 1265:
1.35 kristaps 1266: tag.key = ATTR_CLASS;
1267: tag.val = "name";
1.30 kristaps 1268:
1.35 kristaps 1269: print_otag(h, TAG_SPAN, 1, &tag);
1.30 kristaps 1270: if (NULL == n->child)
1271: print_text(h, m->name);
1272:
1273: return(1);
1274: }
1275:
1276:
1277: /* ARGSUSED */
1278: static int
1279: mdoc_xr_pre(MDOC_ARGS)
1280: {
1.35 kristaps 1281: struct htmlpair tag[2];
1.30 kristaps 1282:
1.35 kristaps 1283: tag[0].key = ATTR_CLASS;
1284: tag[0].val = "link-man";
1285: tag[1].key = ATTR_HREF;
1286: tag[1].val = "#"; /* TODO */
1.30 kristaps 1287:
1.35 kristaps 1288: print_otag(h, TAG_A, 2, tag);
1.30 kristaps 1289:
1290: n = n->child;
1291: print_text(h, n->string);
1292: if (NULL == (n = n->next))
1293: return(0);
1294:
1295: h->flags |= HTML_NOSPACE;
1296: print_text(h, "(");
1297: h->flags |= HTML_NOSPACE;
1298: print_text(h, n->string);
1299: h->flags |= HTML_NOSPACE;
1300: print_text(h, ")");
1301:
1302: return(0);
1303: }
1.31 kristaps 1304:
1305:
1306: /* ARGSUSED */
1307: static int
1308: mdoc_ns_pre(MDOC_ARGS)
1309: {
1310:
1311: h->flags |= HTML_NOSPACE;
1312: return(1);
1313: }
1314:
1.35 kristaps 1315:
1.31 kristaps 1316: /* ARGSUSED */
1317: static int
1318: mdoc_ar_pre(MDOC_ARGS)
1319: {
1320: struct htmlpair tag;
1321:
1322: tag.key = ATTR_CLASS;
1323: tag.val = "arg";
1324:
1325: print_otag(h, TAG_SPAN, 1, &tag);
1326: return(1);
1327: }
1.33 kristaps 1328:
1.35 kristaps 1329:
1.33 kristaps 1330: /* ARGSUSED */
1331: static int
1332: mdoc_xx_pre(MDOC_ARGS)
1333: {
1334: const char *pp;
1.35 kristaps 1335: struct htmlpair tag;
1.33 kristaps 1336:
1337: switch (n->tok) {
1338: case (MDOC_Bsx):
1339: pp = "BSDI BSD/OS";
1340: break;
1341: case (MDOC_Dx):
1342: pp = "DragonFlyBSD";
1343: break;
1344: case (MDOC_Fx):
1345: pp = "FreeBSD";
1346: break;
1347: case (MDOC_Nx):
1348: pp = "NetBSD";
1349: break;
1350: case (MDOC_Ox):
1351: pp = "OpenBSD";
1352: break;
1353: case (MDOC_Ux):
1354: pp = "UNIX";
1355: break;
1356: default:
1357: return(1);
1358: }
1359:
1.35 kristaps 1360: tag.key = ATTR_CLASS;
1361: tag.val = "unix";
1362:
1363: print_otag(h, TAG_SPAN, 1, &tag);
1.33 kristaps 1364: print_text(h, pp);
1365: return(1);
1366: }
1367:
1368:
1.35 kristaps 1369: /* ARGSUSED */
1.33 kristaps 1370: static int
1.37 kristaps 1371: mdoc_tbl_block_pre(MDOC_ARGS, int t, int w, int o, int c)
1.34 kristaps 1372: {
1373: struct htmlpair tag;
1374:
1.37 kristaps 1375: switch (t) {
1.38 kristaps 1376: case (MDOC_Column):
1377: /* FALLTHROUGH */
1.37 kristaps 1378: case (MDOC_Item):
1379: /* FALLTHROUGH */
1380: case (MDOC_Ohang):
1381: buffmt("margin-left: %dem; clear: both;", o);
1382: break;
1383: default:
1384: buffmt("margin-left: %dem; clear: both;", w + o);
1385: break;
1386: }
1.34 kristaps 1387:
1.37 kristaps 1388: if ( ! c && n->prev && n->prev->body->child)
1.35 kristaps 1389: bufcat("padding-top: 1em;");
1.34 kristaps 1390:
1391: tag.key = ATTR_STYLE;
1392: tag.val = buf;
1393: print_otag(h, TAG_DIV, 1, &tag);
1394: return(1);
1395: }
1396:
1397:
1.35 kristaps 1398: /* ARGSUSED */
1.34 kristaps 1399: static int
1400: mdoc_tbl_body_pre(MDOC_ARGS, int t, int w)
1401: {
1402:
1.37 kristaps 1403: print_otag(h, TAG_DIV, 0, NULL);
1.34 kristaps 1404: return(1);
1405: }
1406:
1407:
1.35 kristaps 1408: /* ARGSUSED */
1.34 kristaps 1409: static int
1.35 kristaps 1410: mdoc_tbl_head_pre(MDOC_ARGS, int t, int w)
1.33 kristaps 1411: {
1.34 kristaps 1412: struct htmlpair tag;
1.37 kristaps 1413: struct ord *ord;
1414: char nbuf[BUFSIZ];
1.34 kristaps 1415:
1.35 kristaps 1416: switch (t) {
1.37 kristaps 1417: case (MDOC_Item):
1.35 kristaps 1418: /* FALLTHROUGH */
1.37 kristaps 1419: case (MDOC_Ohang):
1420: print_otag(h, TAG_DIV, 0, NULL);
1421: break;
1.38 kristaps 1422: case (MDOC_Column):
1423: buffmt("min-width: %dem;", w);
1424: bufcat("clear: none;");
1425: if (n->next && MDOC_HEAD == n->next->type)
1426: bufcat("float: left;");
1427: tag.key = ATTR_STYLE;
1428: tag.val = buf;
1429: print_otag(h, TAG_DIV, 1, &tag);
1430: break;
1.37 kristaps 1431: default:
1.35 kristaps 1432: buffmt("margin-left: -%dem;", w);
1433: bufcat("clear: left;");
1434: bufcat("float: left;");
1435: bufcat("padding-right: 1em;");
1.37 kristaps 1436: tag.key = ATTR_STYLE;
1437: tag.val = buf;
1438: print_otag(h, TAG_DIV, 1, &tag);
1.34 kristaps 1439: break;
1.37 kristaps 1440: }
1441:
1442: switch (t) {
1443: case (MDOC_Diag):
1444: tag.key = ATTR_CLASS;
1445: tag.val = "diag";
1446: print_otag(h, TAG_SPAN, 1, &tag);
1447: break;
1448: case (MDOC_Enum):
1449: ord = SLIST_FIRST(&h->ords);
1450: assert(ord);
1451: nbuf[BUFSIZ - 1] = 0;
1452: (void)snprintf(nbuf, BUFSIZ - 1, "%d.", ord->pos++);
1453: print_text(h, nbuf);
1454: return(0);
1455: case (MDOC_Dash):
1456: print_text(h, "\\(en");
1457: return(0);
1458: case (MDOC_Hyphen):
1459: print_text(h, "\\-");
1460: return(0);
1461: case (MDOC_Bullet):
1462: print_text(h, "\\(bu");
1463: return(0);
1.34 kristaps 1464: default:
1465: break;
1.33 kristaps 1466: }
1467:
1.34 kristaps 1468: return(1);
1469: }
1470:
1471:
1472: static int
1473: mdoc_tbl_pre(MDOC_ARGS, int type)
1474: {
1.38 kristaps 1475: int i, w, o, c, wp;
1476: const struct mdoc_node *bl, *nn;
1.34 kristaps 1477:
1.33 kristaps 1478: bl = n->parent->parent;
1479: if (MDOC_BLOCK != n->type)
1480: bl = bl->parent;
1481:
1.34 kristaps 1482: /* FIXME: fmt_vspace() equivalent. */
1483:
1.33 kristaps 1484: assert(bl->args);
1485:
1.34 kristaps 1486: w = o = c = 0;
1.38 kristaps 1487: wp = -1;
1.34 kristaps 1488:
1489: for (i = 0; i < (int)bl->args->argc; i++)
1.33 kristaps 1490: if (MDOC_Width == bl->args->argv[i].arg) {
1491: assert(bl->args->argv[i].sz);
1.38 kristaps 1492: wp = i;
1.34 kristaps 1493: w = a2width(bl->args->argv[i].value[0]);
1494: } else if (MDOC_Offset == bl->args->argv[i].arg) {
1495: assert(bl->args->argv[i].sz);
1496: o = a2offs(bl->args->argv[i].value[0]);
1497: } else if (MDOC_Compact == bl->args->argv[i].arg)
1498: c = 1;
1.38 kristaps 1499:
1500: if (MDOC_HEAD == n->type && MDOC_Column == type) {
1501: nn = n->parent->child;
1502: assert(nn && MDOC_HEAD == nn->type);
1503: for (i = 0; nn && nn != n; nn = nn->next, i++)
1504: /* Counter... */ ;
1505: assert(nn);
1506: if (wp >= 0 && i < (int)bl->args[wp].argv->sz)
1507: w = a2width(bl->args->argv[wp].value[i]);
1508: }
1.34 kristaps 1509:
1.37 kristaps 1510: switch (type) {
1511: case (MDOC_Enum):
1512: /* FALLTHROUGH */
1513: case (MDOC_Dash):
1514: /* FALLTHROUGH */
1515: case (MDOC_Hyphen):
1516: /* FALLTHROUGH */
1517: case (MDOC_Bullet):
1518: if (w < 4)
1519: w = 4;
1520: break;
1521: case (MDOC_Inset):
1522: /* FALLTHROUGH */
1523: case (MDOC_Diag):
1524: w = 1;
1525: break;
1526: default:
1527: if (0 == w)
1528: w = 10;
1529: break;
1530: }
1531:
1.34 kristaps 1532: switch (n->type) {
1533: case (MDOC_BLOCK):
1534: break;
1535: case (MDOC_HEAD):
1536: return(mdoc_tbl_head_pre(m, n, h, type, w));
1537: case (MDOC_BODY):
1538: return(mdoc_tbl_body_pre(m, n, h, type, w));
1539: default:
1540: abort();
1541: /* NOTREACHED */
1542: }
1543:
1.37 kristaps 1544: return(mdoc_tbl_block_pre(m, n, h, type, w, o, c));
1.34 kristaps 1545: }
1546:
1547:
1548: static int
1.37 kristaps 1549: mdoc_bl_pre(MDOC_ARGS)
1.34 kristaps 1550: {
1.37 kristaps 1551: struct ord *ord;
1.34 kristaps 1552:
1553: if (MDOC_BLOCK != n->type)
1554: return(1);
1.37 kristaps 1555: if (MDOC_Enum != a2list(n))
1556: return(1);
1.34 kristaps 1557:
1.37 kristaps 1558: ord = malloc(sizeof(struct ord));
1559: if (NULL == ord)
1560: err(EXIT_FAILURE, "malloc");
1561: ord->cookie = n;
1562: ord->pos = 1;
1563: SLIST_INSERT_HEAD(&h->ords, ord, entry);
1.33 kristaps 1564:
1565: return(1);
1566: }
1567:
1568:
1.37 kristaps 1569: static void
1570: mdoc_bl_post(MDOC_ARGS)
1.33 kristaps 1571: {
1.37 kristaps 1572: struct ord *ord;
1.33 kristaps 1573:
1.34 kristaps 1574: if (MDOC_BLOCK != n->type)
1.37 kristaps 1575: return;
1576: if (MDOC_Enum != a2list(n))
1577: return;
1.33 kristaps 1578:
1.37 kristaps 1579: ord = SLIST_FIRST(&h->ords);
1580: assert(ord);
1581: SLIST_REMOVE_HEAD(&h->ords, entry);
1582: free(ord);
1.33 kristaps 1583: }
1584:
1585:
1586: static int
1.34 kristaps 1587: mdoc_it_pre(MDOC_ARGS)
1.33 kristaps 1588: {
1.37 kristaps 1589: int type;
1.33 kristaps 1590:
1.34 kristaps 1591: if (MDOC_BLOCK == n->type)
1.37 kristaps 1592: type = a2list(n->parent->parent);
1.34 kristaps 1593: else
1.37 kristaps 1594: type = a2list(n->parent->parent->parent);
1.33 kristaps 1595:
1.37 kristaps 1596: return(mdoc_tbl_pre(m, n, h, type));
1.33 kristaps 1597: }
1.34 kristaps 1598:
1599:
1600: /* ARGSUSED */
1601: static int
1602: mdoc_ex_pre(MDOC_ARGS)
1603: {
1604: const struct mdoc_node *nn;
1605: struct tag *t;
1606: struct htmlpair tag;
1607:
1608: print_text(h, "The");
1609:
1610: tag.key = ATTR_CLASS;
1611: tag.val = "utility";
1612:
1613: for (nn = n->child; nn; nn = nn->next) {
1614: t = print_otag(h, TAG_SPAN, 1, &tag);
1615: print_text(h, nn->string);
1616: print_tagq(h, t);
1617:
1618: h->flags |= HTML_NOSPACE;
1619:
1620: if (nn->next && NULL == nn->next->next)
1621: print_text(h, ", and");
1622: else if (nn->next)
1623: print_text(h, ",");
1624: else
1625: h->flags &= ~HTML_NOSPACE;
1626: }
1627:
1628: if (n->child->next)
1629: print_text(h, "utilities exit");
1630: else
1631: print_text(h, "utility exits");
1632:
1633: print_text(h, "0 on success, and >0 if an error occurs.");
1634: return(0);
1635: }
1636:
1637:
1638: /* ARGSUSED */
1639: static int
1640: mdoc_dq_pre(MDOC_ARGS)
1641: {
1642:
1643: if (MDOC_BODY != n->type)
1644: return(1);
1645: print_text(h, "\\(lq");
1646: h->flags |= HTML_NOSPACE;
1647: return(1);
1648: }
1649:
1650:
1651: /* ARGSUSED */
1652: static void
1653: mdoc_dq_post(MDOC_ARGS)
1654: {
1655:
1656: if (MDOC_BODY != n->type)
1657: return;
1658: h->flags |= HTML_NOSPACE;
1659: print_text(h, "\\(rq");
1660: }
1661:
1662:
1663: /* ARGSUSED */
1664: static int
1665: mdoc_pq_pre(MDOC_ARGS)
1666: {
1667:
1668: if (MDOC_BODY != n->type)
1669: return(1);
1670: print_text(h, "\\&(");
1671: h->flags |= HTML_NOSPACE;
1672: return(1);
1673: }
1674:
1675:
1676: /* ARGSUSED */
1677: static void
1678: mdoc_pq_post(MDOC_ARGS)
1679: {
1680:
1681: if (MDOC_BODY != n->type)
1682: return;
1683: print_text(h, ")");
1684: }
1685:
1686:
1687: /* ARGSUSED */
1688: static int
1689: mdoc_sq_pre(MDOC_ARGS)
1690: {
1691:
1692: if (MDOC_BODY != n->type)
1693: return(1);
1694: print_text(h, "\\(oq");
1695: h->flags |= HTML_NOSPACE;
1696: return(1);
1697: }
1698:
1699:
1700: /* ARGSUSED */
1701: static void
1702: mdoc_sq_post(MDOC_ARGS)
1703: {
1704:
1705: if (MDOC_BODY != n->type)
1706: return;
1707: h->flags |= HTML_NOSPACE;
1708: print_text(h, "\\(aq");
1709: }
1710:
1711:
1712: /* ARGSUSED */
1713: static int
1714: mdoc_em_pre(MDOC_ARGS)
1715: {
1716: struct htmlpair tag;
1717:
1718: tag.key = ATTR_CLASS;
1719: tag.val = "emph";
1720:
1721: print_otag(h, TAG_SPAN, 1, &tag);
1722: return(1);
1723: }
1724:
1725:
1726: /* ARGSUSED */
1727: static int
1728: mdoc_d1_pre(MDOC_ARGS)
1729: {
1.35 kristaps 1730: struct htmlpair tag[2];
1.34 kristaps 1731:
1732: if (MDOC_BLOCK != n->type)
1733: return(1);
1734:
1.35 kristaps 1735: buffmt("margin-left: %dem;", INDENT);
1.34 kristaps 1736:
1.35 kristaps 1737: tag[0].key = ATTR_CLASS;
1738: tag[0].val = "lit-block";
1739: tag[1].key = ATTR_STYLE;
1740: tag[1].val = buf;
1.34 kristaps 1741:
1.35 kristaps 1742: print_otag(h, TAG_DIV, 2, tag);
1.34 kristaps 1743: return(1);
1744: }
1745:
1746:
1747: /* ARGSUSED */
1748: static int
1749: mdoc_sx_pre(MDOC_ARGS)
1750: {
1.35 kristaps 1751: struct htmlpair tag[2];
1.34 kristaps 1752:
1.35 kristaps 1753: tag[0].key = ATTR_HREF;
1754: tag[0].val = "#"; /* XXX */
1755: tag[1].key = ATTR_CLASS;
1756: tag[1].val = "link-sec";
1.34 kristaps 1757:
1.35 kristaps 1758: print_otag(h, TAG_A, 2, tag);
1.34 kristaps 1759: return(1);
1760: }
1.37 kristaps 1761:
1762:
1763: /* ARGSUSED */
1764: static int
1765: mdoc_aq_pre(MDOC_ARGS)
1766: {
1767:
1768: if (MDOC_BODY != n->type)
1769: return(1);
1770: print_text(h, "\\(la");
1771: h->flags |= HTML_NOSPACE;
1772: return(1);
1773: }
1774:
1775:
1776: /* ARGSUSED */
1777: static void
1778: mdoc_aq_post(MDOC_ARGS)
1779: {
1780:
1781: if (MDOC_BODY != n->type)
1782: return;
1783: h->flags |= HTML_NOSPACE;
1784: print_text(h, "\\(ra");
1785: }
1786:
1.39 kristaps 1787:
1788: /* ARGSUSED */
1789: static int
1790: mdoc_bd_pre(MDOC_ARGS)
1791: {
1792: struct htmlpair tag[2];
1793: int t, c, o, i;
1794: const struct mdoc_node *bl;
1795:
1796: /* FIXME: fmt_vspace() shit. */
1797:
1798: if (MDOC_BLOCK == n->type)
1799: bl = n;
1800: else if (MDOC_HEAD == n->type)
1801: return(0);
1802: else
1803: bl = n->parent;
1804:
1805: t = o = c = 0;
1806:
1807: for (i = 0; i < (int)bl->args->argc; i++)
1808: switch (bl->args->argv[i].arg) {
1809: case (MDOC_Offset):
1810: assert(bl->args->argv[i].sz);
1811: o = a2offs(bl->args->argv[i].value[0]);
1812: break;
1813: case (MDOC_Compact):
1814: c = 1;
1815: break;
1816: case (MDOC_Ragged):
1817: /* FALLTHROUGH */
1818: case (MDOC_Filled):
1819: /* FALLTHROUGH */
1820: case (MDOC_Unfilled):
1821: /* FALLTHROUGH */
1822: case (MDOC_Literal):
1823: t = bl->args->argv[i].arg;
1824: break;
1825: }
1826:
1827: if (MDOC_BLOCK == n->type) {
1828: if (o)
1829: buffmt("margin-left: %dem;", o);
1830: bufcat("margin-top: 1em;");
1831: tag[0].key = ATTR_STYLE;
1832: tag[0].val = buf;
1833: print_otag(h, TAG_DIV, 1, tag);
1834: return(1);
1835: }
1836:
1837: switch (t) {
1838: case (MDOC_Unfilled):
1839: case (MDOC_Literal):
1840: break;
1841: default:
1842: return(1);
1843: }
1844:
1845: bufcat("white-space: pre;");
1846: tag[0].key = ATTR_STYLE;
1847: tag[0].val = buf;
1848: tag[1].key = ATTR_CLASS;
1849: tag[1].val = "lit-block";
1850:
1851: print_otag(h, TAG_DIV, 2, tag);
1852:
1853: for (n = n->child; n; n = n->next) {
1854: h->flags |= HTML_NOSPACE;
1855: print_mdoc_node(m, n, h);
1856: if (n->next)
1857: print_text(h, "\n");
1858: }
1859:
1860: return(0);
1861: }
1862:
1863:
1864: /* ARGSUSED */
1865: static int
1866: mdoc_pa_pre(MDOC_ARGS)
1867: {
1868: struct htmlpair tag;
1869:
1870: tag.key = ATTR_CLASS;
1871: tag.val = "file";
1872:
1873: print_otag(h, TAG_SPAN, 1, &tag);
1874: return(1);
1875: }
1876:
1877:
1878: /* ARGSUSED */
1879: static int
1880: mdoc_qq_pre(MDOC_ARGS)
1881: {
1882:
1883: if (MDOC_BODY != n->type)
1884: return(1);
1885: print_text(h, "\\*q");
1886: h->flags |= HTML_NOSPACE;
1887: return(1);
1888: }
1889:
1890:
1891: /* ARGSUSED */
1892: static void
1893: mdoc_qq_post(MDOC_ARGS)
1894: {
1895:
1896: if (MDOC_BODY != n->type)
1897: return;
1898: h->flags |= HTML_NOSPACE;
1899: print_text(h, "\\*q");
1900: }
CVSweb