Annotation of src/usr.bin/mandoc/html.c, Revision 1.132
1.132 ! schwarze 1: /* $OpenBSD: html.c,v 1.131 2019/09/03 18:07:57 schwarze Exp $ */
1.1 schwarze 2: /*
1.42 schwarze 3: * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
1.119 schwarze 4: * Copyright (c) 2011-2015, 2017-2019 Ingo Schwarze <schwarze@openbsd.org>
1.1 schwarze 5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
1.56 schwarze 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.1 schwarze 11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.56 schwarze 12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.1 schwarze 13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18: #include <sys/types.h>
1.110 schwarze 19: #include <sys/stat.h>
1.1 schwarze 20:
21: #include <assert.h>
1.3 schwarze 22: #include <ctype.h>
1.4 schwarze 23: #include <stdarg.h>
1.99 schwarze 24: #include <stddef.h>
1.1 schwarze 25: #include <stdio.h>
26: #include <stdint.h>
27: #include <stdlib.h>
28: #include <string.h>
29: #include <unistd.h>
30:
1.80 schwarze 31: #include "mandoc_aux.h"
1.99 schwarze 32: #include "mandoc_ohash.h"
1.9 schwarze 33: #include "mandoc.h"
1.80 schwarze 34: #include "roff.h"
1.1 schwarze 35: #include "out.h"
36: #include "html.h"
1.56 schwarze 37: #include "manconf.h"
1.1 schwarze 38: #include "main.h"
39:
40: struct htmldata {
41: const char *name;
42: int flags;
1.127 schwarze 43: #define HTML_INPHRASE (1 << 0) /* Can appear in phrasing context. */
44: #define HTML_TOPHRASE (1 << 1) /* Establishes phrasing context. */
45: #define HTML_NOSTACK (1 << 2) /* Does not have an end tag. */
46: #define HTML_NLBEFORE (1 << 3) /* Output line break before opening. */
47: #define HTML_NLBEGIN (1 << 4) /* Output line break after opening. */
48: #define HTML_NLEND (1 << 5) /* Output line break before closing. */
49: #define HTML_NLAFTER (1 << 6) /* Output line break after closing. */
1.66 schwarze 50: #define HTML_NLAROUND (HTML_NLBEFORE | HTML_NLAFTER)
51: #define HTML_NLINSIDE (HTML_NLBEGIN | HTML_NLEND)
52: #define HTML_NLALL (HTML_NLAROUND | HTML_NLINSIDE)
1.127 schwarze 53: #define HTML_INDENT (1 << 7) /* Indent content by two spaces. */
54: #define HTML_NOINDENT (1 << 8) /* Exception: never indent content. */
1.1 schwarze 55: };
56:
57: static const struct htmldata htmltags[TAG_MAX] = {
1.66 schwarze 58: {"html", HTML_NLALL},
59: {"head", HTML_NLALL | HTML_INDENT},
1.127 schwarze 60: {"meta", HTML_NOSTACK | HTML_NLALL},
61: {"link", HTML_NOSTACK | HTML_NLALL},
62: {"style", HTML_NLALL | HTML_INDENT},
63: {"title", HTML_NLAROUND},
1.66 schwarze 64: {"body", HTML_NLALL},
65: {"div", HTML_NLAROUND},
1.123 schwarze 66: {"section", HTML_NLALL},
1.66 schwarze 67: {"table", HTML_NLALL | HTML_INDENT},
68: {"tr", HTML_NLALL | HTML_INDENT},
69: {"td", HTML_NLAROUND},
70: {"li", HTML_NLAROUND | HTML_INDENT},
71: {"ul", HTML_NLALL | HTML_INDENT},
72: {"ol", HTML_NLALL | HTML_INDENT},
73: {"dl", HTML_NLALL | HTML_INDENT},
74: {"dt", HTML_NLAROUND},
75: {"dd", HTML_NLAROUND | HTML_INDENT},
1.127 schwarze 76: {"h1", HTML_TOPHRASE | HTML_NLAROUND},
77: {"h2", HTML_TOPHRASE | HTML_NLAROUND},
78: {"p", HTML_TOPHRASE | HTML_NLAROUND | HTML_INDENT},
79: {"pre", HTML_TOPHRASE | HTML_NLALL | HTML_NOINDENT},
80: {"a", HTML_INPHRASE | HTML_TOPHRASE},
81: {"b", HTML_INPHRASE | HTML_TOPHRASE},
82: {"cite", HTML_INPHRASE | HTML_TOPHRASE},
83: {"code", HTML_INPHRASE | HTML_TOPHRASE},
84: {"i", HTML_INPHRASE | HTML_TOPHRASE},
85: {"small", HTML_INPHRASE | HTML_TOPHRASE},
86: {"span", HTML_INPHRASE | HTML_TOPHRASE},
87: {"var", HTML_INPHRASE | HTML_TOPHRASE},
88: {"br", HTML_INPHRASE | HTML_NOSTACK | HTML_NLALL},
89: {"math", HTML_INPHRASE | HTML_NLALL | HTML_INDENT},
1.66 schwarze 90: {"mrow", 0},
91: {"mi", 0},
1.85 schwarze 92: {"mn", 0},
1.66 schwarze 93: {"mo", 0},
94: {"msup", 0},
95: {"msub", 0},
96: {"msubsup", 0},
97: {"mfrac", 0},
98: {"msqrt", 0},
99: {"mfenced", 0},
100: {"mtable", 0},
101: {"mtr", 0},
102: {"mtd", 0},
103: {"munderover", 0},
104: {"munder", 0},
105: {"mover", 0},
1.5 schwarze 106: };
107:
1.99 schwarze 108: /* Avoid duplicate HTML id= attributes. */
109: static struct ohash id_unique;
110:
1.124 schwarze 111: static void html_reset_internal(struct html *);
1.67 schwarze 112: static void print_byte(struct html *, char);
113: static void print_endword(struct html *);
114: static void print_indent(struct html *);
115: static void print_word(struct html *, const char *);
116:
1.54 schwarze 117: static void print_ctag(struct html *, struct tag *);
1.67 schwarze 118: static int print_escape(struct html *, char);
1.65 schwarze 119: static int print_encode(struct html *, const char *, const char *, int);
120: static void print_href(struct html *, const char *, const char *, int);
1.125 schwarze 121: static void print_metaf(struct html *);
1.5 schwarze 122:
1.35 schwarze 123:
1.50 schwarze 124: void *
1.61 schwarze 125: html_alloc(const struct manoutput *outopts)
1.1 schwarze 126: {
127: struct html *h;
128:
1.24 schwarze 129: h = mandoc_calloc(1, sizeof(struct html));
1.1 schwarze 130:
1.74 schwarze 131: h->tag = NULL;
1.56 schwarze 132: h->style = outopts->style;
1.110 schwarze 133: if ((h->base_man1 = outopts->man) == NULL)
134: h->base_man2 = NULL;
135: else if ((h->base_man2 = strchr(h->base_man1, ';')) != NULL)
136: *h->base_man2++ = '\0';
1.56 schwarze 137: h->base_includes = outopts->includes;
138: if (outopts->fragment)
139: h->oflags |= HTML_FRAGMENT;
1.111 schwarze 140: if (outopts->toc)
141: h->oflags |= HTML_TOC;
1.1 schwarze 142:
1.99 schwarze 143: mandoc_ohash_init(&id_unique, 4, 0);
144:
1.58 schwarze 145: return h;
1.1 schwarze 146: }
147:
1.124 schwarze 148: static void
149: html_reset_internal(struct html *h)
1.1 schwarze 150: {
151: struct tag *tag;
1.99 schwarze 152: char *cp;
153: unsigned int slot;
1.1 schwarze 154:
1.74 schwarze 155: while ((tag = h->tag) != NULL) {
156: h->tag = tag->next;
1.1 schwarze 157: free(tag);
158: }
1.99 schwarze 159: cp = ohash_first(&id_unique, &slot);
160: while (cp != NULL) {
161: free(cp);
162: cp = ohash_next(&id_unique, &slot);
163: }
164: ohash_delete(&id_unique);
1.124 schwarze 165: }
166:
167: void
168: html_reset(void *p)
169: {
170: html_reset_internal(p);
171: mandoc_ohash_init(&id_unique, 4, 0);
172: }
173:
174: void
175: html_free(void *p)
176: {
177: html_reset_internal(p);
178: free(p);
1.1 schwarze 179: }
180:
181: void
182: print_gen_head(struct html *h)
183: {
1.42 schwarze 184: struct tag *t;
185:
1.64 schwarze 186: print_otag(h, TAG_META, "?", "charset", "utf-8");
1.92 schwarze 187: if (h->style != NULL) {
188: print_otag(h, TAG_LINK, "?h??", "rel", "stylesheet",
189: h->style, "type", "text/css", "media", "all");
190: return;
191: }
1.1 schwarze 192:
1.42 schwarze 193: /*
1.92 schwarze 194: * Print a minimal embedded style sheet.
1.42 schwarze 195: */
1.66 schwarze 196:
1.64 schwarze 197: t = print_otag(h, TAG_STYLE, "");
1.66 schwarze 198: print_text(h, "table.head, table.foot { width: 100%; }");
1.67 schwarze 199: print_endline(h);
1.66 schwarze 200: print_text(h, "td.head-rtitle, td.foot-os { text-align: right; }");
1.67 schwarze 201: print_endline(h);
1.66 schwarze 202: print_text(h, "td.head-vol { text-align: center; }");
1.67 schwarze 203: print_endline(h);
1.126 schwarze 204: print_text(h, ".Nd, .Bf, .Op { display: inline; }");
1.95 schwarze 205: print_endline(h);
1.126 schwarze 206: print_text(h, ".Pa, .Ad { font-style: italic; }");
1.96 schwarze 207: print_endline(h);
1.126 schwarze 208: print_text(h, ".Ms { font-weight: bold; }");
1.98 schwarze 209: print_endline(h);
1.126 schwarze 210: print_text(h, ".Bl-diag ");
1.94 schwarze 211: print_byte(h, '>');
212: print_text(h, " dt { font-weight: bold; }");
1.93 schwarze 213: print_endline(h);
1.126 schwarze 214: print_text(h, "code.Nm, .Fl, .Cm, .Ic, code.In, .Fd, .Fn, .Cd "
215: "{ font-weight: bold; font-family: inherit; }");
1.42 schwarze 216: print_tagq(h, t);
1.1 schwarze 217: }
218:
1.125 schwarze 219: int
220: html_setfont(struct html *h, enum mandoc_esc font)
1.5 schwarze 221: {
1.125 schwarze 222: switch (font) {
1.35 schwarze 223: case ESCAPE_FONTPREV:
1.5 schwarze 224: font = h->metal;
225: break;
1.35 schwarze 226: case ESCAPE_FONTITALIC:
227: case ESCAPE_FONTBOLD:
228: case ESCAPE_FONTBI:
1.112 schwarze 229: case ESCAPE_FONTCW:
1.125 schwarze 230: case ESCAPE_FONTROMAN:
1.112 schwarze 231: break;
1.35 schwarze 232: case ESCAPE_FONT:
1.125 schwarze 233: font = ESCAPE_FONTROMAN;
1.5 schwarze 234: break;
235: default:
1.125 schwarze 236: return 0;
1.1 schwarze 237: }
1.125 schwarze 238: h->metal = h->metac;
239: h->metac = font;
240: return 1;
241: }
1.1 schwarze 242:
1.125 schwarze 243: static void
244: print_metaf(struct html *h)
245: {
1.20 schwarze 246: if (h->metaf) {
247: print_tagq(h, h->metaf);
248: h->metaf = NULL;
249: }
1.125 schwarze 250: switch (h->metac) {
251: case ESCAPE_FONTITALIC:
1.64 schwarze 252: h->metaf = print_otag(h, TAG_I, "");
1.31 schwarze 253: break;
1.125 schwarze 254: case ESCAPE_FONTBOLD:
1.64 schwarze 255: h->metaf = print_otag(h, TAG_B, "");
1.31 schwarze 256: break;
1.125 schwarze 257: case ESCAPE_FONTBI:
1.64 schwarze 258: h->metaf = print_otag(h, TAG_B, "");
259: print_otag(h, TAG_I, "");
1.31 schwarze 260: break;
1.125 schwarze 261: case ESCAPE_FONTCW:
1.112 schwarze 262: h->metaf = print_otag(h, TAG_SPAN, "c", "Li");
263: break;
1.31 schwarze 264: default:
265: break;
266: }
1.118 schwarze 267: }
268:
1.119 schwarze 269: void
270: html_close_paragraph(struct html *h)
271: {
1.129 schwarze 272: struct tag *this, *next;
1.130 schwarze 273: int flags;
1.119 schwarze 274:
1.129 schwarze 275: this = h->tag;
276: for (;;) {
277: next = this->next;
1.130 schwarze 278: flags = htmltags[this->tag].flags;
279: if (flags & (HTML_INPHRASE | HTML_TOPHRASE))
1.129 schwarze 280: print_ctag(h, this);
1.130 schwarze 281: if ((flags & HTML_INPHRASE) == 0)
1.119 schwarze 282: break;
1.129 schwarze 283: this = next;
1.119 schwarze 284: }
285: }
286:
1.118 schwarze 287: /*
288: * ROFF_nf switches to no-fill mode, ROFF_fi to fill mode.
289: * TOKEN_NONE does not switch. The old mode is returned.
290: */
291: enum roff_tok
292: html_fillmode(struct html *h, enum roff_tok want)
293: {
294: struct tag *t;
295: enum roff_tok had;
296:
297: for (t = h->tag; t != NULL; t = t->next)
298: if (t->tag == TAG_PRE)
299: break;
300:
301: had = t == NULL ? ROFF_fi : ROFF_nf;
302:
303: if (want != had) {
304: switch (want) {
305: case ROFF_fi:
306: print_tagq(h, t);
307: break;
308: case ROFF_nf:
1.119 schwarze 309: html_close_paragraph(h);
1.118 schwarze 310: print_otag(h, TAG_PRE, "");
311: break;
312: case TOKEN_NONE:
313: break;
314: default:
315: abort();
316: }
317: }
318: return had;
1.80 schwarze 319: }
320:
321: char *
1.99 schwarze 322: html_make_id(const struct roff_node *n, int unique)
1.80 schwarze 323: {
324: const struct roff_node *nch;
1.99 schwarze 325: char *buf, *bufs, *cp;
326: unsigned int slot;
327: int suffix;
1.80 schwarze 328:
329: for (nch = n->child; nch != NULL; nch = nch->next)
330: if (nch->type != ROFFT_TEXT)
331: return NULL;
332:
333: buf = NULL;
334: deroff(&buf, n);
1.90 schwarze 335: if (buf == NULL)
336: return NULL;
1.80 schwarze 337:
1.100 schwarze 338: /*
339: * In ID attributes, only use ASCII characters that are
340: * permitted in URL-fragment strings according to the
341: * explicit list at:
342: * https://url.spec.whatwg.org/#url-fragment-string
343: */
1.80 schwarze 344:
345: for (cp = buf; *cp != '\0'; cp++)
1.100 schwarze 346: if (isalnum((unsigned char)*cp) == 0 &&
347: strchr("!$&'()*+,-./:;=?@_~", *cp) == NULL)
1.80 schwarze 348: *cp = '_';
349:
1.99 schwarze 350: if (unique == 0)
351: return buf;
352:
353: /* Avoid duplicate HTML id= attributes. */
354:
355: bufs = NULL;
356: suffix = 1;
357: slot = ohash_qlookup(&id_unique, buf);
358: cp = ohash_find(&id_unique, slot);
359: if (cp != NULL) {
360: while (cp != NULL) {
361: free(bufs);
362: if (++suffix > 127) {
363: free(buf);
364: return NULL;
365: }
366: mandoc_asprintf(&bufs, "%s_%d", buf, suffix);
367: slot = ohash_qlookup(&id_unique, bufs);
368: cp = ohash_find(&id_unique, slot);
369: }
370: free(buf);
371: buf = bufs;
372: }
373: ohash_insert(&id_unique, slot, buf);
1.80 schwarze 374: return buf;
1.1 schwarze 375: }
376:
1.5 schwarze 377: static int
1.67 schwarze 378: print_escape(struct html *h, char c)
1.38 schwarze 379: {
380:
381: switch (c) {
382: case '<':
1.67 schwarze 383: print_word(h, "<");
1.38 schwarze 384: break;
385: case '>':
1.67 schwarze 386: print_word(h, ">");
1.38 schwarze 387: break;
388: case '&':
1.67 schwarze 389: print_word(h, "&");
1.38 schwarze 390: break;
391: case '"':
1.67 schwarze 392: print_word(h, """);
1.38 schwarze 393: break;
394: case ASCII_NBRSP:
1.67 schwarze 395: print_word(h, " ");
1.38 schwarze 396: break;
397: case ASCII_HYPH:
1.67 schwarze 398: print_byte(h, '-');
1.59 schwarze 399: break;
1.38 schwarze 400: case ASCII_BREAK:
401: break;
402: default:
1.58 schwarze 403: return 0;
1.38 schwarze 404: }
1.58 schwarze 405: return 1;
1.38 schwarze 406: }
407:
408: static int
1.65 schwarze 409: print_encode(struct html *h, const char *p, const char *pend, int norecurse)
1.1 schwarze 410: {
1.67 schwarze 411: char numbuf[16];
1.84 schwarze 412: const char *seq;
1.4 schwarze 413: size_t sz;
1.84 schwarze 414: int c, len, breakline, nospace;
1.26 schwarze 415: enum mandoc_esc esc;
1.84 schwarze 416: static const char rejs[10] = { ' ', '\\', '<', '>', '&', '"',
1.33 schwarze 417: ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' };
1.5 schwarze 418:
1.65 schwarze 419: if (pend == NULL)
420: pend = strchr(p, '\0');
421:
1.84 schwarze 422: breakline = 0;
1.5 schwarze 423: nospace = 0;
1.1 schwarze 424:
1.65 schwarze 425: while (p < pend) {
1.30 schwarze 426: if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
427: h->flags &= ~HTML_SKIPCHAR;
428: p++;
429: continue;
430: }
431:
1.67 schwarze 432: for (sz = strcspn(p, rejs); sz-- && p < pend; p++)
1.84 schwarze 433: print_byte(h, *p);
434:
435: if (breakline &&
436: (p >= pend || *p == ' ' || *p == ASCII_NBRSP)) {
1.115 schwarze 437: print_otag(h, TAG_BR, "");
1.84 schwarze 438: breakline = 0;
439: while (p < pend && (*p == ' ' || *p == ASCII_NBRSP))
440: p++;
441: continue;
442: }
1.4 schwarze 443:
1.65 schwarze 444: if (p >= pend)
1.26 schwarze 445: break;
446:
1.84 schwarze 447: if (*p == ' ') {
448: print_endword(h);
449: p++;
450: continue;
451: }
452:
1.67 schwarze 453: if (print_escape(h, *p++))
1.33 schwarze 454: continue;
1.4 schwarze 455:
1.26 schwarze 456: esc = mandoc_escape(&p, &seq, &len);
457: switch (esc) {
1.35 schwarze 458: case ESCAPE_FONT:
459: case ESCAPE_FONTPREV:
460: case ESCAPE_FONTBOLD:
461: case ESCAPE_FONTITALIC:
462: case ESCAPE_FONTBI:
1.112 schwarze 463: case ESCAPE_FONTCW:
1.35 schwarze 464: case ESCAPE_FONTROMAN:
1.113 schwarze 465: if (0 == norecurse) {
466: h->flags |= HTML_NOSPACE;
1.125 schwarze 467: if (html_setfont(h, esc))
468: print_metaf(h);
1.113 schwarze 469: h->flags &= ~HTML_NOSPACE;
470: }
1.30 schwarze 471: continue;
1.35 schwarze 472: case ESCAPE_SKIPCHAR:
1.30 schwarze 473: h->flags |= HTML_SKIPCHAR;
474: continue;
1.116 schwarze 475: case ESCAPE_ERROR:
476: continue;
1.30 schwarze 477: default:
478: break;
479: }
480:
481: if (h->flags & HTML_SKIPCHAR) {
482: h->flags &= ~HTML_SKIPCHAR;
483: continue;
484: }
485:
486: switch (esc) {
1.35 schwarze 487: case ESCAPE_UNICODE:
1.38 schwarze 488: /* Skip past "u" header. */
1.26 schwarze 489: c = mchars_num2uc(seq + 1, len - 1);
490: break;
1.35 schwarze 491: case ESCAPE_NUMBERED:
1.26 schwarze 492: c = mchars_num2char(seq, len);
1.51 schwarze 493: if (c < 0)
494: continue;
1.26 schwarze 495: break;
1.35 schwarze 496: case ESCAPE_SPECIAL:
1.61 schwarze 497: c = mchars_spec2cp(seq, len);
1.51 schwarze 498: if (c <= 0)
499: continue;
1.116 schwarze 500: break;
501: case ESCAPE_UNDEF:
502: c = *seq;
1.26 schwarze 503: break;
1.109 schwarze 504: case ESCAPE_DEVICE:
505: print_word(h, "html");
506: continue;
1.84 schwarze 507: case ESCAPE_BREAK:
508: breakline = 1;
509: continue;
1.35 schwarze 510: case ESCAPE_NOSPACE:
1.26 schwarze 511: if ('\0' == *p)
512: nospace = 1;
1.49 schwarze 513: continue;
1.55 schwarze 514: case ESCAPE_OVERSTRIKE:
515: if (len == 0)
516: continue;
517: c = seq[len - 1];
518: break;
1.5 schwarze 519: default:
1.49 schwarze 520: continue;
1.5 schwarze 521: }
1.51 schwarze 522: if ((c < 0x20 && c != 0x09) ||
523: (c > 0x7E && c < 0xA0))
1.49 schwarze 524: c = 0xFFFD;
1.67 schwarze 525: if (c > 0x7E) {
1.86 bentley 526: (void)snprintf(numbuf, sizeof(numbuf), "&#x%.4X;", c);
1.67 schwarze 527: print_word(h, numbuf);
528: } else if (print_escape(h, c) == 0)
529: print_byte(h, c);
1.1 schwarze 530: }
1.5 schwarze 531:
1.58 schwarze 532: return nospace;
1.1 schwarze 533: }
534:
1.6 schwarze 535: static void
1.65 schwarze 536: print_href(struct html *h, const char *name, const char *sec, int man)
1.6 schwarze 537: {
1.110 schwarze 538: struct stat sb;
1.65 schwarze 539: const char *p, *pp;
1.110 schwarze 540: char *filename;
541:
542: if (man) {
543: pp = h->base_man1;
544: if (h->base_man2 != NULL) {
545: mandoc_asprintf(&filename, "%s.%s", name, sec);
546: if (stat(filename, &sb) == -1)
547: pp = h->base_man2;
548: free(filename);
549: }
550: } else
551: pp = h->base_includes;
1.65 schwarze 552:
553: while ((p = strchr(pp, '%')) != NULL) {
554: print_encode(h, pp, p, 1);
555: if (man && p[1] == 'S') {
556: if (sec == NULL)
1.67 schwarze 557: print_byte(h, '1');
1.65 schwarze 558: else
559: print_encode(h, sec, NULL, 1);
560: } else if ((man && p[1] == 'N') ||
561: (man == 0 && p[1] == 'I'))
562: print_encode(h, name, NULL, 1);
563: else
564: print_encode(h, p, p + 2, 1);
565: pp = p + 2;
566: }
567: if (*pp != '\0')
568: print_encode(h, pp, NULL, 1);
1.6 schwarze 569: }
570:
1.1 schwarze 571: struct tag *
1.64 schwarze 572: print_otag(struct html *h, enum htmltag tag, const char *fmt, ...)
1.1 schwarze 573: {
1.64 schwarze 574: va_list ap;
1.1 schwarze 575: struct tag *t;
1.65 schwarze 576: const char *attr;
1.73 schwarze 577: char *arg1, *arg2;
1.114 schwarze 578: int style_written, tflags;
1.66 schwarze 579:
580: tflags = htmltags[tag].flags;
1.1 schwarze 581:
1.127 schwarze 582: /* Flow content is not allowed in phrasing context. */
583:
584: if ((tflags & HTML_INPHRASE) == 0) {
585: for (t = h->tag; t != NULL; t = t->next) {
586: if (t->closed)
587: continue;
588: assert((htmltags[t->tag].flags & HTML_TOPHRASE) == 0);
589: break;
590: }
1.131 schwarze 591:
592: /*
593: * Always wrap phrasing elements in a paragraph
594: * unless already contained in some flow container;
595: * never put them directly into a section.
596: */
597:
598: } else if (tflags & HTML_TOPHRASE && h->tag->tag == TAG_SECTION)
599: print_otag(h, TAG_P, "c", "Pp");
1.127 schwarze 600:
1.74 schwarze 601: /* Push this tag onto the stack of open scopes. */
1.6 schwarze 602:
1.66 schwarze 603: if ((tflags & HTML_NOSTACK) == 0) {
1.24 schwarze 604: t = mandoc_malloc(sizeof(struct tag));
1.1 schwarze 605: t->tag = tag;
1.74 schwarze 606: t->next = h->tag;
1.122 schwarze 607: t->refcnt = 0;
608: t->closed = 0;
1.74 schwarze 609: h->tag = t;
1.1 schwarze 610: } else
611: t = NULL;
612:
1.66 schwarze 613: if (tflags & HTML_NLBEFORE)
1.67 schwarze 614: print_endline(h);
615: if (h->col == 0)
616: print_indent(h);
1.66 schwarze 617: else if ((h->flags & HTML_NOSPACE) == 0) {
618: if (h->flags & HTML_KEEP)
1.86 bentley 619: print_word(h, " ");
1.66 schwarze 620: else {
621: if (h->flags & HTML_PREKEEP)
622: h->flags |= HTML_KEEP;
1.67 schwarze 623: print_endword(h);
1.12 schwarze 624: }
1.66 schwarze 625: }
1.1 schwarze 626:
1.13 schwarze 627: if ( ! (h->flags & HTML_NONOSPACE))
628: h->flags &= ~HTML_NOSPACE;
1.14 schwarze 629: else
630: h->flags |= HTML_NOSPACE;
1.13 schwarze 631:
1.6 schwarze 632: /* Print out the tag name and attributes. */
633:
1.67 schwarze 634: print_byte(h, '<');
635: print_word(h, htmltags[tag].name);
1.64 schwarze 636:
637: va_start(ap, fmt);
638:
1.114 schwarze 639: while (*fmt != '\0' && *fmt != 's') {
1.73 schwarze 640:
1.108 schwarze 641: /* Parse attributes and arguments. */
1.73 schwarze 642:
643: arg1 = va_arg(ap, char *);
1.108 schwarze 644: arg2 = NULL;
1.64 schwarze 645: switch (*fmt++) {
646: case 'c':
1.65 schwarze 647: attr = "class";
1.64 schwarze 648: break;
649: case 'h':
1.65 schwarze 650: attr = "href";
1.64 schwarze 651: break;
652: case 'i':
1.65 schwarze 653: attr = "id";
1.64 schwarze 654: break;
655: case '?':
1.73 schwarze 656: attr = arg1;
657: arg1 = va_arg(ap, char *);
1.64 schwarze 658: break;
659: default:
660: abort();
661: }
1.73 schwarze 662: if (*fmt == 'M')
663: arg2 = va_arg(ap, char *);
664: if (arg1 == NULL)
665: continue;
666:
1.108 schwarze 667: /* Print the attributes. */
1.73 schwarze 668:
1.67 schwarze 669: print_byte(h, ' ');
670: print_word(h, attr);
671: print_byte(h, '=');
672: print_byte(h, '"');
1.65 schwarze 673: switch (*fmt) {
1.78 schwarze 674: case 'I':
675: print_href(h, arg1, NULL, 0);
676: fmt++;
677: break;
1.65 schwarze 678: case 'M':
1.73 schwarze 679: print_href(h, arg1, arg2, 1);
1.65 schwarze 680: fmt++;
681: break;
1.78 schwarze 682: case 'R':
683: print_byte(h, '#');
684: print_encode(h, arg1, NULL, 1);
1.65 schwarze 685: fmt++;
1.78 schwarze 686: break;
1.65 schwarze 687: default:
1.114 schwarze 688: print_encode(h, arg1, NULL, 1);
1.65 schwarze 689: break;
690: }
1.67 schwarze 691: print_byte(h, '"');
1.64 schwarze 692: }
1.114 schwarze 693:
694: style_written = 0;
695: while (*fmt++ == 's') {
696: arg1 = va_arg(ap, char *);
697: arg2 = va_arg(ap, char *);
698: if (arg2 == NULL)
699: continue;
700: print_byte(h, ' ');
701: if (style_written == 0) {
702: print_word(h, "style=\"");
703: style_written = 1;
704: }
705: print_word(h, arg1);
706: print_byte(h, ':');
707: print_byte(h, ' ');
708: print_word(h, arg2);
709: print_byte(h, ';');
710: }
711: if (style_written)
712: print_byte(h, '"');
713:
1.64 schwarze 714: va_end(ap);
1.6 schwarze 715:
1.42 schwarze 716: /* Accommodate for "well-formed" singleton escaping. */
1.6 schwarze 717:
1.127 schwarze 718: if (htmltags[tag].flags & HTML_NOSTACK)
1.67 schwarze 719: print_byte(h, '/');
1.6 schwarze 720:
1.67 schwarze 721: print_byte(h, '>');
1.1 schwarze 722:
1.66 schwarze 723: if (tflags & HTML_NLBEGIN)
1.67 schwarze 724: print_endline(h);
1.66 schwarze 725: else
726: h->flags |= HTML_NOSPACE;
1.18 schwarze 727:
1.66 schwarze 728: if (tflags & HTML_INDENT)
729: h->indent++;
730: if (tflags & HTML_NOINDENT)
731: h->noindent++;
1.18 schwarze 732:
1.58 schwarze 733: return t;
1.1 schwarze 734: }
735:
736: static void
1.54 schwarze 737: print_ctag(struct html *h, struct tag *tag)
1.1 schwarze 738: {
1.66 schwarze 739: int tflags;
1.35 schwarze 740:
1.122 schwarze 741: if (tag->closed == 0) {
742: tag->closed = 1;
743: if (tag == h->metaf)
744: h->metaf = NULL;
745: if (tag == h->tblt)
746: h->tblt = NULL;
747:
748: tflags = htmltags[tag->tag].flags;
749: if (tflags & HTML_INDENT)
750: h->indent--;
751: if (tflags & HTML_NOINDENT)
752: h->noindent--;
753: if (tflags & HTML_NLEND)
754: print_endline(h);
755: print_indent(h);
756: print_byte(h, '<');
757: print_byte(h, '/');
758: print_word(h, htmltags[tag->tag].name);
759: print_byte(h, '>');
760: if (tflags & HTML_NLAFTER)
761: print_endline(h);
762: }
763: if (tag->refcnt == 0) {
764: h->tag = tag->next;
765: free(tag);
766: }
1.1 schwarze 767: }
768:
769: void
1.6 schwarze 770: print_gen_decls(struct html *h)
771: {
1.67 schwarze 772: print_word(h, "<!DOCTYPE html>");
773: print_endline(h);
1.91 schwarze 774: }
775:
776: void
777: print_gen_comment(struct html *h, struct roff_node *n)
778: {
779: int wantblank;
780:
781: print_word(h, "<!-- This is an automatically generated file."
782: " Do not edit.");
783: h->indent = 1;
784: wantblank = 0;
785: while (n != NULL && n->type == ROFFT_COMMENT) {
786: if (strstr(n->string, "-->") == NULL &&
787: (wantblank || *n->string != '\0')) {
788: print_endline(h);
789: print_indent(h);
790: print_word(h, n->string);
791: wantblank = *n->string != '\0';
792: }
793: n = n->next;
794: }
795: if (wantblank)
796: print_endline(h);
797: print_word(h, " -->");
798: print_endline(h);
799: h->indent = 0;
1.1 schwarze 800: }
801:
802: void
1.12 schwarze 803: print_text(struct html *h, const char *word)
1.1 schwarze 804: {
1.131 schwarze 805: /*
806: * Always wrap text in a paragraph unless already contained in
807: * some flow container; never put it directly into a section.
808: */
809:
810: if (h->tag->tag == TAG_SECTION)
811: print_otag(h, TAG_P, "c", "Pp");
812:
813: /* Output whitespace before this text? */
814:
1.67 schwarze 815: if (h->col && (h->flags & HTML_NOSPACE) == 0) {
1.12 schwarze 816: if ( ! (HTML_KEEP & h->flags)) {
817: if (HTML_PREKEEP & h->flags)
818: h->flags |= HTML_KEEP;
1.67 schwarze 819: print_endword(h);
1.12 schwarze 820: } else
1.86 bentley 821: print_word(h, " ");
1.12 schwarze 822: }
1.131 schwarze 823:
824: /*
825: * Print the text, optionally surrounded by HTML whitespace,
826: * optionally manually switching fonts before and after.
827: */
1.1 schwarze 828:
1.125 schwarze 829: assert(h->metaf == NULL);
830: print_metaf(h);
831: print_indent(h);
1.65 schwarze 832: if ( ! print_encode(h, word, NULL, 0)) {
1.13 schwarze 833: if ( ! (h->flags & HTML_NONOSPACE))
834: h->flags &= ~HTML_NOSPACE;
1.53 schwarze 835: h->flags &= ~HTML_NONEWLINE;
1.28 schwarze 836: } else
1.53 schwarze 837: h->flags |= HTML_NOSPACE | HTML_NONEWLINE;
1.20 schwarze 838:
1.125 schwarze 839: if (h->metaf != NULL) {
1.20 schwarze 840: print_tagq(h, h->metaf);
841: h->metaf = NULL;
842: }
1.17 schwarze 843:
844: h->flags &= ~HTML_IGNDELIM;
1.1 schwarze 845: }
846:
847: void
848: print_tagq(struct html *h, const struct tag *until)
849: {
1.122 schwarze 850: struct tag *this, *next;
1.1 schwarze 851:
1.122 schwarze 852: for (this = h->tag; this != NULL; this = next) {
853: next = this == until ? NULL : this->next;
854: print_ctag(h, this);
1.1 schwarze 855: }
856: }
857:
1.120 schwarze 858: /*
859: * Close out all open elements up to but excluding suntil.
860: * Note that a paragraph just inside stays open together with it
861: * because paragraphs include subsequent phrasing content.
862: */
1.1 schwarze 863: void
864: print_stagq(struct html *h, const struct tag *suntil)
865: {
1.122 schwarze 866: struct tag *this, *next;
1.1 schwarze 867:
1.122 schwarze 868: for (this = h->tag; this != NULL; this = next) {
869: next = this->next;
870: if (this == suntil || (next == suntil &&
871: (this->tag == TAG_P || this->tag == TAG_PRE)))
872: break;
873: print_ctag(h, this);
1.1 schwarze 874: }
1.42 schwarze 875: }
876:
1.67 schwarze 877:
878: /***********************************************************************
879: * Low level output functions.
880: * They implement line breaking using a short static buffer.
881: ***********************************************************************/
882:
883: /*
884: * Buffer one HTML output byte.
885: * If the buffer is full, flush and deactivate it and start a new line.
886: * If the buffer is inactive, print directly.
887: */
888: static void
889: print_byte(struct html *h, char c)
890: {
891: if ((h->flags & HTML_BUFFER) == 0) {
892: putchar(c);
893: h->col++;
894: return;
895: }
896:
897: if (h->col + h->bufcol < sizeof(h->buf)) {
898: h->buf[h->bufcol++] = c;
899: return;
900: }
901:
902: putchar('\n');
903: h->col = 0;
904: print_indent(h);
905: putchar(' ');
906: putchar(' ');
907: fwrite(h->buf, h->bufcol, 1, stdout);
908: putchar(c);
909: h->col = (h->indent + 1) * 2 + h->bufcol + 1;
910: h->bufcol = 0;
911: h->flags &= ~HTML_BUFFER;
912: }
913:
1.66 schwarze 914: /*
915: * If something was printed on the current output line, end it.
1.67 schwarze 916: * Not to be called right after print_indent().
1.66 schwarze 917: */
1.72 schwarze 918: void
1.67 schwarze 919: print_endline(struct html *h)
1.66 schwarze 920: {
1.67 schwarze 921: if (h->col == 0)
1.66 schwarze 922: return;
923:
1.67 schwarze 924: if (h->bufcol) {
925: putchar(' ');
926: fwrite(h->buf, h->bufcol, 1, stdout);
927: h->bufcol = 0;
928: }
1.66 schwarze 929: putchar('\n');
1.67 schwarze 930: h->col = 0;
931: h->flags |= HTML_NOSPACE;
932: h->flags &= ~HTML_BUFFER;
933: }
934:
935: /*
936: * Flush the HTML output buffer.
937: * If it is inactive, activate it.
938: */
939: static void
940: print_endword(struct html *h)
941: {
942: if (h->noindent) {
943: print_byte(h, ' ');
944: return;
945: }
946:
947: if ((h->flags & HTML_BUFFER) == 0) {
948: h->col++;
949: h->flags |= HTML_BUFFER;
950: } else if (h->bufcol) {
951: putchar(' ');
952: fwrite(h->buf, h->bufcol, 1, stdout);
953: h->col += h->bufcol + 1;
954: }
955: h->bufcol = 0;
1.66 schwarze 956: }
957:
958: /*
959: * If at the beginning of a new output line,
960: * perform indentation and mark the line as containing output.
961: * Make sure to really produce some output right afterwards,
962: * but do not use print_otag() for producing it.
963: */
964: static void
1.67 schwarze 965: print_indent(struct html *h)
1.66 schwarze 966: {
1.67 schwarze 967: size_t i;
1.66 schwarze 968:
1.132 ! schwarze 969: if (h->col || h->noindent)
1.66 schwarze 970: return;
971:
1.132 ! schwarze 972: h->col = h->indent * 2;
! 973: for (i = 0; i < h->col; i++)
! 974: putchar(' ');
1.67 schwarze 975: }
976:
977: /*
978: * Print or buffer some characters
979: * depending on the current HTML output buffer state.
980: */
981: static void
982: print_word(struct html *h, const char *cp)
983: {
984: while (*cp != '\0')
985: print_byte(h, *cp++);
1.3 schwarze 986: }