]>
git.cameronkatri.com Git - mandoc.git/blob - html.c
1 /* $Id: html.c,v 1.163 2014/08/14 00:31:43 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011, 2012, 2013, 2014 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 AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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>
32 #include "mandoc_aux.h"
33 #include "libmandoc.h"
41 #define HTML_CLRLINE (1 << 0)
42 #define HTML_NOSTACK (1 << 1)
43 #define HTML_AUTOCLOSE (1 << 2) /* Tag has auto-closure. */
46 static const struct htmldata htmltags
[TAG_MAX
] = {
47 {"html", HTML_CLRLINE
}, /* TAG_HTML */
48 {"head", HTML_CLRLINE
}, /* TAG_HEAD */
49 {"body", HTML_CLRLINE
}, /* TAG_BODY */
50 {"meta", HTML_CLRLINE
| HTML_NOSTACK
| HTML_AUTOCLOSE
}, /* TAG_META */
51 {"title", HTML_CLRLINE
}, /* TAG_TITLE */
52 {"div", HTML_CLRLINE
}, /* TAG_DIV */
53 {"h1", 0}, /* TAG_H1 */
54 {"h2", 0}, /* TAG_H2 */
55 {"span", 0}, /* TAG_SPAN */
56 {"link", HTML_CLRLINE
| HTML_NOSTACK
| HTML_AUTOCLOSE
}, /* TAG_LINK */
57 {"br", HTML_CLRLINE
| HTML_NOSTACK
| HTML_AUTOCLOSE
}, /* TAG_BR */
59 {"table", HTML_CLRLINE
}, /* TAG_TABLE */
60 {"tbody", HTML_CLRLINE
}, /* TAG_TBODY */
61 {"col", HTML_CLRLINE
| HTML_NOSTACK
| HTML_AUTOCLOSE
}, /* TAG_COL */
62 {"tr", HTML_CLRLINE
}, /* TAG_TR */
63 {"td", HTML_CLRLINE
}, /* TAG_TD */
64 {"li", HTML_CLRLINE
}, /* TAG_LI */
65 {"ul", HTML_CLRLINE
}, /* TAG_UL */
66 {"ol", HTML_CLRLINE
}, /* TAG_OL */
67 {"dl", HTML_CLRLINE
}, /* TAG_DL */
68 {"dt", HTML_CLRLINE
}, /* TAG_DT */
69 {"dd", HTML_CLRLINE
}, /* TAG_DD */
70 {"blockquote", HTML_CLRLINE
}, /* TAG_BLOCKQUOTE */
71 {"p", HTML_CLRLINE
| HTML_NOSTACK
| HTML_AUTOCLOSE
}, /* TAG_P */
72 {"pre", HTML_CLRLINE
}, /* TAG_PRE */
73 {"b", 0 }, /* TAG_B */
74 {"i", 0 }, /* TAG_I */
75 {"code", 0 }, /* TAG_CODE */
76 {"small", 0 }, /* TAG_SMALL */
79 static const char *const htmlattrs
[ATTR_MAX
] = {
80 "http-equiv", /* ATTR_HTTPEQUIV */
81 "content", /* ATTR_CONTENT */
82 "name", /* ATTR_NAME */
84 "href", /* ATTR_HREF */
85 "type", /* ATTR_TYPE */
86 "media", /* ATTR_MEDIA */
87 "class", /* ATTR_CLASS */
88 "style", /* ATTR_STYLE */
89 "width", /* ATTR_WIDTH */
91 "summary", /* ATTR_SUMMARY */
92 "align", /* ATTR_ALIGN */
93 "colspan", /* ATTR_COLSPAN */
96 static const char *const roffscales
[SCALE_MAX
] = {
109 static void bufncat(struct html
*, const char *, size_t);
110 static void print_ctag(struct html
*, enum htmltag
);
111 static int print_escape(char);
112 static int print_encode(struct html
*, const char *, int);
113 static void print_metaf(struct html
*, enum mandoc_esc
);
114 static void print_attr(struct html
*, const char *, const char *);
115 static void *ml_alloc(char *, enum htmltype
);
119 ml_alloc(char *outopts
, enum htmltype type
)
127 toks
[2] = "includes";
128 toks
[3] = "fragment";
131 h
= mandoc_calloc(1, sizeof(struct html
));
135 h
->symtab
= mchars_alloc();
137 while (outopts
&& *outopts
)
138 switch (getsubopt(&outopts
, UNCONST(toks
), &v
)) {
146 h
->base_includes
= v
;
149 h
->oflags
|= HTML_FRAGMENT
;
159 html_alloc(char *outopts
)
162 return(ml_alloc(outopts
, HTML_HTML_4_01_STRICT
));
166 xhtml_alloc(char *outopts
)
169 return(ml_alloc(outopts
, HTML_XHTML_1_0_STRICT
));
178 h
= (struct html
*)p
;
180 while ((tag
= h
->tags
.head
) != NULL
) {
181 h
->tags
.head
= tag
->next
;
186 mchars_free(h
->symtab
);
192 print_gen_head(struct html
*h
)
194 struct htmlpair tag
[4];
196 tag
[0].key
= ATTR_HTTPEQUIV
;
197 tag
[0].val
= "Content-Type";
198 tag
[1].key
= ATTR_CONTENT
;
199 tag
[1].val
= "text/html; charset=utf-8";
200 print_otag(h
, TAG_META
, 2, tag
);
202 tag
[0].key
= ATTR_NAME
;
203 tag
[0].val
= "resource-type";
204 tag
[1].key
= ATTR_CONTENT
;
205 tag
[1].val
= "document";
206 print_otag(h
, TAG_META
, 2, tag
);
209 tag
[0].key
= ATTR_REL
;
210 tag
[0].val
= "stylesheet";
211 tag
[1].key
= ATTR_HREF
;
212 tag
[1].val
= h
->style
;
213 tag
[2].key
= ATTR_TYPE
;
214 tag
[2].val
= "text/css";
215 tag
[3].key
= ATTR_MEDIA
;
217 print_otag(h
, TAG_LINK
, 4, tag
);
222 print_metaf(struct html
*h
, enum mandoc_esc deco
)
227 case ESCAPE_FONTPREV
:
230 case ESCAPE_FONTITALIC
:
231 font
= HTMLFONT_ITALIC
;
233 case ESCAPE_FONTBOLD
:
234 font
= HTMLFONT_BOLD
;
241 case ESCAPE_FONTROMAN
:
242 font
= HTMLFONT_NONE
;
250 print_tagq(h
, h
->metaf
);
258 case HTMLFONT_ITALIC
:
259 h
->metaf
= print_otag(h
, TAG_I
, 0, NULL
);
262 h
->metaf
= print_otag(h
, TAG_B
, 0, NULL
);
265 h
->metaf
= print_otag(h
, TAG_B
, 0, NULL
);
266 print_otag(h
, TAG_I
, 0, NULL
);
274 html_strlen(const char *cp
)
280 * Account for escaped sequences within string length
281 * calculations. This follows the logic in term_strlen() as we
282 * must calculate the width of produced strings.
283 * Assume that characters are always width of "1". This is
284 * hacky, but it gets the job done for approximation of widths.
290 rsz
= strcspn(cp
, "\\");
302 switch (mandoc_escape(&cp
, NULL
, NULL
)) {
307 case ESCAPE_NUMBERED
:
315 case ESCAPE_SKIPCHAR
:
357 print_encode(struct html
*h
, const char *p
, int norecurse
)
363 static const char rejs
[9] = { '\\', '<', '>', '&', '"',
364 ASCII_NBRSP
, ASCII_HYPH
, ASCII_BREAK
, '\0' };
369 if (HTML_SKIPCHAR
& h
->flags
&& '\\' != *p
) {
370 h
->flags
&= ~HTML_SKIPCHAR
;
375 sz
= strcspn(p
, rejs
);
377 fwrite(p
, 1, sz
, stdout
);
383 if (print_escape(*p
++))
386 esc
= mandoc_escape(&p
, &seq
, &len
);
387 if (ESCAPE_ERROR
== esc
)
393 case ESCAPE_FONTPREV
:
395 case ESCAPE_FONTBOLD
:
397 case ESCAPE_FONTITALIC
:
401 case ESCAPE_FONTROMAN
:
405 case ESCAPE_SKIPCHAR
:
406 h
->flags
|= HTML_SKIPCHAR
;
412 if (h
->flags
& HTML_SKIPCHAR
) {
413 h
->flags
&= ~HTML_SKIPCHAR
;
419 /* Skip past "u" header. */
420 c
= mchars_num2uc(seq
+ 1, len
- 1);
424 case ESCAPE_NUMBERED
:
425 c
= mchars_num2char(seq
, len
);
426 if ( ! ('\0' == c
|| print_escape(c
)))
430 c
= mchars_spec2cp(h
->symtab
, seq
, len
);
433 else if (-1 == c
&& 1 == len
&&
450 print_attr(struct html
*h
, const char *key
, const char *val
)
452 printf(" %s=\"", key
);
453 (void)print_encode(h
, val
, 1);
458 print_otag(struct html
*h
, enum htmltag tag
,
459 int sz
, const struct htmlpair
*p
)
464 /* Push this tags onto the stack of open scopes. */
466 if ( ! (HTML_NOSTACK
& htmltags
[tag
].flags
)) {
467 t
= mandoc_malloc(sizeof(struct tag
));
469 t
->next
= h
->tags
.head
;
474 if ( ! (HTML_NOSPACE
& h
->flags
))
475 if ( ! (HTML_CLRLINE
& htmltags
[tag
].flags
)) {
477 if ( ! (HTML_KEEP
& h
->flags
)) {
478 if (HTML_PREKEEP
& h
->flags
)
479 h
->flags
|= HTML_KEEP
;
485 if ( ! (h
->flags
& HTML_NONOSPACE
))
486 h
->flags
&= ~HTML_NOSPACE
;
488 h
->flags
|= HTML_NOSPACE
;
490 /* Print out the tag name and attributes. */
492 printf("<%s", htmltags
[tag
].name
);
493 for (i
= 0; i
< sz
; i
++)
494 print_attr(h
, htmlattrs
[p
[i
].key
], p
[i
].val
);
496 /* Add non-overridable attributes. */
498 if (TAG_HTML
== tag
&& HTML_XHTML_1_0_STRICT
== h
->type
) {
499 print_attr(h
, "xmlns", "http://www.w3.org/1999/xhtml");
500 print_attr(h
, "xml:lang", "en");
501 print_attr(h
, "lang", "en");
504 /* Accommodate for XML "well-formed" singleton escaping. */
506 if (HTML_AUTOCLOSE
& htmltags
[tag
].flags
)
508 case HTML_XHTML_1_0_STRICT
:
517 h
->flags
|= HTML_NOSPACE
;
519 if ((HTML_AUTOCLOSE
| HTML_CLRLINE
) & htmltags
[tag
].flags
)
526 print_ctag(struct html
*h
, enum htmltag tag
)
529 printf("</%s>", htmltags
[tag
].name
);
530 if (HTML_CLRLINE
& htmltags
[tag
].flags
) {
531 h
->flags
|= HTML_NOSPACE
;
537 print_gen_decls(struct html
*h
)
544 case HTML_HTML_4_01_STRICT
:
546 doctype
= "-//W3C//DTD HTML 4.01//EN";
547 dtd
= "http://www.w3.org/TR/html4/strict.dtd";
550 puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
552 doctype
= "-//W3C//DTD XHTML 1.0 Strict//EN";
553 dtd
= "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";
557 printf("<!DOCTYPE %s PUBLIC \"%s\" \"%s\">\n",
562 print_text(struct html
*h
, const char *word
)
565 if ( ! (HTML_NOSPACE
& h
->flags
)) {
567 if ( ! (HTML_KEEP
& h
->flags
)) {
568 if (HTML_PREKEEP
& h
->flags
)
569 h
->flags
|= HTML_KEEP
;
575 assert(NULL
== h
->metaf
);
577 case HTMLFONT_ITALIC
:
578 h
->metaf
= print_otag(h
, TAG_I
, 0, NULL
);
581 h
->metaf
= print_otag(h
, TAG_B
, 0, NULL
);
584 h
->metaf
= print_otag(h
, TAG_B
, 0, NULL
);
585 print_otag(h
, TAG_I
, 0, NULL
);
592 if ( ! print_encode(h
, word
, 0)) {
593 if ( ! (h
->flags
& HTML_NONOSPACE
))
594 h
->flags
&= ~HTML_NOSPACE
;
596 h
->flags
|= HTML_NOSPACE
;
599 print_tagq(h
, h
->metaf
);
603 h
->flags
&= ~HTML_IGNDELIM
;
607 print_tagq(struct html
*h
, const struct tag
*until
)
611 while ((tag
= h
->tags
.head
) != NULL
) {
613 * Remember to close out and nullify the current
614 * meta-font and table, if applicable.
620 print_ctag(h
, tag
->tag
);
621 h
->tags
.head
= tag
->next
;
623 if (until
&& tag
== until
)
629 print_stagq(struct html
*h
, const struct tag
*suntil
)
633 while ((tag
= h
->tags
.head
) != NULL
) {
634 if (suntil
&& tag
== suntil
)
637 * Remember to close out and nullify the current
638 * meta-font and table, if applicable.
644 print_ctag(h
, tag
->tag
);
645 h
->tags
.head
= tag
->next
;
651 bufinit(struct html
*h
)
659 bufcat_style(struct html
*h
, const char *key
, const char *val
)
669 bufcat(struct html
*h
, const char *p
)
673 * XXX This is broken and not easy to fix.
674 * When using the -Oincludes option, buffmt_includes()
675 * may pass in strings overrunning BUFSIZ, causing a crash.
678 h
->buflen
= strlcat(h
->buf
, p
, BUFSIZ
);
679 assert(h
->buflen
< BUFSIZ
);
683 bufcat_fmt(struct html
*h
, const char *fmt
, ...)
688 (void)vsnprintf(h
->buf
+ (int)h
->buflen
,
689 BUFSIZ
- h
->buflen
- 1, fmt
, ap
);
691 h
->buflen
= strlen(h
->buf
);
695 bufncat(struct html
*h
, const char *p
, size_t sz
)
698 assert(h
->buflen
+ sz
+ 1 < BUFSIZ
);
699 strncat(h
->buf
, p
, sz
);
704 buffmt_includes(struct html
*h
, const char *name
)
708 pp
= h
->base_includes
;
711 while (NULL
!= (p
= strchr(pp
, '%'))) {
712 bufncat(h
, pp
, (size_t)(p
- pp
));
728 buffmt_man(struct html
*h
, const char *name
, const char *sec
)
735 while (NULL
!= (p
= strchr(pp
, '%'))) {
736 bufncat(h
, pp
, (size_t)(p
- pp
));
739 bufcat(h
, sec
? sec
: "1");
742 bufcat_fmt(h
, "%s", name
);
755 bufcat_su(struct html
*h
, const char *p
, const struct roffsu
*su
)
760 if (SCALE_MM
== su
->unit
&& 0.0 == (v
/= 100.0))
762 else if (SCALE_BU
== su
->unit
)
765 bufcat_fmt(h
, "%s: %.2f%s;", p
, v
, roffscales
[su
->unit
]);
769 bufcat_id(struct html
*h
, const char *src
)
772 /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
775 bufcat_fmt(h
, "%.2x", *src
++);