]> git.cameronkatri.com Git - mandoc.git/blobdiff - html.c
implement the roff(7) \p (break output line) escape sequence
[mandoc.git] / html.c
diff --git a/html.c b/html.c
index 95df14eac5ed835ebe5f6275ea6b5e51d338577c..d92ec16aee64017b17731a4072184b379c567625 100644 (file)
--- a/html.c
+++ b/html.c
@@ -1,4 +1,4 @@
-/*     $Id: html.c,v 1.208 2017/03/13 19:01:38 schwarze Exp $ */
+/*     $Id: html.c,v 1.214 2017/06/14 01:31:26 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"
@@ -236,6 +237,28 @@ 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);
+
+       /* 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 +345,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 +367,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 +433,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;
@@ -586,13 +628,21 @@ print_otag(struct html *h, enum htmltag tag, const char *fmt, ...)
                        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')
+                       if (*fmt == '+') {
+                               /* Increase to make even bold text fit. */
+                               su->scale *= 1.2;
+                               /* Add padding. */
+                               su->scale += 3.0;
+                               fmt++;
+                       }
+                       if (*fmt == '-') {
                                su->scale *= -1.0;
+                               fmt++;
+                       }
                        break;
                default:
                        abort();
@@ -919,7 +969,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)