]>
git.cameronkatri.com Git - mandoc.git/blob - html.c
1 /* $Id: html.c,v 1.262 2020/01/19 18:02: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_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 {"mark", HTML_INPHRASE
| HTML_NOSTACK
},
92 {"math", HTML_INPHRASE
| HTML_NLALL
| HTML_INDENT
},
111 /* Avoid duplicate HTML id= attributes. */
112 static struct ohash id_unique
;
114 static void html_reset_internal(struct html
*);
115 static void print_byte(struct html
*, char);
116 static void print_endword(struct html
*);
117 static void print_indent(struct html
*);
118 static void print_word(struct html
*, const char *);
120 static void print_ctag(struct html
*, struct tag
*);
121 static int print_escape(struct html
*, char);
122 static int print_encode(struct html
*, const char *, const char *, int);
123 static void print_href(struct html
*, const char *, const char *, int);
124 static void print_metaf(struct html
*);
128 html_alloc(const struct manoutput
*outopts
)
132 h
= mandoc_calloc(1, sizeof(struct html
));
135 h
->style
= outopts
->style
;
136 if ((h
->base_man1
= outopts
->man
) == NULL
)
138 else if ((h
->base_man2
= strchr(h
->base_man1
, ';')) != NULL
)
139 *h
->base_man2
++ = '\0';
140 h
->base_includes
= outopts
->includes
;
141 if (outopts
->fragment
)
142 h
->oflags
|= HTML_FRAGMENT
;
144 h
->oflags
|= HTML_TOC
;
146 mandoc_ohash_init(&id_unique
, 4, 0);
152 html_reset_internal(struct html
*h
)
158 while ((tag
= h
->tag
) != NULL
) {
162 cp
= ohash_first(&id_unique
, &slot
);
165 cp
= ohash_next(&id_unique
, &slot
);
167 ohash_delete(&id_unique
);
173 html_reset_internal(p
);
174 mandoc_ohash_init(&id_unique
, 4, 0);
180 html_reset_internal(p
);
185 print_gen_head(struct html
*h
)
189 print_otag(h
, TAG_META
, "?", "charset", "utf-8");
190 if (h
->style
!= NULL
) {
191 print_otag(h
, TAG_LINK
, "?h??", "rel", "stylesheet",
192 h
->style
, "type", "text/css", "media", "all");
197 * Print a minimal embedded style sheet.
200 t
= print_otag(h
, TAG_STYLE
, "");
201 print_text(h
, "table.head, table.foot { width: 100%; }");
203 print_text(h
, "td.head-rtitle, td.foot-os { text-align: right; }");
205 print_text(h
, "td.head-vol { text-align: center; }");
207 print_text(h
, ".Nd, .Bf, .Op { display: inline; }");
209 print_text(h
, ".Pa, .Ad { font-style: italic; }");
211 print_text(h
, ".Ms { font-weight: bold; }");
213 print_text(h
, ".Bl-diag ");
215 print_text(h
, " dt { font-weight: bold; }");
217 print_text(h
, "code.Nm, .Fl, .Cm, .Ic, code.In, .Fd, .Fn, .Cd "
218 "{ font-weight: bold; font-family: inherit; }");
223 html_setfont(struct html
*h
, enum mandoc_esc font
)
226 case ESCAPE_FONTPREV
:
229 case ESCAPE_FONTITALIC
:
230 case ESCAPE_FONTBOLD
:
233 case ESCAPE_FONTROMAN
:
236 font
= ESCAPE_FONTROMAN
;
247 print_metaf(struct html
*h
)
250 print_tagq(h
, h
->metaf
);
254 case ESCAPE_FONTITALIC
:
255 h
->metaf
= print_otag(h
, TAG_I
, "");
257 case ESCAPE_FONTBOLD
:
258 h
->metaf
= print_otag(h
, TAG_B
, "");
261 h
->metaf
= print_otag(h
, TAG_B
, "");
262 print_otag(h
, TAG_I
, "");
265 h
->metaf
= print_otag(h
, TAG_SPAN
, "c", "Li");
273 html_close_paragraph(struct html
*h
)
275 struct tag
*this, *next
;
281 flags
= htmltags
[this->tag
].flags
;
282 if (flags
& (HTML_INPHRASE
| HTML_TOPHRASE
))
284 if ((flags
& HTML_INPHRASE
) == 0)
291 * ROFF_nf switches to no-fill mode, ROFF_fi to fill mode.
292 * TOKEN_NONE does not switch. The old mode is returned.
295 html_fillmode(struct html
*h
, enum roff_tok want
)
300 for (t
= h
->tag
; t
!= NULL
; t
= t
->next
)
301 if (t
->tag
== TAG_PRE
)
304 had
= t
== NULL
? ROFF_fi
: ROFF_nf
;
312 html_close_paragraph(h
);
313 print_otag(h
, TAG_PRE
, "");
325 html_make_id(const struct roff_node
*n
, int unique
)
327 const struct roff_node
*nch
;
328 char *buf
, *bufs
, *cp
;
332 for (nch
= n
->child
; nch
!= NULL
; nch
= nch
->next
)
333 if (nch
->type
!= ROFFT_TEXT
)
342 * In ID attributes, only use ASCII characters that are
343 * permitted in URL-fragment strings according to the
345 * https://url.spec.whatwg.org/#url-fragment-string
348 for (cp
= buf
; *cp
!= '\0'; cp
++)
349 if (isalnum((unsigned char)*cp
) == 0 &&
350 strchr("!$&'()*+,-./:;=?@_~", *cp
) == NULL
)
356 /* Avoid duplicate HTML id= attributes. */
360 slot
= ohash_qlookup(&id_unique
, buf
);
361 cp
= ohash_find(&id_unique
, slot
);
365 if (++suffix
> 127) {
369 mandoc_asprintf(&bufs
, "%s_%d", buf
, suffix
);
370 slot
= ohash_qlookup(&id_unique
, bufs
);
371 cp
= ohash_find(&id_unique
, slot
);
376 ohash_insert(&id_unique
, slot
, buf
);
381 print_escape(struct html
*h
, char c
)
386 print_word(h
, "<");
389 print_word(h
, ">");
392 print_word(h
, "&");
395 print_word(h
, """);
398 print_word(h
, " ");
412 print_encode(struct html
*h
, const char *p
, const char *pend
, int norecurse
)
417 int c
, len
, breakline
, nospace
;
419 static const char rejs
[10] = { ' ', '\\', '<', '>', '&', '"',
420 ASCII_NBRSP
, ASCII_HYPH
, ASCII_BREAK
, '\0' };
423 pend
= strchr(p
, '\0');
429 if (HTML_SKIPCHAR
& h
->flags
&& '\\' != *p
) {
430 h
->flags
&= ~HTML_SKIPCHAR
;
435 for (sz
= strcspn(p
, rejs
); sz
-- && p
< pend
; p
++)
439 (p
>= pend
|| *p
== ' ' || *p
== ASCII_NBRSP
)) {
440 print_otag(h
, TAG_BR
, "");
442 while (p
< pend
&& (*p
== ' ' || *p
== ASCII_NBRSP
))
456 if (print_escape(h
, *p
++))
459 esc
= mandoc_escape(&p
, &seq
, &len
);
462 case ESCAPE_FONTPREV
:
463 case ESCAPE_FONTBOLD
:
464 case ESCAPE_FONTITALIC
:
467 case ESCAPE_FONTROMAN
:
468 if (0 == norecurse
) {
469 h
->flags
|= HTML_NOSPACE
;
470 if (html_setfont(h
, esc
))
472 h
->flags
&= ~HTML_NOSPACE
;
475 case ESCAPE_SKIPCHAR
:
476 h
->flags
|= HTML_SKIPCHAR
;
484 if (h
->flags
& HTML_SKIPCHAR
) {
485 h
->flags
&= ~HTML_SKIPCHAR
;
491 /* Skip past "u" header. */
492 c
= mchars_num2uc(seq
+ 1, len
- 1);
494 case ESCAPE_NUMBERED
:
495 c
= mchars_num2char(seq
, len
);
500 c
= mchars_spec2cp(seq
, len
);
508 print_word(h
, "html");
517 case ESCAPE_OVERSTRIKE
:
525 if ((c
< 0x20 && c
!= 0x09) ||
526 (c
> 0x7E && c
< 0xA0))
529 (void)snprintf(numbuf
, sizeof(numbuf
), "&#x%.4X;", c
);
530 print_word(h
, numbuf
);
531 } else if (print_escape(h
, c
) == 0)
539 print_href(struct html
*h
, const char *name
, const char *sec
, int man
)
547 if (h
->base_man2
!= NULL
) {
548 mandoc_asprintf(&filename
, "%s.%s", name
, sec
);
549 if (stat(filename
, &sb
) == -1)
554 pp
= h
->base_includes
;
556 while ((p
= strchr(pp
, '%')) != NULL
) {
557 print_encode(h
, pp
, p
, 1);
558 if (man
&& p
[1] == 'S') {
562 print_encode(h
, sec
, NULL
, 1);
563 } else if ((man
&& p
[1] == 'N') ||
564 (man
== 0 && p
[1] == 'I'))
565 print_encode(h
, name
, NULL
, 1);
567 print_encode(h
, p
, p
+ 2, 1);
571 print_encode(h
, pp
, NULL
, 1);
575 print_otag(struct html
*h
, enum htmltag tag
, const char *fmt
, ...)
581 int style_written
, tflags
;
583 tflags
= htmltags
[tag
].flags
;
585 /* Flow content is not allowed in phrasing context. */
587 if ((tflags
& HTML_INPHRASE
) == 0) {
588 for (t
= h
->tag
; t
!= NULL
; t
= t
->next
) {
591 assert((htmltags
[t
->tag
].flags
& HTML_TOPHRASE
) == 0);
596 * Always wrap phrasing elements in a paragraph
597 * unless already contained in some flow container;
598 * never put them directly into a section.
601 } else if (tflags
& HTML_TOPHRASE
&& h
->tag
->tag
== TAG_SECTION
)
602 print_otag(h
, TAG_P
, "c", "Pp");
604 /* Push this tag onto the stack of open scopes. */
606 if ((tflags
& HTML_NOSTACK
) == 0) {
607 t
= mandoc_malloc(sizeof(struct tag
));
616 if (tflags
& HTML_NLBEFORE
)
620 else if ((h
->flags
& HTML_NOSPACE
) == 0) {
621 if (h
->flags
& HTML_KEEP
)
622 print_word(h
, " ");
624 if (h
->flags
& HTML_PREKEEP
)
625 h
->flags
|= HTML_KEEP
;
630 if ( ! (h
->flags
& HTML_NONOSPACE
))
631 h
->flags
&= ~HTML_NOSPACE
;
633 h
->flags
|= HTML_NOSPACE
;
635 /* Print out the tag name and attributes. */
638 print_word(h
, htmltags
[tag
].name
);
642 while (*fmt
!= '\0' && *fmt
!= 's') {
644 /* Parse attributes and arguments. */
646 arg1
= va_arg(ap
, char *);
660 arg1
= va_arg(ap
, char *);
666 arg2
= va_arg(ap
, char *);
670 /* Print the attributes. */
678 print_href(h
, arg1
, NULL
, 0);
682 print_href(h
, arg1
, arg2
, 1);
687 print_encode(h
, arg1
, NULL
, 1);
691 print_encode(h
, arg1
, NULL
, 1);
698 while (*fmt
++ == 's') {
699 arg1
= va_arg(ap
, char *);
700 arg2
= va_arg(ap
, char *);
704 if (style_written
== 0) {
705 print_word(h
, "style=\"");
719 /* Accommodate for "well-formed" singleton escaping. */
721 if (htmltags
[tag
].flags
& HTML_NOSTACK
)
726 if (tflags
& HTML_NLBEGIN
)
729 h
->flags
|= HTML_NOSPACE
;
731 if (tflags
& HTML_INDENT
)
733 if (tflags
& HTML_NOINDENT
)
740 print_ctag(struct html
*h
, struct tag
*tag
)
744 if (tag
->closed
== 0) {
751 tflags
= htmltags
[tag
->tag
].flags
;
752 if (tflags
& HTML_INDENT
)
754 if (tflags
& HTML_NOINDENT
)
756 if (tflags
& HTML_NLEND
)
761 print_word(h
, htmltags
[tag
->tag
].name
);
763 if (tflags
& HTML_NLAFTER
)
766 if (tag
->refcnt
== 0) {
773 print_gen_decls(struct html
*h
)
775 print_word(h
, "<!DOCTYPE html>");
780 print_gen_comment(struct html
*h
, struct roff_node
*n
)
784 print_word(h
, "<!-- This is an automatically generated file."
788 while (n
!= NULL
&& n
->type
== ROFFT_COMMENT
) {
789 if (strstr(n
->string
, "-->") == NULL
&&
790 (wantblank
|| *n
->string
!= '\0')) {
793 print_word(h
, n
->string
);
794 wantblank
= *n
->string
!= '\0';
800 print_word(h
, " -->");
806 print_text(struct html
*h
, const char *word
)
809 * Always wrap text in a paragraph unless already contained in
810 * some flow container; never put it directly into a section.
813 if (h
->tag
->tag
== TAG_SECTION
)
814 print_otag(h
, TAG_P
, "c", "Pp");
816 /* Output whitespace before this text? */
818 if (h
->col
&& (h
->flags
& HTML_NOSPACE
) == 0) {
819 if ( ! (HTML_KEEP
& h
->flags
)) {
820 if (HTML_PREKEEP
& h
->flags
)
821 h
->flags
|= HTML_KEEP
;
824 print_word(h
, " ");
828 * Print the text, optionally surrounded by HTML whitespace,
829 * optionally manually switching fonts before and after.
832 assert(h
->metaf
== NULL
);
835 if ( ! print_encode(h
, word
, NULL
, 0)) {
836 if ( ! (h
->flags
& HTML_NONOSPACE
))
837 h
->flags
&= ~HTML_NOSPACE
;
838 h
->flags
&= ~HTML_NONEWLINE
;
840 h
->flags
|= HTML_NOSPACE
| HTML_NONEWLINE
;
842 if (h
->metaf
!= NULL
) {
843 print_tagq(h
, h
->metaf
);
847 h
->flags
&= ~HTML_IGNDELIM
;
851 print_tagq(struct html
*h
, const struct tag
*until
)
853 struct tag
*this, *next
;
855 for (this = h
->tag
; this != NULL
; this = next
) {
856 next
= this == until
? NULL
: this->next
;
862 * Close out all open elements up to but excluding suntil.
863 * Note that a paragraph just inside stays open together with it
864 * because paragraphs include subsequent phrasing content.
867 print_stagq(struct html
*h
, const struct tag
*suntil
)
869 struct tag
*this, *next
;
871 for (this = h
->tag
; this != NULL
; this = next
) {
873 if (this == suntil
|| (next
== suntil
&&
874 (this->tag
== TAG_P
|| this->tag
== TAG_PRE
)))
881 /***********************************************************************
882 * Low level output functions.
883 * They implement line breaking using a short static buffer.
884 ***********************************************************************/
887 * Buffer one HTML output byte.
888 * If the buffer is full, flush and deactivate it and start a new line.
889 * If the buffer is inactive, print directly.
892 print_byte(struct html
*h
, char c
)
894 if ((h
->flags
& HTML_BUFFER
) == 0) {
900 if (h
->col
+ h
->bufcol
< sizeof(h
->buf
)) {
901 h
->buf
[h
->bufcol
++] = c
;
910 fwrite(h
->buf
, h
->bufcol
, 1, stdout
);
912 h
->col
= (h
->indent
+ 1) * 2 + h
->bufcol
+ 1;
914 h
->flags
&= ~HTML_BUFFER
;
918 * If something was printed on the current output line, end it.
919 * Not to be called right after print_indent().
922 print_endline(struct html
*h
)
929 fwrite(h
->buf
, h
->bufcol
, 1, stdout
);
934 h
->flags
|= HTML_NOSPACE
;
935 h
->flags
&= ~HTML_BUFFER
;
939 * Flush the HTML output buffer.
940 * If it is inactive, activate it.
943 print_endword(struct html
*h
)
950 if ((h
->flags
& HTML_BUFFER
) == 0) {
952 h
->flags
|= HTML_BUFFER
;
953 } else if (h
->bufcol
) {
955 fwrite(h
->buf
, h
->bufcol
, 1, stdout
);
956 h
->col
+= h
->bufcol
+ 1;
962 * If at the beginning of a new output line,
963 * perform indentation and mark the line as containing output.
964 * Make sure to really produce some output right afterwards,
965 * but do not use print_otag() for producing it.
968 print_indent(struct html
*h
)
972 if (h
->col
|| h
->noindent
)
975 h
->col
= h
->indent
* 2;
976 for (i
= 0; i
< h
->col
; i
++)
981 * Print or buffer some characters
982 * depending on the current HTML output buffer state.
985 print_word(struct html
*h
, const char *cp
)
988 print_byte(h
, *cp
++);