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