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