]>
git.cameronkatri.com Git - mandoc.git/blob - html.c
1 /* $Id: html.c,v 1.255 2019/04/30 15:53:00 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);
123 static void print_metaf(struct html
*);
127 html_alloc(const struct manoutput
*outopts
)
131 h
= mandoc_calloc(1, sizeof(struct html
));
134 h
->style
= outopts
->style
;
135 if ((h
->base_man1
= outopts
->man
) == NULL
)
137 else if ((h
->base_man2
= strchr(h
->base_man1
, ';')) != NULL
)
138 *h
->base_man2
++ = '\0';
139 h
->base_includes
= outopts
->includes
;
140 if (outopts
->fragment
)
141 h
->oflags
|= HTML_FRAGMENT
;
143 h
->oflags
|= HTML_TOC
;
145 mandoc_ohash_init(&id_unique
, 4, 0);
151 html_reset_internal(struct html
*h
)
157 while ((tag
= h
->tag
) != NULL
) {
161 cp
= ohash_first(&id_unique
, &slot
);
164 cp
= ohash_next(&id_unique
, &slot
);
166 ohash_delete(&id_unique
);
172 html_reset_internal(p
);
173 mandoc_ohash_init(&id_unique
, 4, 0);
179 html_reset_internal(p
);
184 print_gen_head(struct html
*h
)
188 print_otag(h
, TAG_META
, "?", "charset", "utf-8");
189 if (h
->style
!= NULL
) {
190 print_otag(h
, TAG_LINK
, "?h??", "rel", "stylesheet",
191 h
->style
, "type", "text/css", "media", "all");
196 * Print a minimal embedded style sheet.
199 t
= print_otag(h
, TAG_STYLE
, "");
200 print_text(h
, "table.head, table.foot { width: 100%; }");
202 print_text(h
, "td.head-rtitle, td.foot-os { text-align: right; }");
204 print_text(h
, "td.head-vol { text-align: center; }");
206 print_text(h
, "div.Pp { margin: 1ex 0ex; }");
208 print_text(h
, "div.Nd, div.Bf, div.Op { display: inline; }");
210 print_text(h
, "span.Pa, span.Ad { font-style: italic; }");
212 print_text(h
, "span.Ms { font-weight: bold; }");
214 print_text(h
, "dl.Bl-diag ");
216 print_text(h
, " dt { font-weight: bold; }");
218 print_text(h
, "code.Nm, code.Fl, code.Cm, code.Ic, "
219 "code.In, code.Fd, code.Fn,");
221 print_text(h
, "code.Cd { font-weight: bold; "
222 "font-family: inherit; }");
227 html_setfont(struct html
*h
, enum mandoc_esc font
)
230 case ESCAPE_FONTPREV
:
233 case ESCAPE_FONTITALIC
:
234 case ESCAPE_FONTBOLD
:
237 case ESCAPE_FONTROMAN
:
240 font
= ESCAPE_FONTROMAN
;
251 print_metaf(struct html
*h
)
254 print_tagq(h
, h
->metaf
);
258 case ESCAPE_FONTITALIC
:
259 h
->metaf
= print_otag(h
, TAG_I
, "");
261 case ESCAPE_FONTBOLD
:
262 h
->metaf
= print_otag(h
, TAG_B
, "");
265 h
->metaf
= print_otag(h
, TAG_B
, "");
266 print_otag(h
, TAG_I
, "");
269 h
->metaf
= print_otag(h
, TAG_SPAN
, "c", "Li");
277 html_close_paragraph(struct html
*h
)
281 for (t
= h
->tag
; t
!= NULL
&& t
->closed
== 0; t
= t
->next
) {
298 * ROFF_nf switches to no-fill mode, ROFF_fi to fill mode.
299 * TOKEN_NONE does not switch. The old mode is returned.
302 html_fillmode(struct html
*h
, enum roff_tok want
)
307 for (t
= h
->tag
; t
!= NULL
; t
= t
->next
)
308 if (t
->tag
== TAG_PRE
)
311 had
= t
== NULL
? ROFF_fi
: ROFF_nf
;
319 html_close_paragraph(h
);
320 print_otag(h
, TAG_PRE
, "");
332 html_make_id(const struct roff_node
*n
, int unique
)
334 const struct roff_node
*nch
;
335 char *buf
, *bufs
, *cp
;
339 for (nch
= n
->child
; nch
!= NULL
; nch
= nch
->next
)
340 if (nch
->type
!= ROFFT_TEXT
)
349 * In ID attributes, only use ASCII characters that are
350 * permitted in URL-fragment strings according to the
352 * https://url.spec.whatwg.org/#url-fragment-string
355 for (cp
= buf
; *cp
!= '\0'; cp
++)
356 if (isalnum((unsigned char)*cp
) == 0 &&
357 strchr("!$&'()*+,-./:;=?@_~", *cp
) == NULL
)
363 /* Avoid duplicate HTML id= attributes. */
367 slot
= ohash_qlookup(&id_unique
, buf
);
368 cp
= ohash_find(&id_unique
, slot
);
372 if (++suffix
> 127) {
376 mandoc_asprintf(&bufs
, "%s_%d", buf
, suffix
);
377 slot
= ohash_qlookup(&id_unique
, bufs
);
378 cp
= ohash_find(&id_unique
, slot
);
383 ohash_insert(&id_unique
, slot
, buf
);
388 print_escape(struct html
*h
, char c
)
393 print_word(h
, "<");
396 print_word(h
, ">");
399 print_word(h
, "&");
402 print_word(h
, """);
405 print_word(h
, " ");
419 print_encode(struct html
*h
, const char *p
, const char *pend
, int norecurse
)
424 int c
, len
, breakline
, nospace
;
426 static const char rejs
[10] = { ' ', '\\', '<', '>', '&', '"',
427 ASCII_NBRSP
, ASCII_HYPH
, ASCII_BREAK
, '\0' };
430 pend
= strchr(p
, '\0');
436 if (HTML_SKIPCHAR
& h
->flags
&& '\\' != *p
) {
437 h
->flags
&= ~HTML_SKIPCHAR
;
442 for (sz
= strcspn(p
, rejs
); sz
-- && p
< pend
; p
++)
446 (p
>= pend
|| *p
== ' ' || *p
== ASCII_NBRSP
)) {
447 print_otag(h
, TAG_BR
, "");
449 while (p
< pend
&& (*p
== ' ' || *p
== ASCII_NBRSP
))
463 if (print_escape(h
, *p
++))
466 esc
= mandoc_escape(&p
, &seq
, &len
);
469 case ESCAPE_FONTPREV
:
470 case ESCAPE_FONTBOLD
:
471 case ESCAPE_FONTITALIC
:
474 case ESCAPE_FONTROMAN
:
475 if (0 == norecurse
) {
476 h
->flags
|= HTML_NOSPACE
;
477 if (html_setfont(h
, esc
))
479 h
->flags
&= ~HTML_NOSPACE
;
482 case ESCAPE_SKIPCHAR
:
483 h
->flags
|= HTML_SKIPCHAR
;
491 if (h
->flags
& HTML_SKIPCHAR
) {
492 h
->flags
&= ~HTML_SKIPCHAR
;
498 /* Skip past "u" header. */
499 c
= mchars_num2uc(seq
+ 1, len
- 1);
501 case ESCAPE_NUMBERED
:
502 c
= mchars_num2char(seq
, len
);
507 c
= mchars_spec2cp(seq
, len
);
515 print_word(h
, "html");
524 case ESCAPE_OVERSTRIKE
:
532 if ((c
< 0x20 && c
!= 0x09) ||
533 (c
> 0x7E && c
< 0xA0))
536 (void)snprintf(numbuf
, sizeof(numbuf
), "&#x%.4X;", c
);
537 print_word(h
, numbuf
);
538 } else if (print_escape(h
, c
) == 0)
546 print_href(struct html
*h
, const char *name
, const char *sec
, int man
)
554 if (h
->base_man2
!= NULL
) {
555 mandoc_asprintf(&filename
, "%s.%s", name
, sec
);
556 if (stat(filename
, &sb
) == -1)
561 pp
= h
->base_includes
;
563 while ((p
= strchr(pp
, '%')) != NULL
) {
564 print_encode(h
, pp
, p
, 1);
565 if (man
&& p
[1] == 'S') {
569 print_encode(h
, sec
, NULL
, 1);
570 } else if ((man
&& p
[1] == 'N') ||
571 (man
== 0 && p
[1] == 'I'))
572 print_encode(h
, name
, NULL
, 1);
574 print_encode(h
, p
, p
+ 2, 1);
578 print_encode(h
, pp
, NULL
, 1);
582 print_otag(struct html
*h
, enum htmltag tag
, const char *fmt
, ...)
588 int style_written
, tflags
;
590 tflags
= htmltags
[tag
].flags
;
592 /* Push this tag onto the stack of open scopes. */
594 if ((tflags
& HTML_NOSTACK
) == 0) {
595 t
= mandoc_malloc(sizeof(struct tag
));
604 if (tflags
& HTML_NLBEFORE
)
608 else if ((h
->flags
& HTML_NOSPACE
) == 0) {
609 if (h
->flags
& HTML_KEEP
)
610 print_word(h
, " ");
612 if (h
->flags
& HTML_PREKEEP
)
613 h
->flags
|= HTML_KEEP
;
618 if ( ! (h
->flags
& HTML_NONOSPACE
))
619 h
->flags
&= ~HTML_NOSPACE
;
621 h
->flags
|= HTML_NOSPACE
;
623 /* Print out the tag name and attributes. */
626 print_word(h
, htmltags
[tag
].name
);
630 while (*fmt
!= '\0' && *fmt
!= 's') {
632 /* Parse attributes and arguments. */
634 arg1
= va_arg(ap
, char *);
648 arg1
= va_arg(ap
, char *);
654 arg2
= va_arg(ap
, char *);
658 /* Print the attributes. */
666 print_href(h
, arg1
, NULL
, 0);
670 print_href(h
, arg1
, arg2
, 1);
675 print_encode(h
, arg1
, NULL
, 1);
679 print_encode(h
, arg1
, NULL
, 1);
686 while (*fmt
++ == 's') {
687 arg1
= va_arg(ap
, char *);
688 arg2
= va_arg(ap
, char *);
692 if (style_written
== 0) {
693 print_word(h
, "style=\"");
707 /* Accommodate for "well-formed" singleton escaping. */
709 if (HTML_AUTOCLOSE
& htmltags
[tag
].flags
)
714 if (tflags
& HTML_NLBEGIN
)
717 h
->flags
|= HTML_NOSPACE
;
719 if (tflags
& HTML_INDENT
)
721 if (tflags
& HTML_NOINDENT
)
728 print_ctag(struct html
*h
, struct tag
*tag
)
732 if (tag
->closed
== 0) {
739 tflags
= htmltags
[tag
->tag
].flags
;
740 if (tflags
& HTML_INDENT
)
742 if (tflags
& HTML_NOINDENT
)
744 if (tflags
& HTML_NLEND
)
749 print_word(h
, htmltags
[tag
->tag
].name
);
751 if (tflags
& HTML_NLAFTER
)
754 if (tag
->refcnt
== 0) {
761 print_gen_decls(struct html
*h
)
763 print_word(h
, "<!DOCTYPE html>");
768 print_gen_comment(struct html
*h
, struct roff_node
*n
)
772 print_word(h
, "<!-- This is an automatically generated file."
776 while (n
!= NULL
&& n
->type
== ROFFT_COMMENT
) {
777 if (strstr(n
->string
, "-->") == NULL
&&
778 (wantblank
|| *n
->string
!= '\0')) {
781 print_word(h
, n
->string
);
782 wantblank
= *n
->string
!= '\0';
788 print_word(h
, " -->");
794 print_text(struct html
*h
, const char *word
)
796 if (h
->col
&& (h
->flags
& HTML_NOSPACE
) == 0) {
797 if ( ! (HTML_KEEP
& h
->flags
)) {
798 if (HTML_PREKEEP
& h
->flags
)
799 h
->flags
|= HTML_KEEP
;
802 print_word(h
, " ");
805 assert(h
->metaf
== NULL
);
808 if ( ! print_encode(h
, word
, NULL
, 0)) {
809 if ( ! (h
->flags
& HTML_NONOSPACE
))
810 h
->flags
&= ~HTML_NOSPACE
;
811 h
->flags
&= ~HTML_NONEWLINE
;
813 h
->flags
|= HTML_NOSPACE
| HTML_NONEWLINE
;
815 if (h
->metaf
!= NULL
) {
816 print_tagq(h
, h
->metaf
);
820 h
->flags
&= ~HTML_IGNDELIM
;
824 print_tagq(struct html
*h
, const struct tag
*until
)
826 struct tag
*this, *next
;
828 for (this = h
->tag
; this != NULL
; this = next
) {
829 next
= this == until
? NULL
: this->next
;
835 * Close out all open elements up to but excluding suntil.
836 * Note that a paragraph just inside stays open together with it
837 * because paragraphs include subsequent phrasing content.
840 print_stagq(struct html
*h
, const struct tag
*suntil
)
842 struct tag
*this, *next
;
844 for (this = h
->tag
; this != NULL
; this = next
) {
846 if (this == suntil
|| (next
== suntil
&&
847 (this->tag
== TAG_P
|| this->tag
== TAG_PRE
)))
854 /***********************************************************************
855 * Low level output functions.
856 * They implement line breaking using a short static buffer.
857 ***********************************************************************/
860 * Buffer one HTML output byte.
861 * If the buffer is full, flush and deactivate it and start a new line.
862 * If the buffer is inactive, print directly.
865 print_byte(struct html
*h
, char c
)
867 if ((h
->flags
& HTML_BUFFER
) == 0) {
873 if (h
->col
+ h
->bufcol
< sizeof(h
->buf
)) {
874 h
->buf
[h
->bufcol
++] = c
;
883 fwrite(h
->buf
, h
->bufcol
, 1, stdout
);
885 h
->col
= (h
->indent
+ 1) * 2 + h
->bufcol
+ 1;
887 h
->flags
&= ~HTML_BUFFER
;
891 * If something was printed on the current output line, end it.
892 * Not to be called right after print_indent().
895 print_endline(struct html
*h
)
902 fwrite(h
->buf
, h
->bufcol
, 1, stdout
);
907 h
->flags
|= HTML_NOSPACE
;
908 h
->flags
&= ~HTML_BUFFER
;
912 * Flush the HTML output buffer.
913 * If it is inactive, activate it.
916 print_endword(struct html
*h
)
923 if ((h
->flags
& HTML_BUFFER
) == 0) {
925 h
->flags
|= HTML_BUFFER
;
926 } else if (h
->bufcol
) {
928 fwrite(h
->buf
, h
->bufcol
, 1, stdout
);
929 h
->col
+= h
->bufcol
+ 1;
935 * If at the beginning of a new output line,
936 * perform indentation and mark the line as containing output.
937 * Make sure to really produce some output right afterwards,
938 * but do not use print_otag() for producing it.
941 print_indent(struct html
*h
)
948 if (h
->noindent
== 0) {
949 h
->col
= h
->indent
* 2;
950 for (i
= 0; i
< h
->col
; i
++)
953 h
->flags
&= ~HTML_NOSPACE
;
957 * Print or buffer some characters
958 * depending on the current HTML output buffer state.
961 print_word(struct html
*h
, const char *cp
)
964 print_byte(h
, *cp
++);