]>
git.cameronkatri.com Git - mandoc.git/blob - html.c
1 /* $Id: html.c,v 1.241 2018/10/02 14:56:47 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);
120 static void print_metaf(struct html
*, enum mandoc_esc
);
124 html_alloc(const struct manoutput
*outopts
)
128 h
= mandoc_calloc(1, sizeof(struct html
));
131 h
->style
= outopts
->style
;
132 if ((h
->base_man1
= outopts
->man
) == NULL
)
134 else if ((h
->base_man2
= strchr(h
->base_man1
, ';')) != NULL
)
135 *h
->base_man2
++ = '\0';
136 h
->base_includes
= outopts
->includes
;
137 if (outopts
->fragment
)
138 h
->oflags
|= HTML_FRAGMENT
;
140 h
->oflags
|= HTML_TOC
;
142 mandoc_ohash_init(&id_unique
, 4, 0);
155 h
= (struct html
*)p
;
156 while ((tag
= h
->tag
) != NULL
) {
162 cp
= ohash_first(&id_unique
, &slot
);
165 cp
= ohash_next(&id_unique
, &slot
);
167 ohash_delete(&id_unique
);
171 print_gen_head(struct html
*h
)
175 print_otag(h
, TAG_META
, "?", "charset", "utf-8");
176 if (h
->style
!= NULL
) {
177 print_otag(h
, TAG_LINK
, "?h??", "rel", "stylesheet",
178 h
->style
, "type", "text/css", "media", "all");
183 * Print a minimal embedded style sheet.
186 t
= print_otag(h
, TAG_STYLE
, "");
187 print_text(h
, "table.head, table.foot { width: 100%; }");
189 print_text(h
, "td.head-rtitle, td.foot-os { text-align: right; }");
191 print_text(h
, "td.head-vol { text-align: center; }");
193 print_text(h
, "div.Pp { margin: 1ex 0ex; }");
195 print_text(h
, "div.Nd, div.Bf, div.Op { display: inline; }");
197 print_text(h
, "span.Pa, span.Ad { font-style: italic; }");
199 print_text(h
, "span.Ms { font-weight: bold; }");
201 print_text(h
, "dl.Bl-diag ");
203 print_text(h
, " dt { font-weight: bold; }");
205 print_text(h
, "code.Nm, code.Fl, code.Cm, code.Ic, "
206 "code.In, code.Fd, code.Fn,");
208 print_text(h
, "code.Cd { font-weight: bold; "
209 "font-family: inherit; }");
214 print_metaf(struct html
*h
, enum mandoc_esc deco
)
219 case ESCAPE_FONTPREV
:
222 case ESCAPE_FONTITALIC
:
223 font
= HTMLFONT_ITALIC
;
225 case ESCAPE_FONTBOLD
:
226 font
= HTMLFONT_BOLD
;
232 case ESCAPE_FONTROMAN
:
233 font
= HTMLFONT_NONE
;
240 print_tagq(h
, h
->metaf
);
248 case HTMLFONT_ITALIC
:
249 h
->metaf
= print_otag(h
, TAG_I
, "");
252 h
->metaf
= print_otag(h
, TAG_B
, "");
255 h
->metaf
= print_otag(h
, TAG_B
, "");
256 print_otag(h
, TAG_I
, "");
264 html_make_id(const struct roff_node
*n
, int unique
)
266 const struct roff_node
*nch
;
267 char *buf
, *bufs
, *cp
;
271 for (nch
= n
->child
; nch
!= NULL
; nch
= nch
->next
)
272 if (nch
->type
!= ROFFT_TEXT
)
281 * In ID attributes, only use ASCII characters that are
282 * permitted in URL-fragment strings according to the
284 * https://url.spec.whatwg.org/#url-fragment-string
287 for (cp
= buf
; *cp
!= '\0'; cp
++)
288 if (isalnum((unsigned char)*cp
) == 0 &&
289 strchr("!$&'()*+,-./:;=?@_~", *cp
) == NULL
)
295 /* Avoid duplicate HTML id= attributes. */
299 slot
= ohash_qlookup(&id_unique
, buf
);
300 cp
= ohash_find(&id_unique
, slot
);
304 if (++suffix
> 127) {
308 mandoc_asprintf(&bufs
, "%s_%d", buf
, suffix
);
309 slot
= ohash_qlookup(&id_unique
, bufs
);
310 cp
= ohash_find(&id_unique
, slot
);
315 ohash_insert(&id_unique
, slot
, buf
);
320 print_escape(struct html
*h
, char c
)
325 print_word(h
, "<");
328 print_word(h
, ">");
331 print_word(h
, "&");
334 print_word(h
, """);
337 print_word(h
, " ");
351 print_encode(struct html
*h
, const char *p
, const char *pend
, int norecurse
)
357 int c
, len
, breakline
, nospace
;
359 static const char rejs
[10] = { ' ', '\\', '<', '>', '&', '"',
360 ASCII_NBRSP
, ASCII_HYPH
, ASCII_BREAK
, '\0' };
363 pend
= strchr(p
, '\0');
369 if (HTML_SKIPCHAR
& h
->flags
&& '\\' != *p
) {
370 h
->flags
&= ~HTML_SKIPCHAR
;
375 for (sz
= strcspn(p
, rejs
); sz
-- && p
< pend
; p
++)
379 (p
>= pend
|| *p
== ' ' || *p
== ASCII_NBRSP
)) {
380 t
= print_otag(h
, TAG_DIV
, "");
381 print_text(h
, "\\~");
384 while (p
< pend
&& (*p
== ' ' || *p
== ASCII_NBRSP
))
398 if (print_escape(h
, *p
++))
401 esc
= mandoc_escape(&p
, &seq
, &len
);
402 if (ESCAPE_ERROR
== esc
)
407 case ESCAPE_FONTPREV
:
408 case ESCAPE_FONTBOLD
:
409 case ESCAPE_FONTITALIC
:
411 case ESCAPE_FONTROMAN
:
415 case ESCAPE_SKIPCHAR
:
416 h
->flags
|= HTML_SKIPCHAR
;
422 if (h
->flags
& HTML_SKIPCHAR
) {
423 h
->flags
&= ~HTML_SKIPCHAR
;
429 /* Skip past "u" header. */
430 c
= mchars_num2uc(seq
+ 1, len
- 1);
432 case ESCAPE_NUMBERED
:
433 c
= mchars_num2char(seq
, len
);
438 c
= mchars_spec2cp(seq
, len
);
443 print_word(h
, "html");
452 case ESCAPE_OVERSTRIKE
:
460 if ((c
< 0x20 && c
!= 0x09) ||
461 (c
> 0x7E && c
< 0xA0))
464 (void)snprintf(numbuf
, sizeof(numbuf
), "&#x%.4X;", c
);
465 print_word(h
, numbuf
);
466 } else if (print_escape(h
, c
) == 0)
474 print_href(struct html
*h
, const char *name
, const char *sec
, int man
)
482 if (h
->base_man2
!= NULL
) {
483 mandoc_asprintf(&filename
, "%s.%s", name
, sec
);
484 if (stat(filename
, &sb
) == -1)
489 pp
= h
->base_includes
;
491 while ((p
= strchr(pp
, '%')) != NULL
) {
492 print_encode(h
, pp
, p
, 1);
493 if (man
&& p
[1] == 'S') {
497 print_encode(h
, sec
, NULL
, 1);
498 } else if ((man
&& p
[1] == 'N') ||
499 (man
== 0 && p
[1] == 'I'))
500 print_encode(h
, name
, NULL
, 1);
502 print_encode(h
, p
, p
+ 2, 1);
506 print_encode(h
, pp
, NULL
, 1);
510 print_otag(struct html
*h
, enum htmltag tag
, const char *fmt
, ...)
518 tflags
= htmltags
[tag
].flags
;
520 /* Push this tag onto the stack of open scopes. */
522 if ((tflags
& HTML_NOSTACK
) == 0) {
523 t
= mandoc_malloc(sizeof(struct tag
));
530 if (tflags
& HTML_NLBEFORE
)
534 else if ((h
->flags
& HTML_NOSPACE
) == 0) {
535 if (h
->flags
& HTML_KEEP
)
536 print_word(h
, " ");
538 if (h
->flags
& HTML_PREKEEP
)
539 h
->flags
|= HTML_KEEP
;
544 if ( ! (h
->flags
& HTML_NONOSPACE
))
545 h
->flags
&= ~HTML_NOSPACE
;
547 h
->flags
|= HTML_NOSPACE
;
549 /* Print out the tag name and attributes. */
552 print_word(h
, htmltags
[tag
].name
);
556 while (*fmt
!= '\0') {
558 /* Parse attributes and arguments. */
560 arg1
= va_arg(ap
, char *);
574 arg2
= va_arg(ap
, char *);
578 arg1
= va_arg(ap
, char *);
584 arg2
= va_arg(ap
, char *);
588 /* Print the attributes. */
596 print_href(h
, arg1
, NULL
, 0);
600 print_href(h
, arg1
, arg2
, 1);
605 print_encode(h
, arg1
, NULL
, 1);
609 print_encode(h
, arg1
, NULL
, 1);
610 print_word(h
, "\" title=\"");
611 print_encode(h
, arg1
, NULL
, 1);
616 print_encode(h
, arg1
, NULL
, 1);
630 /* Accommodate for "well-formed" singleton escaping. */
632 if (HTML_AUTOCLOSE
& htmltags
[tag
].flags
)
637 if (tflags
& HTML_NLBEGIN
)
640 h
->flags
|= HTML_NOSPACE
;
642 if (tflags
& HTML_INDENT
)
644 if (tflags
& HTML_NOINDENT
)
651 print_ctag(struct html
*h
, struct tag
*tag
)
656 * Remember to close out and nullify the current
657 * meta-font and table, if applicable.
664 tflags
= htmltags
[tag
->tag
].flags
;
666 if (tflags
& HTML_INDENT
)
668 if (tflags
& HTML_NOINDENT
)
670 if (tflags
& HTML_NLEND
)
675 print_word(h
, htmltags
[tag
->tag
].name
);
677 if (tflags
& HTML_NLAFTER
)
685 print_gen_decls(struct html
*h
)
687 print_word(h
, "<!DOCTYPE html>");
692 print_gen_comment(struct html
*h
, struct roff_node
*n
)
696 print_word(h
, "<!-- This is an automatically generated file."
700 while (n
!= NULL
&& n
->type
== ROFFT_COMMENT
) {
701 if (strstr(n
->string
, "-->") == NULL
&&
702 (wantblank
|| *n
->string
!= '\0')) {
705 print_word(h
, n
->string
);
706 wantblank
= *n
->string
!= '\0';
712 print_word(h
, " -->");
718 print_text(struct html
*h
, const char *word
)
720 if (h
->col
&& (h
->flags
& HTML_NOSPACE
) == 0) {
721 if ( ! (HTML_KEEP
& h
->flags
)) {
722 if (HTML_PREKEEP
& h
->flags
)
723 h
->flags
|= HTML_KEEP
;
726 print_word(h
, " ");
729 assert(NULL
== h
->metaf
);
731 case HTMLFONT_ITALIC
:
732 h
->metaf
= print_otag(h
, TAG_I
, "");
735 h
->metaf
= print_otag(h
, TAG_B
, "");
738 h
->metaf
= print_otag(h
, TAG_B
, "");
739 print_otag(h
, TAG_I
, "");
747 if ( ! print_encode(h
, word
, NULL
, 0)) {
748 if ( ! (h
->flags
& HTML_NONOSPACE
))
749 h
->flags
&= ~HTML_NOSPACE
;
750 h
->flags
&= ~HTML_NONEWLINE
;
752 h
->flags
|= HTML_NOSPACE
| HTML_NONEWLINE
;
755 print_tagq(h
, h
->metaf
);
759 h
->flags
&= ~HTML_IGNDELIM
;
763 print_tagq(struct html
*h
, const struct tag
*until
)
767 while ((tag
= h
->tag
) != NULL
) {
769 if (until
&& tag
== until
)
775 print_stagq(struct html
*h
, const struct tag
*suntil
)
779 while ((tag
= h
->tag
) != NULL
) {
780 if (suntil
&& tag
== suntil
)
787 print_paragraph(struct html
*h
)
791 t
= print_otag(h
, TAG_DIV
, "c", "Pp");
796 /***********************************************************************
797 * Low level output functions.
798 * They implement line breaking using a short static buffer.
799 ***********************************************************************/
802 * Buffer one HTML output byte.
803 * If the buffer is full, flush and deactivate it and start a new line.
804 * If the buffer is inactive, print directly.
807 print_byte(struct html
*h
, char c
)
809 if ((h
->flags
& HTML_BUFFER
) == 0) {
815 if (h
->col
+ h
->bufcol
< sizeof(h
->buf
)) {
816 h
->buf
[h
->bufcol
++] = c
;
825 fwrite(h
->buf
, h
->bufcol
, 1, stdout
);
827 h
->col
= (h
->indent
+ 1) * 2 + h
->bufcol
+ 1;
829 h
->flags
&= ~HTML_BUFFER
;
833 * If something was printed on the current output line, end it.
834 * Not to be called right after print_indent().
837 print_endline(struct html
*h
)
844 fwrite(h
->buf
, h
->bufcol
, 1, stdout
);
849 h
->flags
|= HTML_NOSPACE
;
850 h
->flags
&= ~HTML_BUFFER
;
854 * Flush the HTML output buffer.
855 * If it is inactive, activate it.
858 print_endword(struct html
*h
)
865 if ((h
->flags
& HTML_BUFFER
) == 0) {
867 h
->flags
|= HTML_BUFFER
;
868 } else if (h
->bufcol
) {
870 fwrite(h
->buf
, h
->bufcol
, 1, stdout
);
871 h
->col
+= h
->bufcol
+ 1;
877 * If at the beginning of a new output line,
878 * perform indentation and mark the line as containing output.
879 * Make sure to really produce some output right afterwards,
880 * but do not use print_otag() for producing it.
883 print_indent(struct html
*h
)
890 if (h
->noindent
== 0) {
891 h
->col
= h
->indent
* 2;
892 for (i
= 0; i
< h
->col
; i
++)
895 h
->flags
&= ~HTML_NOSPACE
;
899 * Print or buffer some characters
900 * depending on the current HTML output buffer state.
903 print_word(struct html
*h
, const char *cp
)
906 print_byte(h
, *cp
++);