]> git.cameronkatri.com Git - mandoc.git/blobdiff - html.c
While I'm rooting around, note that we depend on sqlite3 now, not berkeley.
[mandoc.git] / html.c
diff --git a/html.c b/html.c
index 9299c976b787d8d4cbb92acbe47df3ee0cc3adb3..115b79a0697a208e18436182407e54cc2be2ef58 100644 (file)
--- a/html.c
+++ b/html.c
@@ -1,7 +1,7 @@
-/*     $Id: html.c,v 1.146 2011/05/24 21:31:23 kristaps Exp $ */
+/*     $Id: html.c,v 1.151 2012/05/31 22:29:13 schwarze Exp $ */
 /*
  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
- * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
+ * Copyright (c) 2011, 2012 Ingo Schwarze <schwarze@openbsd.org>
  *
  * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
@@ -108,7 +108,6 @@ static      const char      *const roffscales[SCALE_MAX] = {
 };
 
 static void     bufncat(struct html *, const char *, size_t);
-static void     print_spec(struct html *, const char *, size_t);
 static void     print_ctag(struct html *, enum htmltag);
 static int      print_encode(struct html *, const char *, int);
 static void     print_metaf(struct html *, enum mandoc_esc);
@@ -119,13 +118,14 @@ static void *
 ml_alloc(char *outopts, enum htmltype type)
 {
        struct html     *h;
-       const char      *toks[4];
+       const char      *toks[5];
        char            *v;
 
        toks[0] = "style";
        toks[1] = "man";
        toks[2] = "includes";
-       toks[3] = NULL;
+       toks[3] = "fragment";
+       toks[4] = NULL;
 
        h = mandoc_calloc(1, sizeof(struct html));
 
@@ -144,6 +144,9 @@ ml_alloc(char *outopts, enum htmltype type)
                case (2):
                        h->base_includes = v;
                        break;
+               case (3):
+                       h->oflags |= HTML_FRAGMENT;
+                       break;
                default:
                        break;
                }
@@ -217,26 +220,6 @@ print_gen_head(struct html *h)
        }
 }
 
-static void
-print_spec(struct html *h, const char *p, size_t len)
-{
-       int              cp;
-       const char      *rhs;
-       size_t           sz;
-
-       if ((cp = mchars_spec2cp(h->symtab, p, len)) > 0) {
-               printf("&#%d;", cp);
-               return;
-       } else if (-1 == cp && 1 == len) {
-               fwrite(p, 1, len, stdout);
-               return;
-       } else if (-1 == cp)
-               return;
-
-       if (NULL != (rhs = mchars_spec2str(h->symtab, p, len, &sz)))
-               fwrite(rhs, 1, sz, stdout);
-}
-
 static void
 print_metaf(struct html *h, enum mandoc_esc deco)
 {
@@ -279,8 +262,8 @@ print_metaf(struct html *h, enum mandoc_esc deco)
 int
 html_strlen(const char *cp)
 {
-       int              ssz, sz;
-       const char      *seq, *p;
+       size_t           rsz;
+       int              skip, sz;
 
        /*
         * Account for escaped sequences within string length
@@ -291,10 +274,21 @@ html_strlen(const char *cp)
         */
 
        sz = 0;
