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