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