-       while (NULL != (p = strchr(cp, '\\'))) {
-               sz += (int)(p - cp);
-               ++cp;
-               switch (mandoc_escape(&cp, &seq, &ssz)) {
+       skip = 0;
+       while (1) {
+               rsz = strcspn(cp, "\\");
+               if (rsz) {
+                       cp += rsz;
+                       if (skip) {
+                               skip = 0;
+                               rsz--;
+                       }
+                       sz += rsz;
+               }
+               if ('\0' == *cp)
+                       break;
+               cp++;
+               switch (mandoc_escape(&cp, NULL, NULL)) {
                case (ESCAPE_ERROR):
                        return(sz);
                case (ESCAPE_UNICODE):
@@ -302,15 +296,19 @@ html_strlen(const char *cp)
                case (ESCAPE_NUMBERED):
                        /* FALLTHROUGH */
                case (ESCAPE_SPECIAL):
-                       sz++;
+                       if (skip)
+                               skip = 0;
+                       else
+                               sz++;
+                       break;
+               case (ESCAPE_SKIPCHAR):
+                       skip = 1;
                        break;
                default:
                        break;
                }
        }
-
-       assert(sz >= 0);
-       return(sz + strlen(cp));
+       return(sz);
 }
 
 static int
@@ -325,6 +323,12 @@ print_encode(struct html *h, const char *p, int norecurse)
        nospace = 0;
 
        while ('\0' != *p) {
+               if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
+                       h->flags &= ~HTML_SKIPCHAR;
+                       p++;
+                       continue;
+               }
+
                sz = strcspn(p, rejs);
 
                fwrite(p, 1, sz, stdout);
@@ -354,6 +358,31 @@ print_encode(struct html *h, const char *p, int norecurse)
                if (ESCAPE_ERROR == esc)
                        break;
 
+               switch (esc) {
+               case (ESCAPE_FONT):
+                       /* FALLTHROUGH */
+               case (ESCAPE_FONTPREV):
+                       /* FALLTHROUGH */
+               case (ESCAPE_FONTBOLD):
+                       /* FALLTHROUGH */
+               case (ESCAPE_FONTITALIC):
+                       /* FALLTHROUGH */
+               case (ESCAPE_FONTROMAN):
+                       if (0 == norecurse)
+                               print_metaf(h, esc);
+                       continue;
+               case (ESCAPE_SKIPCHAR):
+                       h->flags |= HTML_SKIPCHAR;
+                       continue;
+               default:
+                       break;
+               }
+
+               if (h->flags & HTML_SKIPCHAR) {
+                       h->flags &= ~HTML_SKIPCHAR;
+                       continue;
+               }
+
                switch (esc) {
                case (ESCAPE_UNICODE):
                        /* Skip passed "u" header. */
@@ -367,20 +396,11 @@ print_encode(struct html *h, const char *p, int norecurse)
                                putchar(c);
                        break;
                case (ESCAPE_SPECIAL):
-                       print_spec(h, seq, len);
-                       break;
-               case (ESCAPE_FONT):
-                       /* FALLTHROUGH */
-               case (ESCAPE_FONTPREV):
-                       /* FALLTHROUGH */
-               case (ESCAPE_FONTBOLD):
-                       /* FALLTHROUGH */
-               case (ESCAPE_FONTITALIC):
-                       /* FALLTHROUGH */
-               case (ESCAPE_FONTROMAN):
-                       if (norecurse)
-                               break;
-                       print_metaf(h, esc);
+                       c = mchars_spec2cp(h->symtab, seq, len);
+                       if (c > 0)
+                               printf("&#%d;", c);
+                       else if (-1 == c && 1 == len)
+                               putchar((int)*seq);
                        break;
                case (ESCAPE_NOSPACE):
                        if ('\0' == *p)
@@ -530,9 +550,11 @@ print_text(struct html *h, const char *word)
                        print_otag(h, TAG_I, 0, NULL);
 
        assert(word);
-       if ( ! print_encode(h, word, 0))
+       if ( ! print_encode(h, word, 0)) {
                if ( ! (h->flags & HTML_NONOSPACE))
                        h->flags &= ~HTML_NOSPACE;
+       } else
+               h->flags |= HTML_NOSPACE;
 
        if (h->metaf) {
                print_tagq(h, h->metaf);
@@ -612,7 +634,6 @@ bufcat(struct html *h, const char *p)
 
        h->buflen = strlcat(h->buf, p, BUFSIZ);
        assert(h->buflen < BUFSIZ);
-       h->buflen--;
 }
 
 void