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