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