]> git.cameronkatri.com Git - mandoc.git/blobdiff - html.c
duplicate word, found by igor(1)
[mandoc.git] / html.c
diff --git a/html.c b/html.c
index 40f2cc076bf97fbb2c372cbb059f7baf001759dd..65f7eea408fd34620a2845afc5214a9ca24b04dd 100644 (file)
--- a/html.c
+++ b/html.c
@@ -1,4 +1,4 @@
-/*     $Id: html.c,v 1.207 2017/02/05 20:22:04 schwarze Exp $ */
+/*     $Id: html.c,v 1.220 2017/09/06 16:24:25 schwarze Exp $ */
 /*
  * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
  * Copyright (c) 2011-2015, 2017 Ingo Schwarze <schwarze@openbsd.org>
@@ -28,8 +28,9 @@
 #include <string.h>
 #include <unistd.h>
 
-#include "mandoc.h"
 #include "mandoc_aux.h"
+#include "mandoc.h"
+#include "roff.h"
 #include "out.h"
 #include "html.h"
 #include "manconf.h"
@@ -86,6 +87,7 @@ static        const struct htmldata htmltags[TAG_MAX] = {
        {"math",        HTML_NLALL | HTML_INDENT},
        {"mrow",        0},
        {"mi",          0},
+       {"mn",          0},
        {"mo",          0},
        {"msup",        0},
        {"msub",        0},
@@ -236,6 +238,30 @@ print_metaf(struct html *h, enum mandoc_esc deco)
        }
 }
 
+char *
+html_make_id(const struct roff_node *n)
+{
+       const struct roff_node  *nch;
+       char                    *buf, *cp;
+
+       for (nch = n->child; nch != NULL; nch = nch->next)
+               if (nch->type != ROFFT_TEXT)
+                       return NULL;
+
+       buf = NULL;
+       deroff(&buf, n);
+       if (buf == NULL)
+               return NULL;
+
+       /* http://www.w3.org/TR/html5/dom.html#the-id-attribute */
+
+       for (cp = buf; *cp != '\0'; cp++)
+               if (*cp == ' ')
+                       *cp = '_';
+
+       return buf;
+}
+
 int
 html_strlen(const char *cp)
 {
@@ -322,16 +348,18 @@ static int
 print_encode(struct html *h, const char *p, const char *pend, int norecurse)
 {
        char             numbuf[16];
-       size_t           sz;
-       int              c, len, nospace;
+       struct tag      *t;
        const char      *seq;
+       size_t           sz;
+       int              c, len, breakline, nospace;
        enum mandoc_esc  esc;
-       static const char rejs[9] = { '\\', '<', '>', '&', '"',
+       static const char rejs[10] = { ' ', '\\', '<', '>', '&', '"',
                ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' };
 
        if (pend == NULL)
                pend = strchr(p, '\0');
 
+       breakline = 0;
        nospace = 0;
 
        while (p < pend) {
@@ -342,14 +370,28 @@ print_encode(struct html *h, const char *p, const char *pend, int norecurse)
                }
 
                for (sz = strcspn(p, rejs); sz-- && p < pend; p++)
-                       if (*p == ' ')
-                               print_endword(h);
-                       else
-                               print_byte(h, *p);
+                       print_byte(h, *p);
+
+               if (breakline &&
+                   (p >= pend || *p == ' ' || *p == ASCII_NBRSP)) {
+                       t = print_otag(h, TAG_DIV, "");
+                       print_text(h, "\\~");
+                       print_tagq(h, t);
+                       breakline = 0;
+                       while (p < pend && (*p == ' ' || *p == ASCII_NBRSP))
+                               p++;
+                       continue;
+               }
 
                if (p >= pend)
                        break;
 
+               if (*p == ' ') {
+                       print_endword(h);
+                       p++;
+                       continue;
+               }
+
                if (print_escape(h, *p++))
                        continue;
 
@@ -394,6 +436,9 @@ print_encode(struct html *h, const char *p, const char *pend, int norecurse)
                        if (c <= 0)
                                continue;
                        break;
+               case ESCAPE_BREAK:
+                       breakline = 1;
+                       continue;
                case ESCAPE_NOSPACE:
                        if ('\0' == *p)
                                nospace = 1;
@@ -410,7 +455,7 @@ print_encode(struct html *h, const char *p, const char *pend, int norecurse)
                    (c > 0x7E && c < 0xA0))
                        c = 0xFFFD;
                if (c > 0x7E) {
-                       (void)snprintf(numbuf, sizeof(numbuf), "&#%d;", c);
+                       (void)snprintf(numbuf, sizeof(numbuf), "&#x%.4X;", c);
                        print_word(h, numbuf);
                } else if (print_escape(h, c) == 0)
                        print_byte(h, c);
@@ -473,7 +518,7 @@ print_otag(struct html *h, enum htmltag tag, const char *fmt, ...)
                print_indent(h);
        else if ((h->flags & HTML_NOSPACE) == 0) {
                if (h->flags & HTML_KEEP)
-                       print_word(h, "&#160;");
+                       print_word(h, "&#x00A0;");
                else {
                        if (h->flags & HTML_PREKEEP)
                                h->flags |= HTML_KEEP;
@@ -534,18 +579,25 @@ print_otag(struct html *h, enum htmltag tag, const char *fmt, ...)
                print_byte(h, '=');
                print_byte(h, '"');
                switch (*fmt) {
-               case 'M':
-                       print_href(h, arg1, arg2, 1);
-                       fmt++;
-                       break;
                case 'I':
                        print_href(h, arg1, NULL, 0);
                        fmt++;
                        break;
+               case 'M':
+                       print_href(h, arg1, arg2, 1);
+                       fmt++;
+                       break;
                case 'R':
                        print_byte(h, '#');
+                       print_encode(h, arg1, NULL, 1);
+                       fmt++;
+                       break;
+               case 'T':
+                       print_encode(h, arg1, NULL, 1);
+                       print_word(h, "\" title=\"");
+                       print_encode(h, arg1, NULL, 1);
                        fmt++;
-                       /* FALLTHROUGH */
+                       break;
                default:
                        print_encode(h, arg1, NULL, 1);
                        break;
@@ -573,19 +625,31 @@ print_otag(struct html *h, enum htmltag tag, const char *fmt, ...)
                case 'u':
                        su = va_arg(ap, struct roffsu *);
                        break;
-               case 'v':
-                       i = va_arg(ap, int);
-                       su = &mysu;
-                       SCALE_VS_INIT(su, i);
-                       break;
                case 'w':
-               case 'W':
-                       if ((arg2 = va_arg(ap, char *)) == NULL)
-                               break;
-                       su = &mysu;
-                       a2width(arg2, su);
-                       if (fmt[-1] == 'W')
-                               su->scale *= -1.0;
+                       if ((arg2 = va_arg(ap, char *)) != NULL) {
+                               su = &mysu;
+                               a2width(arg2, su);
+                       }
+                       if (*fmt == '*') {
+                               if (su != NULL && su->unit == SCALE_EN &&
+                                   su->scale > 5.9 && su->scale < 6.1)
+                                       su = NULL;
+                               fmt++;
+                       }
+                       if (*fmt == '+') {
+                               if (su != NULL) {
+                                       /* Make even bold text fit. */
+                                       su->scale *= 1.2;
+                                       /* Add padding. */
+                                       su->scale += 3.0;
+                               }
+                               fmt++;
+                       }
+                       if (*fmt == '-') {
+                               if (su != NULL)
+                                       su->scale *= -1.0;
+                               fmt++;
+                       }
                        break;
                default:
                        abort();
@@ -594,9 +658,6 @@ print_otag(struct html *h, enum htmltag tag, const char *fmt, ...)
                /* Second letter: style name. */
 
                switch (*fmt++) {
-               case 'b':
-                       attr = "margin-bottom";
-                       break;
                case 'h':
                        attr = "height";
                        break;
@@ -606,9 +667,6 @@ print_otag(struct html *h, enum htmltag tag, const char *fmt, ...)
                case 'l':
                        attr = "margin-left";
                        break;
-               case 't':
-                       attr = "margin-top";
-                       break;
                case 'w':
                        attr = "width";
                        break;
@@ -721,7 +779,7 @@ print_text(struct html *h, const char *word)
                                h->flags |= HTML_KEEP;
                        print_endword(h);
                } else
-                       print_word(h, "&#160;");
+                       print_word(h, "&#x00A0;");
        }
 
        assert(NULL == h->metaf);
@@ -912,7 +970,10 @@ print_word(struct html *h, const char *cp)
 static void
 a2width(const char *p, struct roffsu *su)
 {
-       if (a2roffsu(p, su, SCALE_MAX) < 2) {
+       const char      *end;
+
+       end = a2roffsu(p, su, SCALE_MAX);
+       if (end == NULL || *end != '\0') {
                su->unit = SCALE_EN;
                su->scale = html_strlen(p);
        } else if (su->scale < 0.0)