]>
git.cameronkatri.com Git - mandoc.git/blob - html.c
1 /* $Id: html.c,v 1.222 2018/05/01 23:36:55 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
},
62 {"h1", HTML_NLAROUND
},
63 {"h2", HTML_NLAROUND
},
65 {"link", HTML_NOSTACK
| HTML_AUTOCLOSE
| HTML_NLALL
},
66 {"br", HTML_NOSTACK
| HTML_AUTOCLOSE
| HTML_NLALL
},
68 {"table", HTML_NLALL
| HTML_INDENT
},
69 {"colgroup", HTML_NLALL
| HTML_INDENT
},
70 {"col", HTML_NOSTACK
| HTML_AUTOCLOSE
| HTML_NLALL
},
71 {"tr", HTML_NLALL
| HTML_INDENT
},
72 {"td", HTML_NLAROUND
},
73 {"li", HTML_NLAROUND
| HTML_INDENT
},
74 {"ul", HTML_NLALL
| HTML_INDENT
},
75 {"ol", HTML_NLALL
| HTML_INDENT
},
76 {"dl", HTML_NLALL
| HTML_INDENT
},
77 {"dt", HTML_NLAROUND
},
78 {"dd", HTML_NLAROUND
| HTML_INDENT
},
79 {"pre", HTML_NLALL
| HTML_NOINDENT
},
86 {"style", HTML_NLALL
| HTML_INDENT
},
87 {"math", HTML_NLALL
| HTML_INDENT
},
106 static const char *const roffscales
[SCALE_MAX
] = {
119 static void a2width(const char *, struct roffsu
*);
120 static void print_byte(struct html
*, char);
121 static void print_endword(struct html
*);
122 static void print_indent(struct html
*);
123 static void print_word(struct html
*, const char *);
125 static void print_ctag(struct html
*, struct tag
*);
126 static int print_escape(struct html
*, char);
127 static int print_encode(struct html
*, const char *, const char *, int);
128 static void print_href(struct html
*, const char *, const char *, int);
129 static void print_metaf(struct html
*, enum mandoc_esc
);
133 html_alloc(const struct manoutput
*outopts
)
137 h
= mandoc_calloc(1, sizeof(struct html
));
140 h
->style
= outopts
->style
;
141 h
->base_man
= outopts
->man
;
142 h
->base_includes
= outopts
->includes
;
143 if (outopts
->fragment
)
144 h
->oflags
|= HTML_FRAGMENT
;
155 h
= (struct html
*)p
;
157 while ((tag
= h
->tag
) != NULL
) {
166 print_gen_head(struct html
*h
)
170 print_otag(h
, TAG_META
, "?", "charset", "utf-8");
171 if (h
->style
!= NULL
) {
172 print_otag(h
, TAG_LINK
, "?h??", "rel", "stylesheet",
173 h
->style
, "type", "text/css", "media", "all");
178 * Print a minimal embedded style sheet.
181 t
= print_otag(h
, TAG_STYLE
, "");
182 print_text(h
, "table.head, table.foot { width: 100%; }");
184 print_text(h
, "td.head-rtitle, td.foot-os { text-align: right; }");
186 print_text(h
, "td.head-vol { text-align: center; }");
188 print_text(h
, "div.Pp { margin: 1ex 0ex; }");
193 print_metaf(struct html
*h
, enum mandoc_esc deco
)
198 case ESCAPE_FONTPREV
:
201 case ESCAPE_FONTITALIC
:
202 font
= HTMLFONT_ITALIC
;
204 case ESCAPE_FONTBOLD
:
205 font
= HTMLFONT_BOLD
;
211 case ESCAPE_FONTROMAN
:
212 font
= HTMLFONT_NONE
;
219 print_tagq(h
, h
->metaf
);
227 case HTMLFONT_ITALIC
:
228 h
->metaf
= print_otag(h
, TAG_I
, "");
231 h
->metaf
= print_otag(h
, TAG_B
, "");
234 h
->metaf
= print_otag(h
, TAG_B
, "");
235 print_otag(h
, TAG_I
, "");
243 html_make_id(const struct roff_node
*n
)
245 const struct roff_node
*nch
;
248 for (nch
= n
->child
; nch
!= NULL
; nch
= nch
->next
)
249 if (nch
->type
!= ROFFT_TEXT
)
257 /* http://www.w3.org/TR/html5/dom.html#the-id-attribute */
259 for (cp
= buf
; *cp
!= '\0'; cp
++)
267 html_strlen(const char *cp
)
273 * Account for escaped sequences within string length
274 * calculations. This follows the logic in term_strlen() as we
275 * must calculate the width of produced strings.
276 * Assume that characters are always width of "1". This is
277 * hacky, but it gets the job done for approximation of widths.
283 rsz
= strcspn(cp
, "\\");
295 switch (mandoc_escape(&cp
, NULL
, NULL
)) {
299 case ESCAPE_NUMBERED
:
301 case ESCAPE_OVERSTRIKE
:
307 case ESCAPE_SKIPCHAR
:
318 print_escape(struct html
*h
, char c
)
323 print_word(h
, "<");
326 print_word(h
, ">");
329 print_word(h
, "&");
332 print_word(h
, """);
335 print_word(h
, " ");
349 print_encode(struct html
*h
, const char *p
, const char *pend
, int norecurse
)
355 int c
, len
, breakline
, nospace
;
357 static const char rejs
[10] = { ' ', '\\', '<', '>', '&', '"',
358 ASCII_NBRSP
, ASCII_HYPH
, ASCII_BREAK
, '\0' };
361 pend
= strchr(p
, '\0');
367 if (HTML_SKIPCHAR
& h
->flags
&& '\\' != *p
) {
368 h
->flags
&= ~HTML_SKIPCHAR
;
373 for (sz
= strcspn(p
, rejs
); sz
-- && p
< pend
; p
++)
377 (p
>= pend
|| *p
== ' ' || *p
== ASCII_NBRSP
)) {
378 t
= print_otag(h
, TAG_DIV
, "");
379 print_text(h
, "\\~");
382 while (p
< pend
&& (*p
== ' ' || *p
== ASCII_NBRSP
))
396 if (print_escape(h
, *p
++))
399 esc
= mandoc_escape(&p
, &seq
, &len
);
400 if (ESCAPE_ERROR
== esc
)
405 case ESCAPE_FONTPREV
:
406 case ESCAPE_FONTBOLD
:
407 case ESCAPE_FONTITALIC
:
409 case ESCAPE_FONTROMAN
:
413 case ESCAPE_SKIPCHAR
:
414 h
->flags
|= HTML_SKIPCHAR
;
420 if (h
->flags
& HTML_SKIPCHAR
) {
421 h
->flags
&= ~HTML_SKIPCHAR
;
427 /* Skip past "u" header. */
428 c
= mchars_num2uc(seq
+ 1, len
- 1);
430 case ESCAPE_NUMBERED
:
431 c
= mchars_num2char(seq
, len
);
436 c
= mchars_spec2cp(seq
, len
);
447 case ESCAPE_OVERSTRIKE
:
455 if ((c
< 0x20 && c
!= 0x09) ||
456 (c
> 0x7E && c
< 0xA0))
459 (void)snprintf(numbuf
, sizeof(numbuf
), "&#x%.4X;", c
);
460 print_word(h
, numbuf
);
461 } else if (print_escape(h
, c
) == 0)
469 print_href(struct html
*h
, const char *name
, const char *sec
, int man
)
473 pp
= man
? h
->base_man
: h
->base_includes
;
474 while ((p
= strchr(pp
, '%')) != NULL
) {
475 print_encode(h
, pp
, p
, 1);
476 if (man
&& p
[1] == 'S') {
480 print_encode(h
, sec
, NULL
, 1);
481 } else if ((man
&& p
[1] == 'N') ||
482 (man
== 0 && p
[1] == 'I'))
483 print_encode(h
, name
, NULL
, 1);
485 print_encode(h
, p
, p
+ 2, 1);
489 print_encode(h
, pp
, NULL
, 1);
493 print_otag(struct html
*h
, enum htmltag tag
, const char *fmt
, ...)
496 struct roffsu mysu
, *su
;
502 int i
, have_style
, tflags
;
504 tflags
= htmltags
[tag
].flags
;
506 /* Push this tag onto the stack of open scopes. */
508 if ((tflags
& HTML_NOSTACK
) == 0) {
509 t
= mandoc_malloc(sizeof(struct tag
));
516 if (tflags
& HTML_NLBEFORE
)
520 else if ((h
->flags
& HTML_NOSPACE
) == 0) {
521 if (h
->flags
& HTML_KEEP
)
522 print_word(h
, " ");
524 if (h
->flags
& HTML_PREKEEP
)
525 h
->flags
|= HTML_KEEP
;
530 if ( ! (h
->flags
& HTML_NONOSPACE
))
531 h
->flags
&= ~HTML_NOSPACE
;
533 h
->flags
|= HTML_NOSPACE
;
535 /* Print out the tag name and attributes. */
538 print_word(h
, htmltags
[tag
].name
);
543 while (*fmt
!= '\0') {
550 /* Parse a non-style attribute and its arguments. */
552 arg1
= va_arg(ap
, char *);
565 arg1
= va_arg(ap
, char *);
572 arg2
= va_arg(ap
, char *);
576 /* Print the non-style attributes. */
584 print_href(h
, arg1
, NULL
, 0);
588 print_href(h
, arg1
, arg2
, 1);
593 print_encode(h
, arg1
, NULL
, 1);
597 print_encode(h
, arg1
, NULL
, 1);
598 print_word(h
, "\" title=\"");
599 print_encode(h
, arg1
, NULL
, 1);
603 print_encode(h
, arg1
, NULL
, 1);
609 /* Print out styles. */
611 while (*fmt
!= '\0') {
615 /* First letter: input argument type. */
621 SCALE_HS_INIT(su
, i
);
624 arg1
= va_arg(ap
, char *);
627 su
= va_arg(ap
, struct roffsu
*);
630 if ((arg2
= va_arg(ap
, char *)) != NULL
) {
635 if (su
!= NULL
&& su
->unit
== SCALE_EN
&&
636 su
->scale
> 5.9 && su
->scale
< 6.1)
642 /* Make even bold text fit. */
659 /* Second letter: style name. */
666 attr
= "text-indent";
669 attr
= "margin-left";
679 arg1
= va_arg(ap
, char *);
684 if (su
== NULL
&& arg1
== NULL
)
688 print_word(h
, " style=\"");
696 if (su
->unit
== SCALE_MM
&& (v
/= 100.0) == 0.0)
698 else if (su
->unit
== SCALE_BU
)
700 (void)snprintf(numbuf
, sizeof(numbuf
), "%.2f", v
);
701 print_word(h
, numbuf
);
702 print_word(h
, roffscales
[su
->unit
]);
713 /* Accommodate for "well-formed" singleton escaping. */
715 if (HTML_AUTOCLOSE
& htmltags
[tag
].flags
)
720 if (tflags
& HTML_NLBEGIN
)
723 h
->flags
|= HTML_NOSPACE
;
725 if (tflags
& HTML_INDENT
)
727 if (tflags
& HTML_NOINDENT
)
734 print_ctag(struct html
*h
, struct tag
*tag
)
739 * Remember to close out and nullify the current
740 * meta-font and table, if applicable.
747 tflags
= htmltags
[tag
->tag
].flags
;
749 if (tflags
& HTML_INDENT
)
751 if (tflags
& HTML_NOINDENT
)
753 if (tflags
& HTML_NLEND
)
758 print_word(h
, htmltags
[tag
->tag
].name
);
760 if (tflags
& HTML_NLAFTER
)
768 print_gen_decls(struct html
*h
)
770 print_word(h
, "<!DOCTYPE html>");
775 print_gen_comment(struct html
*h
, struct roff_node
*n
)
779 print_word(h
, "<!-- This is an automatically generated file."
783 while (n
!= NULL
&& n
->type
== ROFFT_COMMENT
) {
784 if (strstr(n
->string
, "-->") == NULL
&&
785 (wantblank
|| *n
->string
!= '\0')) {
788 print_word(h
, n
->string
);
789 wantblank
= *n
->string
!= '\0';
795 print_word(h
, " -->");
801 print_text(struct html
*h
, const char *word
)
803 if (h
->col
&& (h
->flags
& HTML_NOSPACE
) == 0) {
804 if ( ! (HTML_KEEP
& h
->flags
)) {
805 if (HTML_PREKEEP
& h
->flags
)
806 h
->flags
|= HTML_KEEP
;
809 print_word(h
, " ");
812 assert(NULL
== h
->metaf
);
814 case HTMLFONT_ITALIC
:
815 h
->metaf
= print_otag(h
, TAG_I
, "");
818 h
->metaf
= print_otag(h
, TAG_B
, "");
821 h
->metaf
= print_otag(h
, TAG_B
, "");
822 print_otag(h
, TAG_I
, "");
830 if ( ! print_encode(h
, word
, NULL
, 0)) {
831 if ( ! (h
->flags
& HTML_NONOSPACE
))
832 h
->flags
&= ~HTML_NOSPACE
;
833 h
->flags
&= ~HTML_NONEWLINE
;
835 h
->flags
|= HTML_NOSPACE
| HTML_NONEWLINE
;
838 print_tagq(h
, h
->metaf
);
842 h
->flags
&= ~HTML_IGNDELIM
;
846 print_tagq(struct html
*h
, const struct tag
*until
)
850 while ((tag
= h
->tag
) != NULL
) {
852 if (until
&& tag
== until
)
858 print_stagq(struct html
*h
, const struct tag
*suntil
)
862 while ((tag
= h
->tag
) != NULL
) {
863 if (suntil
&& tag
== suntil
)
870 print_paragraph(struct html
*h
)
874 t
= print_otag(h
, TAG_DIV
, "c", "Pp");
879 /***********************************************************************
880 * Low level output functions.
881 * They implement line breaking using a short static buffer.
882 ***********************************************************************/
885 * Buffer one HTML output byte.
886 * If the buffer is full, flush and deactivate it and start a new line.
887 * If the buffer is inactive, print directly.
890 print_byte(struct html
*h
, char c
)
892 if ((h
->flags
& HTML_BUFFER
) == 0) {
898 if (h
->col
+ h
->bufcol
< sizeof(h
->buf
)) {
899 h
->buf
[h
->bufcol
++] = c
;
908 fwrite(h
->buf
, h
->bufcol
, 1, stdout
);
910 h
->col
= (h
->indent
+ 1) * 2 + h
->bufcol
+ 1;
912 h
->flags
&= ~HTML_BUFFER
;
916 * If something was printed on the current output line, end it.
917 * Not to be called right after print_indent().
920 print_endline(struct html
*h
)
927 fwrite(h
->buf
, h
->bufcol
, 1, stdout
);
932 h
->flags
|= HTML_NOSPACE
;
933 h
->flags
&= ~HTML_BUFFER
;
937 * Flush the HTML output buffer.
938 * If it is inactive, activate it.
941 print_endword(struct html
*h
)
948 if ((h
->flags
& HTML_BUFFER
) == 0) {
950 h
->flags
|= HTML_BUFFER
;
951 } else if (h
->bufcol
) {
953 fwrite(h
->buf
, h
->bufcol
, 1, stdout
);
954 h
->col
+= h
->bufcol
+ 1;
960 * If at the beginning of a new output line,
961 * perform indentation and mark the line as containing output.
962 * Make sure to really produce some output right afterwards,
963 * but do not use print_otag() for producing it.
966 print_indent(struct html
*h
)
973 if (h
->noindent
== 0) {
974 h
->col
= h
->indent
* 2;
975 for (i
= 0; i
< h
->col
; i
++)
978 h
->flags
&= ~HTML_NOSPACE
;
982 * Print or buffer some characters
983 * depending on the current HTML output buffer state.
986 print_word(struct html
*h
, const char *cp
)
989 print_byte(h
, *cp
++);
993 * Calculate the scaling unit passed in a `-width' argument. This uses
994 * either a native scaling unit (e.g., 1i, 2m) or the string length of
998 a2width(const char *p
, struct roffsu
*su
)
1002 end
= a2roffsu(p
, su
, SCALE_MAX
);
1003 if (end
== NULL
|| *end
!= '\0') {
1004 su
->unit
= SCALE_EN
;
1005 su
->scale
= html_strlen(p
);
1006 } else if (su
->scale
< 0.0)