From 25d5e41b7ceb7a9d916caafd4fdf59e798731dc1 Mon Sep 17 00:00:00 2001 From: Ali Gholami Rudi Date: Wed, 11 Mar 2009 22:03:37 +0330 Subject: [PATCH] html: add html output format --- Makefile | 2 +- ctxt.c | 34 ++++++++++++++++-- ctxt.h | 1 + html.c | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 155 insertions(+), 3 deletions(-) create mode 100644 html.c diff --git a/Makefile b/Makefile index a817b11..c44e8fc 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ LDFLAGS = all: ctxt .c.o: $(CC) -c $(CFLAGS) $< -ctxt: ctxt.o util.o txt.o fmt.o tex.o +ctxt: ctxt.o util.o txt.o fmt.o tex.o html.o $(CC) $(LDFLAGS) -o $@ $^ clean: rm -f *.o ctxt diff --git a/ctxt.c b/ctxt.c index da6afe1..5d75d27 100644 --- a/ctxt.c +++ b/ctxt.c @@ -1,5 +1,7 @@ +#include #include #include +#include #include #include "ctxt.h" #include "util.h" @@ -13,10 +15,38 @@ void file_put(int fd, char *s) n += c; } +static struct fmt_ops *get_ops(char *name) +{ + if (!strcmp(name, "latex")) + return &latex_ops; + if (!strcmp(name, "html")) + return &html_ops; + return NULL; +} + int main(int argc, char **argv) { - struct txt *txt = txt_alloc(STDIN_FILENO); - fmt(STDOUT_FILENO, txt, &latex_ops); + struct txt *txt; + char *mode = "latex"; + struct fmt_ops *ops; + int c; + while ((c = getopt(argc, argv, "m:")) != -1) { + switch (c) { + case 'm': + mode = optarg; + break; + case '?': + default: + fprintf(stderr, "bad command line option\n"); + return 1; + } + } + if (!(ops = get_ops(mode))) { + fprintf(stderr, "unknown output format\n"); + return 1; + } + txt = txt_alloc(STDIN_FILENO); + fmt(STDOUT_FILENO, txt, ops); txt_free(txt); return 0; } diff --git a/ctxt.h b/ctxt.h index 3c32ac8..2315916 100644 --- a/ctxt.h +++ b/ctxt.h @@ -37,5 +37,6 @@ void txt_free(struct txt *txt); void fmt(int fd, struct txt *txt, struct fmt_ops *ops); extern struct fmt_ops latex_ops; +extern struct fmt_ops html_ops; #endif diff --git a/html.c b/html.c new file mode 100644 index 0000000..75746b5 --- /dev/null +++ b/html.c @@ -0,0 +1,121 @@ +#include +#include "ctxt.h" + +static void html_doc_beg(int fd) +{ + file_put(fd, "\n"); +} + +static void html_doc_end(int fd) +{ + file_put(fd, "\n"); +} + +static int last_head = 0; + +static void html_head_beg(int fd, int level) +{ + char buf[64]; + sprintf(buf, "", level + 1); + last_head = level; + file_put(fd, buf); +} + +static void html_head_end(int fd) +{ + char buf[64]; + sprintf(buf, "", last_head); + file_put(fd, buf); +} + +static void html_par_beg(int fd) +{ + file_put(fd, "

\n"); +} + +static void html_par_end(int fd) +{ +} + +static void html_put(int fd, char *s) +{ + file_put(fd, s); +} + +static void html_list_beg(int fd) +{ + file_put(fd, "

    \n"); +} + +static void html_list_end(int fd) +{ + file_put(fd, "
\n"); +} + +static void html_item_beg(int fd) +{ + file_put(fd, "
  • "); +} + +static void html_item_end(int fd) +{ + file_put(fd, "
  • "); +} + +static void html_pre_beg(int fd) +{ + file_put(fd, "
    ");
    +}
    +
    +static void html_pre_end(int fd)
    +{
    +	file_put(fd, "
    "); +} + +static void html_formula_beg(int fd) +{ + file_put(fd, "

    "); +} + +static void html_formula_end(int fd) +{ + file_put(fd, "\n"); +} + +static void html_put_emph(int fd, char *s) +{ + file_put(fd, ""); + file_put(fd, s); + file_put(fd, ""); +} + +static void html_put_raw(int fd, char *s) +{ + file_put(fd, s); +} + +static void html_put_ref(int fd, char *s) +{ + file_put(fd, s); +} + +struct fmt_ops html_ops = { + .doc_beg = html_doc_beg, + .doc_end = html_doc_end, + .head_beg = html_head_beg, + .head_end = html_head_end, + .par_beg = html_par_beg, + .par_end = html_par_end, + .list_beg = html_list_beg, + .list_end = html_list_end, + .item_beg = html_item_beg, + .item_end = html_item_end, + .pre_beg = html_pre_beg, + .pre_end = html_pre_end, + .formula_beg = html_formula_beg, + .formula_end = html_formula_end, + .put_emph = html_put_emph, + .put_raw = html_put_raw, + .put_ref = html_put_ref, + .put = html_put +}; -- 2.11.4.GIT