summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--html.c59
-rw-r--r--ml.h8
-rw-r--r--mlg.c10
-rw-r--r--xml.c18
4 files changed, 68 insertions, 27 deletions
diff --git a/html.c b/html.c
index 8403dea9..049817a3 100644
--- a/html.c
+++ b/html.c
@@ -1,4 +1,4 @@
-/* $Id: html.c,v 1.7 2008/12/05 11:28:16 kristaps Exp $ */
+/* $Id: html.c,v 1.8 2008/12/05 17:43:14 kristaps Exp $ */
/*
* Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
*
@@ -32,8 +32,12 @@
#include "ml.h"
+/* TODO: allow head/tail-less invocations (just "div" start). */
+/* FIXME: free htmlq. */
+
struct htmlnode {
- int type;
+ int tok;
+ enum md_ns ns;
int *argc[ROFF_MAXLINEARG];
char *argv[ROFF_MAXLINEARG];
struct htmlnode *parent;
@@ -45,12 +49,15 @@ struct htmlq {
};
+static void htmlnode_free(struct htmlnode *);
+static void htmlnode_free(struct htmlnode *);
+
static int html_loadcss(struct md_mbuf *, const char *);
-static ssize_t html_endtag(struct md_mbuf *,
+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 **);
@@ -352,15 +359,31 @@ html_inlinetagname(struct md_mbuf *mbuf,
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 +420,19 @@ html_begintag(struct md_mbuf *mbuf, const struct md_args *args,
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,6 +452,11 @@ html_endtag(struct md_mbuf *mbuf, const struct md_args *args,
break;
}
+ node = q->last;
+ q->last = node->parent;
+
+ htmlnode_free(node);
+
return((ssize_t)res);
}
@@ -448,8 +481,14 @@ void *
md_init_html(const struct md_args *args,
struct md_mbuf *mbuf, const struct md_rbuf *rbuf)
{
+ struct htmlq *q;
+
+ if (NULL == (q = calloc(1, sizeof(struct htmlq)))) {
+ warn("calloc");
+ return(NULL);
+ }
- return(mlg_alloc(args, rbuf, mbuf, html_begintag,
+ return(mlg_alloc(args, q, rbuf, mbuf, html_begintag,
html_endtag, html_begin, html_end));
}
diff --git a/ml.h b/ml.h
index ab6dab75..1994ec32 100644
--- a/ml.h
+++ b/ml.h
@@ -1,4 +1,4 @@
-/* $Id: ml.h,v 1.5 2008/12/05 11:28:17 kristaps Exp $ */
+/* $Id: ml.h,v 1.6 2008/12/05 17:43:14 kristaps Exp $ */
/*
* Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
*
@@ -34,9 +34,9 @@ typedef int (*ml_begin)(struct md_mbuf *, const struct md_args *,
const char *, const char *);
typedef int (*ml_end)(struct md_mbuf *,
const struct md_args *);
-typedef ssize_t (*ml_endtag)(struct md_mbuf *,
+typedef ssize_t (*ml_endtag)(struct md_mbuf *, void *,
const struct md_args *, enum md_ns, int);
-typedef ssize_t (*ml_begintag)(struct md_mbuf *,
+typedef ssize_t (*ml_begintag)(struct md_mbuf *, void *,
const struct md_args *, enum md_ns, int,
const int *, const char **);
@@ -52,7 +52,7 @@ int ml_puts(struct md_mbuf *, const char *, size_t *);
int ml_putchars(struct md_mbuf *,
char, size_t, size_t *);
-struct md_mlg *mlg_alloc(const struct md_args *,
+struct md_mlg *mlg_alloc(const struct md_args *, void *,
const struct md_rbuf *, struct md_mbuf *,
ml_begintag, ml_endtag, ml_begin, ml_end);
int mlg_exit(struct md_mlg *, int);
diff --git a/mlg.c b/mlg.c
index f21956b3..148888e3 100644
--- a/mlg.c
+++ b/mlg.c
@@ -1,4 +1,4 @@
-/* $Id: mlg.c,v 1.9 2008/12/05 11:28:17 kristaps Exp $ */
+/* $Id: mlg.c,v 1.10 2008/12/05 17:43:14 kristaps Exp $ */
/*
* Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
*
@@ -58,6 +58,7 @@ struct md_mlg {
int flags;
#define ML_OVERRIDE_ONE (1 << 0)
#define ML_OVERRIDE_ALL (1 << 1)
+ void *data;
};
@@ -144,7 +145,7 @@ mlg_begintag(struct md_mlg *p, enum md_ns ns, int tok,
if ( ! ml_nputs(p->mbuf, "<", 1, &p->pos))
return(0);
- res = (*p->begintag)(p->mbuf, p->args, ns, tok,
+ res = (*p->begintag)(p->mbuf, p->data, p->args, ns, tok,
argc, (const char **)argv);
if (-1 == res)
return(0);
@@ -193,7 +194,7 @@ mlg_endtag(struct md_mlg *p, enum md_ns ns, int tok)
if ( ! ml_nputs(p->mbuf, "</", 2, &p->pos))
return(0);
- res = (*p->endtag)(p->mbuf, p->args, ns, tok);
+ res = (*p->endtag)(p->mbuf, p->data, p->args, ns, tok);
if (-1 == res)
return(0);
@@ -333,7 +334,7 @@ mlg_exit(struct md_mlg *p, int flush)
struct md_mlg *
-mlg_alloc(const struct md_args *args,
+mlg_alloc(const struct md_args *args, void *data,
const struct md_rbuf *rbuf,
struct md_mbuf *mbuf,
ml_begintag begintag, ml_endtag endtag,
@@ -366,6 +367,7 @@ mlg_alloc(const struct md_args *args,
p->endtag = endtag;
p->begin = begin;
p->end = end;
+ p->data = data;
if (NULL == (p->tree = roff_alloc(&cb, p))) {
free(p);
diff --git a/xml.c b/xml.c
index 78edc429..5ff28cb7 100644
--- a/xml.c
+++ b/xml.c
@@ -1,4 +1,4 @@
-/* $Id: xml.c,v 1.14 2008/12/04 23:10:51 kristaps Exp $ */
+/* $Id: xml.c,v 1.15 2008/12/05 17:43:14 kristaps Exp $ */
/*
* Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
*
@@ -24,10 +24,10 @@
#include "ml.h"
-static ssize_t xml_endtag(struct md_mbuf *,
+static ssize_t xml_endtag(struct md_mbuf *, void *,
const struct md_args *,
enum md_ns, int);
-static ssize_t xml_begintag(struct md_mbuf *,
+static ssize_t xml_begintag(struct md_mbuf *, void *,
const struct md_args *,
enum md_ns, int,
const int *, const char **);
@@ -77,9 +77,9 @@ xml_end(struct md_mbuf *mbuf, const struct md_args *args)
/* ARGSUSED */
static ssize_t
-xml_begintag(struct md_mbuf *mbuf, const struct md_args *args,
- enum md_ns ns, int tok,
- const int *argc, const char **argv)
+xml_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;
@@ -117,8 +117,8 @@ xml_begintag(struct md_mbuf *mbuf, const struct md_args *args,
/* ARGSUSED */
static ssize_t
-xml_endtag(struct md_mbuf *mbuf, const struct md_args *args,
- enum md_ns ns, int tok)
+xml_endtag(struct md_mbuf *mbuf, void *data,
+ const struct md_args *args, enum md_ns ns, int tok)
{
size_t res;
@@ -173,7 +173,7 @@ md_init_xml(const struct md_args *args,
struct md_mbuf *mbuf, const struct md_rbuf *rbuf)
{
- return(mlg_alloc(args, rbuf, mbuf, xml_begintag,
+ return(mlg_alloc(args, NULL, rbuf, mbuf, xml_begintag,
xml_endtag, xml_begin, xml_end));
}