]>
git.cameronkatri.com Git - mandoc.git/blob - html.c
1 /* $Id: html.c,v 1.100 2010/05/25 12:37:20 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/types.h>
38 #define UNCONST(a) ((void *)(uintptr_t)(const void *)(a))
43 #define HTML_CLRLINE (1 << 0)
44 #define HTML_NOSTACK (1 << 1)
45 #define HTML_AUTOCLOSE (1 << 2) /* Tag has auto-closure. */
48 static const struct htmldata htmltags
[TAG_MAX
] = {
49 {"html", HTML_CLRLINE
}, /* TAG_HTML */
50 {"head", HTML_CLRLINE
}, /* TAG_HEAD */
51 {"body", HTML_CLRLINE
}, /* TAG_BODY */
52 {"meta", HTML_CLRLINE
| HTML_NOSTACK
| HTML_AUTOCLOSE
}, /* TAG_META */
53 {"title", HTML_CLRLINE
}, /* TAG_TITLE */
54 {"div", HTML_CLRLINE
}, /* TAG_DIV */
55 {"h1", 0}, /* TAG_H1 */
56 {"h2", 0}, /* TAG_H2 */
57 {"span", 0}, /* TAG_SPAN */
58 {"link", HTML_CLRLINE
| HTML_NOSTACK
| HTML_AUTOCLOSE
}, /* TAG_LINK */
59 {"br", HTML_CLRLINE
| HTML_NOSTACK
| HTML_AUTOCLOSE
}, /* TAG_BR */
61 {"table", HTML_CLRLINE
}, /* TAG_TABLE */
62 {"col", HTML_CLRLINE
| HTML_NOSTACK
| HTML_AUTOCLOSE
}, /* TAG_COL */
63 {"tr", HTML_CLRLINE
}, /* TAG_TR */
64 {"td", HTML_CLRLINE
}, /* TAG_TD */
65 {"li", HTML_CLRLINE
}, /* TAG_LI */
66 {"ul", HTML_CLRLINE
}, /* TAG_UL */
67 {"ol", HTML_CLRLINE
}, /* TAG_OL */
70 static const char *const htmlfonts
[HTMLFONT_MAX
] = {
76 static const char *const htmlattrs
[ATTR_MAX
] = {
93 static void print_spec(struct html
*, const char *, size_t);
94 static void print_res(struct html
*, const char *, size_t);
95 static void print_ctag(struct html
*, enum htmltag
);
96 static void print_doctype(struct html
*);
97 static void print_xmltype(struct html
*);
98 static int print_encode(struct html
*, const char *, int);
99 static void print_metaf(struct html
*, enum roffdeco
);
100 static void print_attr(struct html
*,
101 const char *, const char *);
102 static void *ml_alloc(char *, enum htmltype
);
106 ml_alloc(char *outopts
, enum htmltype type
)
114 toks
[2] = "includes";
117 h
= calloc(1, sizeof(struct html
));
126 h
->symtab
= chars_init(CHARS_HTML
);
128 while (outopts
&& *outopts
)
129 switch (getsubopt(&outopts
, UNCONST(toks
), &v
)) {
137 h
->base_includes
= v
;
147 html_alloc(char *outopts
)
150 return(ml_alloc(outopts
, HTML_HTML_4_01_STRICT
));
155 xhtml_alloc(char *outopts
)
158 return(ml_alloc(outopts
, HTML_XHTML_1_0_STRICT
));
169 h
= (struct html
*)p
;
171 while ((ord
= h
->ords
.head
) != NULL
) {
172 h
->ords
.head
= ord
->next
;
176 while ((tag
= h
->tags
.head
) != NULL
) {
177 h
->tags
.head
= tag
->next
;
182 chars_free(h
->symtab
);
189 print_gen_head(struct html
*h
)
191 struct htmlpair tag
[4];
193 tag
[0].key
= ATTR_HTTPEQUIV
;
194 tag
[0].val
= "Content-Type";
195 tag
[1].key
= ATTR_CONTENT
;
196 tag
[1].val
= "text/html; charset=utf-8";
197 print_otag(h
, TAG_META
, 2, tag
);
199 tag
[0].key
= ATTR_NAME
;
200 tag
[0].val
= "resource-type";
201 tag
[1].key
= ATTR_CONTENT
;
202 tag
[1].val
= "document";
203 print_otag(h
, TAG_META
, 2, tag
);
206 tag
[0].key
= ATTR_REL
;
207 tag
[0].val
= "stylesheet";
208 tag
[1].key
= ATTR_HREF
;
209 tag
[1].val
= h
->style
;
210 tag
[2].key
= ATTR_TYPE
;
211 tag
[2].val
= "text/css";
212 tag
[3].key
= ATTR_MEDIA
;
214 print_otag(h
, TAG_LINK
, 4, tag
);
220 print_spec(struct html
*h
, const char *p
, size_t len
)
225 rhs
= chars_a2ascii(h
->symtab
, p
, len
, &sz
);
229 fwrite(rhs
, 1, sz
, stdout
);
234 print_res(struct html
*h
, const char *p
, size_t len
)
239 rhs
= chars_a2res(h
->symtab
, p
, len
, &sz
);
243 fwrite(rhs
, 1, sz
, stdout
);
248 print_ofont(struct html
*h
, enum htmlfont font
)
255 /* FIXME: DECO_ROMAN should just close out preexisting. */
257 if (h
->metaf
&& h
->tags
.head
== h
->metaf
)
258 print_tagq(h
, h
->metaf
);
260 PAIR_CLASS_INIT(&tag
, htmlfonts
[font
]);
261 h
->metaf
= print_otag(h
, TAG_SPAN
, 1, &tag
);
267 print_metaf(struct html
*h
, enum roffdeco deco
)
272 case (DECO_PREVIOUS
):
276 font
= HTMLFONT_ITALIC
;
279 font
= HTMLFONT_BOLD
;
282 font
= HTMLFONT_NONE
;
289 (void)print_ofont(h
, font
);
294 print_encode(struct html
*h
, const char *p
, int norecurse
)
300 static const char rejs
[6] = { '\\', '<', '>', '&', ASCII_HYPH
, '\0' };
305 sz
= strcspn(p
, rejs
);
307 fwrite(p
, 1, sz
, stdout
);
314 } else if ('>' == *p
) {
317 } else if ('&' == *p
) {
320 } else if (ASCII_HYPH
== *p
) {
322 * Note: "soft hyphens" aren't graphically
323 * displayed when not breaking the text; we want
324 * them to be displayed.
326 /*printf("­");*/
329 } else if ('\0' == *p
)
333 len
= a2roffdeco(&deco
, &seq
, &sz
);
336 case (DECO_RESERVED
):
337 print_res(h
, seq
, sz
);
340 print_spec(h
, seq
, sz
);
342 case (DECO_PREVIOUS
):
351 print_metaf(h
, deco
);
359 if (DECO_NOSPACE
== deco
&& '\0' == *(p
+ 1))
368 print_attr(struct html
*h
, const char *key
, const char *val
)
370 printf(" %s=\"", key
);
371 (void)print_encode(h
, val
, 1);
377 print_otag(struct html
*h
, enum htmltag tag
,
378 int sz
, const struct htmlpair
*p
)
383 /* Push this tags onto the stack of open scopes. */
385 if ( ! (HTML_NOSTACK
& htmltags
[tag
].flags
)) {
386 t
= malloc(sizeof(struct tag
));
392 t
->next
= h
->tags
.head
;
397 if ( ! (HTML_NOSPACE
& h
->flags
))
398 if ( ! (HTML_CLRLINE
& htmltags
[tag
].flags
))
401 /* Print out the tag name and attributes. */
403 printf("<%s", htmltags
[tag
].name
);
404 for (i
= 0; i
< sz
; i
++)
405 print_attr(h
, htmlattrs
[p
[i
].key
], p
[i
].val
);
407 /* Add non-overridable attributes. */
409 if (TAG_HTML
== tag
&& HTML_XHTML_1_0_STRICT
== h
->type
) {
410 print_attr(h
, "xmlns", "http://www.w3.org/1999/xhtml");
411 print_attr(h
, "xml:lang", "en");
412 print_attr(h
, "lang", "en");
415 /* Accomodate for XML "well-formed" singleton escaping. */
417 if (HTML_AUTOCLOSE
& htmltags
[tag
].flags
)
419 case (HTML_XHTML_1_0_STRICT
):
428 h
->flags
|= HTML_NOSPACE
;
434 print_ctag(struct html
*h
, enum htmltag tag
)
437 printf("</%s>", htmltags
[tag
].name
);
438 if (HTML_CLRLINE
& htmltags
[tag
].flags
) {
439 h
->flags
|= HTML_NOSPACE
;
446 print_gen_decls(struct html
*h
)
455 print_xmltype(struct html
*h
)
458 if (HTML_XHTML_1_0_STRICT
== h
->type
)
459 printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
464 print_doctype(struct html
*h
)
471 case (HTML_HTML_4_01_STRICT
):
473 doctype
= "-//W3C//DTD HTML 4.01//EN";
474 dtd
= "http://www.w3.org/TR/html4/strict.dtd";
478 doctype
= "-//W3C//DTD XHTML 1.0 Strict//EN";
479 dtd
= "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";
483 printf("<!DOCTYPE %s PUBLIC \"%s\" \"%s\">\n",
489 print_text(struct html
*h
, const char *p
)
492 if (*p
&& 0 == *(p
+ 1))
509 if ( ! (HTML_IGNDELIM
& h
->flags
))
510 h
->flags
|= HTML_NOSPACE
;
516 if ( ! (h
->flags
& HTML_NOSPACE
))
520 if ( ! print_encode(h
, p
, 0))
521 h
->flags
&= ~HTML_NOSPACE
;
524 * Note that we don't process the pipe: the parser sees it as
525 * punctuation, but we don't in terms of typography.
527 if (*p
&& 0 == *(p
+ 1))
532 h
->flags
|= HTML_NOSPACE
;
541 print_tagq(struct html
*h
, const struct tag
*until
)
545 while ((tag
= h
->tags
.head
) != NULL
) {
548 print_ctag(h
, tag
->tag
);
549 h
->tags
.head
= tag
->next
;
551 if (until
&& tag
== until
)
558 print_stagq(struct html
*h
, const struct tag
*suntil
)
562 while ((tag
= h
->tags
.head
) != NULL
) {
563 if (suntil
&& tag
== suntil
)
567 print_ctag(h
, tag
->tag
);
568 h
->tags
.head
= tag
->next
;
575 bufinit(struct html
*h
)
584 bufcat_style(struct html
*h
, const char *key
, const char *val
)
595 bufcat(struct html
*h
, const char *p
)
598 bufncat(h
, p
, strlen(p
));
603 buffmt(struct html
*h
, const char *fmt
, ...)
608 (void)vsnprintf(h
->buf
+ (int)h
->buflen
,
609 BUFSIZ
- h
->buflen
- 1, fmt
, ap
);
611 h
->buflen
= strlen(h
->buf
);
616 bufncat(struct html
*h
, const char *p
, size_t sz
)
619 if (h
->buflen
+ sz
> BUFSIZ
- 1)
620 sz
= BUFSIZ
- 1 - h
->buflen
;
622 (void)strncat(h
->buf
, p
, sz
);
628 buffmt_includes(struct html
*h
, const char *name
)
632 pp
= h
->base_includes
;
634 while (NULL
!= (p
= strchr(pp
, '%'))) {
635 bufncat(h
, pp
, (size_t)(p
- pp
));
652 buffmt_man(struct html
*h
,
653 const char *name
, const char *sec
)
660 while (NULL
!= (p
= strchr(pp
, '%'))) {
661 bufncat(h
, pp
, (size_t)(p
- pp
));
664 bufcat(h
, sec
? sec
: "1");
681 bufcat_su(struct html
*h
, const char *p
, const struct roffsu
*su
)
724 buffmt(h
, "%s: %f%s;", p
, v
, u
);
727 buffmt(h
, "%s: %d%s;", p
, (int)v
, u
);
732 html_idcat(char *dst
, const char *src
, int sz
)
738 /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
740 for ( ; *dst
!= '\0' && sz
; dst
++, sz
--)
745 /* We can't start with a number (bah). */
751 for ( ; *src
!= '\0' && sz
> 1; src
++) {
752 ssz
= snprintf(dst
, (size_t)sz
, "%.2x", *src
);