Annotation of mandoc/html.c, Revision 1.36
1.36 ! kristaps 1: /* $Id: html.c,v 1.35 2009/09/20 11:05:22 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: }
1.36 ! kristaps 405:
! 406: if (h->symtab)
! 407: chars_free(h->symtab);
1.30 kristaps 408: free(h);
1.10 kristaps 409: }
1.2 kristaps 410:
1.33 kristaps 411:
1.29 kristaps 412: static void
413: print_mdoc(MDOC_ARGS)
1.4 kristaps 414: {
1.30 kristaps 415: struct tag *t;
1.4 kristaps 416:
1.30 kristaps 417: t = print_otag(h, TAG_HEAD, 0, NULL);
1.29 kristaps 418: print_mdoc_head(m, n, h);
1.30 kristaps 419: print_tagq(h, t);
420:
421: t = print_otag(h, TAG_BODY, 0, NULL);
422: print_mdoc_title(m, n, h);
1.29 kristaps 423: print_mdoc_node(m, n, h);
1.30 kristaps 424: print_tagq(h, t);
1.29 kristaps 425: }
1.4 kristaps 426:
1.33 kristaps 427:
1.29 kristaps 428: static void
429: print_gen_head(struct html *h)
430: {
431: struct htmlpair meta0[2];
432: struct htmlpair meta1[2];
433: struct htmlpair link[4];
434:
435: meta0[0].key = ATTR_HTTPEQUIV;
436: meta0[0].val = "Content-Type";
437: meta0[1].key = ATTR_CONTENT;
1.34 kristaps 438: meta0[1].val = "text/html; charset=utf-8";
1.29 kristaps 439:
440: meta1[0].key = ATTR_NAME;
441: meta1[0].val = "resource-type";
442: meta1[1].key = ATTR_CONTENT;
443: meta1[1].val = "document";
444:
445: link[0].key = ATTR_REL;
446: link[0].val = "stylesheet";
447: link[1].key = ATTR_HREF;
1.30 kristaps 448: link[1].val = "style.css"; /* XXX */
1.29 kristaps 449: link[2].key = ATTR_TYPE;
450: link[2].val = "text/css";
451: link[3].key = ATTR_MEDIA;
452: link[3].val = "all";
453:
454: print_otag(h, TAG_META, 2, meta0);
455: print_otag(h, TAG_META, 2, meta1);
456: print_otag(h, TAG_LINK, 4, link);
1.4 kristaps 457: }
458:
1.33 kristaps 459:
1.30 kristaps 460: /* ARGSUSED */
1.29 kristaps 461: static void
462: print_mdoc_head(MDOC_ARGS)
1.18 kristaps 463: {
464:
1.29 kristaps 465: print_gen_head(h);
466: print_otag(h, TAG_TITLE, 0, NULL);
1.32 kristaps 467: print_encode(h, m->title);
1.2 kristaps 468: }
469:
1.33 kristaps 470:
1.30 kristaps 471: /* ARGSUSED */
1.29 kristaps 472: static void
1.30 kristaps 473: print_mdoc_title(MDOC_ARGS)
1.2 kristaps 474: {
475:
1.30 kristaps 476: /* TODO */
1.2 kristaps 477: }
478:
1.33 kristaps 479:
1.29 kristaps 480: static void
481: print_mdoc_node(MDOC_ARGS)
1.2 kristaps 482: {
1.29 kristaps 483: int child;
1.30 kristaps 484: struct tag *t;
1.8 kristaps 485:
1.29 kristaps 486: child = 1;
1.30 kristaps 487: t = SLIST_FIRST(&h->stack);
1.8 kristaps 488:
1.35 kristaps 489: bufinit();
490:
1.29 kristaps 491: switch (n->type) {
492: case (MDOC_ROOT):
493: child = mdoc_root_pre(m, n, h);
1.7 kristaps 494: break;
1.29 kristaps 495: case (MDOC_TEXT):
496: print_text(h, n->string);
1.7 kristaps 497: break;
1.3 kristaps 498: default:
1.29 kristaps 499: if (mdocs[n->tok].pre)
500: child = (*mdocs[n->tok].pre)(m, n, h);
1.3 kristaps 501: break;
1.2 kristaps 502: }
503:
1.29 kristaps 504: if (child && n->child)
505: print_mdoc_node(m, n->child, h);
1.8 kristaps 506:
1.30 kristaps 507: print_stagq(h, t);
508:
1.35 kristaps 509: bufinit();
510:
1.29 kristaps 511: switch (n->type) {
512: case (MDOC_ROOT):
1.7 kristaps 513: break;
1.29 kristaps 514: case (MDOC_TEXT):
1.7 kristaps 515: break;
1.3 kristaps 516: default:
1.29 kristaps 517: if (mdocs[n->tok].post)
518: (*mdocs[n->tok].post)(m, n, h);
1.3 kristaps 519: break;
520: }
1.2 kristaps 521:
1.29 kristaps 522: if (n->next)
523: print_mdoc_node(m, n->next, h);
1.2 kristaps 524: }
525:
1.33 kristaps 526:
1.29 kristaps 527: static void
528: print_man(MAN_ARGS)
1.9 kristaps 529: {
1.30 kristaps 530: struct tag *t;
1.9 kristaps 531:
1.30 kristaps 532: t = print_otag(h, TAG_HEAD, 0, NULL);
1.29 kristaps 533: print_man_head(m, n, h);
1.30 kristaps 534: print_tagq(h, t);
535:
536: t = print_otag(h, TAG_BODY, 0, NULL);
1.29 kristaps 537: print_man_body(m, n, h);
1.30 kristaps 538: print_tagq(h, t);
1.9 kristaps 539: }
540:
1.33 kristaps 541:
1.30 kristaps 542: /* ARGSUSED */
1.9 kristaps 543: static void
1.29 kristaps 544: print_man_head(MAN_ARGS)
1.9 kristaps 545: {
546:
1.29 kristaps 547: print_gen_head(h);
548: print_otag(h, TAG_TITLE, 0, NULL);
1.32 kristaps 549: print_encode(h, m->title);
1.29 kristaps 550: }
1.9 kristaps 551:
1.33 kristaps 552:
1.30 kristaps 553: /* ARGSUSED */
1.29 kristaps 554: static void
555: print_man_body(MAN_ARGS)
556: {
1.30 kristaps 557:
558: /* TODO */
1.9 kristaps 559: }
560:
1.33 kristaps 561:
1.32 kristaps 562: static void
563: print_spec(struct html *h, const char *p, int len)
564: {
565: const char *rhs;
566: int i;
567: size_t sz;
568:
569: rhs = chars_a2ascii(h->symtab, p, (size_t)len, &sz);
570:
571: if (NULL == rhs)
572: return;
573: for (i = 0; i < (int)sz; i++)
574: putchar(rhs[i]);
575: }
576:
1.33 kristaps 577:
1.32 kristaps 578: static void
579: print_res(struct html *h, const char *p, int len)
580: {
581: const char *rhs;
582: int i;
583: size_t sz;
584:
585: rhs = chars_a2res(h->symtab, p, (size_t)len, &sz);
586:
587: if (NULL == rhs)
588: return;
589: for (i = 0; i < (int)sz; i++)
590: putchar(rhs[i]);
591: }
592:
1.33 kristaps 593:
1.32 kristaps 594: static void
595: print_escape(struct html *h, const char **p)
596: {
597: int j, type;
598: const char *wp;
599:
600: wp = *p;
601: type = 1;
602:
603: if (0 == *(++wp)) {
604: *p = wp;
605: return;
606: }
607:
608: if ('(' == *wp) {
609: wp++;
610: if (0 == *wp || 0 == *(wp + 1)) {
611: *p = 0 == *wp ? wp : wp + 1;
612: return;
613: }
614:
615: print_spec(h, wp, 2);
616: *p = ++wp;
617: return;
618:
619: } else if ('*' == *wp) {
620: if (0 == *(++wp)) {
621: *p = wp;
622: return;
623: }
624:
625: switch (*wp) {
626: case ('('):
627: wp++;
628: if (0 == *wp || 0 == *(wp + 1)) {
629: *p = 0 == *wp ? wp : wp + 1;
630: return;
631: }
632:
633: print_res(h, wp, 2);
634: *p = ++wp;
635: return;
636: case ('['):
637: type = 0;
638: break;
639: default:
640: print_res(h, wp, 1);
641: *p = wp;
642: return;
643: }
644:
645: } else if ('f' == *wp) {
646: if (0 == *(++wp)) {
647: *p = wp;
648: return;
649: }
650:
651: switch (*wp) {
652: case ('B'):
653: /* TODO */
654: break;
655: case ('I'):
656: /* TODO */
657: break;
658: case ('P'):
659: /* FALLTHROUGH */
660: case ('R'):
661: /* TODO */
662: break;
663: default:
664: break;
665: }
666:
667: *p = wp;
668: return;
669:
670: } else if ('[' != *wp) {
671: print_spec(h, wp, 1);
672: *p = wp;
673: return;
674: }
675:
676: wp++;
677: for (j = 0; *wp && ']' != *wp; wp++, j++)
678: /* Loop... */ ;
679:
680: if (0 == *wp) {
681: *p = wp;
682: return;
683: }
684:
685: if (type)
686: print_spec(h, wp - j, j);
687: else
688: print_res(h, wp - j, j);
689:
690: *p = wp;
691: }
692:
1.9 kristaps 693:
1.29 kristaps 694: static void
1.32 kristaps 695: print_encode(struct html *h, const char *p)
1.29 kristaps 696: {
1.14 kristaps 697:
1.32 kristaps 698: for (; *p; p++) {
1.34 kristaps 699: if ('\\' == *p) {
700: print_escape(h, &p);
701: continue;
702: }
703: switch (*p) {
704: case ('<'):
705: printf("<");
706: break;
707: case ('>'):
708: printf(">");
709: break;
710: case ('&'):
711: printf("&");
712: break;
713: default:
1.32 kristaps 714: putchar(*p);
1.34 kristaps 715: break;
1.32 kristaps 716: }
717: }
1.14 kristaps 718: }
719:
720:
1.30 kristaps 721: static struct tag *
1.29 kristaps 722: print_otag(struct html *h, enum htmltag tag,
723: int sz, const struct htmlpair *p)
1.14 kristaps 724: {
1.29 kristaps 725: int i;
1.30 kristaps 726: struct tag *t;
727:
728: if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
729: if (NULL == (t = malloc(sizeof(struct tag))))
730: err(EXIT_FAILURE, "malloc");
731: t->tag = tag;
732: SLIST_INSERT_HEAD(&h->stack, t, entry);
733: } else
734: t = NULL;
1.29 kristaps 735:
736: if ( ! (HTML_NOSPACE & h->flags))
1.30 kristaps 737: if ( ! (HTML_CLRLINE & htmltags[tag].flags))
1.29 kristaps 738: printf(" ");
739:
740: printf("<%s", htmltags[tag].name);
741: for (i = 0; i < sz; i++) {
742: printf(" %s=\"", htmlattrs[p[i].key]);
743: assert(p->val);
1.32 kristaps 744: print_encode(h, p[i].val);
1.29 kristaps 745: printf("\"");
746: }
747: printf(">");
1.14 kristaps 748:
1.29 kristaps 749: h->flags |= HTML_NOSPACE;
1.30 kristaps 750: if (HTML_CLRLINE & htmltags[tag].flags)
751: h->flags |= HTML_NEWLINE;
752: else
753: h->flags &= ~HTML_NEWLINE;
1.14 kristaps 754:
1.30 kristaps 755: return(t);
1.14 kristaps 756: }
757:
758:
759: /* ARGSUSED */
1.29 kristaps 760: static void
761: print_ctag(struct html *h, enum htmltag tag)
1.14 kristaps 762: {
763:
1.29 kristaps 764: printf("</%s>", htmltags[tag].name);
1.30 kristaps 765: if (HTML_CLRLINE & htmltags[tag].flags)
1.29 kristaps 766: h->flags |= HTML_NOSPACE;
1.30 kristaps 767: if (HTML_CLRLINE & htmltags[tag].flags)
768: h->flags |= HTML_NEWLINE;
769: else
770: h->flags &= ~HTML_NEWLINE;
1.14 kristaps 771: }
772:
773:
1.29 kristaps 774: /* ARGSUSED */
775: static void
776: print_gen_doctype(struct html *h)
1.1 kristaps 777: {
1.29 kristaps 778:
779: printf("<!DOCTYPE HTML PUBLIC \"%s\" \"%s\">\n", DOCTYPE, DTD);
1.1 kristaps 780: }
781:
782:
1.29 kristaps 783: static void
784: print_text(struct html *h, const char *p)
1.1 kristaps 785: {
786:
1.29 kristaps 787: if (*p && 0 == *(p + 1))
788: switch (*p) {
789: case('.'):
790: /* FALLTHROUGH */
791: case(','):
792: /* FALLTHROUGH */
793: case(';'):
794: /* FALLTHROUGH */
795: case(':'):
796: /* FALLTHROUGH */
797: case('?'):
798: /* FALLTHROUGH */
799: case('!'):
800: /* FALLTHROUGH */
801: case(')'):
802: /* FALLTHROUGH */
803: case(']'):
804: /* FALLTHROUGH */
805: case('}'):
806: h->flags |= HTML_NOSPACE;
1.30 kristaps 807: break;
1.29 kristaps 808: default:
809: break;
810: }
1.1 kristaps 811:
1.29 kristaps 812: if ( ! (h->flags & HTML_NOSPACE))
813: printf(" ");
1.30 kristaps 814:
1.29 kristaps 815: h->flags &= ~HTML_NOSPACE;
1.30 kristaps 816: h->flags &= ~HTML_NEWLINE;
1.1 kristaps 817:
1.29 kristaps 818: if (p)
1.32 kristaps 819: print_encode(h, p);
1.8 kristaps 820:
1.29 kristaps 821: if (*p && 0 == *(p + 1))
822: switch (*p) {
823: case('('):
824: /* FALLTHROUGH */
825: case('['):
826: /* FALLTHROUGH */
827: case('{'):
828: h->flags |= HTML_NOSPACE;
1.30 kristaps 829: break;
1.29 kristaps 830: default:
831: break;
832: }
1.1 kristaps 833: }
1.30 kristaps 834:
835:
836: static void
837: print_tagq(struct html *h, const struct tag *until)
838: {
839: struct tag *tag;
840:
841: while ( ! SLIST_EMPTY(&h->stack)) {
842: tag = SLIST_FIRST(&h->stack);
843: print_ctag(h, tag->tag);
844: SLIST_REMOVE_HEAD(&h->stack, entry);
845: free(tag);
846: if (until && tag == until)
847: return;
848: }
849: }
850:
851:
852: static void
853: print_stagq(struct html *h, const struct tag *suntil)
854: {
855: struct tag *tag;
856:
857: while ( ! SLIST_EMPTY(&h->stack)) {
858: tag = SLIST_FIRST(&h->stack);
859: if (suntil && tag == suntil)
860: return;
861: print_ctag(h, tag->tag);
862: SLIST_REMOVE_HEAD(&h->stack, entry);
863: free(tag);
864: }
865: }
866:
867:
1.35 kristaps 868: /* FIXME: put in utility file for front-ends. */
1.33 kristaps 869: static int
870: a2offs(const char *p)
871: {
872: int len, i;
873:
874: if (0 == strcmp(p, "left"))
875: return(0);
876: if (0 == strcmp(p, "indent"))
877: return(INDENT + 1);
878: if (0 == strcmp(p, "indent-two"))
879: return((INDENT + 1) * 2);
880:
881: if (0 == (len = (int)strlen(p)))
882: return(0);
883:
884: for (i = 0; i < len - 1; i++)
885: if ( ! isdigit((u_char)p[i]))
886: break;
887:
888: if (i == len - 1)
889: if ('n' == p[len - 1] || 'm' == p[len - 1])
890: return(atoi(p));
891:
892: return(len);
893: }
894:
895:
1.35 kristaps 896: /* FIXME: put in utility file for front-ends. */
1.33 kristaps 897: static int
898: a2width(const char *p)
899: {
900: int i, len;
901:
902: if (0 == (len = (int)strlen(p)))
903: return(0);
904: for (i = 0; i < len - 1; i++)
905: if ( ! isdigit((u_char)p[i]))
906: break;
907:
908: if (i == len - 1)
909: if ('n' == p[len - 1] || 'm' == p[len - 1])
910: return(atoi(p) + 2);
911:
912: return(len + 2);
913: }
914:
915:
916:
917:
1.30 kristaps 918: /* ARGSUSED */
919: static int
920: mdoc_root_pre(MDOC_ARGS)
921: {
922: struct htmlpair tag;
923:
924: tag.key = ATTR_CLASS;
925: tag.val = "body";
926:
927: print_otag(h, TAG_DIV, 1, &tag);
928: return(1);
929: }
930:
931:
932: /* ARGSUSED */
933: static int
1.35 kristaps 934: mdoc_sh_pre(MDOC_ARGS)
1.30 kristaps 935: {
1.34 kristaps 936: struct htmlpair tag[2];
937:
1.35 kristaps 938: if (MDOC_HEAD == n->type) {
939: tag[0].key = ATTR_CLASS;
940: tag[0].val = "sec-head";
941: print_otag(h, TAG_DIV, 1, tag);
942: print_otag(h, TAG_SPAN, 1, tag);
943: return(1);
944: } else if (MDOC_BLOCK == n->type) {
945: tag[0].key = ATTR_CLASS;
946: tag[0].val = "sec-block";
947: print_otag(h, TAG_DIV, 1, tag);
948: return(1);
949: }
950:
951: buffmt("margin-left: %dem;", INDENT);
952:
953: if (n->parent->next && n->child)
954: bufcat("margin-bottom: 1em;");
955:
1.34 kristaps 956: tag[0].key = ATTR_CLASS;
1.35 kristaps 957: tag[0].val = "sec-body";
958: tag[1].key = ATTR_STYLE;
959: tag[1].val = buf;
960:
961: print_otag(h, TAG_DIV, 2, tag);
962: return(1);
963: }
964:
965:
966: /* ARGSUSED */
967: static int
968: mdoc_ss_pre(MDOC_ARGS)
969: {
970: struct htmlpair tag[2];
971: int i;
972:
973: i = 0;
974:
975: if (MDOC_BODY == n->type) {
976: tag[i].key = ATTR_CLASS;
977: tag[i++].val = "ssec-body";
978: if (n->parent->next && n->child) {
979: bufcat("margin-bottom: 1em;");
980: tag[i].key = ATTR_STYLE;
981: tag[i++].val = buf;
982: }
983: print_otag(h, TAG_DIV, i, tag);
984: return(1);
985: } else if (MDOC_BLOCK == n->type) {
986: tag[i].key = ATTR_CLASS;
987: tag[i++].val = "ssec-block";
988: if (n->prev) {
989: bufcat("margin-top: 1em;");
990: tag[i].key = ATTR_STYLE;
991: tag[i++].val = buf;
992: }
993: print_otag(h, TAG_DIV, i, tag);
994: return(1);
995: }
996:
997: buffmt("margin-left: -%dem;", INDENT - HALFINDENT);
1.34 kristaps 998:
1.35 kristaps 999: tag[0].key = ATTR_CLASS;
1000: tag[0].val = "ssec-head";
1.34 kristaps 1001: tag[1].key = ATTR_STYLE;
1.35 kristaps 1002: tag[1].val = buf;
1.30 kristaps 1003:
1.35 kristaps 1004: print_otag(h, TAG_DIV, 2, tag);
1005: print_otag(h, TAG_SPAN, 1, tag);
1.30 kristaps 1006: return(1);
1007: }
1008:
1009:
1010: /* ARGSUSED */
1011: static int
1012: mdoc_fl_pre(MDOC_ARGS)
1013: {
1014: struct htmlpair tag;
1015:
1016: tag.key = ATTR_CLASS;
1017: tag.val = "flag";
1018:
1019: print_otag(h, TAG_SPAN, 1, &tag);
1020: print_text(h, "\\-");
1021: h->flags |= HTML_NOSPACE;
1022: return(1);
1023: }
1024:
1025:
1026: /* ARGSUSED */
1027: static int
1028: mdoc_pp_pre(MDOC_ARGS)
1029: {
1.34 kristaps 1030: struct htmlpair tag;
1.30 kristaps 1031:
1.35 kristaps 1032: bufcat("clear: both;");
1033: bufcat("height: 1em;");
1034:
1.34 kristaps 1035: tag.key = ATTR_STYLE;
1.35 kristaps 1036: tag.val = buf;
1.34 kristaps 1037:
1.35 kristaps 1038: print_otag(h, TAG_DIV, 1, &tag);
1.30 kristaps 1039: return(0);
1040: }
1041:
1042:
1043: /* ARGSUSED */
1044: static int
1045: mdoc_nd_pre(MDOC_ARGS)
1046: {
1.35 kristaps 1047: struct htmlpair tag;
1048:
1049: if (MDOC_BODY != n->type)
1050: return(1);
1.30 kristaps 1051:
1.35 kristaps 1052: /* XXX - this can contain block elements! */
1053: print_text(h, "\\(em");
1054: tag.key = ATTR_CLASS;
1055: tag.val = "desc-body";
1056: print_otag(h, TAG_SPAN, 1, &tag);
1.30 kristaps 1057: return(1);
1058: }
1059:
1060:
1061: /* ARGSUSED */
1062: static int
1063: mdoc_op_pre(MDOC_ARGS)
1064: {
1.35 kristaps 1065: struct htmlpair tag;
1066:
1067: if (MDOC_BODY != n->type)
1068: return(1);
1.30 kristaps 1069:
1.35 kristaps 1070: /* XXX - this can contain block elements! */
1071: print_text(h, "\\(lB");
1072: tag.key = ATTR_CLASS;
1073: tag.val = "opt";
1074: print_otag(h, TAG_SPAN, 1, &tag);
1.30 kristaps 1075: return(1);
1076: }
1077:
1078:
1079: /* ARGSUSED */
1080: static void
1081: mdoc_op_post(MDOC_ARGS)
1082: {
1083:
1084: if (MDOC_BODY != n->type)
1085: return;
1086: h->flags |= HTML_NOSPACE;
1087: print_text(h, "\\(rB");
1088: }
1089:
1090:
1091: static int
1092: mdoc_nm_pre(MDOC_ARGS)
1093: {
1.35 kristaps 1094: struct htmlpair tag;
1.30 kristaps 1095:
1096: if ( ! (HTML_NEWLINE & h->flags))
1.35 kristaps 1097: if (SEC_SYNOPSIS == n->sec) {
1098: tag.key = ATTR_STYLE;
1099: tag.val = "clear: both;";
1100: print_otag(h, TAG_BR, 1, &tag);
1101: }
1.30 kristaps 1102:
1.35 kristaps 1103: tag.key = ATTR_CLASS;
1104: tag.val = "name";
1.30 kristaps 1105:
1.35 kristaps 1106: print_otag(h, TAG_SPAN, 1, &tag);
1.30 kristaps 1107: if (NULL == n->child)
1108: print_text(h, m->name);
1109:
1110: return(1);
1111: }
1112:
1113:
1114: /* ARGSUSED */
1115: static int
1116: mdoc_xr_pre(MDOC_ARGS)
1117: {
1.35 kristaps 1118: struct htmlpair tag[2];
1.30 kristaps 1119:
1.35 kristaps 1120: tag[0].key = ATTR_CLASS;
1121: tag[0].val = "link-man";
1122: tag[1].key = ATTR_HREF;
1123: tag[1].val = "#"; /* TODO */
1.30 kristaps 1124:
1.35 kristaps 1125: print_otag(h, TAG_A, 2, tag);
1.30 kristaps 1126:
1127: n = n->child;
1128: print_text(h, n->string);
1129: if (NULL == (n = n->next))
1130: return(0);
1131:
1132: h->flags |= HTML_NOSPACE;
1133: print_text(h, "(");
1134: h->flags |= HTML_NOSPACE;
1135: print_text(h, n->string);
1136: h->flags |= HTML_NOSPACE;
1137: print_text(h, ")");
1138:
1139: return(0);
1140: }
1.31 kristaps 1141:
1142:
1143: /* ARGSUSED */
1144: static int
1145: mdoc_ns_pre(MDOC_ARGS)
1146: {
1147:
1148: h->flags |= HTML_NOSPACE;
1149: return(1);
1150: }
1151:
1.35 kristaps 1152:
1.31 kristaps 1153: /* ARGSUSED */
1154: static int
1155: mdoc_ar_pre(MDOC_ARGS)
1156: {
1157: struct htmlpair tag;
1158:
1159: tag.key = ATTR_CLASS;
1160: tag.val = "arg";
1161:
1162: print_otag(h, TAG_SPAN, 1, &tag);
1163: return(1);
1164: }
1.33 kristaps 1165:
1.35 kristaps 1166:
1.33 kristaps 1167: /* ARGSUSED */
1168: static int
1169: mdoc_xx_pre(MDOC_ARGS)
1170: {
1171: const char *pp;
1.35 kristaps 1172: struct htmlpair tag;
1.33 kristaps 1173:
1174: switch (n->tok) {
1175: case (MDOC_Bsx):
1176: pp = "BSDI BSD/OS";
1177: break;
1178: case (MDOC_Dx):
1179: pp = "DragonFlyBSD";
1180: break;
1181: case (MDOC_Fx):
1182: pp = "FreeBSD";
1183: break;
1184: case (MDOC_Nx):
1185: pp = "NetBSD";
1186: break;
1187: case (MDOC_Ox):
1188: pp = "OpenBSD";
1189: break;
1190: case (MDOC_Ux):
1191: pp = "UNIX";
1192: break;
1193: default:
1194: return(1);
1195: }
1196:
1.35 kristaps 1197: tag.key = ATTR_CLASS;
1198: tag.val = "unix";
1199:
1200: print_otag(h, TAG_SPAN, 1, &tag);
1.33 kristaps 1201: print_text(h, pp);
1202: return(1);
1203: }
1204:
1205:
1.35 kristaps 1206: /* ARGSUSED */
1.33 kristaps 1207: static int
1.34 kristaps 1208: mdoc_tbl_block_pre(MDOC_ARGS, int w, int o, int c)
1209: {
1210: struct htmlpair tag;
1211:
1.35 kristaps 1212: buffmt("margin-left: %dem; clear: both;", w + o);
1.34 kristaps 1213:
1214: if ( ! c)
1.35 kristaps 1215: bufcat("padding-top: 1em;");
1.34 kristaps 1216:
1217: tag.key = ATTR_STYLE;
1218: tag.val = buf;
1219: print_otag(h, TAG_DIV, 1, &tag);
1220: return(1);
1221: }
1222:
1223:
1.35 kristaps 1224: /* ARGSUSED */
1.34 kristaps 1225: static int
1226: mdoc_tbl_body_pre(MDOC_ARGS, int t, int w)
1227: {
1228: struct htmlpair tag;
1229: int i;
1230:
1231: switch (t) {
1232: case (MDOC_Tag):
1.35 kristaps 1233: i = 1;
1.34 kristaps 1234: tag.key = ATTR_STYLE;
1235: tag.val = buf;
1.35 kristaps 1236: bufcat("clear: right;");
1237: bufcat("float: left;");
1238: bufcat("width: 100%%;");
1.34 kristaps 1239: break;
1240: default:
1.35 kristaps 1241: i = 0;
1.34 kristaps 1242: break;
1243: }
1244:
1245: print_otag(h, TAG_DIV, i, &tag);
1246: return(1);
1247: }
1248:
1249:
1.35 kristaps 1250: /* ARGSUSED */
1.34 kristaps 1251: static int
1.35 kristaps 1252: mdoc_tbl_head_pre(MDOC_ARGS, int t, int w)
1.33 kristaps 1253: {
1.34 kristaps 1254: struct htmlpair tag;
1255: int i;
1256:
1.35 kristaps 1257: switch (t) {
1258: case (MDOC_Hang):
1259: /* FALLTHROUGH */
1.34 kristaps 1260: case (MDOC_Tag):
1.35 kristaps 1261: i = 1;
1.34 kristaps 1262: tag.key = ATTR_STYLE;
1263: tag.val = buf;
1.35 kristaps 1264: buffmt("margin-left: -%dem;", w);
1265: bufcat("clear: left;");
1266: bufcat("float: left;");
1267: bufcat("padding-right: 1em;");
1.34 kristaps 1268: break;
1269: default:
1.35 kristaps 1270: i = 0;
1.34 kristaps 1271: break;
1.33 kristaps 1272: }
1273:
1.34 kristaps 1274: print_otag(h, TAG_DIV, i, &tag);
1275: return(1);
1276: }
1277:
1278:
1279: static int
1280: mdoc_tbl_pre(MDOC_ARGS, int type)
1281: {
1282: int i, w, o, c;
1283: const struct mdoc_node *bl;
1284:
1.33 kristaps 1285: bl = n->parent->parent;
1286: if (MDOC_BLOCK != n->type)
1287: bl = bl->parent;
1288:
1.34 kristaps 1289: /* FIXME: fmt_vspace() equivalent. */
1290:
1.33 kristaps 1291: assert(bl->args);
1292:
1.34 kristaps 1293: w = o = c = 0;
1294:
1295: for (i = 0; i < (int)bl->args->argc; i++)
1.33 kristaps 1296: if (MDOC_Width == bl->args->argv[i].arg) {
1297: assert(bl->args->argv[i].sz);
1.34 kristaps 1298: w = a2width(bl->args->argv[i].value[0]);
1299: } else if (MDOC_Offset == bl->args->argv[i].arg) {
1300: assert(bl->args->argv[i].sz);
1301: o = a2offs(bl->args->argv[i].value[0]);
1302: } else if (MDOC_Compact == bl->args->argv[i].arg)
1303: c = 1;
1304:
1305: if (0 == w)
1306: w = 10;
1307:
1308: switch (n->type) {
1309: case (MDOC_BLOCK):
1310: break;
1311: case (MDOC_HEAD):
1312: return(mdoc_tbl_head_pre(m, n, h, type, w));
1313: case (MDOC_BODY):
1314: return(mdoc_tbl_body_pre(m, n, h, type, w));
1315: default:
1316: abort();
1317: /* NOTREACHED */
1318: }
1319:
1320: return(mdoc_tbl_block_pre(m, n, h, w, o, c));
1321: }
1322:
1323:
1324: /* ARGSUSED */
1325: static int
1326: mdoc_listitem_pre(MDOC_ARGS)
1327: {
1328: int i, w, o, c;
1329: const struct mdoc_node *bl;
1330: struct htmlpair tag;
1331:
1332: /* FIXME: fmt_vspace() equivalent. */
1333:
1334: if (MDOC_BLOCK != n->type)
1335: return(1);
1336:
1337: bl = n->parent->parent;
1338: assert(bl);
1339:
1340: w = o = c = 0;
1341:
1342: for (i = 0; i < (int)bl->args->argc; i++)
1343: if (MDOC_Width == bl->args->argv[i].arg) {
1344: assert(bl->args->argv[i].sz);
1345: w = a2width(bl->args->argv[i].value[0]);
1.33 kristaps 1346: } else if (MDOC_Offset == bl->args->argv[i].arg) {
1347: assert(bl->args->argv[i].sz);
1.34 kristaps 1348: o = a2offs(bl->args->argv[i].value[0]);
1349: } else if (MDOC_Compact == bl->args->argv[i].arg)
1350: c = 1;
1.35 kristaps 1351:
1352: if (o && w)
1353: buffmt("margin-left: %dem; padding-left: %dem;", o, w);
1354: else if (w)
1355: buffmt("padding-left: %dem;", w > 4 ? w - 4 : w);
1356: else if (o)
1357: buffmt("margin-left: %dem;", o);
1.34 kristaps 1358:
1359: if ( ! c)
1.35 kristaps 1360: bufcat("padding-top: 1em;");
1.33 kristaps 1361:
1362: tag.key = ATTR_STYLE;
1363: tag.val = buf;
1364:
1.34 kristaps 1365: print_otag(h, TAG_LI, 1, &tag);
1.33 kristaps 1366: return(1);
1367: }
1368:
1369:
1.34 kristaps 1370: /* ARGSUSED */
1.33 kristaps 1371: static int
1.34 kristaps 1372: mdoc_list_pre(MDOC_ARGS, int type)
1.33 kristaps 1373: {
1374:
1.34 kristaps 1375: switch (type) {
1376: case (MDOC_Enum):
1377: print_otag(h, TAG_OL, 0, NULL);
1378: break;
1379: case (MDOC_Bullet):
1380: print_otag(h, TAG_UL, 0, NULL);
1381: break;
1382: default:
1383: break;
1384: }
1385:
1386: return(1);
1.33 kristaps 1387: }
1388:
1389:
1390: static int
1.34 kristaps 1391: mdoc_bl_pre(MDOC_ARGS)
1.33 kristaps 1392: {
1.34 kristaps 1393: int i, len, type;
1.33 kristaps 1394:
1.34 kristaps 1395: if (MDOC_BLOCK != n->type)
1396: return(1);
1.33 kristaps 1397:
1.34 kristaps 1398: assert(n->args);
1399: len = (int)n->args->argc;
1.33 kristaps 1400:
1401: for (i = 0; i < len; i++)
1.34 kristaps 1402: switch ((type = n->args->argv[i].arg)) {
1403: case (MDOC_Enum):
1404: /* FALLTHROUGH */
1.33 kristaps 1405: case (MDOC_Bullet):
1.34 kristaps 1406: return(mdoc_list_pre(m, n, h, type));
1407: case (MDOC_Tag):
1408: /* FALLTHROUGH */
1409: case (MDOC_Hang):
1.33 kristaps 1410: /* FALLTHROUGH */
1411: case (MDOC_Dash):
1412: /* FALLTHROUGH */
1.34 kristaps 1413: case (MDOC_Hyphen):
1.33 kristaps 1414: /* FALLTHROUGH */
1415: case (MDOC_Inset):
1416: /* FALLTHROUGH */
1417: case (MDOC_Diag):
1418: /* FALLTHROUGH */
1419: case (MDOC_Item):
1420: /* FALLTHROUGH */
1421: case (MDOC_Column):
1422: /* FALLTHROUGH */
1423: case (MDOC_Ohang):
1.34 kristaps 1424: return(1);
1.33 kristaps 1425: default:
1.34 kristaps 1426: break;
1.33 kristaps 1427: }
1428:
1429: abort();
1430: /* NOTREACHED */
1431: }
1432:
1433:
1434: static int
1.34 kristaps 1435: mdoc_it_pre(MDOC_ARGS)
1.33 kristaps 1436: {
1.34 kristaps 1437: int i, len, type;
1438: const struct mdoc_node *bl;
1.33 kristaps 1439:
1.34 kristaps 1440: if (MDOC_BLOCK == n->type)
1441: bl = n->parent->parent;
1442: else
1443: bl = n->parent->parent->parent;
1.33 kristaps 1444:
1.34 kristaps 1445: assert(bl->args);
1446: len = (int)bl->args->argc;
1.33 kristaps 1447:
1448: for (i = 0; i < len; i++)
1.34 kristaps 1449: switch ((type = bl->args->argv[i].arg)) {
1450: case (MDOC_Tag):
1451: /* FALLTHROUGH */
1452: case (MDOC_Hang):
1453: return(mdoc_tbl_pre(m, n, h, type));
1454: case (MDOC_Enum):
1455: /* FALLTHROUGH */
1.33 kristaps 1456: case (MDOC_Bullet):
1.34 kristaps 1457: return(mdoc_listitem_pre(m, n, h));
1.33 kristaps 1458: case (MDOC_Dash):
1459: /* FALLTHROUGH */
1.34 kristaps 1460: case (MDOC_Hyphen):
1.33 kristaps 1461: /* FALLTHROUGH */
1462: case (MDOC_Inset):
1463: /* FALLTHROUGH */
1464: case (MDOC_Diag):
1465: /* FALLTHROUGH */
1466: case (MDOC_Item):
1467: /* FALLTHROUGH */
1468: case (MDOC_Column):
1469: /* FALLTHROUGH */
1470: case (MDOC_Ohang):
1.34 kristaps 1471: return(0);
1.33 kristaps 1472: default:
1.34 kristaps 1473: break;
1.33 kristaps 1474: }
1475:
1476: abort();
1477: /* NOTREACHED */
1478: }
1.34 kristaps 1479:
1480:
1481: /* ARGSUSED */
1482: static int
1483: mdoc_ex_pre(MDOC_ARGS)
1484: {
1485: const struct mdoc_node *nn;
1486: struct tag *t;
1487: struct htmlpair tag;
1488:
1489: print_text(h, "The");
1490:
1491: tag.key = ATTR_CLASS;
1492: tag.val = "utility";
1493:
1494: for (nn = n->child; nn; nn = nn->next) {
1495: t = print_otag(h, TAG_SPAN, 1, &tag);
1496: print_text(h, nn->string);
1497: print_tagq(h, t);
1498:
1499: h->flags |= HTML_NOSPACE;
1500:
1501: if (nn->next && NULL == nn->next->next)
1502: print_text(h, ", and");
1503: else if (nn->next)
1504: print_text(h, ",");
1505: else
1506: h->flags &= ~HTML_NOSPACE;
1507: }
1508:
1509: if (n->child->next)
1510: print_text(h, "utilities exit");
1511: else
1512: print_text(h, "utility exits");
1513:
1514: print_text(h, "0 on success, and >0 if an error occurs.");
1515: return(0);
1516: }
1517:
1518:
1519: /* ARGSUSED */
1520: static int
1521: mdoc_dq_pre(MDOC_ARGS)
1522: {
1523:
1524: if (MDOC_BODY != n->type)
1525: return(1);
1526: print_text(h, "\\(lq");
1527: h->flags |= HTML_NOSPACE;
1528: return(1);
1529: }
1530:
1531:
1532: /* ARGSUSED */
1533: static void
1534: mdoc_dq_post(MDOC_ARGS)
1535: {
1536:
1537: if (MDOC_BODY != n->type)
1538: return;
1539: h->flags |= HTML_NOSPACE;
1540: print_text(h, "\\(rq");
1541: }
1542:
1543:
1544: /* ARGSUSED */
1545: static int
1546: mdoc_pq_pre(MDOC_ARGS)
1547: {
1548:
1549: if (MDOC_BODY != n->type)
1550: return(1);
1551: print_text(h, "\\&(");
1552: h->flags |= HTML_NOSPACE;
1553: return(1);
1554: }
1555:
1556:
1557: /* ARGSUSED */
1558: static void
1559: mdoc_pq_post(MDOC_ARGS)
1560: {
1561:
1562: if (MDOC_BODY != n->type)
1563: return;
1564: print_text(h, ")");
1565: }
1566:
1567:
1568: /* ARGSUSED */
1569: static int
1570: mdoc_sq_pre(MDOC_ARGS)
1571: {
1572:
1573: if (MDOC_BODY != n->type)
1574: return(1);
1575: print_text(h, "\\(oq");
1576: h->flags |= HTML_NOSPACE;
1577: return(1);
1578: }
1579:
1580:
1581: /* ARGSUSED */
1582: static void
1583: mdoc_sq_post(MDOC_ARGS)
1584: {
1585:
1586: if (MDOC_BODY != n->type)
1587: return;
1588: h->flags |= HTML_NOSPACE;
1589: print_text(h, "\\(aq");
1590: }
1591:
1592:
1593: /* ARGSUSED */
1594: static int
1595: mdoc_em_pre(MDOC_ARGS)
1596: {
1597: struct htmlpair tag;
1598:
1599: tag.key = ATTR_CLASS;
1600: tag.val = "emph";
1601:
1602: print_otag(h, TAG_SPAN, 1, &tag);
1603: return(1);
1604: }
1605:
1606:
1607: /* ARGSUSED */
1608: static int
1609: mdoc_d1_pre(MDOC_ARGS)
1610: {
1.35 kristaps 1611: struct htmlpair tag[2];
1.34 kristaps 1612:
1613: if (MDOC_BLOCK != n->type)
1614: return(1);
1615:
1.35 kristaps 1616: buffmt("margin-left: %dem;", INDENT);
1617: bufcat("margin-bottom: 0px;");
1.34 kristaps 1618:
1.35 kristaps 1619: tag[0].key = ATTR_CLASS;
1620: tag[0].val = "lit-block";
1621: tag[1].key = ATTR_STYLE;
1622: tag[1].val = buf;
1.34 kristaps 1623:
1.35 kristaps 1624: print_otag(h, TAG_DIV, 2, tag);
1.34 kristaps 1625: return(1);
1626: }
1627:
1628:
1629: /* ARGSUSED */
1630: static int
1631: mdoc_sx_pre(MDOC_ARGS)
1632: {
1.35 kristaps 1633: struct htmlpair tag[2];
1.34 kristaps 1634:
1.35 kristaps 1635: tag[0].key = ATTR_HREF;
1636: tag[0].val = "#"; /* XXX */
1637: tag[1].key = ATTR_CLASS;
1638: tag[1].val = "link-sec";
1.34 kristaps 1639:
1.35 kristaps 1640: print_otag(h, TAG_A, 2, tag);
1.34 kristaps 1641: return(1);
1642: }
CVSweb