]>
git.cameronkatri.com Git - mandoc.git/blob - html.c
1 /* $Id: html.c,v 1.258 2019/09/01 15:12:19 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_INPHRASE (1 << 0) /* Can appear in phrasing context. */
46 #define HTML_TOPHRASE (1 << 1) /* Establishes phrasing context. */
47 #define HTML_NOSTACK (1 << 2) /* Does not have an end tag. */
48 #define HTML_NLBEFORE (1 << 3) /* Output line break before opening. */
49 #define HTML_NLBEGIN (1 << 4) /* Output line break after opening. */
50 #define HTML_NLEND (1 << 5) /* Output line break before closing. */
51 #define HTML_NLAFTER (1 << 6) /* Output line break after closing. */
52 #define HTML_NLAROUND (HTML_NLBEFORE | HTML_NLAFTER)
53 #define HTML_NLINSIDE (HTML_NLBEGIN | HTML_NLEND)
54 #define HTML_NLALL (HTML_NLAROUND | HTML_NLINSIDE)
55 #define HTML_INDENT (1 << 7) /* Indent content by two spaces. */
56 #define HTML_NOINDENT (1 << 8) /* Exception: never indent content. */
59 static const struct htmldata htmltags
[TAG_MAX
] = {
61 {"head", HTML_NLALL
| HTML_INDENT
},
62 {"meta", HTML_NOSTACK
| HTML_NLALL
},
63 {"link", HTML_NOSTACK
| HTML_NLALL
},
64 {"style", HTML_NLALL
| HTML_INDENT
},
65 {"title", HTML_NLAROUND
},
67 {"div", HTML_NLAROUND
},
68 {"section", HTML_NLALL
},
69 {"table", HTML_NLALL
| HTML_INDENT
},
70 {"tr", HTML_NLALL
| HTML_INDENT
},
71 {"td", HTML_NLAROUND
},
72 {"li", HTML_NLAROUND
| HTML_INDENT
},
73 {"ul", HTML_NLALL
| HTML_INDENT
},
74 {"ol", HTML_NLALL
| HTML_INDENT
},
75 {"dl", HTML_NLALL
| HTML_INDENT
},
76 {"dt", HTML_NLAROUND
},
77 {"dd", HTML_NLAROUND
| HTML_INDENT
},
78 {"h1", HTML_TOPHRASE
| HTML_NLAROUND
},
79 {"h2", HTML_TOPHRASE
| HTML_NLAROUND
},
80 {"p", HTML_TOPHRASE
| HTML_NLAROUND
| HTML_INDENT
},
81 {"pre", HTML_TOPHRASE
| HTML_NLALL
| HTML_NOINDENT
},
82 {"a", HTML_INPHRASE
| HTML_TOPHRASE
},
83 {"b", HTML_INPHRASE
| HTML_TOPHRASE
},
84 {"cite", HTML_INPHRASE
| HTML_TOPHRASE
},
85 {"code", HTML_INPHRASE
| HTML_TOPHRASE
},
86 {"i", HTML_INPHRASE
| HTML_TOPHRASE
},
87 {"small", HTML_INPHRASE
| HTML_TOPHRASE
},
88 {"span", HTML_INPHRASE
| HTML_TOPHRASE
},
89 {"var", HTML_INPHRASE
| HTML_TOPHRASE
},
90 {"br", HTML_INPHRASE
| HTML_NOSTACK
| HTML_NLALL
},
91 {"math", HTML_INPHRASE
| 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
, ".Nd, .Bf, .Op { display: inline; }");
208 print_text(h
, ".Pa, .Ad { font-style: italic; }");
210 print_text(h
, ".Ms { font-weight: bold; }");
212 print_text(h
, ".Bl-diag ");
214 print_text(h
, " dt { font-weight: bold; }");
216 print_text(h
, "code.Nm, .Fl, .Cm, .Ic, code.In, .Fd, .Fn, .Cd "
217 "{ font-weight: bold; font-family: inherit; }");
222 html_setfont(struct html
*h
, enum mandoc_esc font
)
225 case ESCAPE_FONTPREV
:
228 case ESCAPE_FONTITALIC
:
229 case ESCAPE_FONTBOLD
:
232 case ESCAPE_FONTROMAN
:
235 font
= ESCAPE_FONTROMAN
;
246 print_metaf(struct html
*h
)
249 print_tagq(h
, h
->metaf
);
253 case ESCAPE_FONTITALIC
:
254 h
->metaf
= print_otag(h
, TAG_I
, "");
256 case ESCAPE_FONTBOLD
:
257 h
->metaf
= print_otag(h
, TAG_B
, "");
260 h
->metaf
= print_otag(h
, TAG_B
, "");
261 print_otag(h
, TAG_I
, "");
264 h
->metaf
= print_otag(h
, TAG_SPAN
, "c", "Li");
272 html_close_paragraph(struct html
*h
)
276 for (t
= h
->tag
; t
!= NULL
&& t
->closed
== 0; t
= t
->next
) {
293 * ROFF_nf switches to no-fill mode, ROFF_fi to fill mode.
294 * TOKEN_NONE does not switch. The old mode is returned.
297 html_fillmode(struct html
*h
, enum roff_tok want
)
302 for (t
= h
->tag
; t
!= NULL
; t
= t
->next
)
303 if (t
->tag
== TAG_PRE
)
306 had
= t
== NULL
? ROFF_fi
: ROFF_nf
;
314 html_close_paragraph(h
);
315 print_otag(h
, TAG_PRE
, "");
327 html_make_id(const struct roff_node
*n
, int unique
)
329 const struct roff_node
*nch
;
330 char *buf
, *bufs
, *cp
;
334 for (nch
= n
->child
; nch
!= NULL
; nch
= nch
->next
)
335 if (nch
->type
!= ROFFT_TEXT
)
344 * In ID attributes, only use ASCII characters that are
345 * permitted in URL-fragment strings according to the
347 * https://url.spec.whatwg.org/#url-fragment-string
350 for (cp
= buf
; *cp
!= '\0'; cp
++)
351 if (isalnum((unsigned char)*cp
) == 0 &&
352 strchr("!$&'()*+,-./:;=?@_~", *cp
) == NULL
)
358 /* Avoid duplicate HTML id= attributes. */
362 slot
= ohash_qlookup(&id_unique
, buf
);
363 cp
= ohash_find(&id_unique
, slot
);
367 if (++suffix
> 127) {
371 mandoc_asprintf(&bufs
, "%s_%d", buf
, suffix
);
372 slot
= ohash_qlookup(&id_unique
, bufs
);
373 cp
= ohash_find(&id_unique
, slot
);
378 ohash_insert(&id_unique
, slot
, buf
);
383 print_escape(struct html
*h
, char c
)
388 print_word(h
, "<");
391 print_word(h
, ">");
394 print_word(h
, "&");
397 print_word(h
, """);
400 print_word(h
, " ");
414 print_encode(struct html
*h
, const char *p
, const char *pend
, int norecurse
)
419 int c
, len
, breakline
, nospace
;
421 static const char rejs
[10] = { ' ', '\\', '<', '>', '&', '"',
422 ASCII_NBRSP
, ASCII_HYPH
, ASCII_BREAK
, '\0' };
425 pend
= strchr(p
, '\0');
431 if (HTML_SKIPCHAR
& h
->flags
&& '\\' != *p
) {
432 h
->flags
&= ~HTML_SKIPCHAR
;
437 for (sz
= strcspn(p
, rejs
); sz
-- && p
< pend
; p
++)
441 (p
>= pend
|| *p
== ' ' || *p
== ASCII_NBRSP
)) {
442 print_otag(h
, TAG_BR
, "");
444 while (p
< pend
&& (*p
== ' ' || *p
== ASCII_NBRSP
))
458 if (print_escape(h
, *p
++))
461 esc
= mandoc_escape(&p
, &seq
, &len
);
464 case ESCAPE_FONTPREV
:
465 case ESCAPE_FONTBOLD
:
466 case ESCAPE_FONTITALIC
:
469 case ESCAPE_FONTROMAN
:
470 if (0 == norecurse
) {
471 h
->flags
|= HTML_NOSPACE
;
472 if (html_setfont(h
, esc
))
474 h
->flags
&= ~HTML_NOSPACE
;
477 case ESCAPE_SKIPCHAR
:
478 h
->flags
|= HTML_SKIPCHAR
;
486 if (h
->flags
& HTML_SKIPCHAR
) {
487 h
->flags
&= ~HTML_SKIPCHAR
;
493 /* Skip past "u" header. */
494 c
= mchars_num2uc(seq
+ 1, len
- 1);
496 case ESCAPE_NUMBERED
:
497 c
= mchars_num2char(seq
, len
);
502 c
= mchars_spec2cp(seq
, len
);
510 print_word(h
, "html");
519 case ESCAPE_OVERSTRIKE
:
527 if ((c
< 0x20 && c
!= 0x09) ||
528 (c
> 0x7E && c
< 0xA0))
531 (void)snprintf(numbuf
, sizeof(numbuf
), "&#x%.4X;", c
);
532 print_word(h
, numbuf
);
533 } else if (print_escape(h
, c
) == 0)
541 print_href(struct html
*h
, const char *name
, const char *sec
, int man
)
549 if (h
->base_man2
!= NULL
) {
550 mandoc_asprintf(&filename
, "%s.%s", name
, sec
);
551 if (stat(filename
, &sb
) == -1)
556 pp
= h
->base_includes
;
558 while ((p
= strchr(pp
, '%')) != NULL
) {
559 print_encode(h
, pp
, p
, 1);
560 if (man
&& p
[1] == 'S') {
564 print_encode(h
, sec
, NULL
, 1);
565 } else if ((man
&& p
[1] == 'N') ||
566 (man
== 0 && p
[1] == 'I'))
567 print_encode(h
, name
, NULL
, 1);
569 print_encode(h
, p
, p
+ 2, 1);
573 print_encode(h
, pp
, NULL
, 1);
577 print_otag(struct html
*h
, enum htmltag tag
, const char *fmt
, ...)
583 int style_written
, tflags
;
585 tflags
= htmltags
[tag
].flags
;
587 /* Flow content is not allowed in phrasing context. */
589 if ((tflags
& HTML_INPHRASE
) == 0) {
590 for (t
= h
->tag
; t
!= NULL
; t
= t
->next
) {
593 assert((htmltags
[t
->tag
].flags
& HTML_TOPHRASE
) == 0);
598 /* Push this tag onto the stack of open scopes. */
600 if ((tflags
& HTML_NOSTACK
) == 0) {
601 t
= mandoc_malloc(sizeof(struct tag
));
610 if (tflags
& HTML_NLBEFORE
)
614 else if ((h
->flags
& HTML_NOSPACE
) == 0) {
615 if (h
->flags
& HTML_KEEP
)
616 print_word(h
, " ");
618 if (h
->flags
& HTML_PREKEEP
)
619 h
->flags
|= HTML_KEEP
;
624 if ( ! (h
->flags
& HTML_NONOSPACE
))
625 h
->flags
&= ~HTML_NOSPACE
;
627 h
->flags
|= HTML_NOSPACE
;
629 /* Print out the tag name and attributes. */
632 print_word(h
, htmltags
[tag
].name
);
636 while (*fmt
!= '\0' && *fmt
!= 's') {
638 /* Parse attributes and arguments. */
640 arg1
= va_arg(ap
, char *);
654 arg1
= va_arg(ap
, char *);
660 arg2
= va_arg(ap
, char *);
664 /* Print the attributes. */
672 print_href(h
, arg1
, NULL
, 0);
676 print_href(h
, arg1
, arg2
, 1);
681 print_encode(h
, arg1
, NULL
, 1);
685 print_encode(h
, arg1
, NULL
, 1);
692 while (*fmt
++ == 's') {
693 arg1
= va_arg(ap
, char *);
694 arg2
= va_arg(ap
, char *);
698 if (style_written
== 0) {
699 print_word(h
, "style=\"");
713 /* Accommodate for "well-formed" singleton escaping. */
715 if (htmltags
[tag
].flags
& HTML_NOSTACK
)
720 if (tflags
& HTML_NLBEGIN
)
723 h
->flags
|= HTML_NOSPACE
;
725 if (tflags
& HTML_INDENT
)
727 if (tflags
& HTML_NOINDENT
)
734 print_ctag(struct html
*h
, struct tag
*tag
)
738 if (tag
->closed
== 0) {
745 tflags
= htmltags
[tag
->tag
].flags
;
746 if (tflags
& HTML_INDENT
)
748 if (tflags
& HTML_NOINDENT
)
750 if (tflags
& HTML_NLEND
)
755 print_word(h
, htmltags
[tag
->tag
].name
);
757 if (tflags
& HTML_NLAFTER
)
760 if (tag
->refcnt
== 0) {
767 print_gen_decls(struct html
*h
)
769 print_word(h
, "<!DOCTYPE html>");
774 print_gen_comment(struct html
*h
, struct roff_node
*n
)
778 print_word(h
, "<!-- This is an automatically generated file."
782 while (n
!= NULL
&& n
->type
== ROFFT_COMMENT
) {
783 if (strstr(n
->string
, "-->") == NULL
&&
784 (wantblank
|| *n
->string
!= '\0')) {
787 print_word(h
, n
->string
);
788 wantblank
= *n
->string
!= '\0';
794 print_word(h
, " -->");
800 print_text(struct html
*h
, const char *word
)
802 if (h
->col
&& (h
->flags
& HTML_NOSPACE
) == 0) {
803 if ( ! (HTML_KEEP
& h
->flags
)) {
804 if (HTML_PREKEEP
& h
->flags
)
805 h
->flags
|= HTML_KEEP
;
808 print_word(h
, " ");
811 assert(h
->metaf
== NULL
);
814 if ( ! print_encode(h
, word
, NULL
, 0)) {
815 if ( ! (h
->flags
& HTML_NONOSPACE
))
816 h
->flags
&= ~HTML_NOSPACE
;
817 h
->flags
&= ~HTML_NONEWLINE
;
819 h
->flags
|= HTML_NOSPACE
| HTML_NONEWLINE
;
821 if (h
->metaf
!= NULL
) {
822 print_tagq(h
, h
->metaf
);
826 h
->flags
&= ~HTML_IGNDELIM
;
830 print_tagq(struct html
*h
, const struct tag
*until
)
832 struct tag
*this, *next
;
834 for (this = h
->tag
; this != NULL
; this = next
) {
835 next
= this == until
? NULL
: this->next
;
841 * Close out all open elements up to but excluding suntil.
842 * Note that a paragraph just inside stays open together with it
843 * because paragraphs include subsequent phrasing content.
846 print_stagq(struct html
*h
, const struct tag
*suntil
)
848 struct tag
*this, *next
;
850 for (this = h
->tag
; this != NULL
; this = next
) {
852 if (this == suntil
|| (next
== suntil
&&
853 (this->tag
== TAG_P
|| this->tag
== TAG_PRE
)))
860 /***********************************************************************
861 * Low level output functions.
862 * They implement line breaking using a short static buffer.
863 ***********************************************************************/
866 * Buffer one HTML output byte.
867 * If the buffer is full, flush and deactivate it and start a new line.
868 * If the buffer is inactive, print directly.
871 print_byte(struct html
*h
, char c
)
873 if ((h
->flags
& HTML_BUFFER
) == 0) {
879 if (h
->col
+ h
->bufcol
< sizeof(h
->buf
)) {
880 h
->buf
[h
->bufcol
++] = c
;
889 fwrite(h
->buf
, h
->bufcol
, 1, stdout
);
891 h
->col
= (h
->indent
+ 1) * 2 + h
->bufcol
+ 1;
893 h
->flags
&= ~HTML_BUFFER
;
897 * If something was printed on the current output line, end it.
898 * Not to be called right after print_indent().
901 print_endline(struct html
*h
)
908 fwrite(h
->buf
, h
->bufcol
, 1, stdout
);
913 h
->flags
|= HTML_NOSPACE
;
914 h
->flags
&= ~HTML_BUFFER
;
918 * Flush the HTML output buffer.
919 * If it is inactive, activate it.
922 print_endword(struct html
*h
)
929 if ((h
->flags
& HTML_BUFFER
) == 0) {
931 h
->flags
|= HTML_BUFFER
;
932 } else if (h
->bufcol
) {
934 fwrite(h
->buf
, h
->bufcol
, 1, stdout
);
935 h
->col
+= h
->bufcol
+ 1;
941 * If at the beginning of a new output line,
942 * perform indentation and mark the line as containing output.
943 * Make sure to really produce some output right afterwards,
944 * but do not use print_otag() for producing it.
947 print_indent(struct html
*h
)
954 if (h
->noindent
== 0) {
955 h
->col
= h
->indent
* 2;
956 for (i
= 0; i
< h
->col
; i
++)
959 h
->flags
&= ~HTML_NOSPACE
;
963 * Print or buffer some characters
964 * depending on the current HTML output buffer state.
967 print_word(struct html
*h
, const char *cp
)
970 print_byte(h
, *cp
++);