version 1.1, 2008/11/30 21:41:35 |
version 1.5, 2008/12/01 16:01:28 |
|
|
#include "libmdocml.h" |
#include "libmdocml.h" |
#include "private.h" |
#include "private.h" |
|
|
#define INDENT 4 |
#define MAXINDENT 8 |
#define COLUMNS 60 |
#define COLUMNS 60 |
|
|
#ifdef __linux__ /* FIXME */ |
#ifdef __linux__ /* FIXME */ |
|
|
MD_BLKOUT, |
MD_BLKOUT, |
MD_IN, |
MD_IN, |
MD_OUT, |
MD_OUT, |
MD_TEXT |
MD_TEXT, |
|
MD_OVERRIDE |
}; |
}; |
|
|
struct md_xml { |
struct md_xml { |
Line 74 static int mbuf_putstring(struct md_xml *, |
|
Line 75 static int mbuf_putstring(struct md_xml *, |
|
const char *); |
const char *); |
static int mbuf_nputstring(struct md_xml *, |
static int mbuf_nputstring(struct md_xml *, |
const char *, size_t); |
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 |
Line 87 mbuf_putstring(struct md_xml *p, const char *buf) |
|
Line 91 mbuf_putstring(struct md_xml *p, const char *buf) |
|
static int |
static int |
mbuf_nputstring(struct md_xml *p, const char *buf, size_t sz) |
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; |
p->pos += sz; |
return(md_buf_puts(p->mbuf, buf, sz)); |
return(md_buf_puts(p->mbuf, buf, sz)); |
} |
} |
|
|
|
|
static int |
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) |
mbuf_indent(struct md_xml *p) |
{ |
{ |
size_t i; |
size_t i; |
Line 101 mbuf_indent(struct md_xml *p) |
|
Line 141 mbuf_indent(struct md_xml *p) |
|
assert(p->pos == 0); |
assert(p->pos == 0); |
|
|
/* LINTED */ |
/* LINTED */ |
for (i = 0; i < MIN(p->indent, INDENT); i++) |
for (i = 0; i < MIN(p->indent, MAXINDENT); i++) |
if ( ! md_buf_putstring(p->mbuf, " ")) |
if ( ! md_buf_putstring(p->mbuf, " ")) |
return(0); |
return(0); |
|
|
p->pos += i * INDENT; |
p->pos += i * 4; |
return(1); |
return(1); |
} |
} |
|
|
Line 155 mbuf_data(struct md_xml *p, int space, char *buf) |
|
Line 195 mbuf_data(struct md_xml *p, int space, char *buf) |
|
return(0); |
return(0); |
if ( ! mbuf_nputstring(p, bufp, sz)) |
if ( ! mbuf_nputstring(p, bufp, sz)) |
return(0); |
return(0); |
if (p->indent * INDENT + sz >= COLUMNS) { |
if (p->indent * MAXINDENT + sz >= COLUMNS) { |
if ( ! mbuf_newline(p)) |
if ( ! mbuf_newline(p)) |
return(0); |
return(0); |
continue; |
continue; |
Line 169 mbuf_data(struct md_xml *p, int space, char *buf) |
|
Line 209 mbuf_data(struct md_xml *p, int space, char *buf) |
|
if ( ! mbuf_indent(p)) |
if ( ! mbuf_indent(p)) |
return(0); |
return(0); |
} else if (space) { |
} else if (space) { |
if ( ! mbuf_nputstring(p, " ", 1)) |
if ( ! mbuf_nputs(p, " ", 1)) |
return(0); |
return(0); |
} |
} |
|
|
Line 253 roffhead(void *arg) |
|
Line 293 roffhead(void *arg) |
|
assert(arg); |
assert(arg); |
p = (struct md_xml *)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")) |
"encoding=\"UTF-8\"?>\n")) |
return(0); |
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); |
return(0); |
|
|
p->indent++; |
p->indent++; |
Line 276 rofftail(void *arg) |
|
Line 318 rofftail(void *arg) |
|
if (0 != p->pos && ! mbuf_newline(p)) |
if (0 != p->pos && ! mbuf_newline(p)) |
return(0); |
return(0); |
|
|
if ( ! mbuf_nputstring(p, "</block:mdoc>", 13)) |
if ( ! mbuf_puts(p, "</mdoc>")) |
return(0); |
return(0); |
|
|
p->last = MD_BLKOUT; |
p->last = MD_BLKOUT; |
Line 288 rofftail(void *arg) |
|
Line 330 rofftail(void *arg) |
|
static int |
static int |
roffspecial(void *arg, int tok) |
roffspecial(void *arg, int tok) |
{ |
{ |
|
struct md_xml *p; |
|
|
/* FIXME */ |
assert(arg); |
|
p = (struct md_xml *)arg; |
|
|
|
switch (tok) { |
|
case (ROFF_Ns): |
|
p->last = MD_OVERRIDE; |
|
break; |
|
default: |
|
break; |
|
} |
|
|
return(1); |
return(1); |
} |
} |
|
|
Line 311 roffblkin(void *arg, int tok, int *argc, char **argv) |
|
Line 364 roffblkin(void *arg, int tok, int *argc, char **argv) |
|
} else if ( ! mbuf_indent(p)) |
} else if ( ! mbuf_indent(p)) |
return(0); |
return(0); |
|
|
if ( ! mbuf_nputstring(p, "<", 1)) |
if ( ! mbuf_nputs(p, "<", 1)) |
return(0); |
return(0); |
if ( ! mbuf_nputstring(p, "block:", 6)) |
if ( ! mbuf_nputs(p, "block:", 6)) |
return(0); |
return(0); |
if ( ! mbuf_putstring(p, toknames[tok])) |
if ( ! mbuf_puts(p, toknames[tok])) |
return(0); |
return(0); |
|
|
|
/* FIXME: xml won't like standards args (e.g., p1003.1-90). */ |
|
|
for (i = 0; ROFF_ARGMAX != argc[i]; i++) { |
for (i = 0; ROFF_ARGMAX != argc[i]; i++) { |
if ( ! mbuf_nputstring(p, " ", 1)) |
if ( ! mbuf_nputs(p, " ", 1)) |
return(0); |
return(0); |
if ( ! mbuf_putstring(p, tokargnames[argc[i]])) |
if ( ! mbuf_puts(p, tokargnames[argc[i]])) |
return(0); |
return(0); |
if ( ! mbuf_nputstring(p, "=\"", 2)) |
if ( ! mbuf_nputs(p, "=\"", 2)) |
return(0); |
return(0); |
if ( ! mbuf_putstring(p, argv[i] ? argv[i] : "true")) |
if ( ! mbuf_putstring(p, argv[i] ? argv[i] : "true")) |
return(0); |
return(0); |
if ( ! mbuf_nputstring(p, "\"", 1)) |
if ( ! mbuf_nputs(p, "\"", 1)) |
return(0); |
return(0); |
} |
} |
|
|
if ( ! mbuf_nputstring(p, ">", 1)) |
if ( ! mbuf_nputs(p, ">", 1)) |
return(0); |
return(0); |
|
|
p->last = MD_BLKIN; |
p->last = MD_BLKIN; |
Line 358 roffblkout(void *arg, int tok) |
|
Line 413 roffblkout(void *arg, int tok) |
|
} else if ( ! mbuf_indent(p)) |
} else if ( ! mbuf_indent(p)) |
return(0); |
return(0); |
|
|
if ( ! mbuf_nputstring(p, "</", 2)) |
if ( ! mbuf_nputs(p, "</", 2)) |
return(0); |
return(0); |
if ( ! mbuf_nputstring(p, "block:", 6)) |
if ( ! mbuf_nputs(p, "block:", 6)) |
return(0); |
return(0); |
if ( ! mbuf_putstring(p, toknames[tok])) |
if ( ! mbuf_puts(p, toknames[tok])) |
return(0); |
return(0); |
if ( ! mbuf_nputstring(p, ">", 1)) |
if ( ! mbuf_nputs(p, ">", 1)) |
return(0); |
return(0); |
|
|
p->last = MD_BLKOUT; |
p->last = MD_BLKOUT; |
Line 396 roffin(void *arg, int tok, int *argc, char **argv) |
|
Line 451 roffin(void *arg, int tok, int *argc, char **argv) |
|
case (MD_TEXT): |
case (MD_TEXT): |
/* FALLTHROUGH */ |
/* FALLTHROUGH */ |
case (MD_OUT): |
case (MD_OUT): |
if ( ! mbuf_nputstring(p, " ", 1)) |
if ( ! mbuf_nputs(p, " ", 1)) |
return(0); |
return(0); |
break; |
break; |
default: |
default: |
Line 407 roffin(void *arg, int tok, int *argc, char **argv) |
|
Line 462 roffin(void *arg, int tok, int *argc, char **argv) |
|
|
|
p->last = MD_IN; |
p->last = MD_IN; |
|
|
if ( ! mbuf_nputstring(p, "<", 1)) |
if ( ! mbuf_nputs(p, "<", 1)) |
return(0); |
return(0); |
if ( ! mbuf_nputstring(p, "inline:", 7)) |
if ( ! mbuf_nputs(p, "inline:", 7)) |
return(0); |
return(0); |
if ( ! mbuf_putstring(p, toknames[tok])) |
if ( ! mbuf_puts(p, toknames[tok])) |
return(0); |
return(0); |
|
|
for (i = 0; ROFF_ARGMAX != argc[i]; i++) { |
for (i = 0; ROFF_ARGMAX != argc[i]; i++) { |
if ( ! mbuf_nputstring(p, " ", 1)) |
if ( ! mbuf_nputs(p, " ", 1)) |
return(0); |
return(0); |
if ( ! mbuf_putstring(p, tokargnames[argc[i]])) |
if ( ! mbuf_puts(p, tokargnames[argc[i]])) |
return(0); |
return(0); |
if ( ! mbuf_nputstring(p, "=\"", 2)) |
if ( ! mbuf_nputs(p, "=\"", 2)) |
return(0); |
return(0); |
if ( ! mbuf_putstring(p, argv[i] ? argv[i] : "true")) |
if ( ! mbuf_putstring(p, argv[i] ? argv[i] : "true")) |
return(0); |
return(0); |
if ( ! mbuf_nputstring(p, "\"", 1)) |
if ( ! mbuf_nputs(p, "\"", 1)) |
return(0); |
return(0); |
} |
} |
return(mbuf_nputstring(p, ">", 1)); |
return(mbuf_nputs(p, ">", 1)); |
} |
} |
|
|
|
|
Line 438 roffout(void *arg, int tok) |
|
Line 493 roffout(void *arg, int tok) |
|
assert(arg); |
assert(arg); |
p = (struct md_xml *)arg; |
p = (struct md_xml *)arg; |
|
|
|
/* Continue with a regular out token. */ |
|
|
if (0 == p->pos && ! mbuf_indent(p)) |
if (0 == p->pos && ! mbuf_indent(p)) |
return(0); |
return(0); |
|
|
p->last = MD_OUT; |
p->last = MD_OUT; |
|
|
if ( ! mbuf_nputstring(p, "</", 2)) |
if ( ! mbuf_nputs(p, "</", 2)) |
return(0); |
return(0); |
if ( ! mbuf_nputstring(p, "inline:", 7)) |
if ( ! mbuf_nputs(p, "inline:", 7)) |
return(0); |
return(0); |
if ( ! mbuf_putstring(p, toknames[tok])) |
if ( ! mbuf_puts(p, toknames[tok])) |
return(0); |
return(0); |
return(mbuf_nputstring(p, ">", 1)); |
return(mbuf_nputs(p, ">", 1)); |
} |
} |
|
|
|
|
Line 500 roffdata(void *arg, int space, char *buf) |
|
Line 557 roffdata(void *arg, int space, char *buf) |
|
p->last = MD_TEXT; |
p->last = MD_TEXT; |
return(1); |
return(1); |
} |
} |
|
|