=================================================================== RCS file: /cvs/mandoc/html.c,v retrieving revision 1.16 retrieving revision 1.20 diff -u -p -r1.16 -r1.20 --- mandoc/html.c 2008/12/08 16:29:57 1.16 +++ mandoc/html.c 2008/12/10 00:58:15 1.20 @@ -1,4 +1,4 @@ -/* $Id: html.c,v 1.16 2008/12/08 16:29:57 kristaps Exp $ */ +/* $Id: html.c,v 1.20 2008/12/10 00:58:15 kristaps Exp $ */ /* * Copyright (c) 2008 Kristaps Dzonsons * @@ -27,11 +27,9 @@ #include #include -#include "libmdocml.h" -#include "private.h" +#include "html.h" #include "ml.h" - /* TODO: allow head/tail-less invocations (just "div" start). */ struct htmlnode { @@ -42,14 +40,13 @@ struct htmlnode { struct htmlnode *parent; }; - struct htmlq { struct htmlnode *last; }; -static int html_loadcss(struct md_mbuf *, const char *); - +static int html_loadcss(struct md_mbuf *, + const char *); static int html_alloc(void **); static void html_free(void *); static ssize_t html_endtag(struct md_mbuf *, void *, @@ -119,6 +116,11 @@ static int html_It_headtagname(struct md_mbuf *, static int html_It_bodytagname(struct md_mbuf *, struct htmlq *, const int *, const char **, size_t *); +static int html_tputln(struct md_mbuf *, + enum ml_scope, int, enum html_tag); +static int html_aputln(struct md_mbuf *, enum ml_scope, + int, enum html_tag, + int, const struct html_pair *); /* ARGSUSED */ @@ -140,11 +142,11 @@ html_It_headtagname(struct md_mbuf *mbuf, struct htmlq i < ROFF_MAXLINEARG; i++) { switch (n->argc[i]) { case (ROFF_Ohang): - return(ml_nputs(mbuf, "div", 3, res)); + return(html_stput(mbuf, HTML_TAG_DIV, res)); case (ROFF_Tag): /* FALLTHROUGH */ case (ROFF_Column): - return(ml_nputs(mbuf, "td", 2, res)); + return(html_stput(mbuf, HTML_TAG_TD, res)); default: break; } @@ -189,11 +191,11 @@ html_It_bodytagname(struct md_mbuf *mbuf, struct htmlq case (ROFF_Ohang): /* FALLTHROUGH */ case (ROFF_Inset): - return(ml_nputs(mbuf, "div", 3, res)); + return(html_stput(mbuf, HTML_TAG_DIV, res)); case (ROFF_Tag): /* FALLTHROUGH */ case (ROFF_Column): - return(ml_nputs(mbuf, "td", 2, res)); + return(html_stput(mbuf, HTML_TAG_TD, res)); default: break; } @@ -215,7 +217,7 @@ html_Bl_bodytagname(struct md_mbuf *mbuf, struct htmlq && i < ROFF_MAXLINEARG; i++) { switch (argc[i]) { case (ROFF_Enum): - return(ml_nputs(mbuf, "ol", 2, res)); + return(html_stput(mbuf, HTML_TAG_OL, res)); case (ROFF_Bullet): /* FALLTHROUGH */ case (ROFF_Dash): @@ -231,11 +233,11 @@ html_Bl_bodytagname(struct md_mbuf *mbuf, struct htmlq case (ROFF_Ohang): /* FALLTHROUGH */ case (ROFF_Inset): - return(ml_nputs(mbuf, "ul", 2, res)); + return(html_stput(mbuf, HTML_TAG_UL, res)); case (ROFF_Tag): /* FALLTHROUGH */ case (ROFF_Column): - return(ml_nputs(mbuf, "table", 5, res)); + return(html_stput(mbuf, HTML_TAG_TABLE, res)); default: break; } @@ -281,11 +283,11 @@ html_It_blocktagname(struct md_mbuf *mbuf, struct html case (ROFF_Ohang): /* FALLTHROUGH */ case (ROFF_Inset): - return(ml_nputs(mbuf, "li", 2, res)); + return(html_stput(mbuf, HTML_TAG_LI, res)); case (ROFF_Tag): /* FALLTHROUGH */ case (ROFF_Column): - return(ml_nputs(mbuf, "tr", 2, res)); + return(html_stput(mbuf, HTML_TAG_TR, res)); default: break; } @@ -350,62 +352,152 @@ out: } +static int +html_tputln(struct md_mbuf *mbuf, enum ml_scope scope, + int i, enum html_tag tag) +{ + + if ( ! ml_putchars(mbuf, ' ', INDENT(i) * INDENT_SZ, NULL)) + return(0); + if ( ! html_tput(mbuf, scope, tag, NULL)) + return(0); + return(ml_nputs(mbuf, "\n", 1, NULL)); +} + + +static int +html_aputln(struct md_mbuf *mbuf, enum ml_scope scope, int i, + enum html_tag tag, int sz, const struct html_pair *p) +{ + + if ( ! ml_putchars(mbuf, ' ', INDENT(i) * INDENT_SZ, NULL)) + return(0); + if ( ! html_aput(mbuf, scope, tag, NULL, sz, p)) + return(0); + return(ml_nputs(mbuf, "\n", 1, NULL)); +} + + /* ARGSUSED */ static int html_begin(struct md_mbuf *mbuf, const struct md_args *args, const struct tm *tm, const char *os, - const char *title, enum roffmsec section, - const char *vol) + const char *name, enum roffmsec msec, const char *vol) { - const char *preamble, *css, *trail; - char buf[512]; - size_t res; + struct html_pair attr[4]; + char ts[32]; + int i; - preamble = - "\n" - "\n" - "\n" - " \n" - " \n" - " Manual Page for %s(%s)\n"; + (void)snprintf(ts, sizeof(ts), "%s(%s)", + name, roff_msecname(msec)); - css = - " \n"; - trail = - "\n" - "\n" - "
"; + i = 0; - res = 0; + if ( ! html_typeput(mbuf, HTML_TYPE_4_01_STRICT, NULL)) + return(0); + if ( ! html_tputln(mbuf, ML_OPEN, i, HTML_TAG_HTML)) + return(0); + if ( ! html_tputln(mbuf, ML_OPEN, i++, HTML_TAG_HEAD)) + return(0); - (void)snprintf(buf, sizeof(buf) - 1, - preamble, title, ml_section(section)); + attr[0].attr = HTML_ATTR_HTTP_EQUIV; + attr[0].val = "content-type"; + attr[1].attr = HTML_ATTR_CONTENT; + attr[1].val = "text/html;charset=utf-8"; - if ( ! ml_puts(mbuf, buf, &res)) + if ( ! html_aputln(mbuf, ML_OPEN, i, HTML_TAG_META, 2, attr)) return(0); - assert(args->params.html.css); + attr[0].attr = HTML_ATTR_NAME; + attr[0].val = "resource-type"; + attr[1].attr = HTML_ATTR_CONTENT; + attr[1].val = "document"; + + if ( ! html_aputln(mbuf, ML_OPEN, i, HTML_TAG_META, 2, attr)) + return(0); + + if ( ! html_tputln(mbuf, ML_OPEN, i, HTML_TAG_TITLE)) + return(0); + if ( ! ml_putstring(mbuf, ts, NULL)) + return(0); + if ( ! html_tputln(mbuf, ML_CLOSE, i, HTML_TAG_TITLE)) + return(0); + if (HTML_CSS_EMBED & args->params.html.flags) { - if ( ! ml_puts(mbuf, " \n", &res)) + + if ( ! html_commentput(mbuf, ML_CLOSE, NULL)) + return(NULL); + if ( ! html_tputln(mbuf, ML_CLOSE, i, HTML_TAG_STYLE)) return(0); } else { - (void)snprintf(buf, sizeof(buf) - 1, css, - args->params.html.css); - if ( ! ml_puts(mbuf, buf, &res)) + attr[0].attr = HTML_ATTR_REL; + attr[0].val = "stylesheet"; + attr[1].attr = HTML_ATTR_TYPE; + attr[1].val = "text/css"; + attr[2].attr = HTML_ATTR_HREF; + attr[2].val = args->params.html.css; + + if ( ! html_aputln(mbuf, ML_OPEN, i, + HTML_TAG_LINK, 3, attr)) return(0); } - if ( ! ml_puts(mbuf, trail, &res)) + if ( ! html_tputln(mbuf, ML_CLOSE, --i, HTML_TAG_HEAD)) return(0); + if ( ! html_tputln(mbuf, ML_OPEN, i, HTML_TAG_BODY)) + return(0); - return(1); + attr[0].attr = HTML_ATTR_CLASS; + attr[0].val = "mdoc"; + + if ( ! html_aputln(mbuf, ML_OPEN, i, HTML_TAG_DIV, 1, attr)) + return(0); + + attr[0].attr = HTML_ATTR_WIDTH; + attr[0].val = "100%"; + + if ( ! html_aputln(mbuf, ML_OPEN, i++, HTML_TAG_TABLE, 1, attr)) + return(0); + if ( ! html_tputln(mbuf, ML_OPEN, i++, HTML_TAG_TR)) + return(0); + + if ( ! html_tputln(mbuf, ML_OPEN, i, HTML_TAG_TD)) + return(0); + if ( ! ml_putstring(mbuf, ts, NULL)) + return(0); + if ( ! html_tputln(mbuf, ML_CLOSE, i, HTML_TAG_TD)) + return(0); + + if ( ! html_tputln(mbuf, ML_OPEN, i, HTML_TAG_TD)) + return(0); + /* TODO: middle. */ + if ( ! html_tputln(mbuf, ML_CLOSE, i, HTML_TAG_TD)) + return(0); + + attr[0].attr = HTML_ATTR_ALIGN; + attr[0].val = "right"; + + if ( ! html_aputln(mbuf, ML_OPEN, i, HTML_TAG_TD, 1, attr)) + return(0); + if ( ! ml_putstring(mbuf, ts, NULL)) + return(0); + if ( ! html_tputln(mbuf, ML_CLOSE, i, HTML_TAG_TD)) + return(0); + + if ( ! html_tputln(mbuf, ML_CLOSE, --i, HTML_TAG_TR)) + return(0); + return(html_tputln(mbuf, ML_CLOSE, --i, HTML_TAG_TABLE)); } @@ -414,7 +506,11 @@ static int html_end(struct md_mbuf *mbuf, const struct md_args *args) { - return(ml_puts(mbuf, "
\n", NULL)); + if ( ! html_tputln(mbuf, ML_CLOSE, 0, HTML_TAG_DIV)) + return(0); + if ( ! html_tputln(mbuf, ML_CLOSE, 0, HTML_TAG_BODY)) + return(0); + return(html_tputln(mbuf, ML_CLOSE, 0, HTML_TAG_HTML)); } @@ -429,16 +525,16 @@ html_bodytagname(struct md_mbuf *mbuf, case (ROFF_Bl): return(html_Bl_bodytagname(mbuf, q, argc, argv, res)); case (ROFF_Fo): - return(ml_nputs(mbuf, "span", 4, res)); + return(html_stput(mbuf, HTML_TAG_SPAN, res)); case (ROFF_It): return(html_It_bodytagname(mbuf, q, argc, argv, res)); case (ROFF_Oo): - return(ml_nputs(mbuf, "span", 4, res)); + return(html_stput(mbuf, HTML_TAG_SPAN, res)); default: break; } - return(ml_puts(mbuf, "div", res)); + return(html_stput(mbuf, HTML_TAG_DIV, res)); } @@ -453,18 +549,18 @@ html_headtagname(struct md_mbuf *mbuf, case (ROFF_It): return(html_It_headtagname(mbuf, q, argc, argv, res)); case (ROFF_Fo): - return(ml_nputs(mbuf, "span", 4, res)); + /* FALLTHROUGH */ case (ROFF_Oo): - return(ml_nputs(mbuf, "span", 4, res)); + return(html_stput(mbuf, HTML_TAG_SPAN, res)); case (ROFF_Sh): - return(ml_nputs(mbuf, "h1", 2, res)); + return(html_stput(mbuf, HTML_TAG_H1, res)); case (ROFF_Ss): - return(ml_nputs(mbuf, "h2", 2, res)); + return(html_stput(mbuf, HTML_TAG_H2, res)); default: break; } - return(ml_nputs(mbuf, "div", 3, res)); + return(html_stput(mbuf, HTML_TAG_DIV, res)); } @@ -477,16 +573,16 @@ html_blocktagname(struct md_mbuf *mbuf, const struct m switch (tok) { case (ROFF_Fo): - return(ml_nputs(mbuf, "span", 4, res)); + /* FALLTHROUGH */ case (ROFF_Oo): - return(ml_nputs(mbuf, "span", 4, res)); + return(html_stput(mbuf, HTML_TAG_SPAN, res)); case (ROFF_It): return(html_It_blocktagname(mbuf, q, argc, argv, res)); default: break; } - return(ml_puts(mbuf, "div", res)); + return(html_stput(mbuf, HTML_TAG_DIV, res)); } @@ -496,6 +592,8 @@ html_printargs(struct md_mbuf *mbuf, int tok, const ch const int *argc, const char **argv, size_t *res) { + /* FIXME: use API in ml.h. */ + if ( ! ml_puts(mbuf, " class=\"", res)) return(0); if ( ! ml_puts(mbuf, ns, res)) @@ -553,6 +651,9 @@ html_inlinetagargs(struct md_mbuf *mbuf, switch (tok) { case (ROFF_Sx): + + /* FIXME: use API in ml.h. */ + assert(*argv); if ( ! ml_nputs(mbuf, " href=\"#", 8, res)) return(0); @@ -577,14 +678,14 @@ html_inlinetagname(struct md_mbuf *mbuf, switch (tok) { case (ROFF_Pp): - return(ml_nputs(mbuf, "div", 3, res)); + return(html_stput(mbuf, HTML_TAG_DIV, res)); case (ROFF_Sx): - return(ml_nputs(mbuf, "a", 1, res)); + return(html_stput(mbuf, HTML_TAG_A, res)); default: break; } - return(ml_puts(mbuf, "span", res)); + return(html_stput(mbuf, HTML_TAG_SPAN, res)); } @@ -750,16 +851,14 @@ html_beginhttp(struct md_mbuf *mbuf, const char *buf, size_t sz) { size_t res; + struct html_pair pair; res = 0; + pair.attr = HTML_ATTR_HREF; + pair.val = (char *)buf; - if ( ! ml_puts(mbuf, "", &res)) - return(-1); - return((ssize_t)res); } @@ -772,10 +871,8 @@ html_endhttp(struct md_mbuf *mbuf, size_t res; res = 0; - - if ( ! ml_puts(mbuf, "", &res)) + if ( ! html_tput(mbuf, ML_CLOSE, HTML_TAG_A, &res)) return(-1); - return((ssize_t)res); }