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