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