-/* $Id: xml.c,v 1.1 2008/11/30 21:41:35 kristaps Exp $ */
+/* $Id: xml.c,v 1.4 2008/12/01 15:32:36 kristaps Exp $ */
/*
* Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
*
MD_BLKOUT,
MD_IN,
MD_OUT,
- MD_TEXT
+ MD_TEXT,
+ MD_OVERRIDE
};
struct md_xml {
const char *);
static int mbuf_nputstring(struct md_xml *,
const char *, size_t);
+static int mbuf_puts(struct md_xml *, const char *);
+static int mbuf_nputs(struct md_xml *,
+ const char *, size_t);
static int
static int
mbuf_nputstring(struct md_xml *p, const char *buf, size_t sz)
+{
+ size_t i;
+
+ for (i = 0; i < sz; i++) {
+ switch (buf[i]) {
+ case ('&'):
+ if ( ! md_buf_puts(p->mbuf, "&", 5))
+ return(0);
+ p->pos += 5;
+ break;
+ case ('"'):
+ if ( ! md_buf_puts(p->mbuf, """, 6))
+ return(0);
+ p->pos += 6;
+ break;
+ default:
+ if ( ! md_buf_putchar(p->mbuf, buf[i]))
+ return(0);
+ p->pos++;
+ break;
+ }
+ }
+ return(1);
+}
+
+
+static int
+mbuf_nputs(struct md_xml *p, const char *buf, size_t sz)
{
p->pos += sz;
}
+static int
+mbuf_puts(struct md_xml *p, const char *buf)
+{
+
+ return(mbuf_nputs(p, buf, strlen(buf)));
+}
+
+
static int
mbuf_indent(struct md_xml *p)
{
if ( ! mbuf_indent(p))
return(0);
} else if (space) {
- if ( ! mbuf_nputstring(p, " ", 1))
+ if ( ! mbuf_nputs(p, " ", 1))
return(0);
}
assert(arg);
p = (struct md_xml *)arg;
- if ( ! mbuf_putstring(p, "<?xml version=\"1.0\" "
+ if ( ! mbuf_puts(p, "<?xml version=\"1.0\" "
"encoding=\"UTF-8\"?>\n"))
return(0);
- if ( ! mbuf_nputstring(p, "<block:mdoc>", 12))
+ if ( ! mbuf_puts(p, "<mdoc xmlns:block=\"block\" "
+ "xmlns:special=\"special\" "
+ "xmlns:inline=\"inline\">"))
return(0);
p->indent++;
if (0 != p->pos && ! mbuf_newline(p))
return(0);
- if ( ! mbuf_nputstring(p, "</block:mdoc>", 13))
+ if ( ! mbuf_puts(p, "</mdoc>"))
return(0);
p->last = MD_BLKOUT;
static int
roffspecial(void *arg, int tok)
{
+ struct md_xml *p;
+
+ assert(arg);
+ p = (struct md_xml *)arg;
+
+ switch (tok) {
+ case (ROFF_Ns):
+ p->last = MD_OVERRIDE;
+ break;
+ default:
+ break;
+ }
- /* FIXME */
return(1);
}
} else if ( ! mbuf_indent(p))
return(0);
- if ( ! mbuf_nputstring(p, "<", 1))
+ if ( ! mbuf_nputs(p, "<", 1))
return(0);
- if ( ! mbuf_nputstring(p, "block:", 6))
+ if ( ! mbuf_nputs(p, "block:", 6))
return(0);
- if ( ! mbuf_putstring(p, toknames[tok]))
+ if ( ! mbuf_puts(p, toknames[tok]))
return(0);
+ /* FIXME: xml won't like standards args (e.g., p1003.1-90). */
+
for (i = 0; ROFF_ARGMAX != argc[i]; i++) {
- if ( ! mbuf_nputstring(p, " ", 1))
+ if ( ! mbuf_nputs(p, " ", 1))
return(0);
- if ( ! mbuf_putstring(p, tokargnames[argc[i]]))
+ if ( ! mbuf_puts(p, tokargnames[argc[i]]))
return(0);
- if ( ! mbuf_nputstring(p, "=\"", 2))
+ if ( ! mbuf_nputs(p, "=\"", 2))
return(0);
if ( ! mbuf_putstring(p, argv[i] ? argv[i] : "true"))
return(0);
- if ( ! mbuf_nputstring(p, "\"", 1))
+ if ( ! mbuf_nputs(p, "\"", 1))
return(0);
}
- if ( ! mbuf_nputstring(p, ">", 1))
+ if ( ! mbuf_nputs(p, ">", 1))
return(0);
p->last = MD_BLKIN;
} else if ( ! mbuf_indent(p))
return(0);
- if ( ! mbuf_nputstring(p, "</", 2))
+ if ( ! mbuf_nputs(p, "</", 2))
return(0);
- if ( ! mbuf_nputstring(p, "block:", 6))
+ if ( ! mbuf_nputs(p, "block:", 6))
return(0);
- if ( ! mbuf_putstring(p, toknames[tok]))
+ if ( ! mbuf_puts(p, toknames[tok]))
return(0);
- if ( ! mbuf_nputstring(p, ">", 1))
+ if ( ! mbuf_nputs(p, ">", 1))
return(0);
p->last = MD_BLKOUT;
case (MD_TEXT):
/* FALLTHROUGH */
case (MD_OUT):
- if ( ! mbuf_nputstring(p, " ", 1))
+ if ( ! mbuf_nputs(p, " ", 1))
return(0);
break;
default:
p->last = MD_IN;
- if ( ! mbuf_nputstring(p, "<", 1))
+ if ( ! mbuf_nputs(p, "<", 1))
return(0);
- if ( ! mbuf_nputstring(p, "inline:", 7))
+ if ( ! mbuf_nputs(p, "inline:", 7))
return(0);
- if ( ! mbuf_putstring(p, toknames[tok]))
+ if ( ! mbuf_puts(p, toknames[tok]))
return(0);
for (i = 0; ROFF_ARGMAX != argc[i]; i++) {
- if ( ! mbuf_nputstring(p, " ", 1))
+ if ( ! mbuf_nputs(p, " ", 1))
return(0);
- if ( ! mbuf_putstring(p, tokargnames[argc[i]]))
+ if ( ! mbuf_puts(p, tokargnames[argc[i]]))
return(0);
- if ( ! mbuf_nputstring(p, "=\"", 2))
+ if ( ! mbuf_nputs(p, "=\"", 2))
return(0);
if ( ! mbuf_putstring(p, argv[i] ? argv[i] : "true"))
return(0);
- if ( ! mbuf_nputstring(p, "\"", 1))
+ if ( ! mbuf_nputs(p, "\"", 1))
return(0);
}
- return(mbuf_nputstring(p, ">", 1));
+ return(mbuf_nputs(p, ">", 1));
}
assert(arg);
p = (struct md_xml *)arg;
+ /* Continue with a regular out token. */
+
if (0 == p->pos && ! mbuf_indent(p))
return(0);
p->last = MD_OUT;
- if ( ! mbuf_nputstring(p, "</", 2))
+ if ( ! mbuf_nputs(p, "</", 2))
return(0);
- if ( ! mbuf_nputstring(p, "inline:", 7))
+ if ( ! mbuf_nputs(p, "inline:", 7))
return(0);
- if ( ! mbuf_putstring(p, toknames[tok]))
+ if ( ! mbuf_puts(p, toknames[tok]))
return(0);
- return(mbuf_nputstring(p, ">", 1));
+ return(mbuf_nputs(p, ">", 1));
}
p->last = MD_TEXT;
return(1);
}
+