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