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