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