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