=================================================================== RCS file: /cvs/mandoc/html.c,v retrieving revision 1.132 retrieving revision 1.138 diff -u -p -r1.132 -r1.138 --- mandoc/html.c 2011/04/09 15:29:40 1.132 +++ mandoc/html.c 2011/05/14 16:28:23 1.138 @@ -1,4 +1,4 @@ -/* $Id: html.c,v 1.132 2011/04/09 15:29:40 kristaps Exp $ */ +/* $Id: html.c,v 1.138 2011/05/14 16:28:23 kristaps Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2011 Ingo Schwarze @@ -31,6 +31,7 @@ #include #include "mandoc.h" +#include "libmandoc.h" #include "out.h" #include "html.h" #include "main.h" @@ -122,7 +123,7 @@ ml_alloc(char *outopts, enum htmltype type) h->type = type; h->tags.head = NULL; - h->symtab = chars_init(CHARS_HTML); + h->symtab = mchars_alloc(); while (outopts && *outopts) switch (getsubopt(&outopts, UNCONST(toks), &v)) { @@ -172,7 +173,7 @@ html_free(void *p) } if (h->symtab) - chars_free(h->symtab); + mchars_free(h->symtab); free(h); } @@ -212,11 +213,10 @@ print_gen_head(struct html *h) static void print_num(struct html *h, const char *p, size_t len) { - const char *rhs; + char c; - rhs = chars_num2char(p, len); - if (rhs) - putchar((int)*rhs); + if ('\0' != (c = mchars_num2char(p, len))) + putchar((int)c); } static void @@ -226,7 +226,7 @@ print_spec(struct html *h, const char *p, size_t len) const char *rhs; size_t sz; - if ((cp = chars_spec2cp(h->symtab, p, len)) > 0) { + if ((cp = mchars_spec2cp(h->symtab, p, len)) > 0) { printf("&#%d;", cp); return; } else if (-1 == cp && 1 == len) { @@ -235,7 +235,7 @@ print_spec(struct html *h, const char *p, size_t len) } else if (-1 == cp) return; - if (NULL != (rhs = chars_spec2str(h->symtab, p, len, &sz))) + if (NULL != (rhs = mchars_spec2str(h->symtab, p, len, &sz))) fwrite(rhs, 1, sz, stdout); } @@ -247,13 +247,13 @@ print_res(struct html *h, const char *p, size_t len) const char *rhs; size_t sz; - if ((cp = chars_res2cp(h->symtab, p, len)) > 0) { + if ((cp = mchars_res2cp(h->symtab, p, len)) > 0) { printf("&#%d;", cp); return; } else if (-1 == cp) return; - if (NULL != (rhs = chars_res2str(h->symtab, p, len, &sz))) + if (NULL != (rhs = mchars_res2str(h->symtab, p, len, &sz))) fwrite(rhs, 1, sz, stdout); } @@ -295,7 +295,42 @@ print_metaf(struct html *h, enum mandoc_esc deco) print_otag(h, TAG_I, 0, NULL); } +int +html_strlen(const char *cp) +{ + int ssz, sz; + const char *seq, *p; + /* + * Account for escaped sequences within string length + * calculations. This follows the logic in term_strlen() as we + * must calculate the width of produced strings. + * Assume that characters are always width of "1". This is + * hacky, but it gets the job done for approximation of widths. + */ + + sz = 0; + while (NULL != (p = strchr(cp, '\\'))) { + sz += (int)(p - cp); + ++cp; + switch (mandoc_escape(&cp, &seq, &ssz)) { + case (ESCAPE_ERROR): + return(sz); + case (ESCAPE_PREDEF): + sz++; + break; + case (ESCAPE_SPECIAL): + sz++; + break; + default: + break; + } + } + + assert(sz >= 0); + return(sz + strlen(cp)); +} + static int print_encode(struct html *h, const char *p, int norecurse) { @@ -427,7 +462,7 @@ print_otag(struct html *h, enum htmltag tag, print_attr(h, "lang", "en"); } - /* Accomodate for XML "well-formed" singleton escaping. */ + /* Accommodate for XML "well-formed" singleton escaping. */ if (HTML_AUTOCLOSE & htmltags[tag].flags) switch (h->type) {