]>
git.cameronkatri.com Git - mandoc.git/blob - html.c
1 /* $Id: html.c,v 1.101 2010/06/07 10:52:44 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>
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 {"col", HTML_CLRLINE
| HTML_NOSTACK
| HTML_AUTOCLOSE
}, /* TAG_COL */
61 {"tr", HTML_CLRLINE
}, /* TAG_TR */
62 {"td", HTML_CLRLINE
}, /* TAG_TD */
63 {"li", HTML_CLRLINE
}, /* TAG_LI */
64 {"ul", HTML_CLRLINE
}, /* TAG_UL */
65 {"ol", HTML_CLRLINE
}, /* TAG_OL */
68 static const char *const htmlfonts
[HTMLFONT_MAX
] = {
74 static const char *const htmlattrs
[ATTR_MAX
] = {
91 static void print_spec(struct html
*, const char *, size_t);
92 static void print_res(struct html
*, const char *, size_t);
93 static void print_ctag(struct html
*, enum htmltag
);
94 static void print_doctype(struct html
*);
95 static void print_xmltype(struct html
*);
96 static int print_encode(struct html
*, const char *, int);
97 static void print_metaf(struct html
*, enum roffdeco
);
98 static void print_attr(struct html
*,
99 const char *, const char *);
100 static void *ml_alloc(char *, enum htmltype
);
104 ml_alloc(char *outopts
, enum htmltype type
)
112 toks
[2] = "includes";
115 h
= calloc(1, sizeof(struct html
));
124 h
->symtab
= chars_init(CHARS_HTML
);
126 while (outopts
&& *outopts
)
127 switch (getsubopt(&outopts
, UNCONST(toks
), &v
)) {
135 h
->base_includes
= v
;
145 html_alloc(char *outopts
)
148 return(ml_alloc(outopts
, HTML_HTML_4_01_STRICT
));
153 xhtml_alloc(char *outopts
)
156 return(ml_alloc(outopts
, HTML_XHTML_1_0_STRICT
));
167 h
= (struct html
*)p
;
169 while ((ord
= h
->ords
.head
) != NULL
) {
170 h
->ords
.head
= ord
->next
;
174 while ((tag
= h
->tags
.head
) != NULL
) {
175 h
->tags
.head
= tag
->next
;
180 chars_free(h
->symtab
);
187 print_gen_head(struct html
*h
)
189 struct htmlpair tag
[4];
191 tag
[0].key
= ATTR_HTTPEQUIV
;
192 tag
[0].val
= "Content-Type";
193 tag
[1].key
= ATTR_CONTENT
;
194 tag
[1].val
= "text/html; charset=utf-8";
195 print_otag(h
, TAG_META
, 2, tag
);
197 tag
[0].key
= ATTR_NAME
;
198 tag
[0].val
= "resource-type";
199 tag
[1].key
= ATTR_CONTENT
;
200 tag
[1].val
= "document";
201 print_otag(h
, TAG_META
, 2, tag
);
204 tag
[0].key
= ATTR_REL
;
205 tag
[0].val
= "stylesheet";
206 tag
[1].key
= ATTR_HREF
;
207 tag
[1].val
= h
->style
;
208 tag
[2].key
= ATTR_TYPE
;
209 tag
[2].val
= "text/css";
210 tag
[3].key
= ATTR_MEDIA
;
212 print_otag(h
, TAG_LINK
, 4, tag
);
218 print_spec(struct html
*h
, const char *p
, size_t len
)
223 rhs
= chars_a2ascii(h
->symtab
, p
, len
, &sz
);
227 fwrite(rhs
, 1, sz
, stdout
);
232 print_res(struct html
*h
, const char *p
, size_t len
)
237 rhs
= chars_a2res(h
->symtab
, p
, len
, &sz
);
241 fwrite(rhs
, 1, sz
, stdout
);
246 print_ofont(struct html
*h
, enum htmlfont font
)
253 /* FIXME: DECO_ROMAN should just close out preexisting. */
255 if (h
->metaf
&& h
->tags
.head
== h
->metaf
)
256 print_tagq(h
, h
->metaf
);
258 PAIR_CLASS_INIT(&tag
, htmlfonts
[font
]);
259 h
->metaf
= print_otag(h
, TAG_SPAN
, 1, &tag
);
265 print_metaf(struct html
*h
, enum roffdeco deco
)
270 case (DECO_PREVIOUS
):
274 font
= HTMLFONT_ITALIC
;
277 font
= HTMLFONT_BOLD
;
280 font
= HTMLFONT_NONE
;
287 (void)print_ofont(h
, font
);
292 print_encode(struct html
*h
, const char *p
, int norecurse
)
298 static const char rejs
[6] = { '\\', '<', '>', '&', ASCII_HYPH
, '\0' };
303 sz
= strcspn(p
, rejs
);
305 fwrite(p
, 1, sz
, stdout
);
312 } else if ('>' == *p
) {
315 } else if ('&' == *p
) {
318 } else if (ASCII_HYPH
== *p
) {
320 * Note: "soft hyphens" aren't graphically
321 * displayed when not breaking the text; we want
322 * them to be displayed.
324 /*printf("­");*/
327 } else if ('\0' == *p
)
331 len
= a2roffdeco(&deco
, &seq
, &sz
);
334 case (DECO_RESERVED
):
335 print_res(h
, seq
, sz
);
338 print_spec(h
, seq
, sz
);
340 case (DECO_PREVIOUS
):
349 print_metaf(h
, deco
);
357 if (DECO_NOSPACE
== deco
&& '\0' == *(p
+ 1))
366 print_attr(struct html
*h
, const char *key
, const char *val
)
368 printf(" %s=\"", key
);
369 (void)print_encode(h
, val
, 1);
375 print_otag(struct html
*h
, enum htmltag tag
,
376 int sz
, const struct htmlpair
*p
)
381 /* Push this tags onto the stack of open scopes. */
383 if ( ! (HTML_NOSTACK
& htmltags
[tag
].flags
)) {
384 t
= malloc(sizeof(struct tag
));
390 t
->next
= h
->tags
.head
;
395 if ( ! (HTML_NOSPACE
& h
->flags
))
396 if ( ! (HTML_CLRLINE
& htmltags
[tag
].flags
))
399 /* Print out the tag name and attributes. */
401 printf("<%s", htmltags
[tag
].name
);
402 for (i
= 0; i
< sz
; i
++)
403 print_attr(h
, htmlattrs
[p
[i
].key
], p
[i
].val
);
405 /* Add non-overridable attributes. */
407 if (TAG_HTML
== tag
&& HTML_XHTML_1_0_STRICT
== h
->type
) {
408 print_attr(h
, "xmlns", "http://www.w3.org/1999/xhtml");
409 print_attr(h
, "xml:lang", "en");
410 print_attr(h
, "lang", "en");
413 /* Accomodate for XML "well-formed" singleton escaping. */
415 if (HTML_AUTOCLOSE
& htmltags
[tag
].flags
)
417 case (HTML_XHTML_1_0_STRICT
):
426 h
->flags
|= HTML_NOSPACE
;
432 print_ctag(struct html
*h
, enum htmltag tag
)
435 printf("</%s>", htmltags
[tag
].name
);
436 if (HTML_CLRLINE
& htmltags
[tag
].flags
) {
437 h
->flags
|= HTML_NOSPACE
;
444 print_gen_decls(struct html
*h
)
453 print_xmltype(struct html
*h
)
456 if (HTML_XHTML_1_0_STRICT
== h
->type
)
457 printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
462 print_doctype(struct html
*h
)
469 case (HTML_HTML_4_01_STRICT
):
471 doctype
= "-//W3C//DTD HTML 4.01//EN";
472 dtd
= "http://www.w3.org/TR/html4/strict.dtd";
476 doctype
= "-//W3C//DTD XHTML 1.0 Strict//EN";
477 dtd
= "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";
481 printf("<!DOCTYPE %s PUBLIC \"%s\" \"%s\">\n",
487 print_text(struct html
*h
, const char *p
)
490 if (*p
&& 0 == *(p
+ 1))
507 if ( ! (HTML_IGNDELIM
& h
->flags
))
508 h
->flags
|= HTML_NOSPACE
;
514 if ( ! (h
->flags
& HTML_NOSPACE
))
518 if ( ! print_encode(h
, p
, 0))
519 h
->flags
&= ~HTML_NOSPACE
;
522 * Note that we don't process the pipe: the parser sees it as
523 * punctuation, but we don't in terms of typography.
525 if (*p
&& 0 == *(p
+ 1))
530 h
->flags
|= HTML_NOSPACE
;
539 print_tagq(struct html
*h
, const struct tag
*until
)
543 while ((tag
= h
->tags
.head
) != NULL
) {
546 print_ctag(h
, tag
->tag
);
547 h
->tags
.head
= tag
->next
;
549 if (until
&& tag
== until
)
556 print_stagq(struct html
*h
, const struct tag
*suntil
)
560 while ((tag
= h
->tags
.head
) != NULL
) {
561 if (suntil
&& tag
== suntil
)
565 print_ctag(h
, tag
->tag
);
566 h
->tags
.head
= tag
->next
;
573 bufinit(struct html
*h
)
582 bufcat_style(struct html
*h
, const char *key
, const char *val
)
593 bufcat(struct html
*h
, const char *p
)
596 bufncat(h
, p
, strlen(p
));
601 buffmt(struct html
*h
, const char *fmt
, ...)
606 (void)vsnprintf(h
->buf
+ (int)h
->buflen
,
607 BUFSIZ
- h
->buflen
- 1, fmt
, ap
);
609 h
->buflen
= strlen(h
->buf
);
614 bufncat(struct html
*h
, const char *p
, size_t sz
)
617 if (h
->buflen
+ sz
> BUFSIZ
- 1)
618 sz
= BUFSIZ
- 1 - h
->buflen
;
620 (void)strncat(h
->buf
, p
, sz
);
626 buffmt_includes(struct html
*h
, const char *name
)
630 pp
= h
->base_includes
;
632 while (NULL
!= (p
= strchr(pp
, '%'))) {
633 bufncat(h
, pp
, (size_t)(p
- pp
));
650 buffmt_man(struct html
*h
,
651 const char *name
, const char *sec
)
658 while (NULL
!= (p
= strchr(pp
, '%'))) {
659 bufncat(h
, pp
, (size_t)(p
- pp
));
662 bufcat(h
, sec
? sec
: "1");
679 bufcat_su(struct html
*h
, const char *p
, const struct roffsu
*su
)
722 buffmt(h
, "%s: %f%s;", p
, v
, u
);
725 buffmt(h
, "%s: %d%s;", p
, (int)v
, u
);
730 html_idcat(char *dst
, const char *src
, int sz
)
736 /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
738 for ( ; *dst
!= '\0' && sz
; dst
++, sz
--)
743 /* We can't start with a number (bah). */
749 for ( ; *src
!= '\0' && sz
> 1; src
++) {
750 ssz
= snprintf(dst
, (size_t)sz
, "%.2x", *src
);