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