version 1.6, 2008/12/04 23:10:51 |
version 1.9, 2008/12/05 19:45:15 |
|
|
#include "ml.h" |
#include "ml.h" |
|
|
|
|
|
/* TODO: allow head/tail-less invocations (just "div" start). */ |
|
|
|
struct htmlnode { |
|
int tok; |
|
enum md_ns ns; |
|
int *argc[ROFF_MAXLINEARG]; |
|
char *argv[ROFF_MAXLINEARG]; |
|
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 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 *, |
const struct md_args *, |
enum md_ns, int); |
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 *, |
const struct md_args *, |
enum md_ns, int, |
enum md_ns, int, |
const int *, const char **); |
const int *, const char **); |
Line 46 static int html_begin(struct md_mbuf *, |
|
Line 64 static int html_begin(struct md_mbuf *, |
|
const struct tm *, |
const struct tm *, |
const char *, const char *, |
const char *, const char *, |
const char *, const char *); |
const char *, const char *); |
|
static int html_printargs(struct md_mbuf *, int, |
|
const char *, const int *, |
|
const char **, size_t *); |
static int html_end(struct md_mbuf *, |
static int html_end(struct md_mbuf *, |
const struct md_args *); |
const struct md_args *); |
static ssize_t html_blocktagname(struct md_mbuf *, |
static int html_blocktagname(struct md_mbuf *, |
const struct md_args *, int); |
const struct md_args *, int, size_t *); |
static ssize_t html_blocktagargs(struct md_mbuf *, |
static int html_blocktagargs(struct md_mbuf *, |
const struct md_args *, int, |
const struct md_args *, int, |
const int *, const char **); |
const int *, const char **, size_t *); |
static ssize_t html_blockheadtagname(struct md_mbuf *, |
static int html_blockheadtagname(struct md_mbuf *, |
const struct md_args *, int); |
const struct md_args *, int, size_t *); |
static ssize_t html_blockheadtagargs(struct md_mbuf *, |
static int html_blockheadtagargs(struct md_mbuf *, |
const struct md_args *, int, |
const struct md_args *, int, |
const int *, const char **); |
const int *, const char **, size_t *); |
static ssize_t html_blockbodytagname(struct md_mbuf *, |
static int html_blockbodytagname(struct md_mbuf *, |
const struct md_args *, int); |
const struct md_args *, int, size_t *); |
static ssize_t html_blockbodytagargs(struct md_mbuf *, |
static int html_blockbodytagargs(struct md_mbuf *, |
const struct md_args *, int, |
const struct md_args *, int, |
const int *, const char **); |
const int *, const char **, size_t *); |
static ssize_t html_inlinetagname(struct md_mbuf *, |
static int html_inlinetagname(struct md_mbuf *, |
const struct md_args *, int); |
const struct md_args *, int, size_t *); |
static ssize_t html_inlinetagargs(struct md_mbuf *, |
static int html_inlinetagargs(struct md_mbuf *, |
const struct md_args *, int, |
const struct md_args *, int, |
const int *, const char **); |
const int *, const char **, size_t *); |
|
|
|
|
static int |
static int |
Line 187 html_begin(struct md_mbuf *mbuf, const struct md_args |
|
Line 208 html_begin(struct md_mbuf *mbuf, const struct md_args |
|
static int |
static int |
html_end(struct md_mbuf *mbuf, const struct md_args *args) |
html_end(struct md_mbuf *mbuf, const struct md_args *args) |
{ |
{ |
size_t res; |
|
|
|
res = 0; |
return(ml_puts(mbuf, "</div></body>\n</html>", NULL)); |
if ( ! ml_puts(mbuf, "</div></body>\n</html>", &res)) |
|
return(0); |
|
|
|
return(1); |
|
} |
} |
|
|
|
|
/* ARGSUSED */ |
/* ARGSUSED */ |
static ssize_t |
static int |
html_blockbodytagname(struct md_mbuf *mbuf, |
html_blockbodytagname(struct md_mbuf *mbuf, |
const struct md_args *args, int tok) |
const struct md_args *args, int tok, size_t *res) |
{ |
{ |
size_t res; |
|
|
|
res = 0; |
return(ml_puts(mbuf, "div", res)); |
if ( ! ml_puts(mbuf, "div", &res)) |
|
return(-1); |
|
|
|
return((ssize_t)res); |
|
} |
} |
|
|
|
|
|
|
|
|
/* ARGSUSED */ |
/* ARGSUSED */ |
static ssize_t |
static int |
html_blockheadtagname(struct md_mbuf *mbuf, |
html_blockheadtagname(struct md_mbuf *mbuf, |
const struct md_args *args, int tok) |
const struct md_args *args, int tok, size_t *res) |
{ |
{ |
size_t res; |
|
|
|
res = 0; |
return(ml_puts(mbuf, "div", res)); |
if ( ! ml_puts(mbuf, "div", &res)) |
|
return(-1); |
|
|
|
return((ssize_t)res); |
|
} |
} |
|
|
|
|
/* ARGSUSED */ |
/* ARGSUSED */ |
static ssize_t |
static int |
html_blocktagname(struct md_mbuf *mbuf, |
html_blocktagname(struct md_mbuf *mbuf, |
const struct md_args *args, int tok) |
const struct md_args *args, int tok, size_t *res) |
{ |
{ |
size_t res; |
|
|
|
res = 0; |
return(ml_puts(mbuf, "div", res)); |
if ( ! ml_puts(mbuf, "div", &res)) |
|
return(-1); |
|
|
|
return((ssize_t)res); |
|
} |
} |
|
|
|
|
/* ARGSUSED */ |
/* ARGSUSED */ |
static ssize_t |
static int |
html_blockheadtagargs(struct md_mbuf *mbuf, const struct md_args *args, |
html_printargs(struct md_mbuf *mbuf, int tok, const char *ns, |
int tok, const int *argc, const char **argv) |
const int *argc, const char **argv, size_t *res) |
{ |
{ |
size_t res; |
|
|
|
res = 0; |
if ( ! ml_puts(mbuf, " class=\"", res)) |
|
|
if ( ! ml_puts(mbuf, " class=\"head-", &res)) |
|
return(0); |
return(0); |
if ( ! ml_puts(mbuf, toknames[tok], &res)) |
if ( ! ml_puts(mbuf, ns, res)) |
return(0); |
return(0); |
if ( ! ml_puts(mbuf, "\"", &res)) |
if ( ! ml_puts(mbuf, "-", res)) |
return(0); |
return(0); |
|
if ( ! ml_puts(mbuf, toknames[tok], res)) |
|
return(0); |
|
return(ml_puts(mbuf, "\"", res)); |
|
} |
|
|
switch (tok) { |
|
default: |
|
break; |
|
} |
|
|
|
return(0); |
/* ARGSUSED */ |
|
static int |
|
html_blockheadtagargs(struct md_mbuf *mbuf, |
|
const struct md_args *args, int tok, |
|
const int *argc, const char **argv, size_t *res) |
|
{ |
|
|
|
return(html_printargs(mbuf, tok, "head", argc, argv, res)); |
} |
} |
|
|
|
|
/* ARGSUSED */ |
/* ARGSUSED */ |
static ssize_t |
static int |
html_blockbodytagargs(struct md_mbuf *mbuf, const struct md_args *args, |
html_blockbodytagargs(struct md_mbuf *mbuf, |
int tok, const int *argc, const char **argv) |
const struct md_args *args, int tok, |
|
const int *argc, const char **argv, size_t *res) |
{ |
{ |
size_t res; |
|
|
|
res = 0; |
return(html_printargs(mbuf, tok, "body", argc, argv, res)); |
|
} |
|
|
if ( ! ml_puts(mbuf, " class=\"body-", &res)) |
|
return(0); |
|
if ( ! ml_puts(mbuf, toknames[tok], &res)) |
|
return(0); |
|
if ( ! ml_puts(mbuf, "\"", &res)) |
|
return(0); |
|
|
|
switch (tok) { |
/* ARGSUSED */ |
default: |
static int |
break; |
html_blocktagargs(struct md_mbuf *mbuf, |
} |
const struct md_args *args, int tok, |
|
const int *argc, const char **argv, size_t *res) |
|
{ |
|
|
return(res); |
return(html_printargs(mbuf, tok, "block", argc, argv, res)); |
} |
} |
|
|
|
|
/* ARGSUSED */ |
/* ARGSUSED */ |
static ssize_t |
static int |
html_blocktagargs(struct md_mbuf *mbuf, const struct md_args *args, |
html_inlinetagargs(struct md_mbuf *mbuf, |
int tok, const int *argc, const char **argv) |
const struct md_args *args, int tok, |
|
const int *argc, const char **argv, size_t *res) |
{ |
{ |
size_t res; |
|
|
|
res = 0; |
return(html_printargs(mbuf, tok, "inline", argc, argv, res)); |
|
} |
|
|
if ( ! ml_puts(mbuf, " class=\"block-", &res)) |
|
return(0); |
|
if ( ! ml_puts(mbuf, toknames[tok], &res)) |
|
return(0); |
|
if ( ! ml_puts(mbuf, "\"", &res)) |
|
return(0); |
|
|
|
|
/* ARGSUSED */ |
|
static int |
|
html_inlinetagname(struct md_mbuf *mbuf, |
|
const struct md_args *args, int tok, size_t *res) |
|
{ |
|
|
switch (tok) { |
switch (tok) { |
|
case (ROFF_Pp): |
|
return(ml_puts(mbuf, "div", res)); |
default: |
default: |
break; |
break; |
} |
} |
|
|
return(0); |
return(ml_puts(mbuf, "span", res)); |
} |
} |
|
|
|
|
/* ARGSUSED */ |
static ssize_t |
static ssize_t |
html_begintag(struct md_mbuf *mbuf, void *data, |
html_inlinetagargs(struct md_mbuf *mbuf, const struct md_args *args, |
const struct md_args *args, enum md_ns ns, |
int tok, const int *argc, const char **argv) |
int tok, const int *argc, const char **argv) |
{ |
{ |
size_t res; |
size_t res; |
|
struct htmlq *q; |
|
struct htmlnode *node; |
|
|
|
assert(ns != MD_NS_DEFAULT); |
res = 0; |
res = 0; |
|
|
if ( ! ml_puts(mbuf, " class=\"inline-", &res)) |
assert(data); |
return(0); |
q = (struct htmlq *)data; |
if ( ! ml_puts(mbuf, toknames[tok], &res)) |
|
return(0); |
|
if ( ! ml_puts(mbuf, "\"", &res)) |
|
return(0); |
|
|
|
|
if (NULL == (node = calloc(1, sizeof(struct htmlnode)))) { |
switch (tok) { |
warn("calloc"); |
default: |
return(-1); |
break; |
|
} |
} |
|
|
return(0); |
node->parent = q->last; |
} |
node->tok = tok; |
|
node->ns = ns; |
|
|
|
q->last = node; |
|
|
/* ARGSUSED */ |
switch (ns) { |
static ssize_t |
case (MD_NS_BLOCK): |
html_inlinetagname(struct md_mbuf *mbuf, |
if ( ! html_blocktagname(mbuf, args, tok, &res)) |
const struct md_args *args, int tok) |
|
{ |
|
size_t res; |
|
|
|
res = 0; |
|
|
|
switch (tok) { |
|
case (ROFF_Pp): |
|
if ( ! ml_puts(mbuf, "div", &res)) |
|
return(-1); |
return(-1); |
|
if ( ! html_blocktagargs(mbuf, args, tok, |
|
argc, argv, &res)) |
|
return(-1); |
break; |
break; |
|
case (MD_NS_BODY): |
|
if ( ! html_blockbodytagname(mbuf, args, tok, &res)) |
|
return(-1); |
|
if ( ! html_blockbodytagargs(mbuf, args, tok, |
|
argc, argv, &res)) |
|
return(-1); |
|
break; |
|
case (MD_NS_HEAD): |
|
if ( ! html_blockheadtagname(mbuf, args, tok, &res)) |
|
return(-1); |
|
if ( ! html_blockheadtagargs(mbuf, args, tok, |
|
argc, argv, &res)) |
|
return(-1); |
|
break; |
default: |
default: |
if ( ! ml_puts(mbuf, "span", &res)) |
if ( ! html_inlinetagname(mbuf, args, tok, &res)) |
return(-1); |
return(-1); |
|
if ( ! html_inlinetagargs(mbuf, args, tok, |
|
argc, argv, &res)) |
|
return(-1); |
break; |
break; |
} |
} |
|
|
Line 370 html_inlinetagname(struct md_mbuf *mbuf, |
|
Line 384 html_inlinetagname(struct md_mbuf *mbuf, |
|
|
|
|
|
static ssize_t |
static ssize_t |
html_begintag(struct md_mbuf *mbuf, const struct md_args *args, |
html_endtag(struct md_mbuf *mbuf, void *data, |
enum md_ns ns, int tok, |
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); |
assert(ns != MD_NS_DEFAULT); |
|
res = 0; |
|
|
|
assert(data); |
|
q = (struct htmlq *)data; |
|
|
switch (ns) { |
switch (ns) { |
case (MD_NS_BLOCK): |
case (MD_NS_BLOCK): |
if ( ! html_blocktagname(mbuf, args, tok)) |
if ( ! html_blocktagname(mbuf, args, tok, &res)) |
return(0); |
return(-1); |
if (NULL == argc || NULL == argv) |
break; |
return(1); |
|
assert(argc && argv); |
|
return(html_blocktagargs(mbuf, args, |
|
tok, argc, argv)); |
|
case (MD_NS_BODY): |
case (MD_NS_BODY): |
if ( ! html_blockbodytagname(mbuf, args, tok)) |
if ( ! html_blockbodytagname(mbuf, args, tok, &res)) |
return(0); |
return(-1); |
if (NULL == argc || NULL == argv) |
break; |
return(1); |
|
assert(argc && argv); |
|
return(html_blockbodytagargs(mbuf, args, |
|
tok, argc, argv)); |
|
case (MD_NS_HEAD): |
case (MD_NS_HEAD): |
if ( ! html_blockheadtagname(mbuf, args, tok)) |
if ( ! html_blockheadtagname(mbuf, args, tok, &res)) |
return(0); |
return(-1); |
if (NULL == argc || NULL == argv) |
break; |
return(1); |
|
assert(argc && argv); |
|
return(html_blockheadtagargs(mbuf, args, |
|
tok, argc, argv)); |
|
default: |
default: |
|
if ( ! html_inlinetagname(mbuf, args, tok, &res)) |
|
return(-1); |
break; |
break; |
} |
} |
|
|
if ( ! html_inlinetagname(mbuf, args, tok)) |
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(0); |
if (NULL == argc || NULL == argv) |
} |
return(1); |
return(1); |
assert(argc && argv); |
|
return(html_inlinetagargs(mbuf, args, tok, argc, argv)); |
|
} |
} |
|
|
|
|
static ssize_t |
static void |
html_endtag(struct md_mbuf *mbuf, const struct md_args *args, |
html_free(void *p) |
enum md_ns ns, int tok) |
|
{ |
{ |
|
struct htmlq *q; |
|
struct htmlnode *n; |
|
|
assert(ns != MD_NS_DEFAULT); |
assert(p); |
switch (ns) { |
q = (struct htmlq *)p; |
case (MD_NS_BLOCK): |
|
return(html_blocktagname(mbuf, args, tok)); |
while ((n = q->last)) { |
case (MD_NS_BODY): |
q->last = n->parent; |
return(html_blockbodytagname(mbuf, args, tok)); |
free(n); |
case (MD_NS_HEAD): |
|
return(html_blockheadtagname(mbuf, args, tok)); |
|
default: |
|
break; |
|
} |
} |
|
|
return(html_inlinetagname(mbuf, args, tok)); |
free(q); |
} |
} |
|
|
|
|
|
|
md_init_html(const struct md_args *args, |
md_init_html(const struct md_args *args, |
struct md_mbuf *mbuf, const struct md_rbuf *rbuf) |
struct md_mbuf *mbuf, const struct md_rbuf *rbuf) |
{ |
{ |
|
struct ml_cbs cbs; |
|
|
return(mlg_alloc(args, rbuf, mbuf, html_begintag, |
cbs.ml_alloc = html_alloc; |
html_endtag, html_begin, html_end)); |
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)); |
} |
} |
|
|