Annotation of src/usr.bin/mandoc/html.c, Revision 1.100
1.100 ! schwarze 1: /* $OpenBSD: html.c,v 1.99 2018/05/25 20:23:39 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>
19:
20: #include <assert.h>
1.3 schwarze 21: #include <ctype.h>
1.4 schwarze 22: #include <stdarg.h>
1.99 schwarze 23: #include <stddef.h>
1.1 schwarze 24: #include <stdio.h>
25: #include <stdint.h>
26: #include <stdlib.h>
27: #include <string.h>
28: #include <unistd.h>
29:
1.80 schwarze 30: #include "mandoc_aux.h"
1.99 schwarze 31: #include "mandoc_ohash.h"
1.9 schwarze 32: #include "mandoc.h"
1.80 schwarze 33: #include "roff.h"
1.1 schwarze 34: #include "out.h"
35: #include "html.h"
1.56 schwarze 36: #include "manconf.h"
1.1 schwarze 37: #include "main.h"
38:
39: struct htmldata {
40: const char *name;
41: int flags;
1.66 schwarze 42: #define HTML_NOSTACK (1 << 0)
43: #define HTML_AUTOCLOSE (1 << 1)
44: #define HTML_NLBEFORE (1 << 2)
45: #define HTML_NLBEGIN (1 << 3)
46: #define HTML_NLEND (1 << 4)
47: #define HTML_NLAFTER (1 << 5)
48: #define HTML_NLAROUND (HTML_NLBEFORE | HTML_NLAFTER)
49: #define HTML_NLINSIDE (HTML_NLBEGIN | HTML_NLEND)
50: #define HTML_NLALL (HTML_NLAROUND | HTML_NLINSIDE)
51: #define HTML_INDENT (1 << 6)
52: #define HTML_NOINDENT (1 << 7)
1.1 schwarze 53: };
54:
55: static const struct htmldata htmltags[TAG_MAX] = {
1.66 schwarze 56: {"html", HTML_NLALL},
57: {"head", HTML_NLALL | HTML_INDENT},
58: {"body", HTML_NLALL},
59: {"meta", HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
60: {"title", HTML_NLAROUND},
61: {"div", HTML_NLAROUND},
1.95 schwarze 62: {"div", 0},
1.66 schwarze 63: {"h1", HTML_NLAROUND},
64: {"h2", HTML_NLAROUND},
65: {"span", 0},
66: {"link", HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
67: {"br", HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
68: {"a", 0},
69: {"table", HTML_NLALL | HTML_INDENT},
1.75 schwarze 70: {"colgroup", HTML_NLALL | HTML_INDENT},
1.66 schwarze 71: {"col", HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
72: {"tr", HTML_NLALL | HTML_INDENT},
73: {"td", HTML_NLAROUND},
74: {"li", HTML_NLAROUND | HTML_INDENT},
75: {"ul", HTML_NLALL | HTML_INDENT},
76: {"ol", HTML_NLALL | HTML_INDENT},
77: {"dl", HTML_NLALL | HTML_INDENT},
78: {"dt", HTML_NLAROUND},
79: {"dd", HTML_NLAROUND | HTML_INDENT},
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.26 schwarze 107: static const char *const roffscales[SCALE_MAX] = {
108: "cm", /* SCALE_CM */
109: "in", /* SCALE_IN */
110: "pc", /* SCALE_PC */
111: "pt", /* SCALE_PT */
112: "em", /* SCALE_EM */
113: "em", /* SCALE_MM */
114: "ex", /* SCALE_EN */
115: "ex", /* SCALE_BU */
116: "em", /* SCALE_VS */
117: "ex", /* SCALE_FS */
118: };
1.5 schwarze 119:
1.99 schwarze 120: /* Avoid duplicate HTML id= attributes. */
121: static struct ohash id_unique;
122:
1.64 schwarze 123: static void a2width(const char *, struct roffsu *);
1.67 schwarze 124: static void print_byte(struct html *, char);
125: static void print_endword(struct html *);
126: static void print_indent(struct html *);
127: static void print_word(struct html *, const char *);
128:
1.54 schwarze 129: static void print_ctag(struct html *, struct tag *);
1.67 schwarze 130: static int print_escape(struct html *, char);
1.65 schwarze 131: static int print_encode(struct html *, const char *, const char *, int);
132: static void print_href(struct html *, const char *, const char *, int);
1.26 schwarze 133: static void print_metaf(struct html *, enum mandoc_esc);
1.5 schwarze 134:
1.35 schwarze 135:
1.50 schwarze 136: void *
1.61 schwarze 137: html_alloc(const struct manoutput *outopts)
1.1 schwarze 138: {
139: struct html *h;
140:
1.24 schwarze 141: h = mandoc_calloc(1, sizeof(struct html));
1.1 schwarze 142:
1.74 schwarze 143: h->tag = NULL;
1.56 schwarze 144: h->style = outopts->style;
145: h->base_man = outopts->man;
146: h->base_includes = outopts->includes;
147: if (outopts->fragment)
148: h->oflags |= HTML_FRAGMENT;
1.1 schwarze 149:
1.99 schwarze 150: mandoc_ohash_init(&id_unique, 4, 0);
151:
1.58 schwarze 152: return h;
1.1 schwarze 153: }
154:
155: void
156: html_free(void *p)
157: {
158: struct tag *tag;
159: struct html *h;
1.99 schwarze 160: char *cp;
161: unsigned int slot;
1.1 schwarze 162:
163: h = (struct html *)p;
1.74 schwarze 164: while ((tag = h->tag) != NULL) {
165: h->tag = tag->next;
1.1 schwarze 166: free(tag);
167: }
1.99 schwarze 168: free(h);
1.1 schwarze 169:
1.99 schwarze 170: cp = ohash_first(&id_unique, &slot);
171: while (cp != NULL) {
172: free(cp);
173: cp = ohash_next(&id_unique, &slot);
174: }
175: ohash_delete(&id_unique);
1.1 schwarze 176: }
177:
178: void
179: print_gen_head(struct html *h)
180: {
1.42 schwarze 181: struct tag *t;
182:
1.64 schwarze 183: print_otag(h, TAG_META, "?", "charset", "utf-8");
1.92 schwarze 184: if (h->style != NULL) {
185: print_otag(h, TAG_LINK, "?h??", "rel", "stylesheet",
186: h->style, "type", "text/css", "media", "all");
187: return;
188: }
1.1 schwarze 189:
1.42 schwarze 190: /*
1.92 schwarze 191: * Print a minimal embedded style sheet.
1.42 schwarze 192: */
1.66 schwarze 193:
1.64 schwarze 194: t = print_otag(h, TAG_STYLE, "");
1.66 schwarze 195: print_text(h, "table.head, table.foot { width: 100%; }");
1.67 schwarze 196: print_endline(h);
1.66 schwarze 197: print_text(h, "td.head-rtitle, td.foot-os { text-align: right; }");
1.67 schwarze 198: print_endline(h);
1.66 schwarze 199: print_text(h, "td.head-vol { text-align: center; }");
1.67 schwarze 200: print_endline(h);
1.68 schwarze 201: print_text(h, "div.Pp { margin: 1ex 0ex; }");
1.95 schwarze 202: print_endline(h);
203: print_text(h, "div.Nd, div.Bf, div.Op { display: inline; }");
1.96 schwarze 204: print_endline(h);
1.97 schwarze 205: print_text(h, "span.Pa, span.Ad { font-style: italic; }");
1.98 schwarze 206: print_endline(h);
207: print_text(h, "span.Ms { font-weight: bold; }");
1.94 schwarze 208: print_endline(h);
209: print_text(h, "dl.Bl-diag ");
210: print_byte(h, '>');
211: print_text(h, " dt { font-weight: bold; }");
1.93 schwarze 212: print_endline(h);
213: print_text(h, "code.Nm, code.Fl, code.Cm, code.Ic, "
214: "code.In, code.Fd, code.Fn,");
215: print_endline(h);
216: print_text(h, "code.Cd { font-weight: bold; "
217: "font-family: inherit; }");
1.42 schwarze 218: print_tagq(h, t);
1.1 schwarze 219: }
220:
1.5 schwarze 221: static void
1.26 schwarze 222: print_metaf(struct html *h, enum mandoc_esc deco)
1.5 schwarze 223: {
224: enum htmlfont font;
1.1 schwarze 225:
1.5 schwarze 226: switch (deco) {
1.35 schwarze 227: case ESCAPE_FONTPREV:
1.5 schwarze 228: font = h->metal;
229: break;
1.35 schwarze 230: case ESCAPE_FONTITALIC:
1.5 schwarze 231: font = HTMLFONT_ITALIC;
232: break;
1.35 schwarze 233: case ESCAPE_FONTBOLD:
1.5 schwarze 234: font = HTMLFONT_BOLD;
235: break;
1.35 schwarze 236: case ESCAPE_FONTBI:
1.31 schwarze 237: font = HTMLFONT_BI;
238: break;
1.35 schwarze 239: case ESCAPE_FONT:
240: case ESCAPE_FONTROMAN:
1.5 schwarze 241: font = HTMLFONT_NONE;
242: break;
243: default:
244: abort();
1.1 schwarze 245: }
246:
1.20 schwarze 247: if (h->metaf) {
248: print_tagq(h, h->metaf);
249: h->metaf = NULL;
250: }
251:
252: h->metal = h->metac;
253: h->metac = font;
254:
1.31 schwarze 255: switch (font) {
1.35 schwarze 256: case HTMLFONT_ITALIC:
1.64 schwarze 257: h->metaf = print_otag(h, TAG_I, "");
1.31 schwarze 258: break;
1.35 schwarze 259: case HTMLFONT_BOLD:
1.64 schwarze 260: h->metaf = print_otag(h, TAG_B, "");
1.31 schwarze 261: break;
1.35 schwarze 262: case HTMLFONT_BI:
1.64 schwarze 263: h->metaf = print_otag(h, TAG_B, "");
264: print_otag(h, TAG_I, "");
1.31 schwarze 265: break;
266: default:
267: break;
268: }
1.80 schwarze 269: }
270:
271: char *
1.99 schwarze 272: html_make_id(const struct roff_node *n, int unique)
1.80 schwarze 273: {
274: const struct roff_node *nch;
1.99 schwarze 275: char *buf, *bufs, *cp;
276: unsigned int slot;
277: int suffix;
1.80 schwarze 278:
279: for (nch = n->child; nch != NULL; nch = nch->next)
280: if (nch->type != ROFFT_TEXT)
281: return NULL;
282:
283: buf = NULL;
284: deroff(&buf, n);
1.90 schwarze 285: if (buf == NULL)
286: return NULL;
1.80 schwarze 287:
1.100 ! schwarze 288: /*
! 289: * In ID attributes, only use ASCII characters that are
! 290: * permitted in URL-fragment strings according to the
! 291: * explicit list at:
! 292: * https://url.spec.whatwg.org/#url-fragment-string
! 293: */
1.80 schwarze 294:
295: for (cp = buf; *cp != '\0'; cp++)
1.100 ! schwarze 296: if (isalnum((unsigned char)*cp) == 0 &&
! 297: strchr("!$&'()*+,-./:;=?@_~", *cp) == NULL)
1.80 schwarze 298: *cp = '_';
299:
1.99 schwarze 300: if (unique == 0)
301: return buf;
302:
303: /* Avoid duplicate HTML id= attributes. */
304:
305: bufs = NULL;
306: suffix = 1;
307: slot = ohash_qlookup(&id_unique, buf);
308: cp = ohash_find(&id_unique, slot);
309: if (cp != NULL) {
310: while (cp != NULL) {
311: free(bufs);
312: if (++suffix > 127) {
313: free(buf);
314: return NULL;
315: }
316: mandoc_asprintf(&bufs, "%s_%d", buf, suffix);
317: slot = ohash_qlookup(&id_unique, bufs);
318: cp = ohash_find(&id_unique, slot);
319: }
320: free(buf);
321: buf = bufs;
322: }
323: ohash_insert(&id_unique, slot, buf);
1.80 schwarze 324: return buf;
1.1 schwarze 325: }
326:
1.26 schwarze 327: int
328: html_strlen(const char *cp)
329: {
1.30 schwarze 330: size_t rsz;
331: int skip, sz;
1.26 schwarze 332:
333: /*
334: * Account for escaped sequences within string length
335: * calculations. This follows the logic in term_strlen() as we
336: * must calculate the width of produced strings.
337: * Assume that characters are always width of "1". This is
338: * hacky, but it gets the job done for approximation of widths.
339: */
340:
341: sz = 0;
1.30 schwarze 342: skip = 0;
343: while (1) {
344: rsz = strcspn(cp, "\\");
345: if (rsz) {
346: cp += rsz;
347: if (skip) {
348: skip = 0;
349: rsz--;
350: }
351: sz += rsz;
352: }
353: if ('\0' == *cp)
354: break;
355: cp++;
356: switch (mandoc_escape(&cp, NULL, NULL)) {
1.35 schwarze 357: case ESCAPE_ERROR:
1.58 schwarze 358: return sz;
1.35 schwarze 359: case ESCAPE_UNICODE:
360: case ESCAPE_NUMBERED:
361: case ESCAPE_SPECIAL:
1.55 schwarze 362: case ESCAPE_OVERSTRIKE:
1.30 schwarze 363: if (skip)
364: skip = 0;
365: else
366: sz++;
367: break;
1.35 schwarze 368: case ESCAPE_SKIPCHAR:
1.30 schwarze 369: skip = 1;
1.26 schwarze 370: break;
371: default:
372: break;
373: }
374: }
1.58 schwarze 375: return sz;
1.26 schwarze 376: }
1.1 schwarze 377:
1.5 schwarze 378: static int
1.67 schwarze 379: print_escape(struct html *h, char c)
1.38 schwarze 380: {
381:
382: switch (c) {
383: case '<':
1.67 schwarze 384: print_word(h, "<");
1.38 schwarze 385: break;
386: case '>':
1.67 schwarze 387: print_word(h, ">");
1.38 schwarze 388: break;
389: case '&':
1.67 schwarze 390: print_word(h, "&");
1.38 schwarze 391: break;
392: case '"':
1.67 schwarze 393: print_word(h, """);
1.38 schwarze 394: break;
395: case ASCII_NBRSP:
1.67 schwarze 396: print_word(h, " ");
1.38 schwarze 397: break;
398: case ASCII_HYPH:
1.67 schwarze 399: print_byte(h, '-');
1.59 schwarze 400: break;
1.38 schwarze 401: case ASCII_BREAK:
402: break;
403: default:
1.58 schwarze 404: return 0;
1.38 schwarze 405: }
1.58 schwarze 406: return 1;
1.38 schwarze 407: }
408:
409: static int
1.65 schwarze 410: print_encode(struct html *h, const char *p, const char *pend, int norecurse)
1.1 schwarze 411: {
1.67 schwarze 412: char numbuf[16];
1.84 schwarze 413: struct tag *t;
414: const char *seq;
1.4 schwarze 415: size_t sz;
1.84 schwarze 416: int c, len, breakline, nospace;
1.26 schwarze 417: enum mandoc_esc esc;
1.84 schwarze 418: static const char rejs[10] = { ' ', '\\', '<', '>', '&', '"',
1.33 schwarze 419: ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' };
1.5 schwarze 420:
1.65 schwarze 421: if (pend == NULL)
422: pend = strchr(p, '\0');
423:
1.84 schwarze 424: breakline = 0;
1.5 schwarze 425: nospace = 0;
1.1 schwarze 426:
1.65 schwarze 427: while (p < pend) {
1.30 schwarze 428: if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
429: h->flags &= ~HTML_SKIPCHAR;
430: p++;
431: continue;
432: }
433:
1.67 schwarze 434: for (sz = strcspn(p, rejs); sz-- && p < pend; p++)
1.84 schwarze 435: print_byte(h, *p);
436:
437: if (breakline &&
438: (p >= pend || *p == ' ' || *p == ASCII_NBRSP)) {
439: t = print_otag(h, TAG_DIV, "");
440: print_text(h, "\\~");
441: print_tagq(h, t);
442: breakline = 0;
443: while (p < pend && (*p == ' ' || *p == ASCII_NBRSP))
444: p++;
445: continue;
446: }
1.4 schwarze 447:
1.65 schwarze 448: if (p >= pend)
1.26 schwarze 449: break;
450:
1.84 schwarze 451: if (*p == ' ') {
452: print_endword(h);
453: p++;
454: continue;
455: }
456:
1.67 schwarze 457: if (print_escape(h, *p++))
1.33 schwarze 458: continue;
1.4 schwarze 459:
1.26 schwarze 460: esc = mandoc_escape(&p, &seq, &len);
461: if (ESCAPE_ERROR == esc)
462: break;
1.5 schwarze 463:
1.26 schwarze 464: switch (esc) {
1.35 schwarze 465: case ESCAPE_FONT:
466: case ESCAPE_FONTPREV:
467: case ESCAPE_FONTBOLD:
468: case ESCAPE_FONTITALIC:
469: case ESCAPE_FONTBI:
470: case ESCAPE_FONTROMAN:
1.30 schwarze 471: if (0 == norecurse)
472: print_metaf(h, esc);
473: continue;
1.35 schwarze 474: case ESCAPE_SKIPCHAR:
1.30 schwarze 475: h->flags |= HTML_SKIPCHAR;
476: continue;
477: default:
478: break;
479: }
480:
481: if (h->flags & HTML_SKIPCHAR) {
482: h->flags &= ~HTML_SKIPCHAR;
483: continue;
484: }
485:
486: switch (esc) {
1.35 schwarze 487: case ESCAPE_UNICODE:
1.38 schwarze 488: /* Skip past "u" header. */
1.26 schwarze 489: c = mchars_num2uc(seq + 1, len - 1);
490: break;
1.35 schwarze 491: case ESCAPE_NUMBERED:
1.26 schwarze 492: c = mchars_num2char(seq, len);
1.51 schwarze 493: if (c < 0)
494: continue;
1.26 schwarze 495: break;
1.35 schwarze 496: case ESCAPE_SPECIAL:
1.61 schwarze 497: c = mchars_spec2cp(seq, len);
1.51 schwarze 498: if (c <= 0)
499: continue;
1.26 schwarze 500: break;
1.84 schwarze 501: case ESCAPE_BREAK:
502: breakline = 1;
503: continue;
1.35 schwarze 504: case ESCAPE_NOSPACE:
1.26 schwarze 505: if ('\0' == *p)
506: nospace = 1;
1.49 schwarze 507: continue;
1.55 schwarze 508: case ESCAPE_OVERSTRIKE:
509: if (len == 0)
510: continue;
511: c = seq[len - 1];
512: break;
1.5 schwarze 513: default:
1.49 schwarze 514: continue;
1.5 schwarze 515: }
1.51 schwarze 516: if ((c < 0x20 && c != 0x09) ||
517: (c > 0x7E && c < 0xA0))
1.49 schwarze 518: c = 0xFFFD;
1.67 schwarze 519: if (c > 0x7E) {
1.86 bentley 520: (void)snprintf(numbuf, sizeof(numbuf), "&#x%.4X;", c);
1.67 schwarze 521: print_word(h, numbuf);
522: } else if (print_escape(h, c) == 0)
523: print_byte(h, c);
1.1 schwarze 524: }
1.5 schwarze 525:
1.58 schwarze 526: return nospace;
1.1 schwarze 527: }
528:
1.6 schwarze 529: static void
1.65 schwarze 530: print_href(struct html *h, const char *name, const char *sec, int man)
1.6 schwarze 531: {
1.65 schwarze 532: const char *p, *pp;
533:
534: pp = man ? h->base_man : h->base_includes;
535: while ((p = strchr(pp, '%')) != NULL) {
536: print_encode(h, pp, p, 1);
537: if (man && p[1] == 'S') {
538: if (sec == NULL)
1.67 schwarze 539: print_byte(h, '1');
1.65 schwarze 540: else
541: print_encode(h, sec, NULL, 1);
542: } else if ((man && p[1] == 'N') ||
543: (man == 0 && p[1] == 'I'))
544: print_encode(h, name, NULL, 1);
545: else
546: print_encode(h, p, p + 2, 1);
547: pp = p + 2;
548: }
549: if (*pp != '\0')
550: print_encode(h, pp, NULL, 1);
1.6 schwarze 551: }
552:
1.1 schwarze 553: struct tag *
1.64 schwarze 554: print_otag(struct html *h, enum htmltag tag, const char *fmt, ...)
1.1 schwarze 555: {
1.64 schwarze 556: va_list ap;
557: struct roffsu mysu, *su;
1.67 schwarze 558: char numbuf[16];
1.1 schwarze 559: struct tag *t;
1.65 schwarze 560: const char *attr;
1.73 schwarze 561: char *arg1, *arg2;
1.65 schwarze 562: double v;
1.66 schwarze 563: int i, have_style, tflags;
564:
565: tflags = htmltags[tag].flags;
1.1 schwarze 566:
1.74 schwarze 567: /* Push this tag onto the stack of open scopes. */
1.6 schwarze 568:
1.66 schwarze 569: if ((tflags & HTML_NOSTACK) == 0) {
1.24 schwarze 570: t = mandoc_malloc(sizeof(struct tag));
1.1 schwarze 571: t->tag = tag;
1.74 schwarze 572: t->next = h->tag;
573: h->tag = t;
1.1 schwarze 574: } else
575: t = NULL;
576:
1.66 schwarze 577: if (tflags & HTML_NLBEFORE)
1.67 schwarze 578: print_endline(h);
579: if (h->col == 0)
580: print_indent(h);
1.66 schwarze 581: else if ((h->flags & HTML_NOSPACE) == 0) {
582: if (h->flags & HTML_KEEP)
1.86 bentley 583: print_word(h, " ");
1.66 schwarze 584: else {
585: if (h->flags & HTML_PREKEEP)
586: h->flags |= HTML_KEEP;
1.67 schwarze 587: print_endword(h);
1.12 schwarze 588: }
1.66 schwarze 589: }
1.1 schwarze 590:
1.13 schwarze 591: if ( ! (h->flags & HTML_NONOSPACE))
592: h->flags &= ~HTML_NOSPACE;
1.14 schwarze 593: else
594: h->flags |= HTML_NOSPACE;
1.13 schwarze 595:
1.6 schwarze 596: /* Print out the tag name and attributes. */
597:
1.67 schwarze 598: print_byte(h, '<');
599: print_word(h, htmltags[tag].name);
1.64 schwarze 600:
601: va_start(ap, fmt);
602:
603: have_style = 0;
604: while (*fmt != '\0') {
605: if (*fmt == 's') {
606: have_style = 1;
607: fmt++;
608: break;
609: }
1.73 schwarze 610:
611: /* Parse a non-style attribute and its arguments. */
612:
613: arg1 = va_arg(ap, char *);
1.64 schwarze 614: switch (*fmt++) {
615: case 'c':
1.65 schwarze 616: attr = "class";
1.64 schwarze 617: break;
618: case 'h':
1.65 schwarze 619: attr = "href";
1.64 schwarze 620: break;
621: case 'i':
1.65 schwarze 622: attr = "id";
1.64 schwarze 623: break;
624: case '?':
1.73 schwarze 625: attr = arg1;
626: arg1 = va_arg(ap, char *);
1.64 schwarze 627: break;
628: default:
629: abort();
630: }
1.73 schwarze 631: arg2 = NULL;
632: if (*fmt == 'M')
633: arg2 = va_arg(ap, char *);
634: if (arg1 == NULL)
635: continue;
636:
637: /* Print the non-style attributes. */
638:
1.67 schwarze 639: print_byte(h, ' ');
640: print_word(h, attr);
641: print_byte(h, '=');
642: print_byte(h, '"');
1.65 schwarze 643: switch (*fmt) {
1.78 schwarze 644: case 'I':
645: print_href(h, arg1, NULL, 0);
646: fmt++;
647: break;
1.65 schwarze 648: case 'M':
1.73 schwarze 649: print_href(h, arg1, arg2, 1);
1.65 schwarze 650: fmt++;
651: break;
1.78 schwarze 652: case 'R':
653: print_byte(h, '#');
654: print_encode(h, arg1, NULL, 1);
1.65 schwarze 655: fmt++;
656: break;
1.78 schwarze 657: case 'T':
658: print_encode(h, arg1, NULL, 1);
659: print_word(h, "\" title=\"");
660: print_encode(h, arg1, NULL, 1);
1.65 schwarze 661: fmt++;
1.78 schwarze 662: break;
1.65 schwarze 663: default:
1.73 schwarze 664: print_encode(h, arg1, NULL, 1);
1.65 schwarze 665: break;
666: }
1.67 schwarze 667: print_byte(h, '"');
1.64 schwarze 668: }
669:
670: /* Print out styles. */
671:
672: while (*fmt != '\0') {
1.73 schwarze 673: arg1 = NULL;
674: su = NULL;
1.64 schwarze 675:
676: /* First letter: input argument type. */
677:
678: switch (*fmt++) {
679: case 'h':
680: i = va_arg(ap, int);
1.73 schwarze 681: su = &mysu;
1.64 schwarze 682: SCALE_HS_INIT(su, i);
683: break;
684: case 's':
1.73 schwarze 685: arg1 = va_arg(ap, char *);
1.64 schwarze 686: break;
687: case 'u':
688: su = va_arg(ap, struct roffsu *);
689: break;
690: case 'w':
1.89 schwarze 691: if ((arg2 = va_arg(ap, char *)) != NULL) {
692: su = &mysu;
693: a2width(arg2, su);
694: }
695: if (*fmt == '*') {
696: if (su != NULL && su->unit == SCALE_EN &&
697: su->scale > 5.9 && su->scale < 6.1)
698: su = NULL;
699: fmt++;
1.88 schwarze 700: }
1.81 schwarze 701: if (*fmt == '+') {
1.89 schwarze 702: if (su != NULL) {
703: /* Make even bold text fit. */
704: su->scale *= 1.2;
705: /* Add padding. */
706: su->scale += 3.0;
707: }
1.81 schwarze 708: fmt++;
709: }
710: if (*fmt == '-') {
1.89 schwarze 711: if (su != NULL)
712: su->scale *= -1.0;
1.81 schwarze 713: fmt++;
714: }
1.64 schwarze 715: break;
716: default:
717: abort();
718: }
719:
720: /* Second letter: style name. */
721:
722: switch (*fmt++) {
723: case 'h':
1.65 schwarze 724: attr = "height";
1.64 schwarze 725: break;
726: case 'i':
1.65 schwarze 727: attr = "text-indent";
1.64 schwarze 728: break;
729: case 'l':
1.65 schwarze 730: attr = "margin-left";
1.64 schwarze 731: break;
732: case 'w':
1.65 schwarze 733: attr = "width";
1.64 schwarze 734: break;
735: case 'W':
1.65 schwarze 736: attr = "min-width";
1.64 schwarze 737: break;
738: case '?':
1.73 schwarze 739: attr = arg1;
740: arg1 = va_arg(ap, char *);
741: break;
1.64 schwarze 742: default:
743: abort();
744: }
1.73 schwarze 745: if (su == NULL && arg1 == NULL)
746: continue;
747:
748: if (have_style == 1)
749: print_word(h, " style=\"");
750: else
751: print_byte(h, ' ');
1.67 schwarze 752: print_word(h, attr);
753: print_byte(h, ':');
754: print_byte(h, ' ');
1.73 schwarze 755: if (su != NULL) {
756: v = su->scale;
757: if (su->unit == SCALE_MM && (v /= 100.0) == 0.0)
758: v = 1.0;
759: else if (su->unit == SCALE_BU)
760: v /= 24.0;
761: (void)snprintf(numbuf, sizeof(numbuf), "%.2f", v);
762: print_word(h, numbuf);
763: print_word(h, roffscales[su->unit]);
764: } else
765: print_word(h, arg1);
1.67 schwarze 766: print_byte(h, ';');
1.73 schwarze 767: have_style = 2;
1.64 schwarze 768: }
1.73 schwarze 769: if (have_style == 2)
1.67 schwarze 770: print_byte(h, '"');
1.64 schwarze 771:
772: va_end(ap);
1.6 schwarze 773:
1.42 schwarze 774: /* Accommodate for "well-formed" singleton escaping. */
1.6 schwarze 775:
776: if (HTML_AUTOCLOSE & htmltags[tag].flags)
1.67 schwarze 777: print_byte(h, '/');
1.6 schwarze 778:
1.67 schwarze 779: print_byte(h, '>');
1.1 schwarze 780:
1.66 schwarze 781: if (tflags & HTML_NLBEGIN)
1.67 schwarze 782: print_endline(h);
1.66 schwarze 783: else
784: h->flags |= HTML_NOSPACE;
1.18 schwarze 785:
1.66 schwarze 786: if (tflags & HTML_INDENT)
787: h->indent++;
788: if (tflags & HTML_NOINDENT)
789: h->noindent++;
1.18 schwarze 790:
1.58 schwarze 791: return t;
1.1 schwarze 792: }
793:
794: static void
1.54 schwarze 795: print_ctag(struct html *h, struct tag *tag)
1.1 schwarze 796: {
1.66 schwarze 797: int tflags;
1.35 schwarze 798:
1.54 schwarze 799: /*
800: * Remember to close out and nullify the current
801: * meta-font and table, if applicable.
802: */
803: if (tag == h->metaf)
804: h->metaf = NULL;
805: if (tag == h->tblt)
806: h->tblt = NULL;
807:
1.66 schwarze 808: tflags = htmltags[tag->tag].flags;
809:
810: if (tflags & HTML_INDENT)
811: h->indent--;
812: if (tflags & HTML_NOINDENT)
813: h->noindent--;
814: if (tflags & HTML_NLEND)
1.67 schwarze 815: print_endline(h);
816: print_indent(h);
817: print_byte(h, '<');
818: print_byte(h, '/');
819: print_word(h, htmltags[tag->tag].name);
820: print_byte(h, '>');
1.66 schwarze 821: if (tflags & HTML_NLAFTER)
1.67 schwarze 822: print_endline(h);
1.54 schwarze 823:
1.74 schwarze 824: h->tag = tag->next;
1.54 schwarze 825: free(tag);
1.1 schwarze 826: }
827:
828: void
1.6 schwarze 829: print_gen_decls(struct html *h)
830: {
1.67 schwarze 831: print_word(h, "<!DOCTYPE html>");
832: print_endline(h);
1.91 schwarze 833: }
834:
835: void
836: print_gen_comment(struct html *h, struct roff_node *n)
837: {
838: int wantblank;
839:
840: print_word(h, "<!-- This is an automatically generated file."
841: " Do not edit.");
842: h->indent = 1;
843: wantblank = 0;
844: while (n != NULL && n->type == ROFFT_COMMENT) {
845: if (strstr(n->string, "-->") == NULL &&
846: (wantblank || *n->string != '\0')) {
847: print_endline(h);
848: print_indent(h);
849: print_word(h, n->string);
850: wantblank = *n->string != '\0';
851: }
852: n = n->next;
853: }
854: if (wantblank)
855: print_endline(h);
856: print_word(h, " -->");
857: print_endline(h);
858: h->indent = 0;
1.1 schwarze 859: }
860:
861: void
1.12 schwarze 862: print_text(struct html *h, const char *word)
1.1 schwarze 863: {
1.67 schwarze 864: if (h->col && (h->flags & HTML_NOSPACE) == 0) {
1.12 schwarze 865: if ( ! (HTML_KEEP & h->flags)) {
866: if (HTML_PREKEEP & h->flags)
867: h->flags |= HTML_KEEP;
1.67 schwarze 868: print_endword(h);
1.12 schwarze 869: } else
1.86 bentley 870: print_word(h, " ");
1.12 schwarze 871: }
1.1 schwarze 872:
1.20 schwarze 873: assert(NULL == h->metaf);
1.31 schwarze 874: switch (h->metac) {
1.35 schwarze 875: case HTMLFONT_ITALIC:
1.64 schwarze 876: h->metaf = print_otag(h, TAG_I, "");
1.31 schwarze 877: break;
1.35 schwarze 878: case HTMLFONT_BOLD:
1.64 schwarze 879: h->metaf = print_otag(h, TAG_B, "");
1.31 schwarze 880: break;
1.35 schwarze 881: case HTMLFONT_BI:
1.64 schwarze 882: h->metaf = print_otag(h, TAG_B, "");
883: print_otag(h, TAG_I, "");
1.31 schwarze 884: break;
885: default:
1.67 schwarze 886: print_indent(h);
1.31 schwarze 887: break;
888: }
1.20 schwarze 889:
1.12 schwarze 890: assert(word);
1.65 schwarze 891: if ( ! print_encode(h, word, NULL, 0)) {
1.13 schwarze 892: if ( ! (h->flags & HTML_NONOSPACE))
893: h->flags &= ~HTML_NOSPACE;
1.53 schwarze 894: h->flags &= ~HTML_NONEWLINE;
1.28 schwarze 895: } else
1.53 schwarze 896: h->flags |= HTML_NOSPACE | HTML_NONEWLINE;
1.20 schwarze 897:
898: if (h->metaf) {
899: print_tagq(h, h->metaf);
900: h->metaf = NULL;
901: }
1.17 schwarze 902:
903: h->flags &= ~HTML_IGNDELIM;
1.1 schwarze 904: }
905:
906: void
907: print_tagq(struct html *h, const struct tag *until)
908: {
909: struct tag *tag;
910:
1.74 schwarze 911: while ((tag = h->tag) != NULL) {
1.54 schwarze 912: print_ctag(h, tag);
1.1 schwarze 913: if (until && tag == until)
914: return;
915: }
916: }
917:
918: void
919: print_stagq(struct html *h, const struct tag *suntil)
920: {
921: struct tag *tag;
922:
1.74 schwarze 923: while ((tag = h->tag) != NULL) {
1.1 schwarze 924: if (suntil && tag == suntil)
925: return;
1.54 schwarze 926: print_ctag(h, tag);
1.1 schwarze 927: }
928: }
1.42 schwarze 929:
930: void
931: print_paragraph(struct html *h)
932: {
933: struct tag *t;
934:
1.68 schwarze 935: t = print_otag(h, TAG_DIV, "c", "Pp");
1.42 schwarze 936: print_tagq(h, t);
937: }
938:
1.67 schwarze 939:
940: /***********************************************************************
941: * Low level output functions.
942: * They implement line breaking using a short static buffer.
943: ***********************************************************************/
944:
945: /*
946: * Buffer one HTML output byte.
947: * If the buffer is full, flush and deactivate it and start a new line.
948: * If the buffer is inactive, print directly.
949: */
950: static void
951: print_byte(struct html *h, char c)
952: {
953: if ((h->flags & HTML_BUFFER) == 0) {
954: putchar(c);
955: h->col++;
956: return;
957: }
958:
959: if (h->col + h->bufcol < sizeof(h->buf)) {
960: h->buf[h->bufcol++] = c;
961: return;
962: }
963:
964: putchar('\n');
965: h->col = 0;
966: print_indent(h);
967: putchar(' ');
968: putchar(' ');
969: fwrite(h->buf, h->bufcol, 1, stdout);
970: putchar(c);
971: h->col = (h->indent + 1) * 2 + h->bufcol + 1;
972: h->bufcol = 0;
973: h->flags &= ~HTML_BUFFER;
974: }
975:
1.66 schwarze 976: /*
977: * If something was printed on the current output line, end it.
1.67 schwarze 978: * Not to be called right after print_indent().
1.66 schwarze 979: */
1.72 schwarze 980: void
1.67 schwarze 981: print_endline(struct html *h)
1.66 schwarze 982: {
1.67 schwarze 983: if (h->col == 0)
1.66 schwarze 984: return;
985:
1.67 schwarze 986: if (h->bufcol) {
987: putchar(' ');
988: fwrite(h->buf, h->bufcol, 1, stdout);
989: h->bufcol = 0;
990: }
1.66 schwarze 991: putchar('\n');
1.67 schwarze 992: h->col = 0;
993: h->flags |= HTML_NOSPACE;
994: h->flags &= ~HTML_BUFFER;
995: }
996:
997: /*
998: * Flush the HTML output buffer.
999: * If it is inactive, activate it.
1000: */
1001: static void
1002: print_endword(struct html *h)
1003: {
1004: if (h->noindent) {
1005: print_byte(h, ' ');
1006: return;
1007: }
1008:
1009: if ((h->flags & HTML_BUFFER) == 0) {
1010: h->col++;
1011: h->flags |= HTML_BUFFER;
1012: } else if (h->bufcol) {
1013: putchar(' ');
1014: fwrite(h->buf, h->bufcol, 1, stdout);
1015: h->col += h->bufcol + 1;
1016: }
1017: h->bufcol = 0;
1.66 schwarze 1018: }
1019:
1020: /*
1021: * If at the beginning of a new output line,
1022: * perform indentation and mark the line as containing output.
1023: * Make sure to really produce some output right afterwards,
1024: * but do not use print_otag() for producing it.
1025: */
1026: static void
1.67 schwarze 1027: print_indent(struct html *h)
1.66 schwarze 1028: {
1.67 schwarze 1029: size_t i;
1.66 schwarze 1030:
1.67 schwarze 1031: if (h->col)
1.66 schwarze 1032: return;
1033:
1.67 schwarze 1034: if (h->noindent == 0) {
1035: h->col = h->indent * 2;
1036: for (i = 0; i < h->col; i++)
1.66 schwarze 1037: putchar(' ');
1.67 schwarze 1038: }
1039: h->flags &= ~HTML_NOSPACE;
1040: }
1041:
1042: /*
1043: * Print or buffer some characters
1044: * depending on the current HTML output buffer state.
1045: */
1046: static void
1047: print_word(struct html *h, const char *cp)
1048: {
1049: while (*cp != '\0')
1050: print_byte(h, *cp++);
1.66 schwarze 1051: }
1.64 schwarze 1052:
1053: /*
1054: * Calculate the scaling unit passed in a `-width' argument. This uses
1055: * either a native scaling unit (e.g., 1i, 2m) or the string length of
1056: * the value.
1057: */
1058: static void
1059: a2width(const char *p, struct roffsu *su)
1060: {
1.83 schwarze 1061: const char *end;
1062:
1063: end = a2roffsu(p, su, SCALE_MAX);
1064: if (end == NULL || *end != '\0') {
1.64 schwarze 1065: su->unit = SCALE_EN;
1066: su->scale = html_strlen(p);
1067: } else if (su->scale < 0.0)
1068: su->scale = 0.0;
1.3 schwarze 1069: }