=================================================================== RCS file: /cvs/mandoc/html.c,v retrieving revision 1.7 retrieving revision 1.9 diff -u -p -r1.7 -r1.9 --- mandoc/html.c 2008/12/05 11:28:16 1.7 +++ mandoc/html.c 2008/12/05 19:45:15 1.9 @@ -1,4 +1,4 @@ -/* $Id: html.c,v 1.7 2008/12/05 11:28:16 kristaps Exp $ */ +/* $Id: html.c,v 1.9 2008/12/05 19:45:15 kristaps Exp $ */ /* * Copyright (c) 2008 Kristaps Dzonsons * @@ -32,8 +32,11 @@ #include "ml.h" +/* TODO: allow head/tail-less invocations (just "div" start). */ + struct htmlnode { - int type; + int tok; + enum md_ns ns; int *argc[ROFF_MAXLINEARG]; char *argv[ROFF_MAXLINEARG]; struct htmlnode *parent; @@ -47,10 +50,12 @@ struct htmlq { static int html_loadcss(struct md_mbuf *, const char *); -static ssize_t html_endtag(struct md_mbuf *, +static int html_alloc(void **); +static void html_free(void *); +static ssize_t html_endtag(struct md_mbuf *, void *, const struct md_args *, enum md_ns, int); -static ssize_t html_begintag(struct md_mbuf *, +static ssize_t html_begintag(struct md_mbuf *, void *, const struct md_args *, enum md_ns, int, const int *, const char **); @@ -203,13 +208,8 @@ html_begin(struct md_mbuf *mbuf, const struct md_args static int html_end(struct md_mbuf *mbuf, const struct md_args *args) { - size_t res; - res = 0; - if ( ! ml_puts(mbuf, "\n", &res)) - return(0); - - return(1); + return(ml_puts(mbuf, "\n", NULL)); } @@ -223,8 +223,6 @@ html_blockbodytagname(struct md_mbuf *mbuf, } - - /* ARGSUSED */ static int html_blockheadtagname(struct md_mbuf *mbuf, @@ -245,11 +243,11 @@ html_blocktagname(struct md_mbuf *mbuf, } +/* ARGSUSED */ static int html_printargs(struct md_mbuf *mbuf, int tok, const char *ns, const int *argc, const char **argv, size_t *res) { - int i, c; if ( ! ml_puts(mbuf, " class=\"", res)) return(0); @@ -259,35 +257,7 @@ html_printargs(struct md_mbuf *mbuf, int tok, const ch return(0); if ( ! ml_puts(mbuf, toknames[tok], res)) return(0); - if ( ! ml_puts(mbuf, "\"", res)) - return(0); - - if (NULL == argv || NULL == argc) - return(1); - assert(argv && argc); - - /* FIXME: ignores values. */ - - for (i = 0; ROFF_ARGMAX != (c = argc[i]); i++) { - if (argv[i]) - continue; - if ( ! ml_puts(mbuf, " class=\"", res)) - return(0); - if ( ! ml_puts(mbuf, ns, res)) - return(0); - if ( ! ml_puts(mbuf, "-", res)) - return(0); - if ( ! ml_puts(mbuf, toknames[tok], res)) - return(0); - if ( ! ml_puts(mbuf, "-", res)) - return(0); - if ( ! ml_puts(mbuf, tokargnames[c], res)) - return(0); - if ( ! ml_puts(mbuf, "\"", res)) - return(0); - } - - return(1); + return(ml_puts(mbuf, "\"", res)); } @@ -345,22 +315,39 @@ html_inlinetagname(struct md_mbuf *mbuf, case (ROFF_Pp): return(ml_puts(mbuf, "div", res)); default: - return(ml_puts(mbuf, "span", res)); + break; } - return(1); + + return(ml_puts(mbuf, "span", res)); } static ssize_t -html_begintag(struct md_mbuf *mbuf, const struct md_args *args, - enum md_ns ns, int tok, - const int *argc, const char **argv) +html_begintag(struct md_mbuf *mbuf, void *data, + const struct md_args *args, enum md_ns ns, + int tok, const int *argc, const char **argv) { size_t res; + struct htmlq *q; + struct htmlnode *node; assert(ns != MD_NS_DEFAULT); res = 0; + assert(data); + q = (struct htmlq *)data; + + if (NULL == (node = calloc(1, sizeof(struct htmlnode)))) { + warn("calloc"); + return(-1); + } + + node->parent = q->last; + node->tok = tok; + node->ns = ns; + + q->last = node; + switch (ns) { case (MD_NS_BLOCK): if ( ! html_blocktagname(mbuf, args, tok, &res)) @@ -397,14 +384,19 @@ html_begintag(struct md_mbuf *mbuf, const struct md_ar static ssize_t -html_endtag(struct md_mbuf *mbuf, const struct md_args *args, - enum md_ns ns, int tok) +html_endtag(struct md_mbuf *mbuf, void *data, + const struct md_args *args, enum md_ns ns, int tok) { size_t res; + struct htmlq *q; + struct htmlnode *node; assert(ns != MD_NS_DEFAULT); res = 0; + assert(data); + q = (struct htmlq *)data; + switch (ns) { case (MD_NS_BLOCK): if ( ! html_blocktagname(mbuf, args, tok, &res)) @@ -424,10 +416,45 @@ html_endtag(struct md_mbuf *mbuf, const struct md_args break; } + node = q->last; + q->last = node->parent; + + free(node); + return((ssize_t)res); } +static int +html_alloc(void **p) +{ + + if (NULL == (*p = calloc(1, sizeof(struct htmlq)))) { + warn("calloc"); + return(0); + } + return(1); +} + + +static void +html_free(void *p) +{ + struct htmlq *q; + struct htmlnode *n; + + assert(p); + q = (struct htmlq *)p; + + while ((n = q->last)) { + q->last = n->parent; + free(n); + } + + free(q); +} + + int md_line_html(void *data, char *buf) { @@ -448,8 +475,15 @@ void * md_init_html(const struct md_args *args, struct md_mbuf *mbuf, const struct md_rbuf *rbuf) { + struct ml_cbs cbs; - return(mlg_alloc(args, rbuf, mbuf, html_begintag, - html_endtag, html_begin, html_end)); + cbs.ml_alloc = html_alloc; + cbs.ml_free = html_free; + cbs.ml_begintag = html_begintag; + cbs.ml_endtag = html_endtag; + cbs.ml_begin = html_begin; + cbs.ml_end = html_end; + + return(mlg_alloc(args, rbuf, mbuf, &cbs)); }