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