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