Annotation of mandoc/html.c, Revision 1.241
1.241 ! schwarze 1: /* $Id: html.c,v 1.240 2018/10/02 12:33:37 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.141 kristaps 120: static void print_metaf(struct html *, enum mandoc_esc);
1.82 kristaps 121:
1.156 schwarze 122:
1.180 schwarze 123: void *
1.191 schwarze 124: html_alloc(const struct manoutput *outopts)
1.10 kristaps 125: {
1.30 kristaps 126: struct html *h;
127:
1.128 kristaps 128: h = mandoc_calloc(1, sizeof(struct html));
1.10 kristaps 129:
1.204 schwarze 130: h->tag = NULL;
1.186 schwarze 131: h->style = outopts->style;
1.240 schwarze 132: if ((h->base_man1 = outopts->man) == NULL)
133: h->base_man2 = NULL;
134: else if ((h->base_man2 = strchr(h->base_man1, ';')) != NULL)
135: *h->base_man2++ = '\0';
1.186 schwarze 136: h->base_includes = outopts->includes;
137: if (outopts->fragment)
138: h->oflags |= HTML_FRAGMENT;
1.241 ! schwarze 139: if (outopts->toc)
! 140: h->oflags |= HTML_TOC;
1.43 kristaps 141:
1.229 schwarze 142: mandoc_ohash_init(&id_unique, 4, 0);
143:
1.188 schwarze 144: return h;
1.29 kristaps 145: }
1.10 kristaps 146:
1.29 kristaps 147: void
148: html_free(void *p)
149: {
1.30 kristaps 150: struct tag *tag;
151: struct html *h;
1.229 schwarze 152: char *cp;
153: unsigned int slot;
1.30 kristaps 154:
155: h = (struct html *)p;
1.204 schwarze 156: while ((tag = h->tag) != NULL) {
157: h->tag = tag->next;
1.30 kristaps 158: free(tag);
159: }
1.229 schwarze 160: free(h);
1.53 kristaps 161:
1.229 schwarze 162: cp = ohash_first(&id_unique, &slot);
163: while (cp != NULL) {
164: free(cp);
165: cp = ohash_next(&id_unique, &slot);
166: }
167: ohash_delete(&id_unique);
1.10 kristaps 168: }
1.2 kristaps 169:
1.51 kristaps 170: void
1.29 kristaps 171: print_gen_head(struct html *h)
172: {
1.165 kristaps 173: struct tag *t;
1.41 kristaps 174:
1.194 schwarze 175: print_otag(h, TAG_META, "?", "charset", "utf-8");
1.222 schwarze 176: if (h->style != NULL) {
177: print_otag(h, TAG_LINK, "?h??", "rel", "stylesheet",
178: h->style, "type", "text/css", "media", "all");
179: return;
180: }
1.165 kristaps 181:
1.168 kristaps 182: /*
1.222 schwarze 183: * Print a minimal embedded style sheet.
1.168 kristaps 184: */
1.196 schwarze 185:
1.194 schwarze 186: t = print_otag(h, TAG_STYLE, "");
1.196 schwarze 187: print_text(h, "table.head, table.foot { width: 100%; }");
1.197 schwarze 188: print_endline(h);
1.196 schwarze 189: print_text(h, "td.head-rtitle, td.foot-os { text-align: right; }");
1.197 schwarze 190: print_endline(h);
1.196 schwarze 191: print_text(h, "td.head-vol { text-align: center; }");
1.197 schwarze 192: print_endline(h);
1.198 schwarze 193: print_text(h, "div.Pp { margin: 1ex 0ex; }");
1.225 schwarze 194: print_endline(h);
195: print_text(h, "div.Nd, div.Bf, div.Op { display: inline; }");
1.226 schwarze 196: print_endline(h);
1.227 schwarze 197: print_text(h, "span.Pa, span.Ad { font-style: italic; }");
1.228 schwarze 198: print_endline(h);
199: print_text(h, "span.Ms { font-weight: bold; }");
1.224 schwarze 200: print_endline(h);
201: print_text(h, "dl.Bl-diag ");
202: print_byte(h, '>');
203: print_text(h, " dt { font-weight: bold; }");
1.223 schwarze 204: print_endline(h);
205: print_text(h, "code.Nm, code.Fl, code.Cm, code.Ic, "
206: "code.In, code.Fd, code.Fn,");
207: print_endline(h);
208: print_text(h, "code.Cd { font-weight: bold; "
209: "font-family: inherit; }");
1.165 kristaps 210: print_tagq(h, t);
1.4 kristaps 211: }
212:
1.126 schwarze 213: static void
1.132 kristaps 214: print_metaf(struct html *h, enum mandoc_esc deco)
1.88 kristaps 215: {
1.90 kristaps 216: enum htmlfont font;
1.88 kristaps 217:
218: switch (deco) {
1.156 schwarze 219: case ESCAPE_FONTPREV:
1.90 kristaps 220: font = h->metal;
1.88 kristaps 221: break;
1.156 schwarze 222: case ESCAPE_FONTITALIC:
1.90 kristaps 223: font = HTMLFONT_ITALIC;
224: break;
1.156 schwarze 225: case ESCAPE_FONTBOLD:
1.90 kristaps 226: font = HTMLFONT_BOLD;
1.88 kristaps 227: break;
1.156 schwarze 228: case ESCAPE_FONTBI:
1.152 schwarze 229: font = HTMLFONT_BI;
230: break;
1.156 schwarze 231: case ESCAPE_FONT:
232: case ESCAPE_FONTROMAN:
1.90 kristaps 233: font = HTMLFONT_NONE;
1.88 kristaps 234: break;
235: default:
236: abort();
237: }
238:
1.122 kristaps 239: if (h->metaf) {
240: print_tagq(h, h->metaf);
241: h->metaf = NULL;
242: }
243:
244: h->metal = h->metac;
245: h->metac = font;
246:
1.152 schwarze 247: switch (font) {
1.156 schwarze 248: case HTMLFONT_ITALIC:
1.194 schwarze 249: h->metaf = print_otag(h, TAG_I, "");
1.152 schwarze 250: break;
1.156 schwarze 251: case HTMLFONT_BOLD:
1.194 schwarze 252: h->metaf = print_otag(h, TAG_B, "");
1.152 schwarze 253: break;
1.156 schwarze 254: case HTMLFONT_BI:
1.194 schwarze 255: h->metaf = print_otag(h, TAG_B, "");
256: print_otag(h, TAG_I, "");
1.152 schwarze 257: break;
258: default:
259: break;
260: }
1.210 schwarze 261: }
262:
263: char *
1.229 schwarze 264: html_make_id(const struct roff_node *n, int unique)
1.210 schwarze 265: {
266: const struct roff_node *nch;
1.229 schwarze 267: char *buf, *bufs, *cp;
268: unsigned int slot;
269: int suffix;
1.210 schwarze 270:
271: for (nch = n->child; nch != NULL; nch = nch->next)
272: if (nch->type != ROFFT_TEXT)
273: return NULL;
274:
275: buf = NULL;
276: deroff(&buf, n);
1.220 schwarze 277: if (buf == NULL)
278: return NULL;
1.210 schwarze 279:
1.230 schwarze 280: /*
281: * In ID attributes, only use ASCII characters that are
282: * permitted in URL-fragment strings according to the
283: * explicit list at:
284: * https://url.spec.whatwg.org/#url-fragment-string
285: */
1.210 schwarze 286:
287: for (cp = buf; *cp != '\0'; cp++)
1.230 schwarze 288: if (isalnum((unsigned char)*cp) == 0 &&
289: strchr("!$&'()*+,-./:;=?@_~", *cp) == NULL)
1.210 schwarze 290: *cp = '_';
291:
1.229 schwarze 292: if (unique == 0)
293: return buf;
294:
295: /* Avoid duplicate HTML id= attributes. */
296:
297: bufs = NULL;
298: suffix = 1;
299: slot = ohash_qlookup(&id_unique, buf);
300: cp = ohash_find(&id_unique, slot);
301: if (cp != NULL) {
302: while (cp != NULL) {
303: free(bufs);
304: if (++suffix > 127) {
305: free(buf);
306: return NULL;
307: }
308: mandoc_asprintf(&bufs, "%s_%d", buf, suffix);
309: slot = ohash_qlookup(&id_unique, bufs);
310: cp = ohash_find(&id_unique, slot);
311: }
312: free(buf);
313: buf = bufs;
314: }
315: ohash_insert(&id_unique, slot, buf);
1.210 schwarze 316: return buf;
1.88 kristaps 317: }
318:
1.85 kristaps 319: static int
1.197 schwarze 320: print_escape(struct html *h, char c)
1.159 schwarze 321: {
322:
323: switch (c) {
324: case '<':
1.197 schwarze 325: print_word(h, "<");
1.159 schwarze 326: break;
327: case '>':
1.197 schwarze 328: print_word(h, ">");
1.159 schwarze 329: break;
330: case '&':
1.197 schwarze 331: print_word(h, "&");
1.159 schwarze 332: break;
333: case '"':
1.197 schwarze 334: print_word(h, """);
1.159 schwarze 335: break;
336: case ASCII_NBRSP:
1.197 schwarze 337: print_word(h, " ");
1.159 schwarze 338: break;
339: case ASCII_HYPH:
1.197 schwarze 340: print_byte(h, '-');
1.189 schwarze 341: break;
1.159 schwarze 342: case ASCII_BREAK:
343: break;
344: default:
1.188 schwarze 345: return 0;
1.159 schwarze 346: }
1.188 schwarze 347: return 1;
1.159 schwarze 348: }
349:
350: static int
1.195 schwarze 351: print_encode(struct html *h, const char *p, const char *pend, int norecurse)
1.29 kristaps 352: {
1.197 schwarze 353: char numbuf[16];
1.214 schwarze 354: struct tag *t;
355: const char *seq;
1.77 kristaps 356: size_t sz;
1.214 schwarze 357: int c, len, breakline, nospace;
1.132 kristaps 358: enum mandoc_esc esc;
1.214 schwarze 359: static const char rejs[10] = { ' ', '\\', '<', '>', '&', '"',
1.154 schwarze 360: ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' };
1.14 kristaps 361:
1.195 schwarze 362: if (pend == NULL)
363: pend = strchr(p, '\0');
364:
1.214 schwarze 365: breakline = 0;
1.85 kristaps 366: nospace = 0;
367:
1.195 schwarze 368: while (p < pend) {
1.151 schwarze 369: if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
370: h->flags &= ~HTML_SKIPCHAR;
371: p++;
372: continue;
373: }
374:
1.197 schwarze 375: for (sz = strcspn(p, rejs); sz-- && p < pend; p++)
1.214 schwarze 376: print_byte(h, *p);
377:
378: if (breakline &&
379: (p >= pend || *p == ' ' || *p == ASCII_NBRSP)) {
380: t = print_otag(h, TAG_DIV, "");
381: print_text(h, "\\~");
382: print_tagq(h, t);
383: breakline = 0;
384: while (p < pend && (*p == ' ' || *p == ASCII_NBRSP))
385: p++;
386: continue;
387: }
1.77 kristaps 388:
1.195 schwarze 389: if (p >= pend)
1.132 kristaps 390: break;
391:
1.214 schwarze 392: if (*p == ' ') {
393: print_endword(h);
394: p++;
395: continue;
396: }
397:
1.197 schwarze 398: if (print_escape(h, *p++))
1.154 schwarze 399: continue;
1.77 kristaps 400:
1.132 kristaps 401: esc = mandoc_escape(&p, &seq, &len);
402: if (ESCAPE_ERROR == esc)
403: break;
1.82 kristaps 404:
1.132 kristaps 405: switch (esc) {
1.156 schwarze 406: case ESCAPE_FONT:
407: case ESCAPE_FONTPREV:
408: case ESCAPE_FONTBOLD:
409: case ESCAPE_FONTITALIC:
410: case ESCAPE_FONTBI:
411: case ESCAPE_FONTROMAN:
1.151 schwarze 412: if (0 == norecurse)
413: print_metaf(h, esc);
414: continue;
1.156 schwarze 415: case ESCAPE_SKIPCHAR:
1.151 schwarze 416: h->flags |= HTML_SKIPCHAR;
417: continue;
418: default:
419: break;
420: }
421:
422: if (h->flags & HTML_SKIPCHAR) {
423: h->flags &= ~HTML_SKIPCHAR;
424: continue;
425: }
426:
427: switch (esc) {
1.156 schwarze 428: case ESCAPE_UNICODE:
1.159 schwarze 429: /* Skip past "u" header. */
1.144 kristaps 430: c = mchars_num2uc(seq + 1, len - 1);
431: break;
1.156 schwarze 432: case ESCAPE_NUMBERED:
1.141 kristaps 433: c = mchars_num2char(seq, len);
1.181 schwarze 434: if (c < 0)
435: continue;
1.82 kristaps 436: break;
1.156 schwarze 437: case ESCAPE_SPECIAL:
1.191 schwarze 438: c = mchars_spec2cp(seq, len);
1.181 schwarze 439: if (c <= 0)
440: continue;
1.132 kristaps 441: break;
1.239 schwarze 442: case ESCAPE_DEVICE:
443: print_word(h, "html");
444: continue;
1.214 schwarze 445: case ESCAPE_BREAK:
446: breakline = 1;
447: continue;
1.156 schwarze 448: case ESCAPE_NOSPACE:
1.132 kristaps 449: if ('\0' == *p)
450: nospace = 1;
1.179 schwarze 451: continue;
1.185 schwarze 452: case ESCAPE_OVERSTRIKE:
453: if (len == 0)
454: continue;
455: c = seq[len - 1];
456: break;
1.82 kristaps 457: default:
1.179 schwarze 458: continue;
1.82 kristaps 459: }
1.181 schwarze 460: if ((c < 0x20 && c != 0x09) ||
461: (c > 0x7E && c < 0xA0))
1.179 schwarze 462: c = 0xFFFD;
1.197 schwarze 463: if (c > 0x7E) {
1.216 schwarze 464: (void)snprintf(numbuf, sizeof(numbuf), "&#x%.4X;", c);
1.197 schwarze 465: print_word(h, numbuf);
466: } else if (print_escape(h, c) == 0)
467: print_byte(h, c);
1.32 kristaps 468: }
1.85 kristaps 469:
1.188 schwarze 470: return nospace;
1.14 kristaps 471: }
472:
1.94 kristaps 473: static void
1.195 schwarze 474: print_href(struct html *h, const char *name, const char *sec, int man)
1.94 kristaps 475: {
1.240 schwarze 476: struct stat sb;
1.195 schwarze 477: const char *p, *pp;
1.240 schwarze 478: char *filename;
479:
480: if (man) {
481: pp = h->base_man1;
482: if (h->base_man2 != NULL) {
483: mandoc_asprintf(&filename, "%s.%s", name, sec);
484: if (stat(filename, &sb) == -1)
485: pp = h->base_man2;
486: free(filename);
487: }
488: } else
489: pp = h->base_includes;
1.195 schwarze 490:
491: while ((p = strchr(pp, '%')) != NULL) {
492: print_encode(h, pp, p, 1);
493: if (man && p[1] == 'S') {
494: if (sec == NULL)
1.197 schwarze 495: print_byte(h, '1');
1.195 schwarze 496: else
497: print_encode(h, sec, NULL, 1);
498: } else if ((man && p[1] == 'N') ||
499: (man == 0 && p[1] == 'I'))
500: print_encode(h, name, NULL, 1);
501: else
502: print_encode(h, p, p + 2, 1);
503: pp = p + 2;
504: }
505: if (*pp != '\0')
506: print_encode(h, pp, NULL, 1);
1.94 kristaps 507: }
508:
1.51 kristaps 509: struct tag *
1.194 schwarze 510: print_otag(struct html *h, enum htmltag tag, const char *fmt, ...)
1.14 kristaps 511: {
1.194 schwarze 512: va_list ap;
1.30 kristaps 513: struct tag *t;
1.195 schwarze 514: const char *attr;
1.203 schwarze 515: char *arg1, *arg2;
1.238 schwarze 516: int tflags;
1.196 schwarze 517:
518: tflags = htmltags[tag].flags;
1.30 kristaps 519:
1.204 schwarze 520: /* Push this tag onto the stack of open scopes. */
1.94 kristaps 521:
1.196 schwarze 522: if ((tflags & HTML_NOSTACK) == 0) {
1.128 kristaps 523: t = mandoc_malloc(sizeof(struct tag));
1.30 kristaps 524: t->tag = tag;
1.204 schwarze 525: t->next = h->tag;
526: h->tag = t;
1.30 kristaps 527: } else
528: t = NULL;
1.29 kristaps 529:
1.196 schwarze 530: if (tflags & HTML_NLBEFORE)
1.197 schwarze 531: print_endline(h);
532: if (h->col == 0)
533: print_indent(h);
1.196 schwarze 534: else if ((h->flags & HTML_NOSPACE) == 0) {
535: if (h->flags & HTML_KEEP)
1.216 schwarze 536: print_word(h, " ");
1.196 schwarze 537: else {
538: if (h->flags & HTML_PREKEEP)
539: h->flags |= HTML_KEEP;
1.197 schwarze 540: print_endword(h);
1.105 kristaps 541: }
1.196 schwarze 542: }
1.29 kristaps 543:
1.109 kristaps 544: if ( ! (h->flags & HTML_NONOSPACE))
545: h->flags &= ~HTML_NOSPACE;
1.110 kristaps 546: else
547: h->flags |= HTML_NOSPACE;
1.109 kristaps 548:
1.94 kristaps 549: /* Print out the tag name and attributes. */
550:
1.197 schwarze 551: print_byte(h, '<');
552: print_word(h, htmltags[tag].name);
1.194 schwarze 553:
554: va_start(ap, fmt);
555:
556: while (*fmt != '\0') {
1.203 schwarze 557:
1.238 schwarze 558: /* Parse attributes and arguments. */
1.203 schwarze 559:
560: arg1 = va_arg(ap, char *);
1.238 schwarze 561: arg2 = NULL;
1.194 schwarze 562: switch (*fmt++) {
563: case 'c':
1.195 schwarze 564: attr = "class";
1.194 schwarze 565: break;
566: case 'h':
1.195 schwarze 567: attr = "href";
1.194 schwarze 568: break;
569: case 'i':
1.195 schwarze 570: attr = "id";
1.194 schwarze 571: break;
1.238 schwarze 572: case 's':
573: attr = "style";
574: arg2 = va_arg(ap, char *);
575: break;
1.194 schwarze 576: case '?':
1.203 schwarze 577: attr = arg1;
578: arg1 = va_arg(ap, char *);
1.194 schwarze 579: break;
580: default:
581: abort();
582: }
1.203 schwarze 583: if (*fmt == 'M')
584: arg2 = va_arg(ap, char *);
585: if (arg1 == NULL)
586: continue;
587:
1.238 schwarze 588: /* Print the attributes. */
1.203 schwarze 589:
1.197 schwarze 590: print_byte(h, ' ');
591: print_word(h, attr);
592: print_byte(h, '=');
593: print_byte(h, '"');
1.195 schwarze 594: switch (*fmt) {
1.208 schwarze 595: case 'I':
596: print_href(h, arg1, NULL, 0);
597: fmt++;
598: break;
1.195 schwarze 599: case 'M':
1.203 schwarze 600: print_href(h, arg1, arg2, 1);
1.195 schwarze 601: fmt++;
602: break;
1.208 schwarze 603: case 'R':
604: print_byte(h, '#');
605: print_encode(h, arg1, NULL, 1);
1.195 schwarze 606: fmt++;
607: break;
1.208 schwarze 608: case 'T':
609: print_encode(h, arg1, NULL, 1);
610: print_word(h, "\" title=\"");
611: print_encode(h, arg1, NULL, 1);
1.195 schwarze 612: fmt++;
1.208 schwarze 613: break;
1.195 schwarze 614: default:
1.238 schwarze 615: if (arg2 == NULL)
616: print_encode(h, arg1, NULL, 1);
617: else {
618: print_word(h, arg1);
619: print_byte(h, ':');
620: print_byte(h, ' ');
621: print_word(h, arg2);
622: print_byte(h, ';');
623: }
1.195 schwarze 624: break;
625: }
1.197 schwarze 626: print_byte(h, '"');
1.194 schwarze 627: }
628: va_end(ap);
1.94 kristaps 629:
1.172 kristaps 630: /* Accommodate for "well-formed" singleton escaping. */
1.94 kristaps 631:
1.93 kristaps 632: if (HTML_AUTOCLOSE & htmltags[tag].flags)
1.197 schwarze 633: print_byte(h, '/');
1.93 kristaps 634:
1.197 schwarze 635: print_byte(h, '>');
1.14 kristaps 636:
1.196 schwarze 637: if (tflags & HTML_NLBEGIN)
1.197 schwarze 638: print_endline(h);
1.196 schwarze 639: else
640: h->flags |= HTML_NOSPACE;
1.117 kristaps 641:
1.196 schwarze 642: if (tflags & HTML_INDENT)
643: h->indent++;
644: if (tflags & HTML_NOINDENT)
645: h->noindent++;
1.117 kristaps 646:
1.188 schwarze 647: return t;
1.14 kristaps 648: }
649:
1.29 kristaps 650: static void
1.184 schwarze 651: print_ctag(struct html *h, struct tag *tag)
1.14 kristaps 652: {
1.196 schwarze 653: int tflags;
1.156 schwarze 654:
1.184 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.196 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.197 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.196 schwarze 677: if (tflags & HTML_NLAFTER)
1.197 schwarze 678: print_endline(h);
1.184 schwarze 679:
1.204 schwarze 680: h->tag = tag->next;
1.184 schwarze 681: free(tag);
1.14 kristaps 682: }
683:
1.51 kristaps 684: void
1.93 kristaps 685: print_gen_decls(struct html *h)
1.1 kristaps 686: {
1.197 schwarze 687: print_word(h, "<!DOCTYPE html>");
688: print_endline(h);
1.221 schwarze 689: }
690:
691: void
692: print_gen_comment(struct html *h, struct roff_node *n)
693: {
694: int wantblank;
695:
696: print_word(h, "<!-- This is an automatically generated file."
697: " Do not edit.");
698: h->indent = 1;
699: wantblank = 0;
700: while (n != NULL && n->type == ROFFT_COMMENT) {
701: if (strstr(n->string, "-->") == NULL &&
702: (wantblank || *n->string != '\0')) {
703: print_endline(h);
704: print_indent(h);
705: print_word(h, n->string);
706: wantblank = *n->string != '\0';
707: }
708: n = n->next;
709: }
710: if (wantblank)
711: print_endline(h);
712: print_word(h, " -->");
713: print_endline(h);
714: h->indent = 0;
1.1 kristaps 715: }
716:
1.51 kristaps 717: void
1.104 kristaps 718: print_text(struct html *h, const char *word)
1.1 kristaps 719: {
1.197 schwarze 720: if (h->col && (h->flags & HTML_NOSPACE) == 0) {
1.105 kristaps 721: if ( ! (HTML_KEEP & h->flags)) {
722: if (HTML_PREKEEP & h->flags)
723: h->flags |= HTML_KEEP;
1.197 schwarze 724: print_endword(h);
1.105 kristaps 725: } else
1.216 schwarze 726: print_word(h, " ");
1.105 kristaps 727: }
1.30 kristaps 728:
1.122 kristaps 729: assert(NULL == h->metaf);
1.152 schwarze 730: switch (h->metac) {
1.156 schwarze 731: case HTMLFONT_ITALIC:
1.194 schwarze 732: h->metaf = print_otag(h, TAG_I, "");
1.152 schwarze 733: break;
1.156 schwarze 734: case HTMLFONT_BOLD:
1.194 schwarze 735: h->metaf = print_otag(h, TAG_B, "");
1.152 schwarze 736: break;
1.156 schwarze 737: case HTMLFONT_BI:
1.194 schwarze 738: h->metaf = print_otag(h, TAG_B, "");
739: print_otag(h, TAG_I, "");
1.152 schwarze 740: break;
741: default:
1.197 schwarze 742: print_indent(h);
1.152 schwarze 743: break;
744: }
1.122 kristaps 745:
1.104 kristaps 746: assert(word);
1.195 schwarze 747: if ( ! print_encode(h, word, NULL, 0)) {
1.109 kristaps 748: if ( ! (h->flags & HTML_NONOSPACE))
749: h->flags &= ~HTML_NOSPACE;
1.183 schwarze 750: h->flags &= ~HTML_NONEWLINE;
1.149 kristaps 751: } else
1.183 schwarze 752: h->flags |= HTML_NOSPACE | HTML_NONEWLINE;
1.122 kristaps 753:
754: if (h->metaf) {
755: print_tagq(h, h->metaf);
756: h->metaf = NULL;
757: }
1.113 schwarze 758:
759: h->flags &= ~HTML_IGNDELIM;
1.1 kristaps 760: }
1.30 kristaps 761:
1.51 kristaps 762: void
1.30 kristaps 763: print_tagq(struct html *h, const struct tag *until)
764: {
765: struct tag *tag;
766:
1.204 schwarze 767: while ((tag = h->tag) != NULL) {
1.184 schwarze 768: print_ctag(h, tag);
1.30 kristaps 769: if (until && tag == until)
770: return;
771: }
772: }
773:
1.51 kristaps 774: void
1.30 kristaps 775: print_stagq(struct html *h, const struct tag *suntil)
776: {
777: struct tag *tag;
778:
1.204 schwarze 779: while ((tag = h->tag) != NULL) {
1.30 kristaps 780: if (suntil && tag == suntil)
781: return;
1.184 schwarze 782: print_ctag(h, tag);
1.30 kristaps 783: }
784: }
1.171 kristaps 785:
786: void
787: print_paragraph(struct html *h)
788: {
789: struct tag *t;
790:
1.198 schwarze 791: t = print_otag(h, TAG_DIV, "c", "Pp");
1.171 kristaps 792: print_tagq(h, t);
793: }
794:
1.197 schwarze 795:
796: /***********************************************************************
797: * Low level output functions.
798: * They implement line breaking using a short static buffer.
799: ***********************************************************************/
800:
801: /*
802: * Buffer one HTML output byte.
803: * If the buffer is full, flush and deactivate it and start a new line.
804: * If the buffer is inactive, print directly.
805: */
806: static void
807: print_byte(struct html *h, char c)
808: {
809: if ((h->flags & HTML_BUFFER) == 0) {
810: putchar(c);
811: h->col++;
812: return;
813: }
814:
815: if (h->col + h->bufcol < sizeof(h->buf)) {
816: h->buf[h->bufcol++] = c;
817: return;
818: }
819:
820: putchar('\n');
821: h->col = 0;
822: print_indent(h);
823: putchar(' ');
824: putchar(' ');
825: fwrite(h->buf, h->bufcol, 1, stdout);
826: putchar(c);
827: h->col = (h->indent + 1) * 2 + h->bufcol + 1;
828: h->bufcol = 0;
829: h->flags &= ~HTML_BUFFER;
830: }
831:
1.196 schwarze 832: /*
833: * If something was printed on the current output line, end it.
1.197 schwarze 834: * Not to be called right after print_indent().
1.196 schwarze 835: */
1.202 schwarze 836: void
1.197 schwarze 837: print_endline(struct html *h)
1.196 schwarze 838: {
1.197 schwarze 839: if (h->col == 0)
1.196 schwarze 840: return;
841:
1.197 schwarze 842: if (h->bufcol) {
843: putchar(' ');
844: fwrite(h->buf, h->bufcol, 1, stdout);
845: h->bufcol = 0;
846: }
1.196 schwarze 847: putchar('\n');
1.197 schwarze 848: h->col = 0;
849: h->flags |= HTML_NOSPACE;
850: h->flags &= ~HTML_BUFFER;
851: }
852:
853: /*
854: * Flush the HTML output buffer.
855: * If it is inactive, activate it.
856: */
857: static void
858: print_endword(struct html *h)
859: {
860: if (h->noindent) {
861: print_byte(h, ' ');
862: return;
863: }
864:
865: if ((h->flags & HTML_BUFFER) == 0) {
866: h->col++;
867: h->flags |= HTML_BUFFER;
868: } else if (h->bufcol) {
869: putchar(' ');
870: fwrite(h->buf, h->bufcol, 1, stdout);
871: h->col += h->bufcol + 1;
872: }
873: h->bufcol = 0;
1.196 schwarze 874: }
875:
876: /*
877: * If at the beginning of a new output line,
878: * perform indentation and mark the line as containing output.
879: * Make sure to really produce some output right afterwards,
880: * but do not use print_otag() for producing it.
881: */
882: static void
1.197 schwarze 883: print_indent(struct html *h)
1.196 schwarze 884: {
1.197 schwarze 885: size_t i;
1.196 schwarze 886:
1.197 schwarze 887: if (h->col)
1.196 schwarze 888: return;
889:
1.197 schwarze 890: if (h->noindent == 0) {
891: h->col = h->indent * 2;
892: for (i = 0; i < h->col; i++)
1.196 schwarze 893: putchar(' ');
1.197 schwarze 894: }
895: h->flags &= ~HTML_NOSPACE;
896: }
897:
898: /*
899: * Print or buffer some characters
900: * depending on the current HTML output buffer state.
901: */
902: static void
903: print_word(struct html *h, const char *cp)
904: {
905: while (*cp != '\0')
906: print_byte(h, *cp++);
1.68 kristaps 907: }
CVSweb