]> git.cameronkatri.com Git - mandoc.git/blobdiff - html.c
Implement the roff(7) .rr (remove register) request.
[mandoc.git] / html.c
diff --git a/html.c b/html.c
index 45471fe3b050c7456a57abd8ea68aee51d4c3008..32c31e50474763dc00e00e9c6692a4ba6a3600ad 100644 (file)
--- a/html.c
+++ b/html.c
@@ -1,7 +1,7 @@
-/*     $Id: html.c,v 1.147 2011/05/24 21:40:14 kristaps Exp $ */
+/*     $Id: html.c,v 1.155 2014/03/23 11:25:26 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, 2013, 2014 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
@@ -31,6 +31,7 @@
 #include <unistd.h>
 
 #include "mandoc.h"
+#include "mandoc_aux.h"
 #include "libmandoc.h"
 #include "out.h"
 #include "html.h"
@@ -118,13 +119,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));
 
@@ -143,6 +145,9 @@ ml_alloc(char *outopts, enum htmltype type)
                case (2):
                        h->base_includes = v;
                        break;
+               case (3):
+                       h->oflags |= HTML_FRAGMENT;
+                       break;
                default:
                        break;
                }
@@ -231,6 +236,9 @@ print_metaf(struct html *h, enum mandoc_esc deco)
        case (ESCAPE_FONTBOLD):
                font = HTMLFONT_BOLD;
                break;
+       case (ESCAPE_FONTBI):
+               font = HTMLFONT_BI;
+               break;
        case (ESCAPE_FONT):
                /* FALLTHROUGH */
        case (ESCAPE_FONTROMAN):
@@ -249,17 +257,27 @@ print_metaf(struct html *h, enum mandoc_esc deco)
        h->metal = h->metac;
        h->metac = font;
 
-       if (HTMLFONT_NONE != font)
-               h->metaf = HTMLFONT_BOLD == font ?
-                       print_otag(h, TAG_B, 0, NULL) :
-                       print_otag(h, TAG_I, 0, NULL);
+       switch (font) {
+       case (HTMLFONT_ITALIC):
+               h->metaf = print_otag(h, TAG_I, 0, NULL);
+               break;
+       case (HTMLFONT_BOLD):
+               h->metaf = print_otag(h, TAG_B, 0, NULL);
+               break;
+       case (HTMLFONT_BI):
+               h->metaf = print_otag(h, TAG_B, 0, NULL);
+               print_otag(h, TAG_I, 0, NULL);
+               break;
+       default:
+               break;
+       }
 }
 
 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
@@ -270,10 +288,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):
@@ -281,15 +310,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
@@ -299,11 +332,18 @@ print_encode(struct html *h, const char *p, int norecurse)
        int              c, len, nospace;
        const char      *seq;
        enum mandoc_esc  esc;
-       static const char rejs[6] = { '\\', '<', '>', '&', ASCII_HYPH, '\0' };
+       static const char rejs[8] = { '\\', '<', '>', '&',
+               ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' };
 
        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);
@@ -322,8 +362,13 @@ print_encode(struct html *h, const char *p, int norecurse)
                case ('&'):
                        printf("&amp;");
                        continue;
+               case (ASCII_NBRSP):
+                       putchar('-');
+                       continue;
                case (ASCII_HYPH):
                        putchar('-');
+                       /* FALLTHROUGH */
+               case (ASCII_BREAK):
                        continue;
                default:
                        break;
@@ -333,6 +378,33 @@ 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_FONTBI):
+                       /* 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. */
@@ -352,19 +424,6 @@ print_encode(struct html *h, const char *p, int norecurse)
                        else if (-1 == c && 1 == len)
                                putchar((int)*seq);
                        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);
-                       break;
                case (ESCAPE_NOSPACE):
                        if ('\0' == *p)
                                nospace = 1;
@@ -507,15 +566,27 @@ print_text(struct html *h, const char *word)
        }
 
        assert(NULL == h->metaf);
-       if (HTMLFONT_NONE != h->metac)
-               h->metaf = HTMLFONT_BOLD == h->metac ?
-                       print_otag(h, TAG_B, 0, NULL) :
-                       print_otag(h, TAG_I, 0, NULL);
+       switch (h->metac) {
+       case (HTMLFONT_ITALIC):
+               h->metaf = print_otag(h, TAG_I, 0, NULL);
+               break;
+       case (HTMLFONT_BOLD):
+               h->metaf = print_otag(h, TAG_B, 0, NULL);
+               break;
+       case (HTMLFONT_BI):
+               h->metaf = print_otag(h, TAG_B, 0, NULL);
+               print_otag(h, TAG_I, 0, NULL);
+               break;
+       default:
+               break;
+       }
 
        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);
@@ -595,7 +666,6 @@ bufcat(struct html *h, const char *p)
 
        h->buflen = strlcat(h->buf, p, BUFSIZ);
        assert(h->buflen < BUFSIZ);
-       h->buflen--;
 }
 
 void
@@ -659,7 +729,7 @@ buffmt_man(struct html *h,
                        bufcat(h, sec ? sec : "1");
                        break;
                case('N'):
-                       bufcat_fmt(h, name);
+                       bufcat_fmt(h, "%s", name);
                        break;
                default:
                        bufncat(h, p, 2);