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