1 /* $Id: html.c,v 1.254 2019/03/03 13:02:11 schwarze Exp $ */
3 * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011-2015, 2017-2019 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.
20 #include <sys/types.h>
33 #include "mandoc_aux.h"
34 #include "mandoc_ohash.h"
45 #define HTML_NOSTACK (1 << 0)
46 #define HTML_AUTOCLOSE (1 << 1)
47 #define HTML_NLBEFORE (1 << 2)
48 #define HTML_NLBEGIN (1 << 3)
49 #define HTML_NLEND (1 << 4)
50 #define HTML_NLAFTER (1 << 5)
51 #define HTML_NLAROUND (HTML_NLBEFORE | HTML_NLAFTER)
52 #define HTML_NLINSIDE (HTML_NLBEGIN | HTML_NLEND)
53 #define HTML_NLALL (HTML_NLAROUND | HTML_NLINSIDE)
54 #define HTML_INDENT (1 << 6)
55 #define HTML_NOINDENT (1 << 7)
58 static const struct htmldata htmltags[TAG_MAX] = {
60 {"head", HTML_NLALL | HTML_INDENT},
62 {"meta", HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
63 {"title", HTML_NLAROUND},
64 {"div", HTML_NLAROUND},
66 {"section", HTML_NLALL},
67 {"h1", HTML_NLAROUND},
68 {"h2", HTML_NLAROUND},
70 {"link", HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
71 {"br", HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
73 {"table", HTML_NLALL | HTML_INDENT},
74 {"tr", HTML_NLALL | HTML_INDENT},
75 {"td", HTML_NLAROUND},
76 {"li", HTML_NLAROUND | HTML_INDENT},
77 {"ul", HTML_NLALL | HTML_INDENT},
78 {"ol", HTML_NLALL | HTML_INDENT},
79 {"dl", HTML_NLALL | HTML_INDENT},
80 {"dt", HTML_NLAROUND},
81 {"dd", HTML_NLAROUND | HTML_INDENT},
82 {"p", HTML_NLAROUND | HTML_INDENT},
83 {"pre", HTML_NLALL | HTML_NOINDENT},
90 {"style", HTML_NLALL | HTML_INDENT},
91 {"math", HTML_NLALL | HTML_INDENT},
110 /* Avoid duplicate HTML id= attributes. */
111 static struct ohash id_unique;
113 static void html_reset_internal(struct html *);
114 static void print_byte(struct html *, char);
115 static void print_endword(struct html *);
116 static void print_indent(struct html *);
117 static void print_word(struct html *, const char *);
119 static void print_ctag(struct html *, struct tag *);
120 static int print_escape(struct html *, char);
121 static int print_encode(struct html *, const char *, const char *, int);
122 static void print_href(struct html *, const char *, const char *, int);
126 html_alloc(const struct manoutput *outopts)
130 h = mandoc_calloc(1, sizeof(struct html));
133 h->style = outopts->style;
134 if ((h->base_man1 = outopts->man) == NULL)
136 else if ((h->base_man2 = strchr(h->base_man1, ';')) != NULL)
137 *h->base_man2++ = '\0';
138 h->base_includes = outopts->includes;
139 if (outopts->fragment)
140 h->oflags |= HTML_FRAGMENT;
142 h->oflags |= HTML_TOC;
144 mandoc_ohash_init(&id_unique, 4, 0);
150 html_reset_internal(struct html *h)
156 while ((tag = h->tag) != NULL) {
160 cp = ohash_first(&id_unique, &slot);
163 cp = ohash_next(&id_unique, &slot);
165 ohash_delete(&id_unique);
171 html_reset_internal(p);
172 mandoc_ohash_init(&id_unique, 4, 0);
178 html_reset_internal(p);
183 print_gen_head(struct html *h)
187 print_otag(h, TAG_META, "?", "charset", "utf-8");
188 if (h->style != NULL) {
189 print_otag(h, TAG_LINK, "?h??", "rel", "stylesheet",
190 h->style, "type", "text/css", "media", "all");
195 * Print a minimal embedded style sheet.
198 t = print_otag(h, TAG_STYLE, "");
199 print_text(h, "table.head, table.foot { width: 100%; }");
201 print_text(h, "td.head-rtitle, td.foot-os { text-align: right; }");
203 print_text(h, "td.head-vol { text-align: center; }");
205 print_text(h, "div.Pp { margin: 1ex 0ex; }");
207 print_text(h, "div.Nd, div.Bf, div.Op { display: inline; }");
209 print_text(h, "span.Pa, span.Ad { font-style: italic; }");
211 print_text(h, "span.Ms { font-weight: bold; }");
213 print_text(h, "dl.Bl-diag ");
215 print_text(h, " dt { font-weight: bold; }");
217 print_text(h, "code.Nm, code.Fl, code.Cm, code.Ic, "
218 "code.In, code.Fd, code.Fn,");
220 print_text(h, "code.Cd { font-weight: bold; "
221 "font-family: inherit; }");
226 print_metaf(struct html *h, enum mandoc_esc deco)
231 case ESCAPE_FONTPREV:
234 case ESCAPE_FONTITALIC:
235 font = HTMLFONT_ITALIC;
237 case ESCAPE_FONTBOLD:
238 font = HTMLFONT_BOLD;
247 case ESCAPE_FONTROMAN:
248 font = HTMLFONT_NONE;
255 print_tagq(h, h->metaf);
263 case HTMLFONT_ITALIC:
264 h->metaf = print_otag(h, TAG_I, "");
267 h->metaf = print_otag(h, TAG_B, "");
270 h->metaf = print_otag(h, TAG_B, "");
271 print_otag(h, TAG_I, "");
274 h->metaf = print_otag(h, TAG_SPAN, "c", "Li");
282 html_close_paragraph(struct html *h)
286 for (t = h->tag; t != NULL && t->closed == 0; t = t->next) {
303 * ROFF_nf switches to no-fill mode, ROFF_fi to fill mode.
304 * TOKEN_NONE does not switch. The old mode is returned.
307 html_fillmode(struct html *h, enum roff_tok want)
312 for (t = h->tag; t != NULL; t = t->next)
313 if (t->tag == TAG_PRE)
316 had = t == NULL ? ROFF_fi : ROFF_nf;
324 html_close_paragraph(h);
325 print_otag(h, TAG_PRE, "");
337 html_make_id(const struct roff_node *n, int unique)
339 const struct roff_node *nch;
340 char *buf, *bufs, *cp;
344 for (nch = n->child; nch != NULL; nch = nch->next)
345 if (nch->type != ROFFT_TEXT)
354 * In ID attributes, only use ASCII characters that are
355 * permitted in URL-fragment strings according to the
357 * https://url.spec.whatwg.org/#url-fragment-string
360 for (cp = buf; *cp != '\0'; cp++)
361 if (isalnum((unsigned char)*cp) == 0 &&
362 strchr("!$&'()*+,-./:;=?@_~", *cp) == NULL)
368 /* Avoid duplicate HTML id= attributes. */
372 slot = ohash_qlookup(&id_unique, buf);
373 cp = ohash_find(&id_unique, slot);
377 if (++suffix > 127) {
381 mandoc_asprintf(&bufs, "%s_%d", buf, suffix);
382 slot = ohash_qlookup(&id_unique, bufs);
383 cp = ohash_find(&id_unique, slot);
388 ohash_insert(&id_unique, slot, buf);
393 print_escape(struct html *h, char c)
398 print_word(h, "<");
401 print_word(h, ">");
404 print_word(h, "&");
407 print_word(h, """);
410 print_word(h, " ");
424 print_encode(struct html *h, const char *p, const char *pend, int norecurse)
429 int c, len, breakline, nospace;
431 static const char rejs[10] = { ' ', '\\', '<', '>', '&', '"',
432 ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' };
435 pend = strchr(p, '\0');
441 if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
442 h->flags &= ~HTML_SKIPCHAR;
447 for (sz = strcspn(p, rejs); sz-- && p < pend; p++)
451 (p >= pend || *p == ' ' || *p == ASCII_NBRSP)) {
452 print_otag(h, TAG_BR, "");
454 while (p < pend && (*p == ' ' || *p == ASCII_NBRSP))
468 if (print_escape(h, *p++))
471 esc = mandoc_escape(&p, &seq, &len);
474 case ESCAPE_FONTPREV:
475 case ESCAPE_FONTBOLD:
476 case ESCAPE_FONTITALIC:
479 case ESCAPE_FONTROMAN:
480 if (0 == norecurse) {
481 h->flags |= HTML_NOSPACE;
483 h->flags &= ~HTML_NOSPACE;
486 case ESCAPE_SKIPCHAR:
487 h->flags |= HTML_SKIPCHAR;
495 if (h->flags & HTML_SKIPCHAR) {
496 h->flags &= ~HTML_SKIPCHAR;
502 /* Skip past "u" header. */
503 c = mchars_num2uc(seq + 1, len - 1);
505 case ESCAPE_NUMBERED:
506 c = mchars_num2char(seq, len);
511 c = mchars_spec2cp(seq, len);
519 print_word(h, "html");
528 case ESCAPE_OVERSTRIKE:
536 if ((c < 0x20 && c != 0x09) ||
537 (c > 0x7E && c < 0xA0))
540 (void)snprintf(numbuf, sizeof(numbuf), "&#x%.4X;", c);
541 print_word(h, numbuf);
542 } else if (print_escape(h, c) == 0)
550 print_href(struct html *h, const char *name, const char *sec, int man)
558 if (h->base_man2 != NULL) {
559 mandoc_asprintf(&filename, "%s.%s", name, sec);
560 if (stat(filename, &sb) == -1)
565 pp = h->base_includes;
567 while ((p = strchr(pp, '%')) != NULL) {
568 print_encode(h, pp, p, 1);
569 if (man && p[1] == 'S') {
573 print_encode(h, sec, NULL, 1);
574 } else if ((man && p[1] == 'N') ||
575 (man == 0 && p[1] == 'I'))
576 print_encode(h, name, NULL, 1);
578 print_encode(h, p, p + 2, 1);
582 print_encode(h, pp, NULL, 1);
586 print_otag(struct html *h, enum htmltag tag, const char *fmt, ...)
592 int style_written, tflags;
594 tflags = htmltags[tag].flags;
596 /* Push this tag onto the stack of open scopes. */
598 if ((tflags & HTML_NOSTACK) == 0) {
599 t = mandoc_malloc(sizeof(struct tag));
608 if (tflags & HTML_NLBEFORE)
612 else if ((h->flags & HTML_NOSPACE) == 0) {
613 if (h->flags & HTML_KEEP)
614 print_word(h, " ");
616 if (h->flags & HTML_PREKEEP)
617 h->flags |= HTML_KEEP;
622 if ( ! (h->flags & HTML_NONOSPACE))
623 h->flags &= ~HTML_NOSPACE;
625 h->flags |= HTML_NOSPACE;
627 /* Print out the tag name and attributes. */
630 print_word(h, htmltags[tag].name);
634 while (*fmt != '\0' && *fmt != 's') {
636 /* Parse attributes and arguments. */
638 arg1 = va_arg(ap, char *);
652 arg1 = va_arg(ap, char *);
658 arg2 = va_arg(ap, char *);
662 /* Print the attributes. */
670 print_href(h, arg1, NULL, 0);
674 print_href(h, arg1, arg2, 1);
679 print_encode(h, arg1, NULL, 1);
683 print_encode(h, arg1, NULL, 1);
690 while (*fmt++ == 's') {
691 arg1 = va_arg(ap, char *);
692 arg2 = va_arg(ap, char *);
696 if (style_written == 0) {
697 print_word(h, "style=\"");
711 /* Accommodate for "well-formed" singleton escaping. */
713 if (HTML_AUTOCLOSE & htmltags[tag].flags)
718 if (tflags & HTML_NLBEGIN)
721 h->flags |= HTML_NOSPACE;
723 if (tflags & HTML_INDENT)
725 if (tflags & HTML_NOINDENT)
732 print_ctag(struct html *h, struct tag *tag)
736 if (tag->closed == 0) {
743 tflags = htmltags[tag->tag].flags;
744 if (tflags & HTML_INDENT)
746 if (tflags & HTML_NOINDENT)
748 if (tflags & HTML_NLEND)
753 print_word(h, htmltags[tag->tag].name);
755 if (tflags & HTML_NLAFTER)
758 if (tag->refcnt == 0) {
765 print_gen_decls(struct html *h)
767 print_word(h, "<!DOCTYPE html>");
772 print_gen_comment(struct html *h, struct roff_node *n)
776 print_word(h, "<!-- This is an automatically generated file."
780 while (n != NULL && n->type == ROFFT_COMMENT) {
781 if (strstr(n->string, "-->") == NULL &&
782 (wantblank || *n->string != '\0')) {
785 print_word(h, n->string);
786 wantblank = *n->string != '\0';
792 print_word(h, " -->");
798 print_text(struct html *h, const char *word)
800 if (h->col && (h->flags & HTML_NOSPACE) == 0) {
801 if ( ! (HTML_KEEP & h->flags)) {
802 if (HTML_PREKEEP & h->flags)
803 h->flags |= HTML_KEEP;
806 print_word(h, " ");
809 assert(NULL == h->metaf);
811 case HTMLFONT_ITALIC:
812 h->metaf = print_otag(h, TAG_I, "");
815 h->metaf = print_otag(h, TAG_B, "");
818 h->metaf = print_otag(h, TAG_B, "");
819 print_otag(h, TAG_I, "");
822 h->metaf = print_otag(h, TAG_SPAN, "c", "Li");
830 if ( ! print_encode(h, word, NULL, 0)) {
831 if ( ! (h->flags & HTML_NONOSPACE))
832 h->flags &= ~HTML_NOSPACE;
833 h->flags &= ~HTML_NONEWLINE;
835 h->flags |= HTML_NOSPACE | HTML_NONEWLINE;
838 print_tagq(h, h->metaf);
842 h->flags &= ~HTML_IGNDELIM;
846 print_tagq(struct html *h, const struct tag *until)
848 struct tag *this, *next;
850 for (this = h->tag; this != NULL; this = next) {
851 next = this == until ? NULL : this->next;
857 * Close out all open elements up to but excluding suntil.
858 * Note that a paragraph just inside stays open together with it
859 * because paragraphs include subsequent phrasing content.
862 print_stagq(struct html *h, const struct tag *suntil)
864 struct tag *this, *next;
866 for (this = h->tag; this != NULL; this = next) {
868 if (this == suntil || (next == suntil &&
869 (this->tag == TAG_P || this->tag == TAG_PRE)))
876 /***********************************************************************
877 * Low level output functions.
878 * They implement line breaking using a short static buffer.
879 ***********************************************************************/
882 * Buffer one HTML output byte.
883 * If the buffer is full, flush and deactivate it and start a new line.
884 * If the buffer is inactive, print directly.
887 print_byte(struct html *h, char c)
889 if ((h->flags & HTML_BUFFER) == 0) {
895 if (h->col + h->bufcol < sizeof(h->buf)) {
896 h->buf[h->bufcol++] = c;
905 fwrite(h->buf, h->bufcol, 1, stdout);
907 h->col = (h->indent + 1) * 2 + h->bufcol + 1;
909 h->flags &= ~HTML_BUFFER;
913 * If something was printed on the current output line, end it.
914 * Not to be called right after print_indent().
917 print_endline(struct html *h)
924 fwrite(h->buf, h->bufcol, 1, stdout);
929 h->flags |= HTML_NOSPACE;
930 h->flags &= ~HTML_BUFFER;
934 * Flush the HTML output buffer.
935 * If it is inactive, activate it.
938 print_endword(struct html *h)
945 if ((h->flags & HTML_BUFFER) == 0) {
947 h->flags |= HTML_BUFFER;
948 } else if (h->bufcol) {
950 fwrite(h->buf, h->bufcol, 1, stdout);
951 h->col += h->bufcol + 1;
957 * If at the beginning of a new output line,
958 * perform indentation and mark the line as containing output.
959 * Make sure to really produce some output right afterwards,
960 * but do not use print_otag() for producing it.
963 print_indent(struct html *h)
970 if (h->noindent == 0) {
971 h->col = h->indent * 2;
972 for (i = 0; i < h->col; i++)
975 h->flags &= ~HTML_NOSPACE;
979 * Print or buffer some characters
980 * depending on the current HTML output buffer state.
983 print_word(struct html *h, const char *cp)
986 print_byte(h, *cp++);