]>
git.cameronkatri.com Git - mandoc.git/blob - html.c
1 /* $Id: html.c,v 1.247 2018/12/16 00:17:02 schwarze Exp $ */
3 * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011-2015, 2017, 2018 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 {"h1", HTML_NLAROUND
},
67 {"h2", HTML_NLAROUND
},
69 {"link", HTML_NOSTACK
| HTML_AUTOCLOSE
| HTML_NLALL
},
70 {"br", HTML_NOSTACK
| HTML_AUTOCLOSE
| 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 {"pre", HTML_NLALL
| HTML_NOINDENT
},
88 {"style", HTML_NLALL
| HTML_INDENT
},
89 {"math", HTML_NLALL
| HTML_INDENT
},
108 /* Avoid duplicate HTML id= attributes. */
109 static struct ohash id_unique
;
111 static void print_byte(struct html
*, char);
112 static void print_endword(struct html
*);
113 static void print_indent(struct html
*);
114 static void print_word(struct html
*, const char *);
116 static void print_ctag(struct html
*, struct tag
*);
117 static int print_escape(struct html
*, char);
118 static int print_encode(struct html
*, const char *, const char *, int);
119 static void print_href(struct html
*, const char *, const char *, int);
123 html_alloc(const struct manoutput
*outopts
)
127 h
= mandoc_calloc(1, sizeof(struct html
));
130 h
->style
= outopts
->style
;
131 if ((h
->base_man1
= outopts
->man
) == NULL
)
133 else if ((h
->base_man2
= strchr(h
->base_man1
, ';')) != NULL
)
134 *h
->base_man2
++ = '\0';
135 h
->base_includes
= outopts
->includes
;
136 if (outopts
->fragment
)
137 h
->oflags
|= HTML_FRAGMENT
;
139 h
->oflags
|= HTML_TOC
;
141 mandoc_ohash_init(&id_unique
, 4, 0);
154 h
= (struct html
*)p
;
155 while ((tag
= h
->tag
) != NULL
) {
161 cp
= ohash_first(&id_unique
, &slot
);
164 cp
= ohash_next(&id_unique
, &slot
);
166 ohash_delete(&id_unique
);
170 print_gen_head(struct html
*h
)
174 print_otag(h
, TAG_META
, "?", "charset", "utf-8");
175 if (h
->style
!= NULL
) {
176 print_otag(h
, TAG_LINK
, "?h??", "rel", "stylesheet",
177 h
->style
, "type", "text/css", "media", "all");
182 * Print a minimal embedded style sheet.
185 t
= print_otag(h
, TAG_STYLE
, "");
186 print_text(h
, "table.head, table.foot { width: 100%; }");
188 print_text(h
, "td.head-rtitle, td.foot-os { text-align: right; }");
190 print_text(h
, "td.head-vol { text-align: center; }");
192 print_text(h
, "div.Pp { margin: 1ex 0ex; }");
194 print_text(h
, "div.Nd, div.Bf, div.Op { display: inline; }");
196 print_text(h
, "span.Pa, span.Ad { font-style: italic; }");
198 print_text(h
, "span.Ms { font-weight: bold; }");
200 print_text(h
, "dl.Bl-diag ");
202 print_text(h
, " dt { font-weight: bold; }");
204 print_text(h
, "code.Nm, code.Fl, code.Cm, code.Ic, "
205 "code.In, code.Fd, code.Fn,");
207 print_text(h
, "code.Cd { font-weight: bold; "
208 "font-family: inherit; }");
213 print_metaf(struct html
*h
, enum mandoc_esc deco
)
218 case ESCAPE_FONTPREV
:
221 case ESCAPE_FONTITALIC
:
222 font
= HTMLFONT_ITALIC
;
224 case ESCAPE_FONTBOLD
:
225 font
= HTMLFONT_BOLD
;
234 case ESCAPE_FONTROMAN
:
235 font
= HTMLFONT_NONE
;
242 print_tagq(h
, h
->metaf
);
250 case HTMLFONT_ITALIC
:
251 h
->metaf
= print_otag(h
, TAG_I
, "");
254 h
->metaf
= print_otag(h
, TAG_B
, "");
257 h
->metaf
= print_otag(h
, TAG_B
, "");
258 print_otag(h
, TAG_I
, "");
261 h
->metaf
= print_otag(h
, TAG_SPAN
, "c", "Li");
269 html_make_id(const struct roff_node
*n
, int unique
)
271 const struct roff_node
*nch
;
272 char *buf
, *bufs
, *cp
;
276 for (nch
= n
->child
; nch
!= NULL
; nch
= nch
->next
)
277 if (nch
->type
!= ROFFT_TEXT
)
286 * In ID attributes, only use ASCII characters that are
287 * permitted in URL-fragment strings according to the
289 * https://url.spec.whatwg.org/#url-fragment-string
292 for (cp
= buf
; *cp
!= '\0'; cp
++)
293 if (isalnum((unsigned char)*cp
) == 0 &&
294 strchr("!$&'()*+,-./:;=?@_~", *cp
) == NULL
)
300 /* Avoid duplicate HTML id= attributes. */
304 slot
= ohash_qlookup(&id_unique
, buf
);
305 cp
= ohash_find(&id_unique
, slot
);
309 if (++suffix
> 127) {
313 mandoc_asprintf(&bufs
, "%s_%d", buf
, suffix
);
314 slot
= ohash_qlookup(&id_unique
, bufs
);
315 cp
= ohash_find(&id_unique
, slot
);
320 ohash_insert(&id_unique
, slot
, buf
);
325 print_escape(struct html
*h
, char c
)
330 print_word(h
, "<");
333 print_word(h
, ">");
336 print_word(h
, "&");
339 print_word(h
, """);
342 print_word(h
, " ");
356 print_encode(struct html
*h
, const char *p
, const char *pend
, int norecurse
)
361 int c
, len
, breakline
, nospace
;
363 static const char rejs
[10] = { ' ', '\\', '<', '>', '&', '"',
364 ASCII_NBRSP
, ASCII_HYPH
, ASCII_BREAK
, '\0' };
367 pend
= strchr(p
, '\0');
373 if (HTML_SKIPCHAR
& h
->flags
&& '\\' != *p
) {
374 h
->flags
&= ~HTML_SKIPCHAR
;
379 for (sz
= strcspn(p
, rejs
); sz
-- && p
< pend
; p
++)
383 (p
>= pend
|| *p
== ' ' || *p
== ASCII_NBRSP
)) {
384 print_otag(h
, TAG_BR
, "");
386 while (p
< pend
&& (*p
== ' ' || *p
== ASCII_NBRSP
))
400 if (print_escape(h
, *p
++))
403 esc
= mandoc_escape(&p
, &seq
, &len
);
406 case ESCAPE_FONTPREV
:
407 case ESCAPE_FONTBOLD
:
408 case ESCAPE_FONTITALIC
:
411 case ESCAPE_FONTROMAN
:
412 if (0 == norecurse
) {
413 h
->flags
|= HTML_NOSPACE
;
415 h
->flags
&= ~HTML_NOSPACE
;
418 case ESCAPE_SKIPCHAR
:
419 h
->flags
|= HTML_SKIPCHAR
;
427 if (h
->flags
& HTML_SKIPCHAR
) {
428 h
->flags
&= ~HTML_SKIPCHAR
;
434 /* Skip past "u" header. */
435 c
= mchars_num2uc(seq
+ 1, len
- 1);
437 case ESCAPE_NUMBERED
:
438 c
= mchars_num2char(seq
, len
);
443 c
= mchars_spec2cp(seq
, len
);
451 print_word(h
, "html");
460 case ESCAPE_OVERSTRIKE
:
468 if ((c
< 0x20 && c
!= 0x09) ||
469 (c
> 0x7E && c
< 0xA0))
472 (void)snprintf(numbuf
, sizeof(numbuf
), "&#x%.4X;", c
);
473 print_word(h
, numbuf
);
474 } else if (print_escape(h
, c
) == 0)
482 print_href(struct html
*h
, const char *name
, const char *sec
, int man
)
490 if (h
->base_man2
!= NULL
) {
491 mandoc_asprintf(&filename
, "%s.%s", name
, sec
);
492 if (stat(filename
, &sb
) == -1)
497 pp
= h
->base_includes
;
499 while ((p
= strchr(pp
, '%')) != NULL
) {
500 print_encode(h
, pp
, p
, 1);
501 if (man
&& p
[1] == 'S') {
505 print_encode(h
, sec
, NULL
, 1);
506 } else if ((man
&& p
[1] == 'N') ||
507 (man
== 0 && p
[1] == 'I'))
508 print_encode(h
, name
, NULL
, 1);
510 print_encode(h
, p
, p
+ 2, 1);
514 print_encode(h
, pp
, NULL
, 1);
518 print_otag(struct html
*h
, enum htmltag tag
, const char *fmt
, ...)
524 int style_written
, tflags
;
526 tflags
= htmltags
[tag
].flags
;
528 /* Push this tag onto the stack of open scopes. */
530 if ((tflags
& HTML_NOSTACK
) == 0) {
531 t
= mandoc_malloc(sizeof(struct tag
));
538 if (tflags
& HTML_NLBEFORE
)
542 else if ((h
->flags
& HTML_NOSPACE
) == 0) {
543 if (h
->flags
& HTML_KEEP
)
544 print_word(h
, " ");
546 if (h
->flags
& HTML_PREKEEP
)
547 h
->flags
|= HTML_KEEP
;
552 if ( ! (h
->flags
& HTML_NONOSPACE
))
553 h
->flags
&= ~HTML_NOSPACE
;
555 h
->flags
|= HTML_NOSPACE
;
557 /* Print out the tag name and attributes. */
560 print_word(h
, htmltags
[tag
].name
);
564 while (*fmt
!= '\0' && *fmt
!= 's') {
566 /* Parse attributes and arguments. */
568 arg1
= va_arg(ap
, char *);
582 arg1
= va_arg(ap
, char *);
588 arg2
= va_arg(ap
, char *);
592 /* Print the attributes. */
600 print_href(h
, arg1
, NULL
, 0);
604 print_href(h
, arg1
, arg2
, 1);
609 print_encode(h
, arg1
, NULL
, 1);
613 print_encode(h
, arg1
, NULL
, 1);
614 print_word(h
, "\" title=\"");
615 print_encode(h
, arg1
, NULL
, 1);
619 print_encode(h
, arg1
, NULL
, 1);
626 while (*fmt
++ == 's') {
627 arg1
= va_arg(ap
, char *);
628 arg2
= va_arg(ap
, char *);
632 if (style_written
== 0) {
633 print_word(h
, "style=\"");
647 /* Accommodate for "well-formed" singleton escaping. */
649 if (HTML_AUTOCLOSE
& htmltags
[tag
].flags
)
654 if (tflags
& HTML_NLBEGIN
)
657 h
->flags
|= HTML_NOSPACE
;
659 if (tflags
& HTML_INDENT
)
661 if (tflags
& HTML_NOINDENT
)
668 print_ctag(struct html
*h
, struct tag
*tag
)
673 * Remember to close out and nullify the current
674 * meta-font and table, if applicable.
681 tflags
= htmltags
[tag
->tag
].flags
;
683 if (tflags
& HTML_INDENT
)
685 if (tflags
& HTML_NOINDENT
)
687 if (tflags
& HTML_NLEND
)
692 print_word(h
, htmltags
[tag
->tag
].name
);
694 if (tflags
& HTML_NLAFTER
)
702 print_gen_decls(struct html
*h
)
704 print_word(h
, "<!DOCTYPE html>");
709 print_gen_comment(struct html
*h
, struct roff_node
*n
)
713 print_word(h
, "<!-- This is an automatically generated file."
717 while (n
!= NULL
&& n
->type
== ROFFT_COMMENT
) {
718 if (strstr(n
->string
, "-->") == NULL
&&
719 (wantblank
|| *n
->string
!= '\0')) {
722 print_word(h
, n
->string
);
723 wantblank
= *n
->string
!= '\0';
729 print_word(h
, " -->");
735 print_text(struct html
*h
, const char *word
)
737 if (h
->col
&& (h
->flags
& HTML_NOSPACE
) == 0) {
738 if ( ! (HTML_KEEP
& h
->flags
)) {
739 if (HTML_PREKEEP
& h
->flags
)
740 h
->flags
|= HTML_KEEP
;
743 print_word(h
, " ");
746 assert(NULL
== h
->metaf
);
748 case HTMLFONT_ITALIC
:
749 h
->metaf
= print_otag(h
, TAG_I
, "");
752 h
->metaf
= print_otag(h
, TAG_B
, "");
755 h
->metaf
= print_otag(h
, TAG_B
, "");
756 print_otag(h
, TAG_I
, "");
759 h
->metaf
= print_otag(h
, TAG_SPAN
, "c", "Li");
767 if ( ! print_encode(h
, word
, NULL
, 0)) {
768 if ( ! (h
->flags
& HTML_NONOSPACE
))
769 h
->flags
&= ~HTML_NOSPACE
;
770 h
->flags
&= ~HTML_NONEWLINE
;
772 h
->flags
|= HTML_NOSPACE
| HTML_NONEWLINE
;
775 print_tagq(h
, h
->metaf
);
779 h
->flags
&= ~HTML_IGNDELIM
;
783 print_tagq(struct html
*h
, const struct tag
*until
)
787 while ((tag
= h
->tag
) != NULL
) {
789 if (until
&& tag
== until
)
795 print_stagq(struct html
*h
, const struct tag
*suntil
)
799 while ((tag
= h
->tag
) != NULL
) {
800 if (suntil
&& tag
== suntil
)
807 print_paragraph(struct html
*h
)
811 t
= print_otag(h
, TAG_DIV
, "c", "Pp");
816 /***********************************************************************
817 * Low level output functions.
818 * They implement line breaking using a short static buffer.
819 ***********************************************************************/
822 * Buffer one HTML output byte.
823 * If the buffer is full, flush and deactivate it and start a new line.
824 * If the buffer is inactive, print directly.
827 print_byte(struct html
*h
, char c
)
829 if ((h
->flags
& HTML_BUFFER
) == 0) {
835 if (h
->col
+ h
->bufcol
< sizeof(h
->buf
)) {
836 h
->buf
[h
->bufcol
++] = c
;
845 fwrite(h
->buf
, h
->bufcol
, 1, stdout
);
847 h
->col
= (h
->indent
+ 1) * 2 + h
->bufcol
+ 1;
849 h
->flags
&= ~HTML_BUFFER
;
853 * If something was printed on the current output line, end it.
854 * Not to be called right after print_indent().
857 print_endline(struct html
*h
)
864 fwrite(h
->buf
, h
->bufcol
, 1, stdout
);
869 h
->flags
|= HTML_NOSPACE
;
870 h
->flags
&= ~HTML_BUFFER
;
874 * Flush the HTML output buffer.
875 * If it is inactive, activate it.
878 print_endword(struct html
*h
)
885 if ((h
->flags
& HTML_BUFFER
) == 0) {
887 h
->flags
|= HTML_BUFFER
;
888 } else if (h
->bufcol
) {
890 fwrite(h
->buf
, h
->bufcol
, 1, stdout
);
891 h
->col
+= h
->bufcol
+ 1;
897 * If at the beginning of a new output line,
898 * perform indentation and mark the line as containing output.
899 * Make sure to really produce some output right afterwards,
900 * but do not use print_otag() for producing it.
903 print_indent(struct html
*h
)
910 if (h
->noindent
== 0) {
911 h
->col
= h
->indent
* 2;
912 for (i
= 0; i
< h
->col
; i
++)
915 h
->flags
&= ~HTML_NOSPACE
;
919 * Print or buffer some characters
920 * depending on the current HTML output buffer state.
923 print_word(struct html
*h
, const char *cp
)
926 print_byte(h
, *cp
++);