From fdfa0ccb8f99138f8c4d4c16c0bb7905154c5aa1 Mon Sep 17 00:00:00 2001 From: Ali Gholami Rudi Date: Thu, 8 Nov 2012 19:28:36 +0330 Subject: [PATCH] ctxt: introduce an escape character to prevent unwanted markups --- ctxt.c | 15 ++++++++++----- ctxt.h | 2 +- fmt.c | 29 +++++++++++++++++------------ 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/ctxt.c b/ctxt.c index 8d09bf3..7ff0172 100644 --- a/ctxt.c +++ b/ctxt.c @@ -3,7 +3,7 @@ * * Copyright (C) 2009-2012 Ali Gholami Rudi * - * This program is released under GNU GPL version 2. + * This program is released under the modified BSD license. */ #include #include @@ -41,7 +41,7 @@ void *xmalloc(int size) } static char *usage = - "usage: ctxt [-m mode] formatted\n\n" + "usage: ctxt [-m mode] [-e esc] <plaintext >formatted\n\n" " mode: latex, html or troff (default: troff)\n"; int main(int argc, char **argv) @@ -50,10 +50,15 @@ int main(int argc, char **argv) struct doc *doc; char *mode = "troff"; struct fmt_ops *ops; + int esc = '\0'; int i; for (i = 1; i < argc; i++) { - if (!strcmp("-m", argv[i]) && i + 1 < argc) { - mode = argv[++i]; + if (argv[i][0] == '-' && argv[i][1] == 'm') { + mode = argv[i][2] ? argv[i] + 2 : argv[++i]; + continue; + } + if (argv[i][0] == '-' && argv[i][1] == 'e') { + esc = argv[i][2] ? argv[i][2] : argv[++i][0]; continue; } die(usage); @@ -62,7 +67,7 @@ int main(int argc, char **argv) die("unknown output format\n"); txt = txt_alloc(0); doc = doc_alloc(1); - format(doc, txt, ops); + format(doc, txt, ops, esc); doc_free(doc); txt_free(txt); return 0; diff --git a/ctxt.h b/ctxt.h index 1257410..d2c5fad 100644 --- a/ctxt.h +++ b/ctxt.h @@ -43,7 +43,7 @@ struct txt *txt_alloc(int fd); char *txt_line(struct txt *txt, int line); void txt_free(struct txt *txt); -void format(struct doc *doc, struct txt *txt, struct fmt_ops *ops); +void format(struct doc *doc, struct txt *txt, struct fmt_ops *ops, int esc); void die(char *msg); void *xmalloc(int size); diff --git a/fmt.c b/fmt.c index 8296c8f..adbbe69 100644 --- a/fmt.c +++ b/fmt.c @@ -23,16 +23,18 @@ struct fmt { struct txt *txt; struct fmt_ops *ops; int level; + int esc; }; static struct fmt *fmt_alloc(struct doc *doc, struct txt *txt, - struct fmt_ops *ops) + struct fmt_ops *ops, int esc) { struct fmt *fmt = xmalloc(sizeof(struct fmt)); fmt->doc = doc; fmt->txt = txt; fmt->ops = ops; fmt->level = 0; + fmt->esc = esc; return fmt; } @@ -53,7 +55,7 @@ static char *fmt_line(struct fmt *fmt, int line) static char *marker_char(char c, char p, char n) { int i; - if (p == '\\' || (p && !isspace(p)) || isspace(n)) + if (p == '\\' || (p && isalnum(p)) || isspace(n)) return NULL; for (i = 0; i < LENGTH(markers); i++) if (markers[i][0] == c) @@ -75,20 +77,23 @@ static void put_text(struct fmt *fmt, char *s) char *o = s; while (*s) { char *r, *m; - while (*s && !marker_char(*s, s == _s ? 0 : s[-1], s[1])) - s++; - if (!*s) - break; + if (s[1] && s[0] == fmt->esc) { + fmt->ops->put(fmt->doc, fillbuf(o, s)); + fmt->ops->put(fmt->doc, fillbuf(s + 1, s + 2)); + o = s + 2; + s = s + 2; + continue; + } m = marker_char(*s, s == _s ? 0 : s[-1], s[1]); - r = strchr(s + 1, m[1]); - if (r) { + r = m ? strchr(s + 1, m[1]) : NULL; + if (m && r) { fmt->ops->put(fmt->doc, fillbuf(o, s)); fmt->ops->put_txt(fmt->doc, fillbuf(s + 1, r), m); o = r + 1; s = r + 1; - } else { - s++; + continue; } + s++; } fmt->ops->put(fmt->doc, o); } @@ -452,9 +457,9 @@ static void fmt_handle(struct fmt *fmt, int beg, int end, int level) fmt->level -= level; } -void format(struct doc *doc, struct txt *txt, struct fmt_ops *ops) +void format(struct doc *doc, struct txt *txt, struct fmt_ops *ops, int esc) { - struct fmt *fmt = fmt_alloc(doc, txt, ops); + struct fmt *fmt = fmt_alloc(doc, txt, ops, esc); fmt->ops->doc_beg(doc); fmt_handle(fmt, 0, txt->n, 0); fmt->ops->doc_end(doc); -- 2.11.4.GIT