]>
git.cameronkatri.com Git - mandoc.git/blob - html.c
1 /* $Id: html.c,v 1.228 2018/05/21 01:11:31 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>
31 #include "mandoc_aux.h"
42 #define HTML_NOSTACK (1 << 0)
43 #define HTML_AUTOCLOSE (1 << 1)
44 #define HTML_NLBEFORE (1 << 2)
45 #define HTML_NLBEGIN (1 << 3)
46 #define HTML_NLEND (1 << 4)
47 #define HTML_NLAFTER (1 << 5)
48 #define HTML_NLAROUND (HTML_NLBEFORE | HTML_NLAFTER)
49 #define HTML_NLINSIDE (HTML_NLBEGIN | HTML_NLEND)
50 #define HTML_NLALL (HTML_NLAROUND | HTML_NLINSIDE)
51 #define HTML_INDENT (1 << 6)
52 #define HTML_NOINDENT (1 << 7)
55 static const struct htmldata htmltags
[TAG_MAX
] = {
57 {"head", HTML_NLALL
| HTML_INDENT
},
59 {"meta", HTML_NOSTACK
| HTML_AUTOCLOSE
| HTML_NLALL
},
60 {"title", HTML_NLAROUND
},
61 {"div", HTML_NLAROUND
},
63 {"h1", HTML_NLAROUND
},
64 {"h2", HTML_NLAROUND
},
66 {"link", HTML_NOSTACK
| HTML_AUTOCLOSE
| HTML_NLALL
},
67 {"br", HTML_NOSTACK
| HTML_AUTOCLOSE
| HTML_NLALL
},
69 {"table", HTML_NLALL
| HTML_INDENT
},
70 {"colgroup", HTML_NLALL
| HTML_INDENT
},
71 {"col", HTML_NOSTACK
| HTML_AUTOCLOSE
| HTML_NLALL
},
72 {"tr", HTML_NLALL
| HTML_INDENT
},
73 {"td", HTML_NLAROUND
},
74 {"li", HTML_NLAROUND
| HTML_INDENT
},
75 {"ul", HTML_NLALL
| HTML_INDENT
},
76 {"ol", HTML_NLALL
| HTML_INDENT
},
77 {"dl", HTML_NLALL
| HTML_INDENT
},
78 {"dt", HTML_NLAROUND
},
79 {"dd", HTML_NLAROUND
| HTML_INDENT
},
80 {"pre", HTML_NLALL
| HTML_NOINDENT
},
87 {"style", HTML_NLALL
| HTML_INDENT
},
88 {"math", HTML_NLALL
| HTML_INDENT
},
107 static const char *const roffscales
[SCALE_MAX
] = {
120 static void a2width(const char *, struct roffsu
*);
121 static void print_byte(struct html
*, char);
122 static void print_endword(struct html
*);
123 static void print_indent(struct html
*);
124 static void print_word(struct html
*, const char *);
126 static void print_ctag(struct html
*, struct tag
*);
127 static int print_escape(struct html
*, char);
128 static int print_encode(struct html
*, const char *, const char *, int);
129 static void print_href(struct html
*, const char *, const char *, int);
130 static void print_metaf(struct html
*, enum mandoc_esc
);
134 html_alloc(const struct manoutput
*outopts
)
138 h
= mandoc_calloc(1, sizeof(struct html
));
141 h
->style
= outopts
->style
;
142 h
->base_man
= outopts
->man
;
143 h
->base_includes
= outopts
->includes
;
144 if (outopts
->fragment
)
145 h
->oflags
|= HTML_FRAGMENT
;
156 h
= (struct html
*)p
;
158 while ((tag
= h
->tag
) != NULL
) {
167 print_gen_head(struct html
*h
)
171 print_otag(h
, TAG_META
, "?", "charset", "utf-8");
172 if (h
->style
!= NULL
) {
173 print_otag(h
, TAG_LINK
, "?h??", "rel", "stylesheet",
174 h
->style
, "type", "text/css", "media", "all");
179 * Print a minimal embedded style sheet.
182 t
= print_otag(h
, TAG_STYLE
, "");
183 print_text(h
, "table.head, table.foot { width: 100%; }");
185 print_text(h
, "td.head-rtitle, td.foot-os { text-align: right; }");
187 print_text(h
, "td.head-vol { text-align: center; }");
189 print_text(h
, "div.Pp { margin: 1ex 0ex; }");
191 print_text(h
, "div.Nd, div.Bf, div.Op { display: inline; }");
193 print_text(h
, "span.Pa, span.Ad { font-style: italic; }");
195 print_text(h
, "span.Ms { font-weight: bold; }");
197 print_text(h
, "dl.Bl-diag ");
199 print_text(h
, " dt { font-weight: bold; }");
201 print_text(h
, "code.Nm, code.Fl, code.Cm, code.Ic, "
202 "code.In, code.Fd, code.Fn,");
204 print_text(h
, "code.Cd { font-weight: bold; "
205 "font-family: inherit; }");
210 print_metaf(struct html
*h
, enum mandoc_esc deco
)
215 case ESCAPE_FONTPREV
:
218 case ESCAPE_FONTITALIC
:
219 font
= HTMLFONT_ITALIC
;
221 case ESCAPE_FONTBOLD
:
222 font
= HTMLFONT_BOLD
;
228 case ESCAPE_FONTROMAN
:
229 font
= HTMLFONT_NONE
;
236 print_tagq(h
, h
->metaf
);
244 case HTMLFONT_ITALIC
:
245 h
->metaf
= print_otag(h
, TAG_I
, "");
248 h
->metaf
= print_otag(h
, TAG_B
, "");
251 h
->metaf
= print_otag(h
, TAG_B
, "");
252 print_otag(h
, TAG_I
, "");
260 html_make_id(const struct roff_node
*n
)
262 const struct roff_node
*nch
;
265 for (nch
= n
->child
; nch
!= NULL
; nch
= nch
->next
)
266 if (nch
->type
!= ROFFT_TEXT
)
274 /* http://www.w3.org/TR/html5/dom.html#the-id-attribute */
276 for (cp
= buf
; *cp
!= '\0'; cp
++)
284 html_strlen(const char *cp
)
290 * Account for escaped sequences within string length
291 * calculations. This follows the logic in term_strlen() as we
292 * must calculate the width of produced strings.
293 * Assume that characters are always width of "1". This is
294 * hacky, but it gets the job done for approximation of widths.
300 rsz
= strcspn(cp
, "\\");
312 switch (mandoc_escape(&cp
, NULL
, NULL
)) {
316 case ESCAPE_NUMBERED
:
318 case ESCAPE_OVERSTRIKE
:
324 case ESCAPE_SKIPCHAR
:
335 print_escape(struct html
*h
, char c
)
340 print_word(h
, "<");
343 print_word(h
, ">");
346 print_word(h
, "&");
349 print_word(h
, """);
352 print_word(h
, " ");
366 print_encode(struct html
*h
, const char *p
, const char *pend
, int norecurse
)
372 int c
, len
, breakline
, nospace
;
374 static const char rejs
[10] = { ' ', '\\', '<', '>', '&', '"',
375 ASCII_NBRSP
, ASCII_HYPH
, ASCII_BREAK
, '\0' };
378 pend
= strchr(p
, '\0');
384 if (HTML_SKIPCHAR
& h
->flags
&& '\\' != *p
) {
385 h
->flags
&= ~HTML_SKIPCHAR
;
390 for (sz
= strcspn(p
, rejs
); sz
-- && p
< pend
; p
++)
394 (p
>= pend
|| *p
== ' ' || *p
== ASCII_NBRSP
)) {
395 t
= print_otag(h
, TAG_DIV
, "");
396 print_text(h
, "\\~");
399 while (p
< pend
&& (*p
== ' ' || *p
== ASCII_NBRSP
))
413 if (print_escape(h
, *p
++))
416 esc
= mandoc_escape(&p
, &seq
, &len
);
417 if (ESCAPE_ERROR
== esc
)
422 case ESCAPE_FONTPREV
:
423 case ESCAPE_FONTBOLD
:
424 case ESCAPE_FONTITALIC
:
426 case ESCAPE_FONTROMAN
:
430 case ESCAPE_SKIPCHAR
:
431 h
->flags
|= HTML_SKIPCHAR
;
437 if (h
->flags
& HTML_SKIPCHAR
) {
438 h
->flags
&= ~HTML_SKIPCHAR
;
444 /* Skip past "u" header. */
445 c
= mchars_num2uc(seq
+ 1, len
- 1);
447 case ESCAPE_NUMBERED
:
448 c
= mchars_num2char(seq
, len
);
453 c
= mchars_spec2cp(seq
, len
);
464 case ESCAPE_OVERSTRIKE
:
472 if ((c
< 0x20 && c
!= 0x09) ||
473 (c
> 0x7E && c
< 0xA0))
476 (void)snprintf(numbuf
, sizeof(numbuf
), "&#x%.4X;", c
);
477 print_word(h
, numbuf
);
478 } else if (print_escape(h
, c
) == 0)
486 print_href(struct html
*h
, const char *name
, const char *sec
, int man
)
490 pp
= man
? h
->base_man
: 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
, ...)
513 struct roffsu mysu
, *su
;
519 int i
, have_style
, tflags
;
521 tflags
= htmltags
[tag
].flags
;
523 /* Push this tag onto the stack of open scopes. */
525 if ((tflags
& HTML_NOSTACK
) == 0) {
526 t
= mandoc_malloc(sizeof(struct tag
));
533 if (tflags
& HTML_NLBEFORE
)
537 else if ((h
->flags
& HTML_NOSPACE
) == 0) {
538 if (h
->flags
& HTML_KEEP
)
539 print_word(h
, " ");
541 if (h
->flags
& HTML_PREKEEP
)
542 h
->flags
|= HTML_KEEP
;
547 if ( ! (h
->flags
& HTML_NONOSPACE
))
548 h
->flags
&= ~HTML_NOSPACE
;
550 h
->flags
|= HTML_NOSPACE
;
552 /* Print out the tag name and attributes. */
555 print_word(h
, htmltags
[tag
].name
);
560 while (*fmt
!= '\0') {
567 /* Parse a non-style attribute and its arguments. */
569 arg1
= va_arg(ap
, char *);
582 arg1
= va_arg(ap
, char *);
589 arg2
= va_arg(ap
, char *);
593 /* Print the non-style attributes. */
601 print_href(h
, arg1
, NULL
, 0);
605 print_href(h
, arg1
, arg2
, 1);
610 print_encode(h
, arg1
, NULL
, 1);
614 print_encode(h
, arg1
, NULL
, 1);
615 print_word(h
, "\" title=\"");
616 print_encode(h
, arg1
, NULL
, 1);
620 print_encode(h
, arg1
, NULL
, 1);
626 /* Print out styles. */
628 while (*fmt
!= '\0') {
632 /* First letter: input argument type. */
638 SCALE_HS_INIT(su
, i
);
641 arg1
= va_arg(ap
, char *);
644 su
= va_arg(ap
, struct roffsu
*);
647 if ((arg2
= va_arg(ap
, char *)) != NULL
) {
652 if (su
!= NULL
&& su
->unit
== SCALE_EN
&&
653 su
->scale
> 5.9 && su
->scale
< 6.1)
659 /* Make even bold text fit. */
676 /* Second letter: style name. */
683 attr
= "text-indent";
686 attr
= "margin-left";
696 arg1
= va_arg(ap
, char *);
701 if (su
== NULL
&& arg1
== NULL
)
705 print_word(h
, " style=\"");
713 if (su
->unit
== SCALE_MM
&& (v
/= 100.0) == 0.0)
715 else if (su
->unit
== SCALE_BU
)
717 (void)snprintf(numbuf
, sizeof(numbuf
), "%.2f", v
);
718 print_word(h
, numbuf
);
719 print_word(h
, roffscales
[su
->unit
]);
730 /* Accommodate for "well-formed" singleton escaping. */
732 if (HTML_AUTOCLOSE
& htmltags
[tag
].flags
)
737 if (tflags
& HTML_NLBEGIN
)
740 h
->flags
|= HTML_NOSPACE
;
742 if (tflags
& HTML_INDENT
)
744 if (tflags
& HTML_NOINDENT
)
751 print_ctag(struct html
*h
, struct tag
*tag
)
756 * Remember to close out and nullify the current
757 * meta-font and table, if applicable.
764 tflags
= htmltags
[tag
->tag
].flags
;
766 if (tflags
& HTML_INDENT
)
768 if (tflags
& HTML_NOINDENT
)
770 if (tflags
& HTML_NLEND
)
775 print_word(h
, htmltags
[tag
->tag
].name
);
777 if (tflags
& HTML_NLAFTER
)
785 print_gen_decls(struct html
*h
)
787 print_word(h
, "<!DOCTYPE html>");
792 print_gen_comment(struct html
*h
, struct roff_node
*n
)
796 print_word(h
, "<!-- This is an automatically generated file."
800 while (n
!= NULL
&& n
->type
== ROFFT_COMMENT
) {
801 if (strstr(n
->string
, "-->") == NULL
&&
802 (wantblank
|| *n
->string
!= '\0')) {
805 print_word(h
, n
->string
);
806 wantblank
= *n
->string
!= '\0';
812 print_word(h
, " -->");
818 print_text(struct html
*h
, const char *word
)
820 if (h
->col
&& (h
->flags
& HTML_NOSPACE
) == 0) {
821 if ( ! (HTML_KEEP
& h
->flags
)) {
822 if (HTML_PREKEEP
& h
->flags
)
823 h
->flags
|= HTML_KEEP
;
826 print_word(h
, " ");
829 assert(NULL
== h
->metaf
);
831 case HTMLFONT_ITALIC
:
832 h
->metaf
= print_otag(h
, TAG_I
, "");
835 h
->metaf
= print_otag(h
, TAG_B
, "");
838 h
->metaf
= print_otag(h
, TAG_B
, "");
839 print_otag(h
, TAG_I
, "");
847 if ( ! print_encode(h
, word
, NULL
, 0)) {
848 if ( ! (h
->flags
& HTML_NONOSPACE
))
849 h
->flags
&= ~HTML_NOSPACE
;
850 h
->flags
&= ~HTML_NONEWLINE
;
852 h
->flags
|= HTML_NOSPACE
| HTML_NONEWLINE
;
855 print_tagq(h
, h
->metaf
);
859 h
->flags
&= ~HTML_IGNDELIM
;
863 print_tagq(struct html
*h
, const struct tag
*until
)
867 while ((tag
= h
->tag
) != NULL
) {
869 if (until
&& tag
== until
)
875 print_stagq(struct html
*h
, const struct tag
*suntil
)
879 while ((tag
= h
->tag
) != NULL
) {
880 if (suntil
&& tag
== suntil
)
887 print_paragraph(struct html
*h
)
891 t
= print_otag(h
, TAG_DIV
, "c", "Pp");
896 /***********************************************************************
897 * Low level output functions.
898 * They implement line breaking using a short static buffer.
899 ***********************************************************************/
902 * Buffer one HTML output byte.
903 * If the buffer is full, flush and deactivate it and start a new line.
904 * If the buffer is inactive, print directly.
907 print_byte(struct html
*h
, char c
)
909 if ((h
->flags
& HTML_BUFFER
) == 0) {
915 if (h
->col
+ h
->bufcol
< sizeof(h
->buf
)) {
916 h
->buf
[h
->bufcol
++] = c
;
925 fwrite(h
->buf
, h
->bufcol
, 1, stdout
);
927 h
->col
= (h
->indent
+ 1) * 2 + h
->bufcol
+ 1;
929 h
->flags
&= ~HTML_BUFFER
;
933 * If something was printed on the current output line, end it.
934 * Not to be called right after print_indent().
937 print_endline(struct html
*h
)
944 fwrite(h
->buf
, h
->bufcol
, 1, stdout
);
949 h
->flags
|= HTML_NOSPACE
;
950 h
->flags
&= ~HTML_BUFFER
;
954 * Flush the HTML output buffer.
955 * If it is inactive, activate it.
958 print_endword(struct html
*h
)
965 if ((h
->flags
& HTML_BUFFER
) == 0) {
967 h
->flags
|= HTML_BUFFER
;
968 } else if (h
->bufcol
) {
970 fwrite(h
->buf
, h
->bufcol
, 1, stdout
);
971 h
->col
+= h
->bufcol
+ 1;
977 * If at the beginning of a new output line,
978 * perform indentation and mark the line as containing output.
979 * Make sure to really produce some output right afterwards,
980 * but do not use print_otag() for producing it.
983 print_indent(struct html
*h
)
990 if (h
->noindent
== 0) {
991 h
->col
= h
->indent
* 2;
992 for (i
= 0; i
< h
->col
; i
++)
995 h
->flags
&= ~HTML_NOSPACE
;
999 * Print or buffer some characters
1000 * depending on the current HTML output buffer state.
1003 print_word(struct html
*h
, const char *cp
)
1006 print_byte(h
, *cp
++);
1010 * Calculate the scaling unit passed in a `-width' argument. This uses
1011 * either a native scaling unit (e.g., 1i, 2m) or the string length of
1015 a2width(const char *p
, struct roffsu
*su
)
1019 end
= a2roffsu(p
, su
, SCALE_MAX
);
1020 if (end
== NULL
|| *end
!= '\0') {
1021 su
->unit
= SCALE_EN
;
1022 su
->scale
= html_strlen(p
);
1023 } else if (su
->scale
< 0.0)