]>
git.cameronkatri.com Git - mandoc.git/blob - html.c
1 /* $Id: html.c,v 1.264 2020/03/13 15:32:28 schwarze Exp $ */
3 * Copyright (c) 2011-2015, 2017-2020 Ingo Schwarze <schwarze@openbsd.org>
4 * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
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_NLALL
| 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 {"mark", HTML_INPHRASE
},
95 {"math", HTML_INPHRASE
| HTML_NLALL
| HTML_INDENT
},
114 /* Avoid duplicate HTML id= attributes. */
115 static struct ohash id_unique
;
117 static void html_reset_internal(struct html
*);
118 static void print_byte(struct html
*, char);
119 static void print_endword(struct html
*);
120 static void print_indent(struct html
*);
121 static void print_word(struct html
*, const char *);
123 static void print_ctag(struct html
*, struct tag
*);
124 static int print_escape(struct html
*, char);
125 static int print_encode(struct html
*, const char *, const char *, int);
126 static void print_href(struct html
*, const char *, const char *, int);
127 static void print_metaf(struct html
*);
131 html_alloc(const struct manoutput
*outopts
)
135 h
= mandoc_calloc(1, sizeof(struct html
));
138 h
->style
= outopts
->style
;
139 if ((h
->base_man1
= outopts
->man
) == NULL
)
141 else if ((h
->base_man2
= strchr(h
->base_man1
, ';')) != NULL
)
142 *h
->base_man2
++ = '\0';
143 h
->base_includes
= outopts
->includes
;
144 if (outopts
->fragment
)
145 h
->oflags
|= HTML_FRAGMENT
;
147 h
->oflags
|= HTML_TOC
;
149 mandoc_ohash_init(&id_unique
, 4, 0);
155 html_reset_internal(struct html
*h
)
161 while ((tag
= h
->tag
) != NULL
) {
165 cp
= ohash_first(&id_unique
, &slot
);
168 cp
= ohash_next(&id_unique
, &slot
);
170 ohash_delete(&id_unique
);
176 html_reset_internal(p
);
177 mandoc_ohash_init(&id_unique
, 4, 0);
183 html_reset_internal(p
);
188 print_gen_head(struct html
*h
)
192 print_otag(h
, TAG_META
, "?", "charset", "utf-8");
193 if (h
->style
!= NULL
) {
194 print_otag(h
, TAG_LINK
, "?h??", "rel", "stylesheet",
195 h
->style
, "type", "text/css", "media", "all");
200 * Print a minimal embedded style sheet.
203 t
= print_otag(h
, TAG_STYLE
, "");
204 print_text(h
, "table.head, table.foot { width: 100%; }");
206 print_text(h
, "td.head-rtitle, td.foot-os { text-align: right; }");
208 print_text(h
, "td.head-vol { text-align: center; }");
210 print_text(h
, ".Nd, .Bf, .Op { display: inline; }");
212 print_text(h
, ".Pa, .Ad { font-style: italic; }");
214 print_text(h
, ".Ms { font-weight: bold; }");
216 print_text(h
, ".Bl-diag ");
218 print_text(h
, " dt { font-weight: bold; }");
220 print_text(h
, "code.Nm, .Fl, .Cm, .Ic, code.In, .Fd, .Fn, .Cd "
221 "{ font-weight: bold; font-family: inherit; }");
226 html_setfont(struct html
*h
, enum mandoc_esc font
)
229 case ESCAPE_FONTPREV
:
232 case ESCAPE_FONTITALIC
:
233 case ESCAPE_FONTBOLD
:
236 case ESCAPE_FONTROMAN
:
239 font
= ESCAPE_FONTROMAN
;
250 print_metaf(struct html
*h
)
253 print_tagq(h
, h
->metaf
);
257 case ESCAPE_FONTITALIC
:
258 h
->metaf
= print_otag(h
, TAG_I
, "");
260 case ESCAPE_FONTBOLD
:
261 h
->metaf
= print_otag(h
, TAG_B
, "");
264 h
->metaf
= print_otag(h
, TAG_B
, "");
265 print_otag(h
, TAG_I
, "");
268 h
->metaf
= print_otag(h
, TAG_SPAN
, "c", "Li");
276 html_close_paragraph(struct html
*h
)
278 struct tag
*this, *next
;
284 flags
= htmltags
[this->tag
].flags
;
285 if (flags
& (HTML_INPHRASE
| HTML_TOPHRASE
))
287 if ((flags
& HTML_INPHRASE
) == 0)
294 * ROFF_nf switches to no-fill mode, ROFF_fi to fill mode.
295 * TOKEN_NONE does not switch. The old mode is returned.
298 html_fillmode(struct html
*h
, enum roff_tok want
)
303 for (t
= h
->tag
; t
!= NULL
; t
= t
->next
)
304 if (t
->tag
== TAG_PRE
)
307 had
= t
== NULL
? ROFF_fi
: ROFF_nf
;
315 html_close_paragraph(h
);
316 print_otag(h
, TAG_PRE
, "");
328 * Allocate a string to be used for the "id=" attribute of an HTML
329 * element and/or as a segment identifier for a URI in an <a> element.
330 * The function may fail and return NULL if the node lacks text data
331 * to create the attribute from.
332 * If the "unique" argument is 0, the caller is responsible for
333 * free(3)ing the returned string after using it.
334 * If the "unique" argument is non-zero, the "id_unique" ohash table
335 * is used for de-duplication and owns the returned string, so the
336 * caller must not free(3) it. In this case, it will be freed
337 * automatically by html_reset() or html_free().
340 html_make_id(const struct roff_node
*n
, int unique
)
342 const struct roff_node
*nch
;
343 char *buf
, *bufs
, *cp
;
347 if (n
->string
!= NULL
)
348 buf
= mandoc_strdup(n
->string
);
356 for (nch
= n
->child
; nch
!= NULL
; nch
= nch
->next
)
357 if (nch
->type
!= ROFFT_TEXT
)
365 if (n
->child
->type
!= ROFFT_TEXT
)
367 buf
= mandoc_strdup(n
->child
->string
);
373 * In ID attributes, only use ASCII characters that are
374 * permitted in URL-fragment strings according to the
376 * https://url.spec.whatwg.org/#url-fragment-string
379 for (cp
= buf
; *cp
!= '\0'; cp
++)
380 if (isalnum((unsigned char)*cp
) == 0 &&
381 strchr("!$&'()*+,-./:;=?@_~", *cp
) == NULL
)
387 /* Avoid duplicate HTML id= attributes. */
391 slot
= ohash_qlookup(&id_unique
, buf
);
392 cp
= ohash_find(&id_unique
, slot
);
396 if (++suffix
> 127) {
400 mandoc_asprintf(&bufs
, "%s_%d", buf
, suffix
);
401 slot
= ohash_qlookup(&id_unique
, bufs
);
402 cp
= ohash_find(&id_unique
, slot
);
407 ohash_insert(&id_unique
, slot
, buf
);
412 print_escape(struct html
*h
, char c
)
417 print_word(h
, "<");
420 print_word(h
, ">");
423 print_word(h
, "&");
426 print_word(h
, """);
429 print_word(h
, " ");
443 print_encode(struct html
*h
, const char *p
, const char *pend
, int norecurse
)
448 int c
, len
, breakline
, nospace
;
450 static const char rejs
[10] = { ' ', '\\', '<', '>', '&', '"',
451 ASCII_NBRSP
, ASCII_HYPH
, ASCII_BREAK
, '\0' };
454 pend
= strchr(p
, '\0');
460 if (HTML_SKIPCHAR
& h
->flags
&& '\\' != *p
) {
461 h
->flags
&= ~HTML_SKIPCHAR
;
466 for (sz
= strcspn(p
, rejs
); sz
-- && p
< pend
; p
++)
470 (p
>= pend
|| *p
== ' ' || *p
== ASCII_NBRSP
)) {
471 print_otag(h
, TAG_BR
, "");
473 while (p
< pend
&& (*p
== ' ' || *p
== ASCII_NBRSP
))
487 if (print_escape(h
, *p
++))
490 esc
= mandoc_escape(&p
, &seq
, &len
);
493 case ESCAPE_FONTPREV
:
494 case ESCAPE_FONTBOLD
:
495 case ESCAPE_FONTITALIC
:
498 case ESCAPE_FONTROMAN
:
499 if (0 == norecurse
) {
500 h
->flags
|= HTML_NOSPACE
;
501 if (html_setfont(h
, esc
))
503 h
->flags
&= ~HTML_NOSPACE
;
506 case ESCAPE_SKIPCHAR
:
507 h
->flags
|= HTML_SKIPCHAR
;
515 if (h
->flags
& HTML_SKIPCHAR
) {
516 h
->flags
&= ~HTML_SKIPCHAR
;
522 /* Skip past "u" header. */
523 c
= mchars_num2uc(seq
+ 1, len
- 1);
525 case ESCAPE_NUMBERED
:
526 c
= mchars_num2char(seq
, len
);
531 c
= mchars_spec2cp(seq
, len
);
539 print_word(h
, "html");
548 case ESCAPE_OVERSTRIKE
:
556 if ((c
< 0x20 && c
!= 0x09) ||
557 (c
> 0x7E && c
< 0xA0))
560 (void)snprintf(numbuf
, sizeof(numbuf
), "&#x%.4X;", c
);
561 print_word(h
, numbuf
);
562 } else if (print_escape(h
, c
) == 0)
570 print_href(struct html
*h
, const char *name
, const char *sec
, int man
)
578 if (h
->base_man2
!= NULL
) {
579 mandoc_asprintf(&filename
, "%s.%s", name
, sec
);
580 if (stat(filename
, &sb
) == -1)
585 pp
= h
->base_includes
;
587 while ((p
= strchr(pp
, '%')) != NULL
) {
588 print_encode(h
, pp
, p
, 1);
589 if (man
&& p
[1] == 'S') {
593 print_encode(h
, sec
, NULL
, 1);
594 } else if ((man
&& p
[1] == 'N') ||
595 (man
== 0 && p
[1] == 'I'))
596 print_encode(h
, name
, NULL
, 1);
598 print_encode(h
, p
, p
+ 2, 1);
602 print_encode(h
, pp
, NULL
, 1);
606 print_otag(struct html
*h
, enum htmltag tag
, const char *fmt
, ...)
612 int style_written
, tflags
;
614 tflags
= htmltags
[tag
].flags
;
616 /* Flow content is not allowed in phrasing context. */
618 if ((tflags
& HTML_INPHRASE
) == 0) {
619 for (t
= h
->tag
; t
!= NULL
; t
= t
->next
) {
622 assert((htmltags
[t
->tag
].flags
& HTML_TOPHRASE
) == 0);
627 * Always wrap phrasing elements in a paragraph
628 * unless already contained in some flow container;
629 * never put them directly into a section.
632 } else if (tflags
& HTML_TOPHRASE
&& h
->tag
->tag
== TAG_SECTION
)
633 print_otag(h
, TAG_P
, "c", "Pp");
635 /* Push this tag onto the stack of open scopes. */
637 if ((tflags
& HTML_NOSTACK
) == 0) {
638 t
= mandoc_malloc(sizeof(struct tag
));
647 if (tflags
& HTML_NLBEFORE
)
651 else if ((h
->flags
& HTML_NOSPACE
) == 0) {
652 if (h
->flags
& HTML_KEEP
)
653 print_word(h
, " ");
655 if (h
->flags
& HTML_PREKEEP
)
656 h
->flags
|= HTML_KEEP
;
661 if ( ! (h
->flags
& HTML_NONOSPACE
))
662 h
->flags
&= ~HTML_NOSPACE
;
664 h
->flags
|= HTML_NOSPACE
;
666 /* Print out the tag name and attributes. */
669 print_word(h
, htmltags
[tag
].name
);
673 while (*fmt
!= '\0' && *fmt
!= 's') {
675 /* Parse attributes and arguments. */
677 arg1
= va_arg(ap
, char *);
691 arg1
= va_arg(ap
, char *);
697 arg2
= va_arg(ap
, char *);
701 /* Print the attributes. */
709 print_href(h
, arg1
, NULL
, 0);
713 print_href(h
, arg1
, arg2
, 1);
718 print_encode(h
, arg1
, NULL
, 1);
722 print_encode(h
, arg1
, NULL
, 1);
729 while (*fmt
++ == 's') {
730 arg1
= va_arg(ap
, char *);
731 arg2
= va_arg(ap
, char *);
735 if (style_written
== 0) {
736 print_word(h
, "style=\"");
750 /* Accommodate for "well-formed" singleton escaping. */
752 if (htmltags
[tag
].flags
& HTML_NOSTACK
)
757 if (tflags
& HTML_NLBEGIN
)
760 h
->flags
|= HTML_NOSPACE
;
762 if (tflags
& HTML_INDENT
)
764 if (tflags
& HTML_NOINDENT
)
771 * Print an element with an optional "id=" attribute.
772 * If there is an "id=" attribute, also add a permalink:
773 * outside if it's a phrasing element, or inside otherwise.
776 print_otag_id(struct html
*h
, enum htmltag elemtype
, const char *cattr
,
784 if (n
->flags
& NODE_ID
)
785 id
= html_make_id(n
, 1);
786 if (id
!= NULL
&& htmltags
[elemtype
].flags
& HTML_INPHRASE
)
787 ret
= print_otag(h
, TAG_A
, "chR", "permalink", id
);
788 t
= print_otag(h
, elemtype
, "ci", cattr
, id
);
792 print_otag(h
, TAG_A
, "chR", "permalink", id
);
798 print_ctag(struct html
*h
, struct tag
*tag
)
802 if (tag
->closed
== 0) {
809 tflags
= htmltags
[tag
->tag
].flags
;
810 if (tflags
& HTML_INDENT
)
812 if (tflags
& HTML_NOINDENT
)
814 if (tflags
& HTML_NLEND
)
819 print_word(h
, htmltags
[tag
->tag
].name
);
821 if (tflags
& HTML_NLAFTER
)
824 if (tag
->refcnt
== 0) {
831 print_gen_decls(struct html
*h
)
833 print_word(h
, "<!DOCTYPE html>");
838 print_gen_comment(struct html
*h
, struct roff_node
*n
)
842 print_word(h
, "<!-- This is an automatically generated file."
846 while (n
!= NULL
&& n
->type
== ROFFT_COMMENT
) {
847 if (strstr(n
->string
, "-->") == NULL
&&
848 (wantblank
|| *n
->string
!= '\0')) {
851 print_word(h
, n
->string
);
852 wantblank
= *n
->string
!= '\0';
858 print_word(h
, " -->");
864 print_text(struct html
*h
, const char *word
)
867 * Always wrap text in a paragraph unless already contained in
868 * some flow container; never put it directly into a section.
871 if (h
->tag
->tag
== TAG_SECTION
)
872 print_otag(h
, TAG_P
, "c", "Pp");
874 /* Output whitespace before this text? */
876 if (h
->col
&& (h
->flags
& HTML_NOSPACE
) == 0) {
877 if ( ! (HTML_KEEP
& h
->flags
)) {
878 if (HTML_PREKEEP
& h
->flags
)
879 h
->flags
|= HTML_KEEP
;
882 print_word(h
, " ");
886 * Print the text, optionally surrounded by HTML whitespace,
887 * optionally manually switching fonts before and after.
890 assert(h
->metaf
== NULL
);
893 if ( ! print_encode(h
, word
, NULL
, 0)) {
894 if ( ! (h
->flags
& HTML_NONOSPACE
))
895 h
->flags
&= ~HTML_NOSPACE
;
896 h
->flags
&= ~HTML_NONEWLINE
;
898 h
->flags
|= HTML_NOSPACE
| HTML_NONEWLINE
;
900 if (h
->metaf
!= NULL
) {
901 print_tagq(h
, h
->metaf
);
905 h
->flags
&= ~HTML_IGNDELIM
;
909 print_tagq(struct html
*h
, const struct tag
*until
)
911 struct tag
*this, *next
;
913 for (this = h
->tag
; this != NULL
; this = next
) {
914 next
= this == until
? NULL
: this->next
;
920 * Close out all open elements up to but excluding suntil.
921 * Note that a paragraph just inside stays open together with it
922 * because paragraphs include subsequent phrasing content.
925 print_stagq(struct html
*h
, const struct tag
*suntil
)
927 struct tag
*this, *next
;
929 for (this = h
->tag
; this != NULL
; this = next
) {
931 if (this == suntil
|| (next
== suntil
&&
932 (this->tag
== TAG_P
|| this->tag
== TAG_PRE
)))
939 /***********************************************************************
940 * Low level output functions.
941 * They implement line breaking using a short static buffer.
942 ***********************************************************************/
945 * Buffer one HTML output byte.
946 * If the buffer is full, flush and deactivate it and start a new line.
947 * If the buffer is inactive, print directly.
950 print_byte(struct html
*h
, char c
)
952 if ((h
->flags
& HTML_BUFFER
) == 0) {
958 if (h
->col
+ h
->bufcol
< sizeof(h
->buf
)) {
959 h
->buf
[h
->bufcol
++] = c
;
968 fwrite(h
->buf
, h
->bufcol
, 1, stdout
);
970 h
->col
= (h
->indent
+ 1) * 2 + h
->bufcol
+ 1;
972 h
->flags
&= ~HTML_BUFFER
;
976 * If something was printed on the current output line, end it.
977 * Not to be called right after print_indent().
980 print_endline(struct html
*h
)
987 fwrite(h
->buf
, h
->bufcol
, 1, stdout
);
992 h
->flags
|= HTML_NOSPACE
;
993 h
->flags
&= ~HTML_BUFFER
;
997 * Flush the HTML output buffer.
998 * If it is inactive, activate it.
1001 print_endword(struct html
*h
)
1008 if ((h
->flags
& HTML_BUFFER
) == 0) {
1010 h
->flags
|= HTML_BUFFER
;
1011 } else if (h
->bufcol
) {
1013 fwrite(h
->buf
, h
->bufcol
, 1, stdout
);
1014 h
->col
+= h
->bufcol
+ 1;
1020 * If at the beginning of a new output line,
1021 * perform indentation and mark the line as containing output.
1022 * Make sure to really produce some output right afterwards,
1023 * but do not use print_otag() for producing it.
1026 print_indent(struct html
*h
)
1030 if (h
->col
|| h
->noindent
)
1033 h
->col
= h
->indent
* 2;
1034 for (i
= 0; i
< h
->col
; i
++)
1039 * Print or buffer some characters
1040 * depending on the current HTML output buffer state.
1043 print_word(struct html
*h
, const char *cp
)
1046 print_byte(h
, *cp
++);