1 /* $Id: html.c,v 1.275 2021/09/09 14:47:24 schwarze Exp $ */
3 * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011-2015, 2017-2021 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
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.
18 * Common functions for mandoc(1) HTML formatters.
19 * For use by individual formatters and by the main program.
23 #include <sys/types.h>
36 #include "mandoc_aux.h"
37 #include "mandoc_ohash.h"
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. */
55 #define HTML_NLAROUND (HTML_NLBEFORE | HTML_NLAFTER)
56 #define HTML_NLINSIDE (HTML_NLBEGIN | HTML_NLEND)
57 #define HTML_NLALL (HTML_NLAROUND | HTML_NLINSIDE)
58 #define HTML_INDENT (1 << 7) /* Indent content by two spaces. */
59 #define HTML_NOINDENT (1 << 8) /* Exception: never indent content. */
62 static const struct htmldata htmltags[TAG_MAX] = {
64 {"head", HTML_NLALL | HTML_INDENT},
65 {"meta", HTML_NOSTACK | HTML_NLALL},
66 {"link", HTML_NOSTACK | HTML_NLALL},
67 {"style", HTML_NLALL | HTML_INDENT},
68 {"title", HTML_NLAROUND},
70 {"div", HTML_NLAROUND},
71 {"section", HTML_NLALL},
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},
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_NLAROUND | 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},
94 {"hr", HTML_INPHRASE | HTML_NOSTACK},
95 {"mark", HTML_INPHRASE },
96 {"math", HTML_INPHRASE | HTML_NLALL | HTML_INDENT},
115 /* Avoid duplicate HTML id= attributes. */
118 int ord; /* Ordinal number of the latest occurrence. */
119 char id[]; /* The id= attribute without any ordinal suffix. */
121 static struct ohash id_unique;
123 static void html_reset_internal(struct html *);
124 static void print_byte(struct html *, char);
125 static void print_endword(struct html *);
126 static void print_indent(struct html *);
127 static void print_word(struct html *, const char *);
129 static void print_ctag(struct html *, struct tag *);
130 static int print_escape(struct html *, char);
131 static int print_encode(struct html *, const char *, const char *, int);
132 static void print_href(struct html *, const char *, const char *, int);
133 static void print_metaf(struct html *);
137 html_alloc(const struct manoutput *outopts)
141 h = mandoc_calloc(1, sizeof(struct html));
144 h->metac = h->metal = ESCAPE_FONTROMAN;
145 h->style = outopts->style;
146 if ((h->base_man1 = outopts->man) == NULL)
148 else if ((h->base_man2 = strchr(h->base_man1, ';')) != NULL)
149 *h->base_man2++ = '\0';
150 h->base_includes = outopts->includes;
151 if (outopts->fragment)
152 h->oflags |= HTML_FRAGMENT;
154 h->oflags |= HTML_TOC;
156 mandoc_ohash_init(&id_unique, 4, offsetof(struct id_entry, id));
162 html_reset_internal(struct html *h)
165 struct id_entry *entry;
168 while ((tag = h->tag) != NULL) {
172 entry = ohash_first(&id_unique, &slot);
173 while (entry != NULL) {
175 entry = ohash_next(&id_unique, &slot);
177 ohash_delete(&id_unique);
183 html_reset_internal(p);
184 mandoc_ohash_init(&id_unique, 4, offsetof(struct id_entry, id));
190 html_reset_internal(p);
195 print_gen_head(struct html *h)
199 print_otag(h, TAG_META, "?", "charset", "utf-8");
200 print_otag(h, TAG_META, "??", "name", "viewport",
201 "content", "width=device-width, initial-scale=1.0");
202 if (h->style != NULL) {
203 print_otag(h, TAG_LINK, "?h??", "rel", "stylesheet",
204 h->style, "type", "text/css", "media", "all");
209 * Print a minimal embedded style sheet.
212 t = print_otag(h, TAG_STYLE, "");
213 print_text(h, "table.head, table.foot { width: 100%; }");
215 print_text(h, "td.head-rtitle, td.foot-os { text-align: right; }");
217 print_text(h, "td.head-vol { text-align: center; }");
219 print_text(h, ".Nd, .Bf, .Op { display: inline; }");
221 print_text(h, ".Pa, .Ad { font-style: italic; }");
223 print_text(h, ".Ms { font-weight: bold; }");
225 print_text(h, ".Bl-diag ");
227 print_text(h, " dt { font-weight: bold; }");
229 print_text(h, "code.Nm, .Fl, .Cm, .Ic, code.In, .Fd, .Fn, .Cd "
230 "{ font-weight: bold; font-family: inherit; }");
235 html_setfont(struct html *h, enum mandoc_esc font)
238 case ESCAPE_FONTPREV:
241 case ESCAPE_FONTITALIC:
242 case ESCAPE_FONTBOLD:
244 case ESCAPE_FONTROMAN:
250 font = ESCAPE_FONTROMAN;
261 print_metaf(struct html *h)
264 print_tagq(h, h->metaf);
268 case ESCAPE_FONTITALIC:
269 h->metaf = print_otag(h, TAG_I, "");
271 case ESCAPE_FONTBOLD:
272 h->metaf = print_otag(h, TAG_B, "");
275 h->metaf = print_otag(h, TAG_B, "");
276 print_otag(h, TAG_I, "");
279 h->metaf = print_otag(h, TAG_SPAN, "c", "Li");
282 h->metaf = print_otag(h, TAG_SPAN, "c", "Li");
283 print_otag(h, TAG_B, "");
286 h->metaf = print_otag(h, TAG_SPAN, "c", "Li");
287 print_otag(h, TAG_I, "");
295 html_close_paragraph(struct html *h)
297 struct tag *this, *next;
303 flags = htmltags[this->tag].flags;
304 if (flags & (HTML_INPHRASE | HTML_TOPHRASE))
306 if ((flags & HTML_INPHRASE) == 0)
313 * ROFF_nf switches to no-fill mode, ROFF_fi to fill mode.
314 * TOKEN_NONE does not switch. The old mode is returned.
317 html_fillmode(struct html *h, enum roff_tok want)
322 for (t = h->tag; t != NULL; t = t->next)
323 if (t->tag == TAG_PRE)
326 had = t == NULL ? ROFF_fi : ROFF_nf;
334 html_close_paragraph(h);
335 print_otag(h, TAG_PRE, "");
347 * Allocate a string to be used for the "id=" attribute of an HTML
348 * element and/or as a segment identifier for a URI in an <a> element.
349 * The function may fail and return NULL if the node lacks text data
350 * to create the attribute from.
351 * The caller is responsible for free(3)ing the returned string.
353 * If the "unique" argument is non-zero, the "id_unique" ohash table
354 * is used for de-duplication. If the "unique" argument is 1,
355 * it is the first time the function is called for this tag and
356 * location, so if an ordinal suffix is needed, it is incremented.
357 * If the "unique" argument is 2, it is the second time the function
358 * is called for this tag and location, so the ordinal suffix
362 html_make_id(const struct roff_node *n, int unique)
364 const struct roff_node *nch;
365 struct id_entry *entry;
371 buf = mandoc_strdup(n->tag);
379 for (nch = n->child; nch != NULL; nch = nch->next)
380 if (nch->type != ROFFT_TEXT)
388 if (n->child == NULL || n->child->type != ROFFT_TEXT)
390 buf = mandoc_strdup(n->child->string);
396 * In ID attributes, only use ASCII characters that are
397 * permitted in URL-fragment strings according to the
399 * https://url.spec.whatwg.org/#url-fragment-string
400 * In addition, reserve '~' for ordinal suffixes.
403 for (cp = buf; *cp != '\0'; cp++)
404 if (isalnum((unsigned char)*cp) == 0 &&
405 strchr("!$&'()*+,-./:;=?@_", *cp) == NULL)
411 /* Avoid duplicate HTML id= attributes. */
413 slot = ohash_qlookup(&id_unique, buf);
414 if ((entry = ohash_find(&id_unique, slot)) == NULL) {
415 len = strlen(buf) + 1;
416 entry = mandoc_malloc(sizeof(*entry) + len);
418 memcpy(entry->id, buf, len);
419 ohash_insert(&id_unique, slot, entry);
420 } else if (unique == 1)
423 if (entry->ord > 1) {
425 mandoc_asprintf(&buf, "%s~%d", cp, entry->ord);
432 print_escape(struct html *h, char c)
437 print_word(h, "<");
440 print_word(h, ">");
443 print_word(h, "&");
446 print_word(h, """);
449 print_word(h, " ");
463 print_encode(struct html *h, const char *p, const char *pend, int norecurse)
468 int c, len, breakline, nospace;
470 static const char rejs[10] = { ' ', '\\', '<', '>', '&', '"',
471 ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' };
474 pend = strchr(p, '\0');
480 if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
481 h->flags &= ~HTML_SKIPCHAR;
486 for (sz = strcspn(p, rejs); sz-- && p < pend; p++)
490 (p >= pend || *p == ' ' || *p == ASCII_NBRSP)) {
491 print_otag(h, TAG_BR, "");
493 while (p < pend && (*p == ' ' || *p == ASCII_NBRSP))
507 if (print_escape(h, *p++))
510 esc = mandoc_escape(&p, &seq, &len);
513 case ESCAPE_FONTPREV:
514 case ESCAPE_FONTBOLD:
515 case ESCAPE_FONTITALIC:
517 case ESCAPE_FONTROMAN:
521 if (0 == norecurse) {
522 h->flags |= HTML_NOSPACE;
523 if (html_setfont(h, esc))
525 h->flags &= ~HTML_NOSPACE;
528 case ESCAPE_SKIPCHAR:
529 h->flags |= HTML_SKIPCHAR;
537 if (h->flags & HTML_SKIPCHAR) {
538 h->flags &= ~HTML_SKIPCHAR;
544 /* Skip past "u" header. */
545 c = mchars_num2uc(seq + 1, len - 1);
547 case ESCAPE_NUMBERED:
548 c = mchars_num2char(seq, len);
553 c = mchars_spec2cp(seq, len);
561 print_word(h, "html");
570 case ESCAPE_OVERSTRIKE:
578 if ((c < 0x20 && c != 0x09) ||
579 (c > 0x7E && c < 0xA0))
582 (void)snprintf(numbuf, sizeof(numbuf), "&#x%.4X;", c);
583 print_word(h, numbuf);
584 } else if (print_escape(h, c) == 0)
592 print_href(struct html *h, const char *name, const char *sec, int man)
600 if (h->base_man2 != NULL) {
601 mandoc_asprintf(&filename, "%s.%s", name, sec);
602 if (stat(filename, &sb) == -1)
607 pp = h->base_includes;
609 while ((p = strchr(pp, '%')) != NULL) {
610 print_encode(h, pp, p, 1);
611 if (man && p[1] == 'S') {
615 print_encode(h, sec, NULL, 1);
616 } else if ((man && p[1] == 'N') ||
617 (man == 0 && p[1] == 'I'))
618 print_encode(h, name, NULL, 1);
620 print_encode(h, p, p + 2, 1);
624 print_encode(h, pp, NULL, 1);
628 print_otag(struct html *h, enum htmltag tag, const char *fmt, ...)
634 int style_written, tflags;
636 tflags = htmltags[tag].flags;
638 /* Flow content is not allowed in phrasing context. */
640 if ((tflags & HTML_INPHRASE) == 0) {
641 for (t = h->tag; t != NULL; t = t->next) {
644 assert((htmltags[t->tag].flags & HTML_TOPHRASE) == 0);
649 * Always wrap phrasing elements in a paragraph
650 * unless already contained in some flow container;
651 * never put them directly into a section.
654 } else if (tflags & HTML_TOPHRASE && h->tag->tag == TAG_SECTION)
655 print_otag(h, TAG_P, "c", "Pp");
657 /* Push this tag onto the stack of open scopes. */
659 if ((tflags & HTML_NOSTACK) == 0) {
660 t = mandoc_malloc(sizeof(struct tag));
669 if (tflags & HTML_NLBEFORE)
673 else if ((h->flags & HTML_NOSPACE) == 0) {
674 if (h->flags & HTML_KEEP)
675 print_word(h, " ");
677 if (h->flags & HTML_PREKEEP)
678 h->flags |= HTML_KEEP;
683 if ( ! (h->flags & HTML_NONOSPACE))
684 h->flags &= ~HTML_NOSPACE;
686 h->flags |= HTML_NOSPACE;
688 /* Print out the tag name and attributes. */
691 print_word(h, htmltags[tag].name);
695 while (*fmt != '\0' && *fmt != 's') {
697 /* Parse attributes and arguments. */
699 arg1 = va_arg(ap, char *);
713 arg1 = va_arg(ap, char *);
719 arg2 = va_arg(ap, char *);
723 /* Print the attributes. */
731 print_href(h, arg1, NULL, 0);
735 print_href(h, arg1, arg2, 1);
740 print_encode(h, arg1, NULL, 1);
744 print_encode(h, arg1, NULL, 1);
751 while (*fmt++ == 's') {
752 arg1 = va_arg(ap, char *);
753 arg2 = va_arg(ap, char *);
757 if (style_written == 0) {
758 print_word(h, "style=\"");
772 /* Accommodate for "well-formed" singleton escaping. */
774 if (htmltags[tag].flags & HTML_NOSTACK)
779 if (tflags & HTML_NLBEGIN)
782 h->flags |= HTML_NOSPACE;
784 if (tflags & HTML_INDENT)
786 if (tflags & HTML_NOINDENT)
793 * Print an element with an optional "id=" attribute.
794 * If the element has phrasing content and an "id=" attribute,
795 * also add a permalink: outside if it can be in phrasing context,
799 print_otag_id(struct html *h, enum htmltag elemtype, const char *cattr,
802 struct roff_node *nch;
808 if (n->flags & NODE_ID)
809 id = html_make_id(n, 1);
810 if (n->flags & NODE_HREF)
811 href = id == NULL ? html_make_id(n, 2) : id;
812 if (href != NULL && htmltags[elemtype].flags & HTML_INPHRASE)
813 ret = print_otag(h, TAG_A, "chR", "permalink", href);
814 t = print_otag(h, elemtype, "ci", cattr, id);
817 if (href != NULL && (nch = n->child) != NULL) {
818 /* man(7) is safe, it tags phrasing content only. */
819 if (n->tok > MDOC_MAX ||
820 htmltags[elemtype].flags & HTML_TOPHRASE)
822 else /* For mdoc(7), beware of nested blocks. */
823 while (nch != NULL && nch->type == ROFFT_TEXT)
826 print_otag(h, TAG_A, "chR", "permalink", href);
836 print_ctag(struct html *h, struct tag *tag)
840 if (tag->closed == 0) {
847 tflags = htmltags[tag->tag].flags;
848 if (tflags & HTML_INDENT)
850 if (tflags & HTML_NOINDENT)
852 if (tflags & HTML_NLEND)
857 print_word(h, htmltags[tag->tag].name);
859 if (tflags & HTML_NLAFTER)
862 if (tag->refcnt == 0) {
869 print_gen_decls(struct html *h)
871 print_word(h, "<!DOCTYPE html>");
876 print_gen_comment(struct html *h, struct roff_node *n)
880 print_word(h, "<!-- This is an automatically generated file."
884 while (n != NULL && n->type == ROFFT_COMMENT) {
885 if (strstr(n->string, "-->") == NULL &&
886 (wantblank || *n->string != '\0')) {
889 print_word(h, n->string);
890 wantblank = *n->string != '\0';
896 print_word(h, " -->");
902 print_text(struct html *h, const char *word)
904 print_tagged_text(h, word, NULL);
908 print_tagged_text(struct html *h, const char *word, struct roff_node *n)
914 * Always wrap text in a paragraph unless already contained in
915 * some flow container; never put it directly into a section.
918 if (h->tag->tag == TAG_SECTION)
919 print_otag(h, TAG_P, "c", "Pp");
921 /* Output whitespace before this text? */
923 if (h->col && (h->flags & HTML_NOSPACE) == 0) {
924 if ( ! (HTML_KEEP & h->flags)) {
925 if (HTML_PREKEEP & h->flags)
926 h->flags |= HTML_KEEP;
929 print_word(h, " ");
933 * Optionally switch fonts, optionally write a permalink, then
934 * print the text, optionally surrounded by HTML whitespace.
937 assert(h->metaf == NULL);
941 if (n != NULL && (href = html_make_id(n, 2)) != NULL) {
942 t = print_otag(h, TAG_A, "chR", "permalink", href);
947 if ( ! print_encode(h, word, NULL, 0)) {
948 if ( ! (h->flags & HTML_NONOSPACE))
949 h->flags &= ~HTML_NOSPACE;
950 h->flags &= ~HTML_NONEWLINE;
952 h->flags |= HTML_NOSPACE | HTML_NONEWLINE;
954 if (h->metaf != NULL) {
955 print_tagq(h, h->metaf);
957 } else if (t != NULL)
960 h->flags &= ~HTML_IGNDELIM;
964 print_tagq(struct html *h, const struct tag *until)
966 struct tag *this, *next;
968 for (this = h->tag; this != NULL; this = next) {
969 next = this == until ? NULL : this->next;
975 * Close out all open elements up to but excluding suntil.
976 * Note that a paragraph just inside stays open together with it
977 * because paragraphs include subsequent phrasing content.
980 print_stagq(struct html *h, const struct tag *suntil)
982 struct tag *this, *next;
984 for (this = h->tag; this != NULL; this = next) {
986 if (this == suntil || (next == suntil &&
987 (this->tag == TAG_P || this->tag == TAG_PRE)))
994 /***********************************************************************
995 * Low level output functions.
996 * They implement line breaking using a short static buffer.
997 ***********************************************************************/
1000 * Buffer one HTML output byte.
1001 * If the buffer is full, flush and deactivate it and start a new line.
1002 * If the buffer is inactive, print directly.
1005 print_byte(struct html *h, char c)
1007 if ((h->flags & HTML_BUFFER) == 0) {
1013 if (h->col + h->bufcol < sizeof(h->buf)) {
1014 h->buf[h->bufcol++] = c;
1023 fwrite(h->buf, h->bufcol, 1, stdout);
1025 h->col = (h->indent + 1) * 2 + h->bufcol + 1;
1027 h->flags &= ~HTML_BUFFER;
1031 * If something was printed on the current output line, end it.
1032 * Not to be called right after print_indent().
1035 print_endline(struct html *h)
1042 fwrite(h->buf, h->bufcol, 1, stdout);
1047 h->flags |= HTML_NOSPACE;
1048 h->flags &= ~HTML_BUFFER;
1052 * Flush the HTML output buffer.
1053 * If it is inactive, activate it.
1056 print_endword(struct html *h)
1063 if ((h->flags & HTML_BUFFER) == 0) {
1065 h->flags |= HTML_BUFFER;
1066 } else if (h->bufcol) {
1068 fwrite(h->buf, h->bufcol, 1, stdout);
1069 h->col += h->bufcol + 1;
1075 * If at the beginning of a new output line,
1076 * perform indentation and mark the line as containing output.
1077 * Make sure to really produce some output right afterwards,
1078 * but do not use print_otag() for producing it.
1081 print_indent(struct html *h)
1085 if (h->col || h->noindent)
1088 h->col = h->indent * 2;
1089 for (i = 0; i < h->col; i++)
1094 * Print or buffer some characters
1095 * depending on the current HTML output buffer state.
1098 print_word(struct html *h, const char *cp)
1101 print_byte(h, *cp++);