Annotation of mandoc/html.c, Revision 1.35
1.35 ! kristaps 1: /* $Id: html.c,v 1.34 2009/09/17 23:35:41 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:
130: SLIST_HEAD(tagq, tag);
131:
1.29 kristaps 132: struct html {
133: int flags;
134: #define HTML_NOSPACE (1 << 0)
1.30 kristaps 135: #define HTML_NEWLINE (1 << 1)
136: struct tagq stack;
1.32 kristaps 137: void *symtab;
1.29 kristaps 138: };
1.10 kristaps 139:
1.29 kristaps 140: #define MDOC_ARGS const struct mdoc_meta *m, \
141: const struct mdoc_node *n, \
142: struct html *h
143: #define MAN_ARGS const struct man_meta *m, \
144: const struct man_node *n, \
145: struct html *h
146: struct htmlmdoc {
147: int (*pre)(MDOC_ARGS);
148: void (*post)(MDOC_ARGS);
149: };
1.13 kristaps 150:
1.29 kristaps 151: static void print_gen_doctype(struct html *);
152: static void print_gen_head(struct html *);
153: static void print_mdoc(MDOC_ARGS);
154: static void print_mdoc_head(MDOC_ARGS);
1.30 kristaps 155: static void print_mdoc_title(MDOC_ARGS);
1.29 kristaps 156: static void print_mdoc_node(MDOC_ARGS);
157: static void print_man(MAN_ARGS);
158: static void print_man_head(MAN_ARGS);
159: static void print_man_body(MAN_ARGS);
1.30 kristaps 160: static struct tag *print_otag(struct html *, enum htmltag,
1.29 kristaps 161: int, const struct htmlpair *);
1.30 kristaps 162: static void print_tagq(struct html *, const struct tag *);
163: static void print_stagq(struct html *, const struct tag *);
1.29 kristaps 164: static void print_ctag(struct html *, enum htmltag);
1.32 kristaps 165: static void print_encode(struct html *, const char *);
166: static void print_escape(struct html *, const char **);
1.29 kristaps 167: static void print_text(struct html *, const char *);
1.32 kristaps 168: static void print_res(struct html *, const char *, int);
169: static void print_spec(struct html *, const char *, int);
1.33 kristaps 170:
171: static int a2width(const char *);
172: static int a2offs(const char *);
173:
1.34 kristaps 174: static int mdoc_list_pre(MDOC_ARGS, int);
175: static int mdoc_listitem_pre(MDOC_ARGS);
1.29 kristaps 176: static int mdoc_root_pre(MDOC_ARGS);
1.34 kristaps 177: static int mdoc_tbl_pre(MDOC_ARGS, int);
178: static int mdoc_tbl_block_pre(MDOC_ARGS, int, int, int);
179: static int mdoc_tbl_body_pre(MDOC_ARGS, int, int);
180: static int mdoc_tbl_head_pre(MDOC_ARGS, int, int);
1.29 kristaps 181:
1.31 kristaps 182: static int mdoc_ar_pre(MDOC_ARGS);
1.33 kristaps 183: static int mdoc_bl_pre(MDOC_ARGS);
1.34 kristaps 184: static int mdoc_d1_pre(MDOC_ARGS);
185: static void mdoc_dq_post(MDOC_ARGS);
186: static int mdoc_dq_pre(MDOC_ARGS);
1.30 kristaps 187: static int mdoc_fl_pre(MDOC_ARGS);
1.34 kristaps 188: static int mdoc_em_pre(MDOC_ARGS);
189: static int mdoc_ex_pre(MDOC_ARGS);
1.33 kristaps 190: static int mdoc_it_pre(MDOC_ARGS);
1.29 kristaps 191: static int mdoc_nd_pre(MDOC_ARGS);
192: static int mdoc_nm_pre(MDOC_ARGS);
1.31 kristaps 193: static int mdoc_ns_pre(MDOC_ARGS);
1.34 kristaps 194: static void mdoc_op_post(MDOC_ARGS);
1.30 kristaps 195: static int mdoc_op_pre(MDOC_ARGS);
1.29 kristaps 196: static int mdoc_pp_pre(MDOC_ARGS);
1.34 kristaps 197: static void mdoc_pq_post(MDOC_ARGS);
198: static int mdoc_pq_pre(MDOC_ARGS);
1.29 kristaps 199: static int mdoc_sh_pre(MDOC_ARGS);
1.34 kristaps 200: static void mdoc_sq_post(MDOC_ARGS);
201: static int mdoc_sq_pre(MDOC_ARGS);
1.29 kristaps 202: static int mdoc_ss_pre(MDOC_ARGS);
1.34 kristaps 203: static int mdoc_sx_pre(MDOC_ARGS);
1.30 kristaps 204: static int mdoc_xr_pre(MDOC_ARGS);
1.33 kristaps 205: static int mdoc_xx_pre(MDOC_ARGS);
206:
207: #ifdef __linux__
1.35 ! kristaps 208: extern size_t strlcpy(char *, const char *, size_t);
! 209: extern size_t strlcat(char *, const char *, size_t);
1.33 kristaps 210: #endif
1.29 kristaps 211:
212: static const struct htmlmdoc mdocs[MDOC_MAX] = {
213: {NULL, NULL}, /* Ap */
214: {NULL, NULL}, /* Dd */
215: {NULL, NULL}, /* Dt */
216: {NULL, NULL}, /* Os */
1.30 kristaps 217: {mdoc_sh_pre, NULL }, /* Sh */
218: {mdoc_ss_pre, NULL }, /* Ss */
1.29 kristaps 219: {mdoc_pp_pre, NULL}, /* Pp */
1.34 kristaps 220: {mdoc_d1_pre, NULL}, /* D1 */
221: {mdoc_d1_pre, NULL}, /* Dl */
1.29 kristaps 222: {NULL, NULL}, /* Bd */
223: {NULL, NULL}, /* Ed */
1.33 kristaps 224: {mdoc_bl_pre, NULL}, /* Bl */
1.29 kristaps 225: {NULL, NULL}, /* El */
1.33 kristaps 226: {mdoc_it_pre, NULL}, /* It */
1.29 kristaps 227: {NULL, NULL}, /* Ad */
228: {NULL, NULL}, /* An */
1.31 kristaps 229: {mdoc_ar_pre, NULL}, /* Ar */
1.29 kristaps 230: {NULL, NULL}, /* Cd */
231: {NULL, NULL}, /* Cm */
232: {NULL, NULL}, /* Dv */
233: {NULL, NULL}, /* Er */
234: {NULL, NULL}, /* Ev */
1.34 kristaps 235: {mdoc_ex_pre, NULL}, /* Ex */
1.29 kristaps 236: {NULL, NULL}, /* Fa */
237: {NULL, NULL}, /* Fd */
1.30 kristaps 238: {mdoc_fl_pre, NULL}, /* Fl */
1.29 kristaps 239: {NULL, NULL}, /* Fn */
240: {NULL, NULL}, /* Ft */
241: {NULL, NULL}, /* Ic */
242: {NULL, NULL}, /* In */
243: {NULL, NULL}, /* Li */
244: {mdoc_nd_pre, NULL}, /* Nd */
1.30 kristaps 245: {mdoc_nm_pre, NULL}, /* Nm */
246: {mdoc_op_pre, mdoc_op_post}, /* Op */
1.29 kristaps 247: {NULL, NULL}, /* Ot */
248: {NULL, NULL}, /* Pa */
249: {NULL, NULL}, /* Rv */
250: {NULL, NULL}, /* St */
251: {NULL, NULL}, /* Va */
252: {NULL, NULL}, /* Vt */
1.30 kristaps 253: {mdoc_xr_pre, NULL}, /* Xr */
1.29 kristaps 254: {NULL, NULL}, /* %A */
255: {NULL, NULL}, /* %B */
256: {NULL, NULL}, /* %D */
257: {NULL, NULL}, /* %I */
258: {NULL, NULL}, /* %J */
259: {NULL, NULL}, /* %N */
260: {NULL, NULL}, /* %O */
261: {NULL, NULL}, /* %P */
262: {NULL, NULL}, /* %R */
263: {NULL, NULL}, /* %T */
264: {NULL, NULL}, /* %V */
265: {NULL, NULL}, /* Ac */
266: {NULL, NULL}, /* Ao */
267: {NULL, NULL}, /* Aq */
268: {NULL, NULL}, /* At */
269: {NULL, NULL}, /* Bc */
270: {NULL, NULL}, /* Bf */
271: {NULL, NULL}, /* Bo */
272: {NULL, NULL}, /* Bq */
1.33 kristaps 273: {mdoc_xx_pre, NULL}, /* Bsx */
1.29 kristaps 274: {NULL, NULL}, /* Bx */
275: {NULL, NULL}, /* Db */
276: {NULL, NULL}, /* Dc */
277: {NULL, NULL}, /* Do */
1.34 kristaps 278: {mdoc_dq_pre, mdoc_dq_post}, /* Dq */
1.29 kristaps 279: {NULL, NULL}, /* Ec */
280: {NULL, NULL}, /* Ef */
1.34 kristaps 281: {mdoc_em_pre, NULL}, /* Em */
1.29 kristaps 282: {NULL, NULL}, /* Eo */
1.33 kristaps 283: {mdoc_xx_pre, NULL}, /* Fx */
1.29 kristaps 284: {NULL, NULL}, /* Ms */
285: {NULL, NULL}, /* No */
1.31 kristaps 286: {mdoc_ns_pre, NULL}, /* Ns */
1.33 kristaps 287: {mdoc_xx_pre, NULL}, /* Nx */
288: {mdoc_xx_pre, NULL}, /* Ox */
1.29 kristaps 289: {NULL, NULL}, /* Pc */
290: {NULL, NULL}, /* Pf */
1.34 kristaps 291: {mdoc_pq_pre, mdoc_pq_post}, /* Po */
292: {mdoc_pq_pre, mdoc_pq_post}, /* Pq */
1.29 kristaps 293: {NULL, NULL}, /* Qc */
294: {NULL, NULL}, /* Ql */
295: {NULL, NULL}, /* Qo */
296: {NULL, NULL}, /* Qq */
297: {NULL, NULL}, /* Re */
298: {NULL, NULL}, /* Rs */
299: {NULL, NULL}, /* Sc */
1.34 kristaps 300: {mdoc_sq_pre, mdoc_sq_post}, /* So */
301: {mdoc_sq_pre, mdoc_sq_post}, /* Sq */
1.29 kristaps 302: {NULL, NULL}, /* Sm */
1.34 kristaps 303: {mdoc_sx_pre, NULL}, /* Sx */
1.29 kristaps 304: {NULL, NULL}, /* Sy */
305: {NULL, NULL}, /* Tn */
1.33 kristaps 306: {mdoc_xx_pre, NULL}, /* Ux */
1.29 kristaps 307: {NULL, NULL}, /* Xc */
308: {NULL, NULL}, /* Xo */
309: {NULL, NULL}, /* Fo */
310: {NULL, NULL}, /* Fc */
311: {NULL, NULL}, /* Oo */
312: {NULL, NULL}, /* Oc */
313: {NULL, NULL}, /* Bk */
314: {NULL, NULL}, /* Ek */
315: {NULL, NULL}, /* Bt */
316: {NULL, NULL}, /* Hf */
317: {NULL, NULL}, /* Fr */
318: {NULL, NULL}, /* Ud */
319: {NULL, NULL}, /* Lb */
320: {NULL, NULL}, /* Lp */
321: {NULL, NULL}, /* Lk */
322: {NULL, NULL}, /* Mt */
323: {NULL, NULL}, /* Brq */
324: {NULL, NULL}, /* Bro */
325: {NULL, NULL}, /* Brc */
326: {NULL, NULL}, /* %C */
327: {NULL, NULL}, /* Es */
328: {NULL, NULL}, /* En */
1.33 kristaps 329: {mdoc_xx_pre, NULL}, /* Dx */
1.29 kristaps 330: {NULL, NULL}, /* %Q */
331: {NULL, NULL}, /* br */
332: {NULL, NULL}, /* sp */
333: };
1.10 kristaps 334:
1.35 ! kristaps 335: static char buf[BUFSIZ]; /* XXX */
! 336:
! 337: #define bufcat(x) (void)strlcat(buf, (x), BUFSIZ)
! 338: #define bufinit() buf[0] = 0
! 339: #define buffmt(...) (void)snprintf(buf, BUFSIZ - 1, __VA_ARGS__)
1.33 kristaps 340:
1.30 kristaps 341: void
1.29 kristaps 342: html_mdoc(void *arg, const struct mdoc *m)
1.10 kristaps 343: {
1.29 kristaps 344: struct html *h;
1.30 kristaps 345: struct tag *t;
1.10 kristaps 346:
1.29 kristaps 347: h = (struct html *)arg;
1.10 kristaps 348:
1.29 kristaps 349: print_gen_doctype(h);
1.30 kristaps 350: t = print_otag(h, TAG_HTML, 0, NULL);
1.29 kristaps 351: print_mdoc(mdoc_meta(m), mdoc_node(m), h);
1.30 kristaps 352: print_tagq(h, t);
353:
1.29 kristaps 354: printf("\n");
1.10 kristaps 355: }
356:
1.33 kristaps 357:
1.30 kristaps 358: void
1.29 kristaps 359: html_man(void *arg, const struct man *m)
1.10 kristaps 360: {
1.29 kristaps 361: struct html *h;
1.30 kristaps 362: struct tag *t;
1.10 kristaps 363:
1.29 kristaps 364: h = (struct html *)arg;
1.10 kristaps 365:
1.29 kristaps 366: print_gen_doctype(h);
1.30 kristaps 367: t = print_otag(h, TAG_HTML, 0, NULL);
1.29 kristaps 368: print_man(man_meta(m), man_node(m), h);
1.30 kristaps 369: print_tagq(h, t);
370:
1.29 kristaps 371: printf("\n");
1.10 kristaps 372: }
373:
1.33 kristaps 374:
1.29 kristaps 375: void *
376: html_alloc(void)
1.10 kristaps 377: {
1.30 kristaps 378: struct html *h;
379:
380: if (NULL == (h = calloc(1, sizeof(struct html))))
381: return(NULL);
1.10 kristaps 382:
1.30 kristaps 383: SLIST_INIT(&h->stack);
1.32 kristaps 384: if (NULL == (h->symtab = chars_init(CHARS_HTML))) {
385: free(h);
386: return(NULL);
387: }
1.30 kristaps 388: return(h);
1.29 kristaps 389: }
1.10 kristaps 390:
1.33 kristaps 391:
1.29 kristaps 392: void
393: html_free(void *p)
394: {
1.30 kristaps 395: struct tag *tag;
396: struct html *h;
397:
398: h = (struct html *)p;
1.10 kristaps 399:
1.30 kristaps 400: while ( ! SLIST_EMPTY(&h->stack)) {
401: tag = SLIST_FIRST(&h->stack);
402: SLIST_REMOVE_HEAD(&h->stack, entry);
403: free(tag);
404: }
405: free(h);
1.10 kristaps 406: }
1.2 kristaps 407:
1.33 kristaps 408:
1.29 kristaps 409: static void
410: print_mdoc(MDOC_ARGS)
1.4 kristaps 411: {
1.30 kristaps 412: struct tag *t;
1.4 kristaps 413:
1.30 kristaps 414: t = print_otag(h, TAG_HEAD, 0, NULL);
1.29 kristaps 415: print_mdoc_head(m, n, h);
1.30 kristaps 416: print_tagq(h, t);
417:
418: t = print_otag(h, TAG_BODY, 0, NULL);
419: print_mdoc_title(m, n, h);
1.29 kristaps 420: print_mdoc_node(m, n, h);
1.30 kristaps 421: print_tagq(h, t);
1.29 kristaps 422: }
1.4 kristaps 423:
1.33 kristaps 424:
1.29 kristaps 425: static void
426: print_gen_head(struct html *h)
427: {
428: struct htmlpair meta0[2];
429: struct htmlpair meta1[2];
430: struct htmlpair link[4];
431:
432: meta0[0].key = ATTR_HTTPEQUIV;
433: meta0[0].val = "Content-Type";
434: meta0[1].key = ATTR_CONTENT;
1.34 kristaps 435: meta0[1].val = "text/html; charset=utf-8";
1.29 kristaps 436:
437: meta1[0].key = ATTR_NAME;
438: meta1[0].val = "resource-type";
439: meta1[1].key = ATTR_CONTENT;
440: meta1[1].val = "document";
441:
442: link[0].key = ATTR_REL;
443: link[0].val = "stylesheet";
444: link[1].key = ATTR_HREF;
1.30 kristaps 445: link[1].val = "style.css"; /* XXX */
1.29 kristaps 446: link[2].key = ATTR_TYPE;
447: link[2].val = "text/css";
448: link[3].key = ATTR_MEDIA;
449: link[3].val = "all";
450:
451: print_otag(h, TAG_META, 2, meta0);
452: print_otag(h, TAG_META, 2, meta1);
453: print_otag(h, TAG_LINK, 4, link);
1.4 kristaps 454: }
455:
1.33 kristaps 456:
1.30 kristaps 457: /* ARGSUSED */
1.29 kristaps 458: static void
459: print_mdoc_head(MDOC_ARGS)
1.18 kristaps 460: {
461:
1.29 kristaps 462: print_gen_head(h);
463: print_otag(h, TAG_TITLE, 0, NULL);
1.32 kristaps 464: print_encode(h, m->title);
1.2 kristaps 465: }
466:
1.33 kristaps 467:
1.30 kristaps 468: /* ARGSUSED */
1.29 kristaps 469: static void
1.30 kristaps 470: print_mdoc_title(MDOC_ARGS)
1.2 kristaps 471: {
472:
1.30 kristaps 473: /* TODO */
1.2 kristaps 474: }
475:
1.33 kristaps 476:
1.29 kristaps 477: static void
478: print_mdoc_node(MDOC_ARGS)
1.2 kristaps 479: {
1.29 kristaps 480: int child;
1.30 kristaps 481: struct tag *t;
1.8 kristaps 482:
1.29 kristaps 483: child = 1;
1.30 kristaps 484: t = SLIST_FIRST(&h->stack);
1.8 kristaps 485:
1.35 ! kristaps 486: bufinit();
! 487:
1.29 kristaps 488: switch (n->type) {
489: case (MDOC_ROOT):
490: child = mdoc_root_pre(m, n, h);
1.7 kristaps 491: break;
1.29 kristaps 492: case (MDOC_TEXT):
493: print_text(h, n->string);
1.7 kristaps 494: break;
1.3 kristaps 495: default:
1.29 kristaps 496: if (mdocs[n->tok].pre)
497: child = (*mdocs[n->tok].pre)(m, n, h);
1.3 kristaps 498: break;
1.2 kristaps 499: }
500:
1.29 kristaps 501: if (child && n->child)
502: print_mdoc_node(m, n->child, h);
1.8 kristaps 503:
1.30 kristaps 504: print_stagq(h, t);
505:
1.35 ! kristaps 506: bufinit();
! 507:
1.29 kristaps 508: switch (n->type) {
509: case (MDOC_ROOT):
1.7 kristaps 510: break;
1.29 kristaps 511: case (MDOC_TEXT):
1.7 kristaps 512: break;
1.3 kristaps 513: default:
1.29 kristaps 514: if (mdocs[n->tok].post)
515: (*mdocs[n->tok].post)(m, n, h);
1.3 kristaps 516: break;
517: }
1.2 kristaps 518:
1.29 kristaps 519: if (n->next)
520: print_mdoc_node(m, n->next, h);
1.2 kristaps 521: }
522:
1.33 kristaps 523:
1.29 kristaps 524: static void
525: print_man(MAN_ARGS)
1.9 kristaps 526: {
1.30 kristaps 527: struct tag *t;
1.9 kristaps 528:
1.30 kristaps 529: t = print_otag(h, TAG_HEAD, 0, NULL);
1.29 kristaps 530: print_man_head(m, n, h);
1.30 kristaps 531: print_tagq(h, t);
532:
533: t = print_otag(h, TAG_BODY, 0, NULL);
1.29 kristaps 534: print_man_body(m, n, h);
1.30 kristaps 535: print_tagq(h, t);
1.9 kristaps 536: }
537:
1.33 kristaps 538:
1.30 kristaps 539: /* ARGSUSED */
1.9 kristaps 540: static void
1.29 kristaps 541: print_man_head(MAN_ARGS)
1.9 kristaps 542: {
543:
1.29 kristaps 544: print_gen_head(h);
545: print_otag(h, TAG_TITLE, 0, NULL);
1.32 kristaps 546: print_encode(h, m->title);
1.29 kristaps 547: }
1.9 kristaps 548:
1.33 kristaps 549:
1.30 kristaps 550: /* ARGSUSED */
1.29 kristaps 551: static void
552: print_man_body(MAN_ARGS)
553: {
1.30 kristaps 554:
555: /* TODO */
1.9 kristaps 556: }
557:
1.33 kristaps 558:
1.32 kristaps 559: static void
560: print_spec(struct html *h, const char *p, int len)
561: {
562: const char *rhs;
563: int i;
564: size_t sz;
565:
566: rhs = chars_a2ascii(h->symtab, p, (size_t)len, &sz);
567:
568: if (NULL == rhs)
569: return;
570: for (i = 0; i < (int)sz; i++)
571: putchar(rhs[i]);
572: }
573:
1.33 kristaps 574:
1.32 kristaps 575: static void
576: print_res(struct html *h, const char *p, int len)
577: {
578: const char *rhs;
579: int i;
580: size_t sz;
581:
582: rhs = chars_a2res(h->symtab, p, (size_t)len, &sz);
583:
584: if (NULL == rhs)
585: return;
586: for (i = 0; i < (int)sz; i++)
587: putchar(rhs[i]);
588: }
589:
1.33 kristaps 590:
1.32 kristaps 591: static void
592: print_escape(struct html *h, const char **p)
593: {
594: int j, type;
595: const char *wp;
596:
597: wp = *p;
598: type = 1;
599:
600: if (0 == *(++wp)) {
601: *p = wp;
602: return;
603: }
604:
605: if ('(' == *wp) {
606: wp++;
607: if (0 == *wp || 0 == *(wp + 1)) {
608: *p = 0 == *wp ? wp : wp + 1;
609: return;
610: }
611:
612: print_spec(h, wp, 2);
613: *p = ++wp;
614: return;
615:
616: } else if ('*' == *wp) {
617: if (0 == *(++wp)) {
618: *p = wp;
619: return;
620: }
621:
622: switch (*wp) {
623: case ('('):
624: wp++;
625: if (0 == *wp || 0 == *(wp + 1)) {
626: *p = 0 == *wp ? wp : wp + 1;
627: return;
628: }
629:
630: print_res(h, wp, 2);
631: *p = ++wp;
632: return;
633: case ('['):
634: type = 0;
635: break;
636: default:
637: print_res(h, wp, 1);
638: *p = wp;
639: return;
640: }
641:
642: } else if ('f' == *wp) {
643: if (0 == *(++wp)) {
644: *p = wp;
645: return;
646: }
647:
648: switch (*wp) {
649: case ('B'):
650: /* TODO */
651: break;
652: case ('I'):
653: /* TODO */
654: break;
655: case ('P'):
656: /* FALLTHROUGH */
657: case ('R'):
658: /* TODO */
659: break;
660: default:
661: break;
662: }
663:
664: *p = wp;
665: return;
666:
667: } else if ('[' != *wp) {
668: print_spec(h, wp, 1);
669: *p = wp;
670: return;
671: }
672:
673: wp++;
674: for (j = 0; *wp && ']' != *wp; wp++, j++)
675: /* Loop... */ ;
676:
677: if (0 == *wp) {
678: *p = wp;
679: return;
680: }
681:
682: if (type)
683: print_spec(h, wp - j, j);
684: else
685: print_res(h, wp - j, j);
686:
687: *p = wp;
688: }
689:
1.9 kristaps 690:
1.29 kristaps 691: static void
1.32 kristaps 692: print_encode(struct html *h, const char *p)
1.29 kristaps 693: {
1.14 kristaps 694:
1.32 kristaps 695: for (; *p; p++) {
1.34 kristaps 696: if ('\\' == *p) {
697: print_escape(h, &p);
698: continue;
699: }
700: switch (*p) {
701: case ('<'):
702: printf("<");
703: break;
704: case ('>'):
705: printf(">");
706: break;
707: case ('&'):
708: printf("&");
709: break;
710: default:
1.32 kristaps 711: putchar(*p);
1.34 kristaps 712: break;
1.32 kristaps 713: }
714: }
1.14 kristaps 715: }
716:
717:
1.30 kristaps 718: static struct tag *
1.29 kristaps 719: print_otag(struct html *h, enum htmltag tag,
720: int sz, const struct htmlpair *p)
1.14 kristaps 721: {
1.29 kristaps 722: int i;
1.30 kristaps 723: struct tag *t;
724:
725: if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
726: if (NULL == (t = malloc(sizeof(struct tag))))
727: err(EXIT_FAILURE, "malloc");
728: t->tag = tag;
729: SLIST_INSERT_HEAD(&h->stack, t, entry);
730: } else
731: t = NULL;
1.29 kristaps 732:
733: if ( ! (HTML_NOSPACE & h->flags))
1.30 kristaps 734: if ( ! (HTML_CLRLINE & htmltags[tag].flags))
1.29 kristaps 735: printf(" ");
736:
737: printf("<%s", htmltags[tag].name);
738: for (i = 0; i < sz; i++) {
739: printf(" %s=\"", htmlattrs[p[i].key]);
740: assert(p->val);
1.32 kristaps 741: print_encode(h, p[i].val);
1.29 kristaps 742: printf("\"");
743: }
744: printf(">");
1.14 kristaps 745:
1.29 kristaps 746: h->flags |= HTML_NOSPACE;
1.30 kristaps 747: if (HTML_CLRLINE & htmltags[tag].flags)
748: h->flags |= HTML_NEWLINE;
749: else
750: h->flags &= ~HTML_NEWLINE;
1.14 kristaps 751:
1.30 kristaps 752: return(t);
1.14 kristaps 753: }
754:
755:
756: /* ARGSUSED */
1.29 kristaps 757: static void
758: print_ctag(struct html *h, enum htmltag tag)
1.14 kristaps 759: {
760:
1.29 kristaps 761: printf("</%s>", htmltags[tag].name);
1.30 kristaps 762: if (HTML_CLRLINE & htmltags[tag].flags)
1.29 kristaps 763: h->flags |= HTML_NOSPACE;
1.30 kristaps 764: if (HTML_CLRLINE & htmltags[tag].flags)
765: h->flags |= HTML_NEWLINE;
766: else
767: h->flags &= ~HTML_NEWLINE;
1.14 kristaps 768: }
769:
770:
1.29 kristaps 771: /* ARGSUSED */
772: static void
773: print_gen_doctype(struct html *h)
1.1 kristaps 774: {
1.29 kristaps 775:
776: printf("<!DOCTYPE HTML PUBLIC \"%s\" \"%s\">\n", DOCTYPE, DTD);
1.1 kristaps 777: }
778:
779:
1.29 kristaps 780: static void
781: print_text(struct html *h, const char *p)
1.1 kristaps 782: {
783:
1.29 kristaps 784: if (*p && 0 == *(p + 1))
785: switch (*p) {
786: case('.'):
787: /* FALLTHROUGH */
788: case(','):
789: /* FALLTHROUGH */
790: case(';'):
791: /* FALLTHROUGH */
792: case(':'):
793: /* FALLTHROUGH */
794: case('?'):
795: /* FALLTHROUGH */
796: case('!'):
797: /* FALLTHROUGH */
798: case(')'):
799: /* FALLTHROUGH */
800: case(']'):
801: /* FALLTHROUGH */
802: case('}'):
803: h->flags |= HTML_NOSPACE;
1.30 kristaps 804: break;
1.29 kristaps 805: default:
806: break;
807: }
1.1 kristaps 808:
1.29 kristaps 809: if ( ! (h->flags & HTML_NOSPACE))
810: printf(" ");
1.30 kristaps 811:
1.29 kristaps 812: h->flags &= ~HTML_NOSPACE;
1.30 kristaps 813: h->flags &= ~HTML_NEWLINE;
1.1 kristaps 814:
1.29 kristaps 815: if (p)
1.32 kristaps 816: print_encode(h, p);
1.8 kristaps 817:
1.29 kristaps 818: if (*p && 0 == *(p + 1))
819: switch (*p) {
820: case('('):
821: /* FALLTHROUGH */
822: case('['):
823: /* FALLTHROUGH */
824: case('{'):
825: h->flags |= HTML_NOSPACE;
1.30 kristaps 826: break;
1.29 kristaps 827: default:
828: break;
829: }
1.1 kristaps 830: }
1.30 kristaps 831:
832:
833: static void
834: print_tagq(struct html *h, const struct tag *until)
835: {
836: struct tag *tag;
837:
838: while ( ! SLIST_EMPTY(&h->stack)) {
839: tag = SLIST_FIRST(&h->stack);
840: print_ctag(h, tag->tag);
841: SLIST_REMOVE_HEAD(&h->stack, entry);
842: free(tag);
843: if (until && tag == until)
844: return;
845: }
846: }
847:
848:
849: static void
850: print_stagq(struct html *h, const struct tag *suntil)
851: {
852: struct tag *tag;
853:
854: while ( ! SLIST_EMPTY(&h->stack)) {
855: tag = SLIST_FIRST(&h->stack);
856: if (suntil && tag == suntil)
857: return;
858: print_ctag(h, tag->tag);
859: SLIST_REMOVE_HEAD(&h->stack, entry);
860: free(tag);
861: }
862: }
863:
864:
1.35 ! kristaps 865: /* FIXME: put in utility file for front-ends. */
1.33 kristaps 866: static int
867: a2offs(const char *p)
868: {
869: int len, i;
870:
871: if (0 == strcmp(p, "left"))
872: return(0);
873: if (0 == strcmp(p, "indent"))
874: return(INDENT + 1);
875: if (0 == strcmp(p, "indent-two"))
876: return((INDENT + 1) * 2);
877:
878: if (0 == (len = (int)strlen(p)))
879: return(0);
880:
881: for (i = 0; i < len - 1; i++)
882: if ( ! isdigit((u_char)p[i]))
883: break;
884:
885: if (i == len - 1)
886: if ('n' == p[len - 1] || 'm' == p[len - 1])
887: return(atoi(p));
888:
889: return(len);
890: }
891:
892:
1.35 ! kristaps 893: /* FIXME: put in utility file for front-ends. */
1.33 kristaps 894: static int
895: a2width(const char *p)
896: {
897: int i, len;
898:
899: if (0 == (len = (int)strlen(p)))
900: return(0);
901: for (i = 0; i < len - 1; i++)
902: if ( ! isdigit((u_char)p[i]))
903: break;
904:
905: if (i == len - 1)
906: if ('n' == p[len - 1] || 'm' == p[len - 1])
907: return(atoi(p) + 2);
908:
909: return(len + 2);
910: }
911:
912:
913:
914:
1.30 kristaps 915: /* ARGSUSED */
916: static int
917: mdoc_root_pre(MDOC_ARGS)
918: {
919: struct htmlpair tag;
920:
921: tag.key = ATTR_CLASS;
922: tag.val = "body";
923:
924: print_otag(h, TAG_DIV, 1, &tag);
925: return(1);
926: }
927:
928:
929: /* ARGSUSED */
930: static int
1.35 ! kristaps 931: mdoc_sh_pre(MDOC_ARGS)
1.30 kristaps 932: {
1.34 kristaps 933: struct htmlpair tag[2];
934:
1.35 ! kristaps 935: if (MDOC_HEAD == n->type) {
! 936: tag[0].key = ATTR_CLASS;
! 937: tag[0].val = "sec-head";
! 938: print_otag(h, TAG_DIV, 1, tag);
! 939: print_otag(h, TAG_SPAN, 1, tag);
! 940: return(1);
! 941: } else if (MDOC_BLOCK == n->type) {
! 942: tag[0].key = ATTR_CLASS;
! 943: tag[0].val = "sec-block";
! 944: print_otag(h, TAG_DIV, 1, tag);
! 945: return(1);
! 946: }
! 947:
! 948: buffmt("margin-left: %dem;", INDENT);
! 949:
! 950: if (n->parent->next && n->child)
! 951: bufcat("margin-bottom: 1em;");
! 952:
1.34 kristaps 953: tag[0].key = ATTR_CLASS;
1.35 ! kristaps 954: tag[0].val = "sec-body";
! 955: tag[1].key = ATTR_STYLE;
! 956: tag[1].val = buf;
! 957:
! 958: print_otag(h, TAG_DIV, 2, tag);
! 959: return(1);
! 960: }
! 961:
! 962:
! 963: /* ARGSUSED */
! 964: static int
! 965: mdoc_ss_pre(MDOC_ARGS)
! 966: {
! 967: struct htmlpair tag[2];
! 968: int i;
! 969:
! 970: i = 0;
! 971:
! 972: if (MDOC_BODY == n->type) {
! 973: tag[i].key = ATTR_CLASS;
! 974: tag[i++].val = "ssec-body";
! 975: if (n->parent->next && n->child) {
! 976: bufcat("margin-bottom: 1em;");
! 977: tag[i].key = ATTR_STYLE;
! 978: tag[i++].val = buf;
! 979: }
! 980: print_otag(h, TAG_DIV, i, tag);
! 981: return(1);
! 982: } else if (MDOC_BLOCK == n->type) {
! 983: tag[i].key = ATTR_CLASS;
! 984: tag[i++].val = "ssec-block";
! 985: if (n->prev) {
! 986: bufcat("margin-top: 1em;");
! 987: tag[i].key = ATTR_STYLE;
! 988: tag[i++].val = buf;
! 989: }
! 990: print_otag(h, TAG_DIV, i, tag);
! 991: return(1);
! 992: }
! 993:
! 994: buffmt("margin-left: -%dem;", INDENT - HALFINDENT);
1.34 kristaps 995:
1.35 ! kristaps 996: tag[0].key = ATTR_CLASS;
! 997: tag[0].val = "ssec-head";
1.34 kristaps 998: tag[1].key = ATTR_STYLE;
1.35 ! kristaps 999: tag[1].val = buf;
1.30 kristaps 1000:
1.35 ! kristaps 1001: print_otag(h, TAG_DIV, 2, tag);
! 1002: print_otag(h, TAG_SPAN, 1, tag);
1.30 kristaps 1003: return(1);
1004: }
1005:
1006:
1007: /* ARGSUSED */
1008: static int
1009: mdoc_fl_pre(MDOC_ARGS)
1010: {
1011: struct htmlpair tag;
1012:
1013: tag.key = ATTR_CLASS;
1014: tag.val = "flag";
1015:
1016: print_otag(h, TAG_SPAN, 1, &tag);
1017: print_text(h, "\\-");
1018: h->flags |= HTML_NOSPACE;
1019: return(1);
1020: }
1021:
1022:
1023: /* ARGSUSED */
1024: static int
1025: mdoc_pp_pre(MDOC_ARGS)
1026: {
1.34 kristaps 1027: struct htmlpair tag;
1.30 kristaps 1028:
1.35 ! kristaps 1029: bufcat("clear: both;");
! 1030: bufcat("height: 1em;");
! 1031:
1.34 kristaps 1032: tag.key = ATTR_STYLE;
1.35 ! kristaps 1033: tag.val = buf;
1.34 kristaps 1034:
1.35 ! kristaps 1035: print_otag(h, TAG_DIV, 1, &tag);
1.30 kristaps 1036: return(0);
1037: }
1038:
1039:
1040: /* ARGSUSED */
1041: static int
1042: mdoc_nd_pre(MDOC_ARGS)
1043: {
1.35 ! kristaps 1044: struct htmlpair tag;
! 1045:
! 1046: if (MDOC_BODY != n->type)
! 1047: return(1);
1.30 kristaps 1048:
1.35 ! kristaps 1049: /* XXX - this can contain block elements! */
! 1050: print_text(h, "\\(em");
! 1051: tag.key = ATTR_CLASS;
! 1052: tag.val = "desc-body";
! 1053: print_otag(h, TAG_SPAN, 1, &tag);
1.30 kristaps 1054: return(1);
1055: }
1056:
1057:
1058: /* ARGSUSED */
1059: static int
1060: mdoc_op_pre(MDOC_ARGS)
1061: {
1.35 ! kristaps 1062: struct htmlpair tag;
! 1063:
! 1064: if (MDOC_BODY != n->type)
! 1065: return(1);
1.30 kristaps 1066:
1.35 ! kristaps 1067: /* XXX - this can contain block elements! */
! 1068: print_text(h, "\\(lB");
! 1069: tag.key = ATTR_CLASS;
! 1070: tag.val = "opt";
! 1071: print_otag(h, TAG_SPAN, 1, &tag);
1.30 kristaps 1072: return(1);
1073: }
1074:
1075:
1076: /* ARGSUSED */
1077: static void
1078: mdoc_op_post(MDOC_ARGS)
1079: {
1080:
1081: if (MDOC_BODY != n->type)
1082: return;
1083: h->flags |= HTML_NOSPACE;
1084: print_text(h, "\\(rB");
1085: }
1086:
1087:
1088: static int
1089: mdoc_nm_pre(MDOC_ARGS)
1090: {
1.35 ! kristaps 1091: struct htmlpair tag;
1.30 kristaps 1092:
1093: if ( ! (HTML_NEWLINE & h->flags))
1.35 ! kristaps 1094: if (SEC_SYNOPSIS == n->sec) {
! 1095: tag.key = ATTR_STYLE;
! 1096: tag.val = "clear: both;";
! 1097: print_otag(h, TAG_BR, 1, &tag);
! 1098: }
1.30 kristaps 1099:
1.35 ! kristaps 1100: tag.key = ATTR_CLASS;
! 1101: tag.val = "name";
1.30 kristaps 1102:
1.35 ! kristaps 1103: print_otag(h, TAG_SPAN, 1, &tag);
1.30 kristaps 1104: if (NULL == n->child)
1105: print_text(h, m->name);
1106:
1107: return(1);
1108: }
1109:
1110:
1111: /* ARGSUSED */
1112: static int
1113: mdoc_xr_pre(MDOC_ARGS)
1114: {
1.35 ! kristaps 1115: struct htmlpair tag[2];
1.30 kristaps 1116:
1.35 ! kristaps 1117: tag[0].key = ATTR_CLASS;
! 1118: tag[0].val = "link-man";
! 1119: tag[1].key = ATTR_HREF;
! 1120: tag[1].val = "#"; /* TODO */
1.30 kristaps 1121:
1.35 ! kristaps 1122: print_otag(h, TAG_A, 2, tag);
1.30 kristaps 1123:
1124: n = n->child;
1125: print_text(h, n->string);
1126: if (NULL == (n = n->next))
1127: return(0);
1128:
1129: h->flags |= HTML_NOSPACE;
1130: print_text(h, "(");
1131: h->flags |= HTML_NOSPACE;
1132: print_text(h, n->string);
1133: h->flags |= HTML_NOSPACE;
1134: print_text(h, ")");
1135:
1136: return(0);
1137: }
1.31 kristaps 1138:
1139:
1140: /* ARGSUSED */
1141: static int
1142: mdoc_ns_pre(MDOC_ARGS)
1143: {
1144:
1145: h->flags |= HTML_NOSPACE;
1146: return(1);
1147: }
1148:
1.35 ! kristaps 1149:
1.31 kristaps 1150: /* ARGSUSED */
1151: static int
1152: mdoc_ar_pre(MDOC_ARGS)
1153: {
1154: struct htmlpair tag;
1155:
1156: tag.key = ATTR_CLASS;
1157: tag.val = "arg";
1158:
1159: print_otag(h, TAG_SPAN, 1, &tag);
1160: return(1);
1161: }
1.33 kristaps 1162:
1.35 ! kristaps 1163:
1.33 kristaps 1164: /* ARGSUSED */
1165: static int
1166: mdoc_xx_pre(MDOC_ARGS)
1167: {
1168: const char *pp;
1.35 ! kristaps 1169: struct htmlpair tag;
1.33 kristaps 1170:
1171: switch (n->tok) {
1172: case (MDOC_Bsx):
1173: pp = "BSDI BSD/OS";
1174: break;
1175: case (MDOC_Dx):
1176: pp = "DragonFlyBSD";
1177: break;
1178: case (MDOC_Fx):
1179: pp = "FreeBSD";
1180: break;
1181: case (MDOC_Nx):
1182: pp = "NetBSD";
1183: break;
1184: case (MDOC_Ox):
1185: pp = "OpenBSD";
1186: break;
1187: case (MDOC_Ux):
1188: pp = "UNIX";
1189: break;
1190: default:
1191: return(1);
1192: }
1193:
1.35 ! kristaps 1194: tag.key = ATTR_CLASS;
! 1195: tag.val = "unix";
! 1196:
! 1197: print_otag(h, TAG_SPAN, 1, &tag);
1.33 kristaps 1198: print_text(h, pp);
1199: return(1);
1200: }
1201:
1202:
1.35 ! kristaps 1203: /* ARGSUSED */
1.33 kristaps 1204: static int
1.34 kristaps 1205: mdoc_tbl_block_pre(MDOC_ARGS, int w, int o, int c)
1206: {
1207: struct htmlpair tag;
1208:
1.35 ! kristaps 1209: buffmt("margin-left: %dem; clear: both;", w + o);
1.34 kristaps 1210:
1211: if ( ! c)
1.35 ! kristaps 1212: bufcat("padding-top: 1em;");
1.34 kristaps 1213:
1214: tag.key = ATTR_STYLE;
1215: tag.val = buf;
1216: print_otag(h, TAG_DIV, 1, &tag);
1217: return(1);
1218: }
1219:
1220:
1.35 ! kristaps 1221: /* ARGSUSED */
1.34 kristaps 1222: static int
1223: mdoc_tbl_body_pre(MDOC_ARGS, int t, int w)
1224: {
1225: struct htmlpair tag;
1226: int i;
1227:
1228: switch (t) {
1229: case (MDOC_Tag):
1.35 ! kristaps 1230: i = 1;
1.34 kristaps 1231: tag.key = ATTR_STYLE;
1232: tag.val = buf;
1.35 ! kristaps 1233: bufcat("clear: right;");
! 1234: bufcat("float: left;");
! 1235: bufcat("width: 100%%;");
1.34 kristaps 1236: break;
1237: default:
1.35 ! kristaps 1238: i = 0;
1.34 kristaps 1239: break;
1240: }
1241:
1242: print_otag(h, TAG_DIV, i, &tag);
1243: return(1);
1244: }
1245:
1246:
1.35 ! kristaps 1247: /* ARGSUSED */
1.34 kristaps 1248: static int
1.35 ! kristaps 1249: mdoc_tbl_head_pre(MDOC_ARGS, int t, int w)
1.33 kristaps 1250: {
1.34 kristaps 1251: struct htmlpair tag;
1252: int i;
1253:
1.35 ! kristaps 1254: switch (t) {
! 1255: case (MDOC_Hang):
! 1256: /* FALLTHROUGH */
1.34 kristaps 1257: case (MDOC_Tag):
1.35 ! kristaps 1258: i = 1;
1.34 kristaps 1259: tag.key = ATTR_STYLE;
1260: tag.val = buf;
1.35 ! kristaps 1261: buffmt("margin-left: -%dem;", w);
! 1262: bufcat("clear: left;");
! 1263: bufcat("float: left;");
! 1264: bufcat("padding-right: 1em;");
1.34 kristaps 1265: break;
1266: default:
1.35 ! kristaps 1267: i = 0;
1.34 kristaps 1268: break;
1.33 kristaps 1269: }
1270:
1.34 kristaps 1271: print_otag(h, TAG_DIV, i, &tag);
1272: return(1);
1273: }
1274:
1275:
1276: static int
1277: mdoc_tbl_pre(MDOC_ARGS, int type)
1278: {
1279: int i, w, o, c;
1280: const struct mdoc_node *bl;
1281:
1.33 kristaps 1282: bl = n->parent->parent;
1283: if (MDOC_BLOCK != n->type)
1284: bl = bl->parent;
1285:
1.34 kristaps 1286: /* FIXME: fmt_vspace() equivalent. */
1287:
1.33 kristaps 1288: assert(bl->args);
1289:
1.34 kristaps 1290: w = o = c = 0;
1291:
1292: for (i = 0; i < (int)bl->args->argc; i++)
1.33 kristaps 1293: if (MDOC_Width == bl->args->argv[i].arg) {
1294: assert(bl->args->argv[i].sz);
1.34 kristaps 1295: w = a2width(bl->args->argv[i].value[0]);
1296: } else if (MDOC_Offset == bl->args->argv[i].arg) {
1297: assert(bl->args->argv[i].sz);
1298: o = a2offs(bl->args->argv[i].value[0]);
1299: } else if (MDOC_Compact == bl->args->argv[i].arg)
1300: c = 1;
1301:
1302: if (0 == w)
1303: w = 10;
1304:
1305: switch (n->type) {
1306: case (MDOC_BLOCK):
1307: break;
1308: case (MDOC_HEAD):
1309: return(mdoc_tbl_head_pre(m, n, h, type, w));
1310: case (MDOC_BODY):
1311: return(mdoc_tbl_body_pre(m, n, h, type, w));
1312: default:
1313: abort();
1314: /* NOTREACHED */
1315: }
1316:
1317: return(mdoc_tbl_block_pre(m, n, h, w, o, c));
1318: }
1319:
1320:
1321: /* ARGSUSED */
1322: static int
1323: mdoc_listitem_pre(MDOC_ARGS)
1324: {
1325: int i, w, o, c;
1326: const struct mdoc_node *bl;
1327: struct htmlpair tag;
1328:
1329: /* FIXME: fmt_vspace() equivalent. */
1330:
1331: if (MDOC_BLOCK != n->type)
1332: return(1);
1333:
1334: bl = n->parent->parent;
1335: assert(bl);
1336:
1337: w = o = c = 0;
1338:
1339: for (i = 0; i < (int)bl->args->argc; i++)
1340: if (MDOC_Width == bl->args->argv[i].arg) {
1341: assert(bl->args->argv[i].sz);
1342: w = a2width(bl->args->argv[i].value[0]);
1.33 kristaps 1343: } else if (MDOC_Offset == bl->args->argv[i].arg) {
1344: assert(bl->args->argv[i].sz);
1.34 kristaps 1345: o = a2offs(bl->args->argv[i].value[0]);
1346: } else if (MDOC_Compact == bl->args->argv[i].arg)
1347: c = 1;
1.35 ! kristaps 1348:
! 1349: if (o && w)
! 1350: buffmt("margin-left: %dem; padding-left: %dem;", o, w);
! 1351: else if (w)
! 1352: buffmt("padding-left: %dem;", w > 4 ? w - 4 : w);
! 1353: else if (o)
! 1354: buffmt("margin-left: %dem;", o);
1.34 kristaps 1355:
1356: if ( ! c)
1.35 ! kristaps 1357: bufcat("padding-top: 1em;");
1.33 kristaps 1358:
1359: tag.key = ATTR_STYLE;
1360: tag.val = buf;
1361:
1.34 kristaps 1362: print_otag(h, TAG_LI, 1, &tag);
1.33 kristaps 1363: return(1);
1364: }
1365:
1366:
1.34 kristaps 1367: /* ARGSUSED */
1.33 kristaps 1368: static int
1.34 kristaps 1369: mdoc_list_pre(MDOC_ARGS, int type)
1.33 kristaps 1370: {
1371:
1.34 kristaps 1372: switch (type) {
1373: case (MDOC_Enum):
1374: print_otag(h, TAG_OL, 0, NULL);
1375: break;
1376: case (MDOC_Bullet):
1377: print_otag(h, TAG_UL, 0, NULL);
1378: break;
1379: default:
1380: break;
1381: }
1382:
1383: return(1);
1.33 kristaps 1384: }
1385:
1386:
1387: static int
1.34 kristaps 1388: mdoc_bl_pre(MDOC_ARGS)
1.33 kristaps 1389: {
1.34 kristaps 1390: int i, len, type;
1.33 kristaps 1391:
1.34 kristaps 1392: if (MDOC_BLOCK != n->type)
1393: return(1);
1.33 kristaps 1394:
1.34 kristaps 1395: assert(n->args);
1396: len = (int)n->args->argc;
1.33 kristaps 1397:
1398: for (i = 0; i < len; i++)
1.34 kristaps 1399: switch ((type = n->args->argv[i].arg)) {
1400: case (MDOC_Enum):
1401: /* FALLTHROUGH */
1.33 kristaps 1402: case (MDOC_Bullet):
1.34 kristaps 1403: return(mdoc_list_pre(m, n, h, type));
1404: case (MDOC_Tag):
1405: /* FALLTHROUGH */
1406: case (MDOC_Hang):
1.33 kristaps 1407: /* FALLTHROUGH */
1408: case (MDOC_Dash):
1409: /* FALLTHROUGH */
1.34 kristaps 1410: case (MDOC_Hyphen):
1.33 kristaps 1411: /* FALLTHROUGH */
1412: case (MDOC_Inset):
1413: /* FALLTHROUGH */
1414: case (MDOC_Diag):
1415: /* FALLTHROUGH */
1416: case (MDOC_Item):
1417: /* FALLTHROUGH */
1418: case (MDOC_Column):
1419: /* FALLTHROUGH */
1420: case (MDOC_Ohang):
1.34 kristaps 1421: return(1);
1.33 kristaps 1422: default:
1.34 kristaps 1423: break;
1.33 kristaps 1424: }
1425:
1426: abort();
1427: /* NOTREACHED */
1428: }
1429:
1430:
1431: static int
1.34 kristaps 1432: mdoc_it_pre(MDOC_ARGS)
1.33 kristaps 1433: {
1.34 kristaps 1434: int i, len, type;
1435: const struct mdoc_node *bl;
1.33 kristaps 1436:
1.34 kristaps 1437: if (MDOC_BLOCK == n->type)
1438: bl = n->parent->parent;
1439: else
1440: bl = n->parent->parent->parent;
1.33 kristaps 1441:
1.34 kristaps 1442: assert(bl->args);
1443: len = (int)bl->args->argc;
1.33 kristaps 1444:
1445: for (i = 0; i < len; i++)
1.34 kristaps 1446: switch ((type = bl->args->argv[i].arg)) {
1447: case (MDOC_Tag):
1448: /* FALLTHROUGH */
1449: case (MDOC_Hang):
1450: return(mdoc_tbl_pre(m, n, h, type));
1451: case (MDOC_Enum):
1452: /* FALLTHROUGH */
1.33 kristaps 1453: case (MDOC_Bullet):
1.34 kristaps 1454: return(mdoc_listitem_pre(m, n, h));
1.33 kristaps 1455: case (MDOC_Dash):
1456: /* FALLTHROUGH */
1.34 kristaps 1457: case (MDOC_Hyphen):
1.33 kristaps 1458: /* FALLTHROUGH */
1459: case (MDOC_Inset):
1460: /* FALLTHROUGH */
1461: case (MDOC_Diag):
1462: /* FALLTHROUGH */
1463: case (MDOC_Item):
1464: /* FALLTHROUGH */
1465: case (MDOC_Column):
1466: /* FALLTHROUGH */
1467: case (MDOC_Ohang):
1.34 kristaps 1468: return(0);
1.33 kristaps 1469: default:
1.34 kristaps 1470: break;
1.33 kristaps 1471: }
1472:
1473: abort();
1474: /* NOTREACHED */
1475: }
1.34 kristaps 1476:
1477:
1478: /* ARGSUSED */
1479: static int
1480: mdoc_ex_pre(MDOC_ARGS)
1481: {
1482: const struct mdoc_node *nn;
1483: struct tag *t;
1484: struct htmlpair tag;
1485:
1486: print_text(h, "The");
1487:
1488: tag.key = ATTR_CLASS;
1489: tag.val = "utility";
1490:
1491: for (nn = n->child; nn; nn = nn->next) {
1492: t = print_otag(h, TAG_SPAN, 1, &tag);
1493: print_text(h, nn->string);
1494: print_tagq(h, t);
1495:
1496: h->flags |= HTML_NOSPACE;
1497:
1498: if (nn->next && NULL == nn->next->next)
1499: print_text(h, ", and");
1500: else if (nn->next)
1501: print_text(h, ",");
1502: else
1503: h->flags &= ~HTML_NOSPACE;
1504: }
1505:
1506: if (n->child->next)
1507: print_text(h, "utilities exit");
1508: else
1509: print_text(h, "utility exits");
1510:
1511: print_text(h, "0 on success, and >0 if an error occurs.");
1512: return(0);
1513: }
1514:
1515:
1516: /* ARGSUSED */
1517: static int
1518: mdoc_dq_pre(MDOC_ARGS)
1519: {
1520:
1521: if (MDOC_BODY != n->type)
1522: return(1);
1523: print_text(h, "\\(lq");
1524: h->flags |= HTML_NOSPACE;
1525: return(1);
1526: }
1527:
1528:
1529: /* ARGSUSED */
1530: static void
1531: mdoc_dq_post(MDOC_ARGS)
1532: {
1533:
1534: if (MDOC_BODY != n->type)
1535: return;
1536: h->flags |= HTML_NOSPACE;
1537: print_text(h, "\\(rq");
1538: }
1539:
1540:
1541: /* ARGSUSED */
1542: static int
1543: mdoc_pq_pre(MDOC_ARGS)
1544: {
1545:
1546: if (MDOC_BODY != n->type)
1547: return(1);
1548: print_text(h, "\\&(");
1549: h->flags |= HTML_NOSPACE;
1550: return(1);
1551: }
1552:
1553:
1554: /* ARGSUSED */
1555: static void
1556: mdoc_pq_post(MDOC_ARGS)
1557: {
1558:
1559: if (MDOC_BODY != n->type)
1560: return;
1561: print_text(h, ")");
1562: }
1563:
1564:
1565: /* ARGSUSED */
1566: static int
1567: mdoc_sq_pre(MDOC_ARGS)
1568: {
1569:
1570: if (MDOC_BODY != n->type)
1571: return(1);
1572: print_text(h, "\\(oq");
1573: h->flags |= HTML_NOSPACE;
1574: return(1);
1575: }
1576:
1577:
1578: /* ARGSUSED */
1579: static void
1580: mdoc_sq_post(MDOC_ARGS)
1581: {
1582:
1583: if (MDOC_BODY != n->type)
1584: return;
1585: h->flags |= HTML_NOSPACE;
1586: print_text(h, "\\(aq");
1587: }
1588:
1589:
1590: /* ARGSUSED */
1591: static int
1592: mdoc_em_pre(MDOC_ARGS)
1593: {
1594: struct htmlpair tag;
1595:
1596: tag.key = ATTR_CLASS;
1597: tag.val = "emph";
1598:
1599: print_otag(h, TAG_SPAN, 1, &tag);
1600: return(1);
1601: }
1602:
1603:
1604: /* ARGSUSED */
1605: static int
1606: mdoc_d1_pre(MDOC_ARGS)
1607: {
1.35 ! kristaps 1608: struct htmlpair tag[2];
1.34 kristaps 1609:
1610: if (MDOC_BLOCK != n->type)
1611: return(1);
1612:
1.35 ! kristaps 1613: buffmt("margin-left: %dem;", INDENT);
! 1614: bufcat("margin-bottom: 0px;");
1.34 kristaps 1615:
1.35 ! kristaps 1616: tag[0].key = ATTR_CLASS;
! 1617: tag[0].val = "lit-block";
! 1618: tag[1].key = ATTR_STYLE;
! 1619: tag[1].val = buf;
1.34 kristaps 1620:
1.35 ! kristaps 1621: print_otag(h, TAG_DIV, 2, tag);
1.34 kristaps 1622: return(1);
1623: }
1624:
1625:
1626: /* ARGSUSED */
1627: static int
1628: mdoc_sx_pre(MDOC_ARGS)
1629: {
1.35 ! kristaps 1630: struct htmlpair tag[2];
1.34 kristaps 1631:
1.35 ! kristaps 1632: tag[0].key = ATTR_HREF;
! 1633: tag[0].val = "#"; /* XXX */
! 1634: tag[1].key = ATTR_CLASS;
! 1635: tag[1].val = "link-sec";
1.34 kristaps 1636:
1.35 ! kristaps 1637: print_otag(h, TAG_A, 2, tag);
1.34 kristaps 1638: return(1);
1639: }
CVSweb