Annotation of src/usr.bin/mandoc/html.c, Revision 1.103
1.103 ! schwarze 1: /* $OpenBSD$
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: }
1.81 schwarze 695: if (*fmt == '+') {
1.89 schwarze 696: if (su != NULL) {
697: /* Make even bold text fit. */
698: su->scale *= 1.2;
699: /* Add padding. */
700: su->scale += 3.0;
701: }
1.81 schwarze 702: fmt++;
703: }
1.64 schwarze 704: break;
705: default:
706: abort();
707: }
708:
709: /* Second letter: style name. */
710:
711: switch (*fmt++) {
1.103 ! schwarze 712: case 'h':
! 713: attr = "height";
! 714: break;
1.64 schwarze 715: case 'i':
1.65 schwarze 716: attr = "text-indent";
1.64 schwarze 717: break;
718: case 'l':
1.65 schwarze 719: attr = "margin-left";
1.64 schwarze 720: break;
721: case 'w':
1.65 schwarze 722: attr = "width";
1.64 schwarze 723: break;
724: case 'W':
1.65 schwarze 725: attr = "min-width";
1.64 schwarze 726: break;
727: case '?':
1.73 schwarze 728: attr = arg1;
729: arg1 = va_arg(ap, char *);
730: break;
1.64 schwarze 731: default:
732: abort();
733: }
1.73 schwarze 734: if (su == NULL && arg1 == NULL)
735: continue;
736:
737: if (have_style == 1)
738: print_word(h, " style=\"");
739: else
740: print_byte(h, ' ');
1.67 schwarze 741: print_word(h, attr);
742: print_byte(h, ':');
743: print_byte(h, ' ');
1.73 schwarze 744: if (su != NULL) {
745: v = su->scale;
746: if (su->unit == SCALE_MM && (v /= 100.0) == 0.0)
747: v = 1.0;
748: else if (su->unit == SCALE_BU)
749: v /= 24.0;
750: (void)snprintf(numbuf, sizeof(numbuf), "%.2f", v);
751: print_word(h, numbuf);
752: print_word(h, roffscales[su->unit]);
753: } else
754: print_word(h, arg1);
1.67 schwarze 755: print_byte(h, ';');
1.73 schwarze 756: have_style = 2;
1.64 schwarze 757: }
1.73 schwarze 758: if (have_style == 2)
1.67 schwarze 759: print_byte(h, '"');
1.64 schwarze 760:
761: va_end(ap);
1.6 schwarze 762:
1.42 schwarze 763: /* Accommodate for "well-formed" singleton escaping. */
1.6 schwarze 764:
765: if (HTML_AUTOCLOSE & htmltags[tag].flags)
1.67 schwarze 766: print_byte(h, '/');
1.6 schwarze 767:
1.67 schwarze 768: print_byte(h, '>');
1.1 schwarze 769:
1.66 schwarze 770: if (tflags & HTML_NLBEGIN)
1.67 schwarze 771: print_endline(h);
1.66 schwarze 772: else
773: h->flags |= HTML_NOSPACE;
1.18 schwarze 774:
1.66 schwarze 775: if (tflags & HTML_INDENT)
776: h->indent++;
777: if (tflags & HTML_NOINDENT)
778: h->noindent++;
1.18 schwarze 779:
1.58 schwarze 780: return t;
1.1 schwarze 781: }
782:
783: static void
1.54 schwarze 784: print_ctag(struct html *h, struct tag *tag)
1.1 schwarze 785: {
1.66 schwarze 786: int tflags;
1.35 schwarze 787:
1.54 schwarze 788: /*
789: * Remember to close out and nullify the current
790: * meta-font and table, if applicable.
791: */
792: if (tag == h->metaf)
793: h->metaf = NULL;
794: if (tag == h->tblt)
795: h->tblt = NULL;
796:
1.66 schwarze 797: tflags = htmltags[tag->tag].flags;
798:
799: if (tflags & HTML_INDENT)
800: h->indent--;
801: if (tflags & HTML_NOINDENT)
802: h->noindent--;
803: if (tflags & HTML_NLEND)
1.67 schwarze 804: print_endline(h);
805: print_indent(h);
806: print_byte(h, '<');
807: print_byte(h, '/');
808: print_word(h, htmltags[tag->tag].name);
809: print_byte(h, '>');
1.66 schwarze 810: if (tflags & HTML_NLAFTER)
1.67 schwarze 811: print_endline(h);
1.54 schwarze 812:
1.74 schwarze 813: h->tag = tag->next;
1.54 schwarze 814: free(tag);
1.1 schwarze 815: }
816:
817: void
1.6 schwarze 818: print_gen_decls(struct html *h)
819: {
1.67 schwarze 820: print_word(h, "<!DOCTYPE html>");
821: print_endline(h);
1.91 schwarze 822: }
823:
824: void
825: print_gen_comment(struct html *h, struct roff_node *n)
826: {
827: int wantblank;
828:
829: print_word(h, "<!-- This is an automatically generated file."
830: " Do not edit.");
831: h->indent = 1;
832: wantblank = 0;
833: while (n != NULL && n->type == ROFFT_COMMENT) {
834: if (strstr(n->string, "-->") == NULL &&
835: (wantblank || *n->string != '\0')) {
836: print_endline(h);
837: print_indent(h);
838: print_word(h, n->string);
839: wantblank = *n->string != '\0';
840: }
841: n = n->next;
842: }
843: if (wantblank)
844: print_endline(h);
845: print_word(h, " -->");
846: print_endline(h);
847: h->indent = 0;
1.1 schwarze 848: }
849:
850: void
1.12 schwarze 851: print_text(struct html *h, const char *word)
1.1 schwarze 852: {
1.67 schwarze 853: if (h->col && (h->flags & HTML_NOSPACE) == 0) {
1.12 schwarze 854: if ( ! (HTML_KEEP & h->flags)) {
855: if (HTML_PREKEEP & h->flags)
856: h->flags |= HTML_KEEP;
1.67 schwarze 857: print_endword(h);
1.12 schwarze 858: } else
1.86 bentley 859: print_word(h, " ");
1.12 schwarze 860: }
1.1 schwarze 861:
1.20 schwarze 862: assert(NULL == h->metaf);
1.31 schwarze 863: switch (h->metac) {
1.35 schwarze 864: case HTMLFONT_ITALIC:
1.64 schwarze 865: h->metaf = print_otag(h, TAG_I, "");
1.31 schwarze 866: break;
1.35 schwarze 867: case HTMLFONT_BOLD:
1.64 schwarze 868: h->metaf = print_otag(h, TAG_B, "");
1.31 schwarze 869: break;
1.35 schwarze 870: case HTMLFONT_BI:
1.64 schwarze 871: h->metaf = print_otag(h, TAG_B, "");
872: print_otag(h, TAG_I, "");
1.31 schwarze 873: break;
874: default:
1.67 schwarze 875: print_indent(h);
1.31 schwarze 876: break;
877: }
1.20 schwarze 878:
1.12 schwarze 879: assert(word);
1.65 schwarze 880: if ( ! print_encode(h, word, NULL, 0)) {
1.13 schwarze 881: if ( ! (h->flags & HTML_NONOSPACE))
882: h->flags &= ~HTML_NOSPACE;
1.53 schwarze 883: h->flags &= ~HTML_NONEWLINE;
1.28 schwarze 884: } else
1.53 schwarze 885: h->flags |= HTML_NOSPACE | HTML_NONEWLINE;
1.20 schwarze 886:
887: if (h->metaf) {
888: print_tagq(h, h->metaf);
889: h->metaf = NULL;
890: }
1.17 schwarze 891:
892: h->flags &= ~HTML_IGNDELIM;
1.1 schwarze 893: }
894:
895: void
896: print_tagq(struct html *h, const struct tag *until)
897: {
898: struct tag *tag;
899:
1.74 schwarze 900: while ((tag = h->tag) != NULL) {
1.54 schwarze 901: print_ctag(h, tag);
1.1 schwarze 902: if (until && tag == until)
903: return;
904: }
905: }
906:
907: void
908: print_stagq(struct html *h, const struct tag *suntil)
909: {
910: struct tag *tag;
911:
1.74 schwarze 912: while ((tag = h->tag) != NULL) {
1.1 schwarze 913: if (suntil && tag == suntil)
914: return;
1.54 schwarze 915: print_ctag(h, tag);
1.1 schwarze 916: }
917: }
1.42 schwarze 918:
919: void
920: print_paragraph(struct html *h)
921: {
922: struct tag *t;
923:
1.68 schwarze 924: t = print_otag(h, TAG_DIV, "c", "Pp");
1.42 schwarze 925: print_tagq(h, t);
926: }
927:
1.67 schwarze 928:
929: /***********************************************************************
930: * Low level output functions.
931: * They implement line breaking using a short static buffer.
932: ***********************************************************************/
933:
934: /*
935: * Buffer one HTML output byte.
936: * If the buffer is full, flush and deactivate it and start a new line.
937: * If the buffer is inactive, print directly.
938: */
939: static void
940: print_byte(struct html *h, char c)
941: {
942: if ((h->flags & HTML_BUFFER) == 0) {
943: putchar(c);
944: h->col++;
945: return;
946: }
947:
948: if (h->col + h->bufcol < sizeof(h->buf)) {
949: h->buf[h->bufcol++] = c;
950: return;
951: }
952:
953: putchar('\n');
954: h->col = 0;
955: print_indent(h);
956: putchar(' ');
957: putchar(' ');
958: fwrite(h->buf, h->bufcol, 1, stdout);
959: putchar(c);
960: h->col = (h->indent + 1) * 2 + h->bufcol + 1;
961: h->bufcol = 0;
962: h->flags &= ~HTML_BUFFER;
963: }
964:
1.66 schwarze 965: /*
966: * If something was printed on the current output line, end it.
1.67 schwarze 967: * Not to be called right after print_indent().
1.66 schwarze 968: */
1.72 schwarze 969: void
1.67 schwarze 970: print_endline(struct html *h)
1.66 schwarze 971: {
1.67 schwarze 972: if (h->col == 0)
1.66 schwarze 973: return;
974:
1.67 schwarze 975: if (h->bufcol) {
976: putchar(' ');
977: fwrite(h->buf, h->bufcol, 1, stdout);
978: h->bufcol = 0;
979: }
1.66 schwarze 980: putchar('\n');
1.67 schwarze 981: h->col = 0;
982: h->flags |= HTML_NOSPACE;
983: h->flags &= ~HTML_BUFFER;
984: }
985:
986: /*
987: * Flush the HTML output buffer.
988: * If it is inactive, activate it.
989: */
990: static void
991: print_endword(struct html *h)
992: {
993: if (h->noindent) {
994: print_byte(h, ' ');
995: return;
996: }
997:
998: if ((h->flags & HTML_BUFFER) == 0) {
999: h->col++;
1000: h->flags |= HTML_BUFFER;
1001: } else if (h->bufcol) {
1002: putchar(' ');
1003: fwrite(h->buf, h->bufcol, 1, stdout);
1004: h->col += h->bufcol + 1;
1005: }
1006: h->bufcol = 0;
1.66 schwarze 1007: }
1008:
1009: /*
1010: * If at the beginning of a new output line,
1011: * perform indentation and mark the line as containing output.
1012: * Make sure to really produce some output right afterwards,
1013: * but do not use print_otag() for producing it.
1014: */
1015: static void
1.67 schwarze 1016: print_indent(struct html *h)
1.66 schwarze 1017: {
1.67 schwarze 1018: size_t i;
1.66 schwarze 1019:
1.67 schwarze 1020: if (h->col)
1.66 schwarze 1021: return;
1022:
1.67 schwarze 1023: if (h->noindent == 0) {
1024: h->col = h->indent * 2;
1025: for (i = 0; i < h->col; i++)
1.66 schwarze 1026: putchar(' ');
1.67 schwarze 1027: }
1028: h->flags &= ~HTML_NOSPACE;
1029: }
1030:
1031: /*
1032: * Print or buffer some characters
1033: * depending on the current HTML output buffer state.
1034: */
1035: static void
1036: print_word(struct html *h, const char *cp)
1037: {
1038: while (*cp != '\0')
1039: print_byte(h, *cp++);
1.66 schwarze 1040: }
1.64 schwarze 1041:
1042: /*
1043: * Calculate the scaling unit passed in a `-width' argument. This uses
1044: * either a native scaling unit (e.g., 1i, 2m) or the string length of
1045: * the value.
1046: */
1047: static void
1048: a2width(const char *p, struct roffsu *su)
1049: {
1.83 schwarze 1050: const char *end;
1051:
1052: end = a2roffsu(p, su, SCALE_MAX);
1053: if (end == NULL || *end != '\0') {
1.64 schwarze 1054: su->unit = SCALE_EN;
1055: su->scale = html_strlen(p);
1056: } else if (su->scale < 0.0)
1057: su->scale = 0.0;
1.3 schwarze 1058: }