Annotation of mandoc/html.c, Revision 1.248
1.248 ! schwarze 1: /* $Id: html.c,v 1.247 2018/12/16 00:17:02 schwarze Exp $ */
1.1 kristaps 2: /*
1.176 schwarze 3: * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
1.221 schwarze 4: * Copyright (c) 2011-2015, 2017, 2018 Ingo Schwarze <schwarze@openbsd.org>
1.1 kristaps 5: *
6: * Permission to use, copy, modify, and distribute this software for any
1.29 kristaps 7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 9: *
1.186 schwarze 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.29 kristaps 11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.186 schwarze 12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.29 kristaps 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.
1.1 kristaps 17: */
1.92 kristaps 18: #include "config.h"
19:
1.41 kristaps 20: #include <sys/types.h>
1.240 schwarze 21: #include <sys/stat.h>
1.30 kristaps 22:
1.1 kristaps 23: #include <assert.h>
1.68 kristaps 24: #include <ctype.h>
1.76 kristaps 25: #include <stdarg.h>
1.229 schwarze 26: #include <stddef.h>
1.29 kristaps 27: #include <stdio.h>
1.63 kristaps 28: #include <stdint.h>
1.1 kristaps 29: #include <stdlib.h>
1.33 kristaps 30: #include <string.h>
1.45 kristaps 31: #include <unistd.h>
1.1 kristaps 32:
1.210 schwarze 33: #include "mandoc_aux.h"
1.229 schwarze 34: #include "mandoc_ohash.h"
1.100 kristaps 35: #include "mandoc.h"
1.210 schwarze 36: #include "roff.h"
1.58 kristaps 37: #include "out.h"
1.51 kristaps 38: #include "html.h"
1.186 schwarze 39: #include "manconf.h"
1.64 kristaps 40: #include "main.h"
1.63 kristaps 41:
1.29 kristaps 42: struct htmldata {
1.63 kristaps 43: const char *name;
1.29 kristaps 44: int flags;
1.196 schwarze 45: #define HTML_NOSTACK (1 << 0)
46: #define HTML_AUTOCLOSE (1 << 1)
47: #define HTML_NLBEFORE (1 << 2)
48: #define HTML_NLBEGIN (1 << 3)
49: #define HTML_NLEND (1 << 4)
50: #define HTML_NLAFTER (1 << 5)
51: #define HTML_NLAROUND (HTML_NLBEFORE | HTML_NLAFTER)
52: #define HTML_NLINSIDE (HTML_NLBEGIN | HTML_NLEND)
53: #define HTML_NLALL (HTML_NLAROUND | HTML_NLINSIDE)
54: #define HTML_INDENT (1 << 6)
55: #define HTML_NOINDENT (1 << 7)
1.29 kristaps 56: };
1.7 kristaps 57:
1.29 kristaps 58: static const struct htmldata htmltags[TAG_MAX] = {
1.196 schwarze 59: {"html", HTML_NLALL},
60: {"head", HTML_NLALL | HTML_INDENT},
61: {"body", HTML_NLALL},
62: {"meta", HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
63: {"title", HTML_NLAROUND},
64: {"div", HTML_NLAROUND},
1.225 schwarze 65: {"div", 0},
1.196 schwarze 66: {"h1", HTML_NLAROUND},
67: {"h2", HTML_NLAROUND},
68: {"span", 0},
69: {"link", HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
70: {"br", HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
71: {"a", 0},
72: {"table", HTML_NLALL | HTML_INDENT},
73: {"tr", HTML_NLALL | HTML_INDENT},
74: {"td", HTML_NLAROUND},
75: {"li", HTML_NLAROUND | HTML_INDENT},
76: {"ul", HTML_NLALL | HTML_INDENT},
77: {"ol", HTML_NLALL | HTML_INDENT},
78: {"dl", HTML_NLALL | HTML_INDENT},
79: {"dt", HTML_NLAROUND},
80: {"dd", HTML_NLAROUND | HTML_INDENT},
81: {"pre", HTML_NLALL | HTML_NOINDENT},
1.207 schwarze 82: {"var", 0},
1.206 schwarze 83: {"cite", 0},
1.196 schwarze 84: {"b", 0},
85: {"i", 0},
86: {"code", 0},
87: {"small", 0},
88: {"style", HTML_NLALL | HTML_INDENT},
89: {"math", HTML_NLALL | HTML_INDENT},
90: {"mrow", 0},
91: {"mi", 0},
1.215 schwarze 92: {"mn", 0},
1.196 schwarze 93: {"mo", 0},
94: {"msup", 0},
95: {"msub", 0},
96: {"msubsup", 0},
97: {"mfrac", 0},
98: {"msqrt", 0},
99: {"mfenced", 0},
100: {"mtable", 0},
101: {"mtr", 0},
102: {"mtd", 0},
103: {"munderover", 0},
104: {"munder", 0},
105: {"mover", 0},
1.90 kristaps 106: };
107:
1.229 schwarze 108: /* Avoid duplicate HTML id= attributes. */
109: static struct ohash id_unique;
110:
1.197 schwarze 111: static void print_byte(struct html *, char);
112: static void print_endword(struct html *);
113: static void print_indent(struct html *);
114: static void print_word(struct html *, const char *);
115:
1.184 schwarze 116: static void print_ctag(struct html *, struct tag *);
1.197 schwarze 117: static int print_escape(struct html *, char);
1.195 schwarze 118: static int print_encode(struct html *, const char *, const char *, int);
119: static void print_href(struct html *, const char *, const char *, int);
1.82 kristaps 120:
1.156 schwarze 121:
1.180 schwarze 122: void *
1.191 schwarze 123: html_alloc(const struct manoutput *outopts)
1.10 kristaps 124: {
1.30 kristaps 125: struct html *h;
126:
1.128 kristaps 127: h = mandoc_calloc(1, sizeof(struct html));
1.10 kristaps 128:
1.204 schwarze 129: h->tag = NULL;
1.186 schwarze 130: h->style = outopts->style;
1.240 schwarze 131: if ((h->base_man1 = outopts->man) == NULL)
132: h->base_man2 = NULL;
133: else if ((h->base_man2 = strchr(h->base_man1, ';')) != NULL)
134: *h->base_man2++ = '\0';
1.186 schwarze 135: h->base_includes = outopts->includes;
136: if (outopts->fragment)
137: h->oflags |= HTML_FRAGMENT;
1.241 schwarze 138: if (outopts->toc)
139: h->oflags |= HTML_TOC;
1.43 kristaps 140:
1.229 schwarze 141: mandoc_ohash_init(&id_unique, 4, 0);
142:
1.188 schwarze 143: return h;
1.29 kristaps 144: }
1.10 kristaps 145:
1.29 kristaps 146: void
147: html_free(void *p)
148: {
1.30 kristaps 149: struct tag *tag;
150: struct html *h;
1.229 schwarze 151: char *cp;
152: unsigned int slot;
1.30 kristaps 153:
154: h = (struct html *)p;
1.204 schwarze 155: while ((tag = h->tag) != NULL) {
156: h->tag = tag->next;
1.30 kristaps 157: free(tag);
158: }
1.229 schwarze 159: free(h);
1.53 kristaps 160:
1.229 schwarze 161: cp = ohash_first(&id_unique, &slot);
162: while (cp != NULL) {
163: free(cp);
164: cp = ohash_next(&id_unique, &slot);
165: }
166: ohash_delete(&id_unique);
1.10 kristaps 167: }
1.2 kristaps 168:
1.51 kristaps 169: void
1.29 kristaps 170: print_gen_head(struct html *h)
171: {
1.165 kristaps 172: struct tag *t;
1.41 kristaps 173:
1.194 schwarze 174: print_otag(h, TAG_META, "?", "charset", "utf-8");
1.222 schwarze 175: if (h->style != NULL) {
176: print_otag(h, TAG_LINK, "?h??", "rel", "stylesheet",
177: h->style, "type", "text/css", "media", "all");
178: return;
179: }
1.165 kristaps 180:
1.168 kristaps 181: /*
1.222 schwarze 182: * Print a minimal embedded style sheet.
1.168 kristaps 183: */
1.196 schwarze 184:
1.194 schwarze 185: t = print_otag(h, TAG_STYLE, "");
1.196 schwarze 186: print_text(h, "table.head, table.foot { width: 100%; }");
1.197 schwarze 187: print_endline(h);
1.196 schwarze 188: print_text(h, "td.head-rtitle, td.foot-os { text-align: right; }");
1.197 schwarze 189: print_endline(h);
1.196 schwarze 190: print_text(h, "td.head-vol { text-align: center; }");
1.197 schwarze 191: print_endline(h);
1.198 schwarze 192: print_text(h, "div.Pp { margin: 1ex 0ex; }");
1.225 schwarze 193: print_endline(h);
194: print_text(h, "div.Nd, div.Bf, div.Op { display: inline; }");
1.226 schwarze 195: print_endline(h);
1.227 schwarze 196: print_text(h, "span.Pa, span.Ad { font-style: italic; }");
1.228 schwarze 197: print_endline(h);
198: print_text(h, "span.Ms { font-weight: bold; }");
1.224 schwarze 199: print_endline(h);
200: print_text(h, "dl.Bl-diag ");
201: print_byte(h, '>');
202: print_text(h, " dt { font-weight: bold; }");
1.223 schwarze 203: print_endline(h);
204: print_text(h, "code.Nm, code.Fl, code.Cm, code.Ic, "
205: "code.In, code.Fd, code.Fn,");
206: print_endline(h);
207: print_text(h, "code.Cd { font-weight: bold; "
208: "font-family: inherit; }");
1.165 kristaps 209: print_tagq(h, t);
1.4 kristaps 210: }
211:
1.247 schwarze 212: void
1.132 kristaps 213: print_metaf(struct html *h, enum mandoc_esc deco)
1.88 kristaps 214: {
1.90 kristaps 215: enum htmlfont font;
1.88 kristaps 216:
217: switch (deco) {
1.156 schwarze 218: case ESCAPE_FONTPREV:
1.90 kristaps 219: font = h->metal;
1.88 kristaps 220: break;
1.156 schwarze 221: case ESCAPE_FONTITALIC:
1.90 kristaps 222: font = HTMLFONT_ITALIC;
223: break;
1.156 schwarze 224: case ESCAPE_FONTBOLD:
1.90 kristaps 225: font = HTMLFONT_BOLD;
1.88 kristaps 226: break;
1.156 schwarze 227: case ESCAPE_FONTBI:
1.152 schwarze 228: font = HTMLFONT_BI;
229: break;
1.242 schwarze 230: case ESCAPE_FONTCW:
231: font = HTMLFONT_CW;
232: break;
1.156 schwarze 233: case ESCAPE_FONT:
234: case ESCAPE_FONTROMAN:
1.90 kristaps 235: font = HTMLFONT_NONE;
1.88 kristaps 236: break;
237: default:
1.247 schwarze 238: return;
1.88 kristaps 239: }
240:
1.122 kristaps 241: if (h->metaf) {
242: print_tagq(h, h->metaf);
243: h->metaf = NULL;
244: }
245:
246: h->metal = h->metac;
247: h->metac = font;
248:
1.152 schwarze 249: switch (font) {
1.156 schwarze 250: case HTMLFONT_ITALIC:
1.194 schwarze 251: h->metaf = print_otag(h, TAG_I, "");
1.152 schwarze 252: break;
1.156 schwarze 253: case HTMLFONT_BOLD:
1.194 schwarze 254: h->metaf = print_otag(h, TAG_B, "");
1.152 schwarze 255: break;
1.156 schwarze 256: case HTMLFONT_BI:
1.194 schwarze 257: h->metaf = print_otag(h, TAG_B, "");
258: print_otag(h, TAG_I, "");
1.152 schwarze 259: break;
1.242 schwarze 260: case HTMLFONT_CW:
261: h->metaf = print_otag(h, TAG_SPAN, "c", "Li");
262: break;
1.152 schwarze 263: default:
264: break;
265: }
1.248 ! schwarze 266: }
! 267:
! 268: /*
! 269: * ROFF_nf switches to no-fill mode, ROFF_fi to fill mode.
! 270: * TOKEN_NONE does not switch. The old mode is returned.
! 271: */
! 272: enum roff_tok
! 273: html_fillmode(struct html *h, enum roff_tok want)
! 274: {
! 275: struct tag *t;
! 276: enum roff_tok had;
! 277:
! 278: for (t = h->tag; t != NULL; t = t->next)
! 279: if (t->tag == TAG_PRE)
! 280: break;
! 281:
! 282: had = t == NULL ? ROFF_fi : ROFF_nf;
! 283:
! 284: if (want != had) {
! 285: switch (want) {
! 286: case ROFF_fi:
! 287: print_tagq(h, t);
! 288: break;
! 289: case ROFF_nf:
! 290: print_otag(h, TAG_PRE, "");
! 291: break;
! 292: case TOKEN_NONE:
! 293: break;
! 294: default:
! 295: abort();
! 296: }
! 297: }
! 298: return had;
1.210 schwarze 299: }
300:
301: char *
1.229 schwarze 302: html_make_id(const struct roff_node *n, int unique)
1.210 schwarze 303: {
304: const struct roff_node *nch;
1.229 schwarze 305: char *buf, *bufs, *cp;
306: unsigned int slot;
307: int suffix;
1.210 schwarze 308:
309: for (nch = n->child; nch != NULL; nch = nch->next)
310: if (nch->type != ROFFT_TEXT)
311: return NULL;
312:
313: buf = NULL;
314: deroff(&buf, n);
1.220 schwarze 315: if (buf == NULL)
316: return NULL;
1.210 schwarze 317:
1.230 schwarze 318: /*
319: * In ID attributes, only use ASCII characters that are
320: * permitted in URL-fragment strings according to the
321: * explicit list at:
322: * https://url.spec.whatwg.org/#url-fragment-string
323: */
1.210 schwarze 324:
325: for (cp = buf; *cp != '\0'; cp++)
1.230 schwarze 326: if (isalnum((unsigned char)*cp) == 0 &&
327: strchr("!$&'()*+,-./:;=?@_~", *cp) == NULL)
1.210 schwarze 328: *cp = '_';
329:
1.229 schwarze 330: if (unique == 0)
331: return buf;
332:
333: /* Avoid duplicate HTML id= attributes. */
334:
335: bufs = NULL;
336: suffix = 1;
337: slot = ohash_qlookup(&id_unique, buf);
338: cp = ohash_find(&id_unique, slot);
339: if (cp != NULL) {
340: while (cp != NULL) {
341: free(bufs);
342: if (++suffix > 127) {
343: free(buf);
344: return NULL;
345: }
346: mandoc_asprintf(&bufs, "%s_%d", buf, suffix);
347: slot = ohash_qlookup(&id_unique, bufs);
348: cp = ohash_find(&id_unique, slot);
349: }
350: free(buf);
351: buf = bufs;
352: }
353: ohash_insert(&id_unique, slot, buf);
1.210 schwarze 354: return buf;
1.88 kristaps 355: }
356:
1.85 kristaps 357: static int
1.197 schwarze 358: print_escape(struct html *h, char c)
1.159 schwarze 359: {
360:
361: switch (c) {
362: case '<':
1.197 schwarze 363: print_word(h, "<");
1.159 schwarze 364: break;
365: case '>':
1.197 schwarze 366: print_word(h, ">");
1.159 schwarze 367: break;
368: case '&':
1.197 schwarze 369: print_word(h, "&");
1.159 schwarze 370: break;
371: case '"':
1.197 schwarze 372: print_word(h, """);
1.159 schwarze 373: break;
374: case ASCII_NBRSP:
1.197 schwarze 375: print_word(h, " ");
1.159 schwarze 376: break;
377: case ASCII_HYPH:
1.197 schwarze 378: print_byte(h, '-');
1.189 schwarze 379: break;
1.159 schwarze 380: case ASCII_BREAK:
381: break;
382: default:
1.188 schwarze 383: return 0;
1.159 schwarze 384: }
1.188 schwarze 385: return 1;
1.159 schwarze 386: }
387:
388: static int
1.195 schwarze 389: print_encode(struct html *h, const char *p, const char *pend, int norecurse)
1.29 kristaps 390: {
1.197 schwarze 391: char numbuf[16];
1.214 schwarze 392: const char *seq;
1.77 kristaps 393: size_t sz;
1.214 schwarze 394: int c, len, breakline, nospace;
1.132 kristaps 395: enum mandoc_esc esc;
1.214 schwarze 396: static const char rejs[10] = { ' ', '\\', '<', '>', '&', '"',
1.154 schwarze 397: ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' };
1.14 kristaps 398:
1.195 schwarze 399: if (pend == NULL)
400: pend = strchr(p, '\0');
401:
1.214 schwarze 402: breakline = 0;
1.85 kristaps 403: nospace = 0;
404:
1.195 schwarze 405: while (p < pend) {
1.151 schwarze 406: if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
407: h->flags &= ~HTML_SKIPCHAR;
408: p++;
409: continue;
410: }
411:
1.197 schwarze 412: for (sz = strcspn(p, rejs); sz-- && p < pend; p++)
1.214 schwarze 413: print_byte(h, *p);
414:
415: if (breakline &&
416: (p >= pend || *p == ' ' || *p == ASCII_NBRSP)) {
1.245 schwarze 417: print_otag(h, TAG_BR, "");
1.214 schwarze 418: breakline = 0;
419: while (p < pend && (*p == ' ' || *p == ASCII_NBRSP))
420: p++;
421: continue;
422: }
1.77 kristaps 423:
1.195 schwarze 424: if (p >= pend)
1.132 kristaps 425: break;
426:
1.214 schwarze 427: if (*p == ' ') {
428: print_endword(h);
429: p++;
430: continue;
431: }
432:
1.197 schwarze 433: if (print_escape(h, *p++))
1.154 schwarze 434: continue;
1.77 kristaps 435:
1.132 kristaps 436: esc = mandoc_escape(&p, &seq, &len);
437: switch (esc) {
1.156 schwarze 438: case ESCAPE_FONT:
439: case ESCAPE_FONTPREV:
440: case ESCAPE_FONTBOLD:
441: case ESCAPE_FONTITALIC:
442: case ESCAPE_FONTBI:
1.242 schwarze 443: case ESCAPE_FONTCW:
1.156 schwarze 444: case ESCAPE_FONTROMAN:
1.243 schwarze 445: if (0 == norecurse) {
446: h->flags |= HTML_NOSPACE;
1.151 schwarze 447: print_metaf(h, esc);
1.243 schwarze 448: h->flags &= ~HTML_NOSPACE;
449: }
1.151 schwarze 450: continue;
1.156 schwarze 451: case ESCAPE_SKIPCHAR:
1.151 schwarze 452: h->flags |= HTML_SKIPCHAR;
453: continue;
1.246 schwarze 454: case ESCAPE_ERROR:
455: continue;
1.151 schwarze 456: default:
457: break;
458: }
459:
460: if (h->flags & HTML_SKIPCHAR) {
461: h->flags &= ~HTML_SKIPCHAR;
462: continue;
463: }
464:
465: switch (esc) {
1.156 schwarze 466: case ESCAPE_UNICODE:
1.159 schwarze 467: /* Skip past "u" header. */
1.144 kristaps 468: c = mchars_num2uc(seq + 1, len - 1);
469: break;
1.156 schwarze 470: case ESCAPE_NUMBERED:
1.141 kristaps 471: c = mchars_num2char(seq, len);
1.181 schwarze 472: if (c < 0)
473: continue;
1.82 kristaps 474: break;
1.156 schwarze 475: case ESCAPE_SPECIAL:
1.191 schwarze 476: c = mchars_spec2cp(seq, len);
1.181 schwarze 477: if (c <= 0)
478: continue;
1.246 schwarze 479: break;
480: case ESCAPE_UNDEF:
481: c = *seq;
1.132 kristaps 482: break;
1.239 schwarze 483: case ESCAPE_DEVICE:
484: print_word(h, "html");
485: continue;
1.214 schwarze 486: case ESCAPE_BREAK:
487: breakline = 1;
488: continue;
1.156 schwarze 489: case ESCAPE_NOSPACE:
1.132 kristaps 490: if ('\0' == *p)
491: nospace = 1;
1.179 schwarze 492: continue;
1.185 schwarze 493: case ESCAPE_OVERSTRIKE:
494: if (len == 0)
495: continue;
496: c = seq[len - 1];
497: break;
1.82 kristaps 498: default:
1.179 schwarze 499: continue;
1.82 kristaps 500: }
1.181 schwarze 501: if ((c < 0x20 && c != 0x09) ||
502: (c > 0x7E && c < 0xA0))
1.179 schwarze 503: c = 0xFFFD;
1.197 schwarze 504: if (c > 0x7E) {
1.216 schwarze 505: (void)snprintf(numbuf, sizeof(numbuf), "&#x%.4X;", c);
1.197 schwarze 506: print_word(h, numbuf);
507: } else if (print_escape(h, c) == 0)
508: print_byte(h, c);
1.32 kristaps 509: }
1.85 kristaps 510:
1.188 schwarze 511: return nospace;
1.14 kristaps 512: }
513:
1.94 kristaps 514: static void
1.195 schwarze 515: print_href(struct html *h, const char *name, const char *sec, int man)
1.94 kristaps 516: {
1.240 schwarze 517: struct stat sb;
1.195 schwarze 518: const char *p, *pp;
1.240 schwarze 519: char *filename;
520:
521: if (man) {
522: pp = h->base_man1;
523: if (h->base_man2 != NULL) {
524: mandoc_asprintf(&filename, "%s.%s", name, sec);
525: if (stat(filename, &sb) == -1)
526: pp = h->base_man2;
527: free(filename);
528: }
529: } else
530: pp = h->base_includes;
1.195 schwarze 531:
532: while ((p = strchr(pp, '%')) != NULL) {
533: print_encode(h, pp, p, 1);
534: if (man && p[1] == 'S') {
535: if (sec == NULL)
1.197 schwarze 536: print_byte(h, '1');
1.195 schwarze 537: else
538: print_encode(h, sec, NULL, 1);
539: } else if ((man && p[1] == 'N') ||
540: (man == 0 && p[1] == 'I'))
541: print_encode(h, name, NULL, 1);
542: else
543: print_encode(h, p, p + 2, 1);
544: pp = p + 2;
545: }
546: if (*pp != '\0')
547: print_encode(h, pp, NULL, 1);
1.94 kristaps 548: }
549:
1.51 kristaps 550: struct tag *
1.194 schwarze 551: print_otag(struct html *h, enum htmltag tag, const char *fmt, ...)
1.14 kristaps 552: {
1.194 schwarze 553: va_list ap;
1.30 kristaps 554: struct tag *t;
1.195 schwarze 555: const char *attr;
1.203 schwarze 556: char *arg1, *arg2;
1.244 schwarze 557: int style_written, tflags;
1.196 schwarze 558:
559: tflags = htmltags[tag].flags;
1.30 kristaps 560:
1.204 schwarze 561: /* Push this tag onto the stack of open scopes. */
1.94 kristaps 562:
1.196 schwarze 563: if ((tflags & HTML_NOSTACK) == 0) {
1.128 kristaps 564: t = mandoc_malloc(sizeof(struct tag));
1.30 kristaps 565: t->tag = tag;
1.204 schwarze 566: t->next = h->tag;
567: h->tag = t;
1.30 kristaps 568: } else
569: t = NULL;
1.29 kristaps 570:
1.196 schwarze 571: if (tflags & HTML_NLBEFORE)
1.197 schwarze 572: print_endline(h);
573: if (h->col == 0)
574: print_indent(h);
1.196 schwarze 575: else if ((h->flags & HTML_NOSPACE) == 0) {
576: if (h->flags & HTML_KEEP)
1.216 schwarze 577: print_word(h, " ");
1.196 schwarze 578: else {
579: if (h->flags & HTML_PREKEEP)
580: h->flags |= HTML_KEEP;
1.197 schwarze 581: print_endword(h);
1.105 kristaps 582: }
1.196 schwarze 583: }
1.29 kristaps 584:
1.109 kristaps 585: if ( ! (h->flags & HTML_NONOSPACE))
586: h->flags &= ~HTML_NOSPACE;
1.110 kristaps 587: else
588: h->flags |= HTML_NOSPACE;
1.109 kristaps 589:
1.94 kristaps 590: /* Print out the tag name and attributes. */
591:
1.197 schwarze 592: print_byte(h, '<');
593: print_word(h, htmltags[tag].name);
1.194 schwarze 594:
595: va_start(ap, fmt);
596:
1.244 schwarze 597: while (*fmt != '\0' && *fmt != 's') {
1.203 schwarze 598:
1.238 schwarze 599: /* Parse attributes and arguments. */
1.203 schwarze 600:
601: arg1 = va_arg(ap, char *);
1.238 schwarze 602: arg2 = NULL;
1.194 schwarze 603: switch (*fmt++) {
604: case 'c':
1.195 schwarze 605: attr = "class";
1.194 schwarze 606: break;
607: case 'h':
1.195 schwarze 608: attr = "href";
1.194 schwarze 609: break;
610: case 'i':
1.195 schwarze 611: attr = "id";
1.194 schwarze 612: break;
613: case '?':
1.203 schwarze 614: attr = arg1;
615: arg1 = va_arg(ap, char *);
1.194 schwarze 616: break;
617: default:
618: abort();
619: }
1.203 schwarze 620: if (*fmt == 'M')
621: arg2 = va_arg(ap, char *);
622: if (arg1 == NULL)
623: continue;
624:
1.238 schwarze 625: /* Print the attributes. */
1.203 schwarze 626:
1.197 schwarze 627: print_byte(h, ' ');
628: print_word(h, attr);
629: print_byte(h, '=');
630: print_byte(h, '"');
1.195 schwarze 631: switch (*fmt) {
1.208 schwarze 632: case 'I':
633: print_href(h, arg1, NULL, 0);
634: fmt++;
635: break;
1.195 schwarze 636: case 'M':
1.203 schwarze 637: print_href(h, arg1, arg2, 1);
1.195 schwarze 638: fmt++;
639: break;
1.208 schwarze 640: case 'R':
641: print_byte(h, '#');
642: print_encode(h, arg1, NULL, 1);
1.195 schwarze 643: fmt++;
644: break;
1.208 schwarze 645: case 'T':
646: print_encode(h, arg1, NULL, 1);
647: print_word(h, "\" title=\"");
648: print_encode(h, arg1, NULL, 1);
1.195 schwarze 649: fmt++;
1.208 schwarze 650: break;
1.195 schwarze 651: default:
1.244 schwarze 652: print_encode(h, arg1, NULL, 1);
1.195 schwarze 653: break;
654: }
1.197 schwarze 655: print_byte(h, '"');
1.194 schwarze 656: }
1.244 schwarze 657:
658: style_written = 0;
659: while (*fmt++ == 's') {
660: arg1 = va_arg(ap, char *);
661: arg2 = va_arg(ap, char *);
662: if (arg2 == NULL)
663: continue;
664: print_byte(h, ' ');
665: if (style_written == 0) {
666: print_word(h, "style=\"");
667: style_written = 1;
668: }
669: print_word(h, arg1);
670: print_byte(h, ':');
671: print_byte(h, ' ');
672: print_word(h, arg2);
673: print_byte(h, ';');
674: }
675: if (style_written)
676: print_byte(h, '"');
677:
1.194 schwarze 678: va_end(ap);
1.94 kristaps 679:
1.172 kristaps 680: /* Accommodate for "well-formed" singleton escaping. */
1.94 kristaps 681:
1.93 kristaps 682: if (HTML_AUTOCLOSE & htmltags[tag].flags)
1.197 schwarze 683: print_byte(h, '/');
1.93 kristaps 684:
1.197 schwarze 685: print_byte(h, '>');
1.14 kristaps 686:
1.196 schwarze 687: if (tflags & HTML_NLBEGIN)
1.197 schwarze 688: print_endline(h);
1.196 schwarze 689: else
690: h->flags |= HTML_NOSPACE;
1.117 kristaps 691:
1.196 schwarze 692: if (tflags & HTML_INDENT)
693: h->indent++;
694: if (tflags & HTML_NOINDENT)
695: h->noindent++;
1.117 kristaps 696:
1.188 schwarze 697: return t;
1.14 kristaps 698: }
699:
1.29 kristaps 700: static void
1.184 schwarze 701: print_ctag(struct html *h, struct tag *tag)
1.14 kristaps 702: {
1.196 schwarze 703: int tflags;
1.156 schwarze 704:
1.184 schwarze 705: /*
706: * Remember to close out and nullify the current
707: * meta-font and table, if applicable.
708: */
709: if (tag == h->metaf)
710: h->metaf = NULL;
711: if (tag == h->tblt)
712: h->tblt = NULL;
713:
1.196 schwarze 714: tflags = htmltags[tag->tag].flags;
715:
716: if (tflags & HTML_INDENT)
717: h->indent--;
718: if (tflags & HTML_NOINDENT)
719: h->noindent--;
720: if (tflags & HTML_NLEND)
1.197 schwarze 721: print_endline(h);
722: print_indent(h);
723: print_byte(h, '<');
724: print_byte(h, '/');
725: print_word(h, htmltags[tag->tag].name);
726: print_byte(h, '>');
1.196 schwarze 727: if (tflags & HTML_NLAFTER)
1.197 schwarze 728: print_endline(h);
1.184 schwarze 729:
1.204 schwarze 730: h->tag = tag->next;
1.184 schwarze 731: free(tag);
1.14 kristaps 732: }
733:
1.51 kristaps 734: void
1.93 kristaps 735: print_gen_decls(struct html *h)
1.1 kristaps 736: {
1.197 schwarze 737: print_word(h, "<!DOCTYPE html>");
738: print_endline(h);
1.221 schwarze 739: }
740:
741: void
742: print_gen_comment(struct html *h, struct roff_node *n)
743: {
744: int wantblank;
745:
746: print_word(h, "<!-- This is an automatically generated file."
747: " Do not edit.");
748: h->indent = 1;
749: wantblank = 0;
750: while (n != NULL && n->type == ROFFT_COMMENT) {
751: if (strstr(n->string, "-->") == NULL &&
752: (wantblank || *n->string != '\0')) {
753: print_endline(h);
754: print_indent(h);
755: print_word(h, n->string);
756: wantblank = *n->string != '\0';
757: }
758: n = n->next;
759: }
760: if (wantblank)
761: print_endline(h);
762: print_word(h, " -->");
763: print_endline(h);
764: h->indent = 0;
1.1 kristaps 765: }
766:
1.51 kristaps 767: void
1.104 kristaps 768: print_text(struct html *h, const char *word)
1.1 kristaps 769: {
1.197 schwarze 770: if (h->col && (h->flags & HTML_NOSPACE) == 0) {
1.105 kristaps 771: if ( ! (HTML_KEEP & h->flags)) {
772: if (HTML_PREKEEP & h->flags)
773: h->flags |= HTML_KEEP;
1.197 schwarze 774: print_endword(h);
1.105 kristaps 775: } else
1.216 schwarze 776: print_word(h, " ");
1.105 kristaps 777: }
1.30 kristaps 778:
1.122 kristaps 779: assert(NULL == h->metaf);
1.152 schwarze 780: switch (h->metac) {
1.156 schwarze 781: case HTMLFONT_ITALIC:
1.194 schwarze 782: h->metaf = print_otag(h, TAG_I, "");
1.152 schwarze 783: break;
1.156 schwarze 784: case HTMLFONT_BOLD:
1.194 schwarze 785: h->metaf = print_otag(h, TAG_B, "");
1.152 schwarze 786: break;
1.156 schwarze 787: case HTMLFONT_BI:
1.194 schwarze 788: h->metaf = print_otag(h, TAG_B, "");
789: print_otag(h, TAG_I, "");
1.242 schwarze 790: break;
791: case HTMLFONT_CW:
792: h->metaf = print_otag(h, TAG_SPAN, "c", "Li");
1.152 schwarze 793: break;
794: default:
1.197 schwarze 795: print_indent(h);
1.152 schwarze 796: break;
797: }
1.122 kristaps 798:
1.104 kristaps 799: assert(word);
1.195 schwarze 800: if ( ! print_encode(h, word, NULL, 0)) {
1.109 kristaps 801: if ( ! (h->flags & HTML_NONOSPACE))
802: h->flags &= ~HTML_NOSPACE;
1.183 schwarze 803: h->flags &= ~HTML_NONEWLINE;
1.149 kristaps 804: } else
1.183 schwarze 805: h->flags |= HTML_NOSPACE | HTML_NONEWLINE;
1.122 kristaps 806:
807: if (h->metaf) {
808: print_tagq(h, h->metaf);
809: h->metaf = NULL;
810: }
1.113 schwarze 811:
812: h->flags &= ~HTML_IGNDELIM;
1.1 kristaps 813: }
1.30 kristaps 814:
1.51 kristaps 815: void
1.30 kristaps 816: print_tagq(struct html *h, const struct tag *until)
817: {
818: struct tag *tag;
819:
1.204 schwarze 820: while ((tag = h->tag) != NULL) {
1.184 schwarze 821: print_ctag(h, tag);
1.30 kristaps 822: if (until && tag == until)
823: return;
824: }
825: }
826:
1.51 kristaps 827: void
1.30 kristaps 828: print_stagq(struct html *h, const struct tag *suntil)
829: {
830: struct tag *tag;
831:
1.204 schwarze 832: while ((tag = h->tag) != NULL) {
1.30 kristaps 833: if (suntil && tag == suntil)
834: return;
1.184 schwarze 835: print_ctag(h, tag);
1.30 kristaps 836: }
837: }
1.171 kristaps 838:
839: void
840: print_paragraph(struct html *h)
841: {
842: struct tag *t;
843:
1.198 schwarze 844: t = print_otag(h, TAG_DIV, "c", "Pp");
1.171 kristaps 845: print_tagq(h, t);
846: }
847:
1.197 schwarze 848:
849: /***********************************************************************
850: * Low level output functions.
851: * They implement line breaking using a short static buffer.
852: ***********************************************************************/
853:
854: /*
855: * Buffer one HTML output byte.
856: * If the buffer is full, flush and deactivate it and start a new line.
857: * If the buffer is inactive, print directly.
858: */
859: static void
860: print_byte(struct html *h, char c)
861: {
862: if ((h->flags & HTML_BUFFER) == 0) {
863: putchar(c);
864: h->col++;
865: return;
866: }
867:
868: if (h->col + h->bufcol < sizeof(h->buf)) {
869: h->buf[h->bufcol++] = c;
870: return;
871: }
872:
873: putchar('\n');
874: h->col = 0;
875: print_indent(h);
876: putchar(' ');
877: putchar(' ');
878: fwrite(h->buf, h->bufcol, 1, stdout);
879: putchar(c);
880: h->col = (h->indent + 1) * 2 + h->bufcol + 1;
881: h->bufcol = 0;
882: h->flags &= ~HTML_BUFFER;
883: }
884:
1.196 schwarze 885: /*
886: * If something was printed on the current output line, end it.
1.197 schwarze 887: * Not to be called right after print_indent().
1.196 schwarze 888: */
1.202 schwarze 889: void
1.197 schwarze 890: print_endline(struct html *h)
1.196 schwarze 891: {
1.197 schwarze 892: if (h->col == 0)
1.196 schwarze 893: return;
894:
1.197 schwarze 895: if (h->bufcol) {
896: putchar(' ');
897: fwrite(h->buf, h->bufcol, 1, stdout);
898: h->bufcol = 0;
899: }
1.196 schwarze 900: putchar('\n');
1.197 schwarze 901: h->col = 0;
902: h->flags |= HTML_NOSPACE;
903: h->flags &= ~HTML_BUFFER;
904: }
905:
906: /*
907: * Flush the HTML output buffer.
908: * If it is inactive, activate it.
909: */
910: static void
911: print_endword(struct html *h)
912: {
913: if (h->noindent) {
914: print_byte(h, ' ');
915: return;
916: }
917:
918: if ((h->flags & HTML_BUFFER) == 0) {
919: h->col++;
920: h->flags |= HTML_BUFFER;
921: } else if (h->bufcol) {
922: putchar(' ');
923: fwrite(h->buf, h->bufcol, 1, stdout);
924: h->col += h->bufcol + 1;
925: }
926: h->bufcol = 0;
1.196 schwarze 927: }
928:
929: /*
930: * If at the beginning of a new output line,
931: * perform indentation and mark the line as containing output.
932: * Make sure to really produce some output right afterwards,
933: * but do not use print_otag() for producing it.
934: */
935: static void
1.197 schwarze 936: print_indent(struct html *h)
1.196 schwarze 937: {
1.197 schwarze 938: size_t i;
1.196 schwarze 939:
1.197 schwarze 940: if (h->col)
1.196 schwarze 941: return;
942:
1.197 schwarze 943: if (h->noindent == 0) {
944: h->col = h->indent * 2;
945: for (i = 0; i < h->col; i++)
1.196 schwarze 946: putchar(' ');
1.197 schwarze 947: }
948: h->flags &= ~HTML_NOSPACE;
949: }
950:
951: /*
952: * Print or buffer some characters
953: * depending on the current HTML output buffer state.
954: */
955: static void
956: print_word(struct html *h, const char *cp)
957: {
958: while (*cp != '\0')
959: print_byte(h, *cp++);
1.68 kristaps 960: }
CVSweb