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