diff options
author | Kristaps Dzonsons <kristaps@bsd.lv> | 2008-12-10 00:52:46 +0000 |
---|---|---|
committer | Kristaps Dzonsons <kristaps@bsd.lv> | 2008-12-10 00:52:46 +0000 |
commit | 14b766df0d1ee7f25fafdd5aafafb9f03d3f0488 (patch) | |
tree | fd4e8d52ac28c902aff3985b63cef09c9b9d24e9 | |
parent | 5181e986114c18c409bb0b17c01aa4306c15c24e (diff) | |
download | mandoc-14b766df0d1ee7f25fafdd5aafafb9f03d3f0488.tar.gz |
*** empty log message ***
-rw-r--r-- | Makefile | 27 | ||||
-rw-r--r-- | html.c | 245 | ||||
-rw-r--r-- | html.h | 83 | ||||
-rw-r--r-- | ml.h | 16 | ||||
-rw-r--r-- | mlg.c | 15 | ||||
-rw-r--r-- | roff.c | 5 | ||||
-rw-r--r-- | tags.c | 187 |
7 files changed, 444 insertions, 134 deletions
@@ -8,19 +8,19 @@ CFLAGS += -W -Wall -Wno-unused-parameter -g -DDEBUG LINTFLAGS += -c -e -f -u LNS = mdocml.ln html.ln xml.ln libmdocml.ln roff.ln ml.ln mlg.ln \ - compat.ln tokens.ln literals.ln + compat.ln tokens.ln literals.ln tags.ln LLNS = llib-lmdocml.ln LIBS = libmdocml.a OBJS = mdocml.o html.o xml.o libmdocml.o roff.o ml.o mlg.o \ - compat.o tokens.o literals.o + compat.o tokens.o literals.o tags.o SRCS = mdocml.c html.c xml.c libmdocml.c roff.c ml.c mlg.c \ - compat.c tokens.c literals.c + compat.c tokens.c literals.c tags.c -HEADS = libmdocml.h private.h ml.h roff.h +HEADS = libmdocml.h private.h ml.h roff.h html.h MANS = mdocml.1 index.7 @@ -90,15 +90,15 @@ mdocml.tgz: $(INSTALL) ( cd .dist/mdocml/ && tar zcf ../../mdocml.tgz mdocml-$(VERSION)/ ) rm -rf .dist/ -llib-lmdocml.ln: mdocml.ln libmdocml.ln html.ln xml.ln roff.ln ml.ln mlg.ln compat.ln tokens.ln literals.ln - $(LINT) $(LINTFLAGS) -Cmdocml mdocml.ln libmdocml.ln html.ln xml.ln roff.ln ml.ln mlg.ln compat.ln tokens.ln literals.ln +llib-lmdocml.ln: mdocml.ln libmdocml.ln html.ln xml.ln roff.ln ml.ln mlg.ln compat.ln tokens.ln literals.ln tags.ln + $(LINT) $(LINTFLAGS) -Cmdocml mdocml.ln libmdocml.ln html.ln xml.ln roff.ln ml.ln mlg.ln compat.ln tokens.ln literals.ln tags.ln mdocml.ln: mdocml.c libmdocml.h mdocml.o: mdocml.c libmdocml.h -libmdocml.a: libmdocml.o html.o xml.o roff.o ml.o mlg.o compat.o tokens.o literals.o - $(AR) rs $@ libmdocml.o html.o xml.o roff.o ml.o mlg.o compat.o tokens.o literals.o +libmdocml.a: libmdocml.o html.o xml.o roff.o ml.o mlg.o compat.o tokens.o literals.o tags.o + $(AR) rs $@ libmdocml.o html.o xml.o roff.o ml.o mlg.o compat.o tokens.o literals.o tags.o xml.ln: xml.c private.h libmdocml.h ml.h @@ -108,6 +108,10 @@ html.ln: html.c private.h libmdocml.h html.o: html.c private.h libmdocml.h +tags.ln: tags.c html.h + +tags.o: tags.c html.h + roff.ln: roff.c private.h roff.h libmdocml.h roff.o: roff.c private.h roff.h libmdocml.h @@ -127,3 +131,10 @@ mlg.o: mlg.c private.h libmdocml.h ml.h compat.ln: compat.c compat.o: compat.c + +html.h: ml.h + +ml.h: private.h + +private.h: libmdocml.h + @@ -27,31 +27,9 @@ #include <string.h> #include <unistd.h> -#include "private.h" +#include "html.h" #include "ml.h" -#define TAG_HTML "<html>" -#define TAG_HTML_END "</html>" -#define TAG_BODY "<body>" -#define TAG_BODY_END "</body>" -#define TAG_DIV_MDOC "<div class=\"mdoc\">" -#define TAG_DIV_END "</div>" -#define TAG_STYLE_CSS "<style type=\"text/css\"><!--" -#define TAG_STYLE_END "--></style>" -#define TAG_HEAD "<head>" -#define TAG_HEAD_END "</head>" -#define TAG_TITLE "<title>" -#define TAG_TITLE_END "</title>" -#define TAG_LINK_CSS "<link rel=\"stylesheet\" " \ - "type=\"text/css\" href=\"%s\">" -#define TAG_DOCTYPE "<!DOCTYPE HTML PUBLIC " \ - "\"-//W3C//DTD HTML 4.01//EN\" " \ - "\"http://www.w3.org/TR/html4/strict.dtd\">" -#define TAG_RESTYPE "<meta name=\"resource-type\" " \ - "content=\"document\">" -#define TAG_CONTTYPE "<meta http-equiv=\"Content-Type\" " \ - "content=\"text/html;charset=utf-8\">" - /* TODO: allow head/tail-less invocations (just "div" start). */ struct htmlnode { @@ -62,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 *, @@ -139,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 */ @@ -160,11 +142,11 @@ html_It_headtagname(struct md_mbuf *mbuf, struct htmlq *q, 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; } @@ -209,11 +191,11 @@ html_It_bodytagname(struct md_mbuf *mbuf, struct htmlq *q, 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; } @@ -235,7 +217,7 @@ html_Bl_bodytagname(struct md_mbuf *mbuf, struct htmlq *q, && 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): @@ -251,11 +233,11 @@ html_Bl_bodytagname(struct md_mbuf *mbuf, struct htmlq *q, 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; } @@ -301,11 +283,11 @@ html_It_blocktagname(struct md_mbuf *mbuf, struct htmlq *q, 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; } @@ -371,27 +353,28 @@ out: static int -html_putline(struct md_mbuf *mbuf, size_t indent, - const char *p, size_t *res) +html_tputln(struct md_mbuf *mbuf, enum ml_scope scope, + int i, enum html_tag tag) { - /* FIXME: use INDENT macro for this. */ - if ( ! ml_putchars(mbuf, ' ', indent * 4, res)) + if ( ! ml_putchars(mbuf, ' ', INDENT(i) * INDENT_SZ, NULL)) return(0); - if ( ! ml_puts(mbuf, p, res)) + if ( ! html_tput(mbuf, scope, tag, NULL)) return(0); - return(ml_nputs(mbuf, "\n", 1, res)); + return(ml_nputs(mbuf, "\n", 1, NULL)); } static int -html_putlinestart(struct md_mbuf *mbuf, size_t indent, - const char *p, size_t *res) +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 * 4, res)) + if ( ! ml_putchars(mbuf, ' ', INDENT(i) * INDENT_SZ, NULL)) return(0); - return(ml_puts(mbuf, p, res)); + if ( ! html_aput(mbuf, scope, tag, NULL, sz, p)) + return(0); + return(ml_nputs(mbuf, "\n", 1, NULL)); } @@ -399,80 +382,119 @@ html_putlinestart(struct md_mbuf *mbuf, size_t indent, 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) { - char mtitle[128], css[128]; - size_t i; + struct html_pair attr[4]; + char ts[32]; + int i; - (void)snprintf(mtitle, sizeof(mtitle), - "Manual Page for %s(%s)", - title, roff_msecname(section)); - (void)snprintf(css, sizeof(css), - TAG_LINK_CSS, args->params.html.css); + (void)snprintf(ts, sizeof(ts), "%s(%s)", + name, roff_msecname(msec)); i = 0; - if ( ! html_putline(mbuf, i, TAG_DOCTYPE, NULL)) + if ( ! html_typeput(mbuf, HTML_TYPE_4_01_STRICT, NULL)) return(0); - if ( ! html_putline(mbuf, i, TAG_HTML, NULL)) + if ( ! html_tputln(mbuf, ML_OPEN, i, HTML_TAG_HTML)) return(0); - if ( ! html_putline(mbuf, i++, TAG_HEAD, NULL)) + if ( ! html_tputln(mbuf, ML_OPEN, i++, HTML_TAG_HEAD)) return(0); - if ( ! html_putline(mbuf, i, TAG_CONTTYPE, NULL)) + + 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 ( ! html_aputln(mbuf, ML_OPEN, i, HTML_TAG_META, 2, attr)) return(0); - if ( ! html_putline(mbuf, i, TAG_RESTYPE, NULL)) + + 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_putlinestart(mbuf, i, TAG_TITLE, NULL)) + + if ( ! html_tputln(mbuf, ML_OPEN, i, HTML_TAG_TITLE)) return(0); - if ( ! ml_putstring(mbuf, mtitle, NULL)) + if ( ! ml_putstring(mbuf, ts, NULL)) return(0); - if ( ! html_putline(mbuf, i, TAG_TITLE_END, NULL)) + if ( ! html_tputln(mbuf, ML_CLOSE, i, HTML_TAG_TITLE)) return(0); if (HTML_CSS_EMBED & args->params.html.flags) { - if ( ! html_putline(mbuf, i, TAG_STYLE_CSS, NULL)) + attr[0].attr = HTML_ATTR_TYPE; + attr[0].val = "text/css"; + + if ( ! html_aputln(mbuf, ML_OPEN, i, + HTML_TAG_STYLE, 1, attr)) return(0); + if ( ! html_commentput(mbuf, ML_OPEN, NULL)) + return(NULL); + if ( ! html_loadcss(mbuf, args->params.html.css)) return(0); - if ( ! html_putline(mbuf, i, TAG_STYLE_END, NULL)) + + if ( ! html_commentput(mbuf, ML_CLOSE, NULL)) + return(NULL); + if ( ! html_tputln(mbuf, ML_CLOSE, i, HTML_TAG_STYLE)) return(0); - } else if ( ! html_putline(mbuf, i, css, NULL)) - return(0); + } else { + 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 ( ! html_putline(mbuf, --i, TAG_HEAD_END, NULL)) - return(0); - if ( ! html_putline(mbuf, i, TAG_BODY, NULL)) - return(0); - if ( ! html_putline(mbuf, i, TAG_DIV_MDOC, NULL)) + if ( ! html_tputln(mbuf, ML_CLOSE, --i, HTML_TAG_HEAD)) return(0); - if ( ! html_putline(mbuf, i++, "<table width=\"100%\">", NULL)) + if ( ! html_tputln(mbuf, ML_OPEN, i, HTML_TAG_BODY)) return(0); - if ( ! html_putline(mbuf, i++, "<tr>", NULL)) + + attr[0].attr = HTML_ATTR_CLASS; + attr[0].val = "mdoc"; + + if ( ! html_aputln(mbuf, ML_OPEN, i, HTML_TAG_DIV, 1, attr)) return(0); - if ( ! html_putline(mbuf, i++, "<td align=\"left\">", NULL)) + + 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 ( ! ml_putstring(mbuf, title, NULL)) + if ( ! html_tputln(mbuf, ML_OPEN, i++, HTML_TAG_TR)) return(0); - if ( ! html_putline(mbuf, --i, "</td>", NULL)) + + if ( ! html_tputln(mbuf, ML_OPEN, i, HTML_TAG_TD)) return(0); - if ( ! html_putline(mbuf, i++, "<td align=\"center\">", NULL)) + if ( ! ml_putstring(mbuf, ts, NULL)) return(0); - if ( ! ml_putstring(mbuf, "Hello, world.", NULL)) + if ( ! html_tputln(mbuf, ML_CLOSE, i, HTML_TAG_TD)) return(0); - if ( ! html_putline(mbuf, --i, "</td>", NULL)) + + if ( ! html_tputln(mbuf, ML_OPEN, i, HTML_TAG_TD)) return(0); - if ( ! html_putline(mbuf, i++, "<td align=\"right\">", NULL)) + /* TODO: middle. */ + if ( ! html_tputln(mbuf, ML_CLOSE, i, HTML_TAG_TD)) return(0); - if ( ! ml_putstring(mbuf, title, NULL)) + + if ( ! html_tputln(mbuf, ML_OPEN, i, HTML_TAG_TD)) return(0); - if ( ! html_putline(mbuf, --i, "</td>", NULL)) + if ( ! ml_putstring(mbuf, ts, NULL)) return(0); - if ( ! html_putline(mbuf, --i, "</tr>", NULL)) + if ( ! html_tputln(mbuf, ML_CLOSE, i, HTML_TAG_TD)) return(0); - if ( ! html_putline(mbuf, --i, "</table>", NULL)) + + if ( ! html_tputln(mbuf, ML_CLOSE, --i, HTML_TAG_TR)) return(0); - return(1); + return(html_tputln(mbuf, ML_CLOSE, --i, HTML_TAG_TABLE)); } @@ -481,11 +503,11 @@ static int html_end(struct md_mbuf *mbuf, const struct md_args *args) { - if ( ! html_putline(mbuf, 0, TAG_DIV_END, NULL)) + if ( ! html_tputln(mbuf, ML_CLOSE, 0, HTML_TAG_DIV)) return(0); - if ( ! html_putline(mbuf, 0, TAG_BODY_END, NULL)) + if ( ! html_tputln(mbuf, ML_CLOSE, 0, HTML_TAG_BODY)) return(0); - return(html_putline(mbuf, 0, TAG_HTML_END, NULL)); + return(html_tputln(mbuf, ML_CLOSE, 0, HTML_TAG_HTML)); } @@ -500,16 +522,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)); } @@ -524,18 +546,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)); } @@ -548,16 +570,16 @@ html_blocktagname(struct md_mbuf *mbuf, const struct md_args *args, 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)); } @@ -567,6 +589,8 @@ html_printargs(struct md_mbuf *mbuf, int tok, const char *ns, 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)) @@ -624,6 +648,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); @@ -648,14 +675,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)); } @@ -821,16 +848,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, "<a href=\"", &res)) - return(-1); - if (1 != ml_nputstring(mbuf, buf, sz, &res)) + if ( ! html_aput(mbuf, ML_OPEN, HTML_TAG_A, &res, 1, &pair)) return(-1); - if ( ! ml_puts(mbuf, "\">", &res)) - return(-1); - return((ssize_t)res); } @@ -843,10 +868,8 @@ html_endhttp(struct md_mbuf *mbuf, size_t res; res = 0; - - if ( ! ml_puts(mbuf, "</a>", &res)) + if ( ! html_tput(mbuf, ML_CLOSE, HTML_TAG_A, &res)) return(-1); - return((ssize_t)res); } @@ -0,0 +1,83 @@ +/* $Id$ */ +/* + * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the + * above copyright notice and this permission notice appear in all + * copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL + * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE + * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER + * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. + */ +#ifndef HTML_H +#define HTML_H + +#include "ml.h" + +enum html_tag { + HTML_TAG_SPAN = 0, + HTML_TAG_HTML = 1, + HTML_TAG_HEAD = 2, + HTML_TAG_META = 3, + HTML_TAG_TITLE = 4, + HTML_TAG_STYLE = 5, + HTML_TAG_LINK = 6, + HTML_TAG_BODY = 7, + HTML_TAG_DIV = 8, + HTML_TAG_TABLE = 9, + HTML_TAG_TD = 10, + HTML_TAG_TR = 11, + HTML_TAG_OL = 12, + HTML_TAG_UL = 13, + HTML_TAG_LI = 14, + HTML_TAG_H1 = 15, + HTML_TAG_H2 = 16, + HTML_TAG_A = 17, +}; + +enum html_attr { + HTML_ATTR_CLASS = 0, + HTML_ATTR_HTTP_EQUIV = 1, + HTML_ATTR_CONTENT = 2, + HTML_ATTR_NAME = 3, + HTML_ATTR_TYPE = 4, + HTML_ATTR_REL = 5, + HTML_ATTR_HREF = 6, + HTML_ATTR_WIDTH = 7, +}; + +enum html_type { + HTML_TYPE_4_01_STRICT = 0 +}; + +struct html_pair { + enum html_attr attr; + char *val; +}; + +__BEGIN_DECLS + +int html_typeput(struct md_mbuf *, + enum html_type, size_t *); +int html_commentput(struct md_mbuf *, + enum ml_scope, size_t *); +int html_tput(struct md_mbuf *, + enum ml_scope, enum html_tag, size_t *); +int html_aput(struct md_mbuf *, enum ml_scope, + enum html_tag, size_t *, + int, const struct html_pair *); +int html_stput(struct md_mbuf *, + enum html_tag, size_t *); +int html_saput(struct md_mbuf *, enum html_tag, + size_t *, int, const struct html_pair *); + +__END_DECLS + +#endif /*!HTML_H*/ @@ -19,6 +19,13 @@ #ifndef ML_H #define ML_H +#include "private.h" + +#define COLUMNS 72 +#define INDENT_SZ 4 +#define INDENT(x) ((x) > MAXINDENT ? MAXINDENT : (x)) +#define MAXINDENT 10 + struct md_mlg; enum md_ns { @@ -29,6 +36,11 @@ enum md_ns { MD_NS_DEFAULT, }; +enum ml_scope { + ML_OPEN, + ML_CLOSE +}; + struct ml_cbs { int (*ml_begin)(struct md_mbuf *, const struct md_args *, @@ -66,12 +78,16 @@ int ml_puts(struct md_mbuf *, const char *, size_t *); int ml_putchars(struct md_mbuf *, char, size_t, size_t *); +/* FIXME: move into mlg.h or private.h. */ struct md_mlg *mlg_alloc(const struct md_args *, const struct md_rbuf *, struct md_mbuf *, const struct ml_cbs *); int mlg_exit(struct md_mlg *, int); int mlg_line(struct md_mlg *, char *); +int ml_tagput(struct md_mbuf *, + enum ml_scope, const char *, size_t *); + __END_DECLS #endif /*!ML_H*/ @@ -24,15 +24,10 @@ #include <stdio.h> #include <string.h> -#include "private.h" #include "ml.h" /* TODO: literal tokens. */ -#define COLUMNS 72 -#define INDENT 4 -#define MAXINDENT 10 - enum md_tok { MD_TEXT, MD_INLINE_IN, @@ -239,14 +234,10 @@ mlg_endtag(struct md_mlg *p, enum md_ns ns, int tok) static int mlg_indent(struct md_mlg *p) { - size_t count; - - count = p->indent > MAXINDENT ? - (size_t)MAXINDENT : p->indent; - count *= INDENT; assert(0 == p->pos); - return(ml_putchars(p->mbuf, ' ', count, &p->pos)); + return(ml_putchars(p->mbuf, ' ', INDENT_SZ * + INDENT(p->indent), &p->pos)); } @@ -325,7 +316,7 @@ mlg_data(struct md_mlg *p, int space, if ( ! mlg_nstring(p, start, buf, sz)) return(0); - if (p->indent * INDENT + sz >= COLUMNS) + if (INDENT(p->indent) * INDENT_SZ + sz >= COLUMNS) if ( ! mlg_newline(p)) return(0); @@ -1154,7 +1154,7 @@ static int roff_layout(ROFFCALL_ARGS) { int i, c, argcp[ROFF_MAXLINEARG]; - char *argvp[ROFF_MAXLINEARG], *p; + char *argvp[ROFF_MAXLINEARG]; /* * The roff_layout function is for multi-line macros. A layout @@ -1180,10 +1180,9 @@ roff_layout(ROFFCALL_ARGS) return((*tree->cb.roffblkout)(tree->arg, tok)); } + argv++; assert( ! (ROFF_CALLABLE & tokens[tok].flags)); - p = *argv++; - if ( ! roffparseopts(tree, tok, &argv, argcp, argvp)) return(0); if (NULL == roffnode_new(tok, tree)) @@ -0,0 +1,187 @@ +/* $Id$ */ +/* + * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the + * above copyright notice and this permission notice appear in all + * copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL + * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE + * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER + * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. + */ +#include <assert.h> +#include <stdarg.h> +#include <stdlib.h> + +#include "html.h" + +static int html_tstart(struct md_mbuf *, enum ml_scope, + enum html_tag, size_t *); +static int html_tclose(struct md_mbuf *, size_t *); + +static const char * const tagnames[] = { + "span", "html", "head", "meta", + "title", "style", "link", "body", + "div", "table", "td", "tr", + "ol", "ul", "li", "h1", + "h2", "a", + }; + +static const char * const attrnames[] = { + "class", "http-equiv", "content", "name", + "type", "rel", "href", "width", + }; + +static const char * const typenames[] = { + "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"" + "\"http://www.w3.org/TR/html4/strict.dtd\">", + }; + + +/* FIXME: move into ml.c. */ +static int +html_tstart(struct md_mbuf *mbuf, enum ml_scope scope, + enum html_tag tag, size_t *res) +{ + + switch (scope) { + case (ML_OPEN): + if ( ! ml_nputs(mbuf, "<", 1, res)) + return(0); + break; + case (ML_CLOSE): + if ( ! ml_nputs(mbuf, "</", 2, res)) + return(0); + break; + default: + abort(); + /* NOTREACHED */ + } + + return(ml_puts(mbuf, tagnames[tag], res)); +} + + +/* FIXME: move into ml.c. */ +static int +html_tclose(struct md_mbuf *mbuf, size_t *res) +{ + + return(ml_nputs(mbuf, ">", 1, res)); +} + + + +int +html_stput(struct md_mbuf *mbuf, enum html_tag tag, size_t *res) +{ + + return(ml_puts(mbuf, tagnames[tag], res)); +} + + +int +html_saput(struct md_mbuf *mbuf, enum html_tag tag, + size_t *res, int sz, const struct html_pair *p) +{ + int i; + + if ( ! ml_puts(mbuf, tagnames[tag], res)) + return(0); + + assert(sz >= 0); + for (i = 0; i < sz; i++) { + + /* FIXME: move into ml.c. */ + + if ( ! ml_nputs(mbuf, " ", 1, res)) + return(0); + if ( ! ml_puts(mbuf, attrnames[p[i].attr], res)) + return(0); + if ( ! ml_nputs(mbuf, "=\"", 2, res)) + return(0); + if ( ! ml_putstring(mbuf, p[i].val, res)) + return(0); + if ( ! ml_nputs(mbuf, "\"", 1, res)) + return(0); + } + + return(1); +} + + +int +html_tput(struct md_mbuf *mbuf, enum ml_scope scope, + enum html_tag tag, size_t *res) +{ + + if ( ! html_tstart(mbuf, scope, tag, res)) + return(0); + return(html_tclose(mbuf, res)); +} + + +int +html_aput(struct md_mbuf *mbuf, enum ml_scope scope, + enum html_tag tag, size_t *res, + int sz, const struct html_pair *p) +{ + int i; + + if ( ! html_tstart(mbuf, scope, tag, res)) + return(0); + + assert(sz >= 0); + for (i = 0; i < sz; i++) { + + /* FIXME: move into ml.c. */ + + if ( ! ml_nputs(mbuf, " ", 1, res)) + return(0); + if ( ! ml_puts(mbuf, attrnames[p[i].attr], res)) + return(0); + if ( ! ml_nputs(mbuf, "=\"", 2, res)) + return(0); + if ( ! ml_putstring(mbuf, p[i].val, res)) + return(0); + if ( ! ml_nputs(mbuf, "\"", 1, res)) + return(0); + } + + return(html_tclose(mbuf, res)); +} + + +int +html_typeput(struct md_mbuf *mbuf, + enum html_type type, size_t *res) +{ + + return(ml_puts(mbuf, typenames[type], res)); +} + + +int +html_commentput(struct md_mbuf *mbuf, + enum ml_scope scope, size_t *res) +{ + + switch (scope) { + case (ML_OPEN): + return(ml_nputs(mbuf, "<!--", 4, res)); + case (ML_CLOSE): + return(ml_nputs(mbuf, "-->", 3, res)); + default: + break; + } + + abort(); + /* NOTREACHED */ +} |