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