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