]> git.cameronkatri.com Git - mandoc.git/blobdiff - mdoc_html.c
With the current architecture, we can't support inline equations
[mandoc.git] / mdoc_html.c
index 04d03f2b92b261b37e412c10e9f1257036020460..30a927a65f0f2df36184f14acde4eaa9744aa325 100644 (file)
@@ -1,6 +1,6 @@
-/*     $Id: mdoc_html.c,v 1.188 2014/04/20 16:46:05 schwarze Exp $ */
+/*     $Id: mdoc_html.c,v 1.208 2014/10/10 15:26:29 schwarze Exp $ */
 /*
- * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
+ * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
  * Copyright (c) 2014 Ingo Schwarze <schwarze@openbsd.org>
  *
  * Permission to use, copy, modify, and distribute this software for any
@@ -15,9 +15,7 @@
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
-#ifdef HAVE_CONFIG_H
 #include "config.h"
-#endif
 
 #include <sys/types.h>
 
@@ -29,6 +27,7 @@
 #include <unistd.h>
 
 #include "mandoc.h"
+#include "mandoc_aux.h"
 #include "out.h"
 #include "html.h"
 #include "mdoc.h"
@@ -96,7 +95,6 @@ static        int               mdoc_it_pre(MDOC_ARGS);
 static int               mdoc_lb_pre(MDOC_ARGS);
 static int               mdoc_li_pre(MDOC_ARGS);
 static int               mdoc_lk_pre(MDOC_ARGS);
-static int               mdoc_ll_pre(MDOC_ARGS);
 static int               mdoc_mt_pre(MDOC_ARGS);
 static int               mdoc_ms_pre(MDOC_ARGS);
 static int               mdoc_nd_pre(MDOC_ARGS);
@@ -110,6 +108,7 @@ static      int               mdoc_quote_pre(MDOC_ARGS);
 static int               mdoc_rs_pre(MDOC_ARGS);
 static int               mdoc_rv_pre(MDOC_ARGS);
 static int               mdoc_sh_pre(MDOC_ARGS);
+static int               mdoc_skip_pre(MDOC_ARGS);
 static int               mdoc_sm_pre(MDOC_ARGS);
 static int               mdoc_sp_pre(MDOC_ARGS);
 static int               mdoc_ss_pre(MDOC_ARGS);
@@ -156,7 +155,7 @@ static      const struct htmlmdoc mdocs[MDOC_MAX] = {
        {mdoc_nd_pre, NULL}, /* Nd */
        {mdoc_nm_pre, NULL}, /* Nm */
        {mdoc_quote_pre, mdoc_quote_post}, /* Op */
-       {NULL, NULL}, /* Ot */
+       {mdoc_ft_pre, NULL}, /* Ot */
        {mdoc_pa_pre, NULL}, /* Pa */
        {mdoc_rv_pre, NULL}, /* Rv */
        {NULL, NULL}, /* St */
@@ -226,7 +225,7 @@ static      const struct htmlmdoc mdocs[MDOC_MAX] = {
        {NULL, NULL}, /* Ek */
        {mdoc_bt_pre, NULL}, /* Bt */
        {NULL, NULL}, /* Hf */
-       {NULL, NULL}, /* Fr */
+       {mdoc_em_pre, NULL}, /* Fr */
        {mdoc_ud_pre, NULL}, /* Ud */
        {mdoc_lb_pre, NULL}, /* Lb */
        {mdoc_pp_pre, NULL}, /* Lp */
@@ -236,15 +235,15 @@ static    const struct htmlmdoc mdocs[MDOC_MAX] = {
        {mdoc_quote_pre, mdoc_quote_post}, /* Bro */
        {NULL, NULL}, /* Brc */
        {mdoc__x_pre, mdoc__x_post}, /* %C */
-       {NULL, NULL}, /* Es */  /* TODO */
-       {NULL, NULL}, /* En */  /* TODO */
+       {mdoc_skip_pre, NULL}, /* Es */
+       {mdoc_quote_pre, mdoc_quote_post}, /* En */
        {mdoc_xx_pre, NULL}, /* Dx */
        {mdoc__x_pre, mdoc__x_post}, /* %Q */
        {mdoc_sp_pre, NULL}, /* br */
        {mdoc_sp_pre, NULL}, /* sp */
        {mdoc__x_pre, mdoc__x_post}, /* %U */
        {NULL, NULL}, /* Ta */
-       {mdoc_ll_pre, NULL}, /* ll */
+       {mdoc_skip_pre, NULL}, /* ll */
 };
 
 static const char * const lists[LIST_MAX] = {
@@ -315,11 +314,11 @@ synopsis_pre(struct html *h, const struct mdoc_node *n)
        case MDOC_In:
                /* FALLTHROUGH */
        case MDOC_Vt:
-               print_otag(h, TAG_P, 0, NULL);
+               print_paragraph(h);
                break;
        case MDOC_Ft:
                if (MDOC_Fn != n->tok && MDOC_Fo != n->tok) {
-                       print_otag(h, TAG_P, 0, NULL);
+                       print_paragraph(h);
                        break;
                }
                /* FALLTHROUGH */
@@ -379,8 +378,9 @@ print_mdoc_head(MDOC_ARGS)
 
        print_gen_head(h);
        bufinit(h);
-       bufcat_fmt(h, "%s(%s)", meta->title, meta->msec);
-
+       bufcat(h, meta->title);
+       if (meta->msec)
+               bufcat_fmt(h, "(%s)", meta->msec);
        if (meta->arch)
                bufcat_fmt(h, " (%s)", meta->arch);
 
@@ -484,29 +484,23 @@ print_mdoc_node(MDOC_ARGS)
 static void
 mdoc_root_post(MDOC_ARGS)
 {
-       struct htmlpair  tag[3];
+       struct htmlpair  tag;
        struct tag      *t, *tt;
 
-       PAIR_SUMMARY_INIT(&tag[0], "Document Footer");
-       PAIR_CLASS_INIT(&tag[1], "foot");
-       PAIR_INIT(&tag[2], ATTR_WIDTH, "100%");
-       t = print_otag(h, TAG_TABLE, 3, tag);
-       PAIR_INIT(&tag[0], ATTR_WIDTH, "50%");
-       print_otag(h, TAG_COL, 1, tag);
-       print_otag(h, TAG_COL, 1, tag);
+       PAIR_CLASS_INIT(&tag, "foot");
+       t = print_otag(h, TAG_TABLE, 1, &tag);
 
        print_otag(h, TAG_TBODY, 0, NULL);
 
        tt = print_otag(h, TAG_TR, 0, NULL);
 
-       PAIR_CLASS_INIT(&tag[0], "foot-date");
-       print_otag(h, TAG_TD, 1, tag);
+       PAIR_CLASS_INIT(&tag, "foot-date");
+       print_otag(h, TAG_TD, 1, &tag);
        print_text(h, meta->date);
        print_stagq(h, tt);
 
-       PAIR_CLASS_INIT(&tag[0], "foot-os");
-       PAIR_INIT(&tag[1], ATTR_ALIGN, "right");
-       print_otag(h, TAG_TD, 2, tag);
+       PAIR_CLASS_INIT(&tag, "foot-os");
+       print_otag(h, TAG_TD, 1, &tag);
        print_text(h, meta->os);
        print_tagq(h, t);
 }
@@ -514,49 +508,46 @@ mdoc_root_post(MDOC_ARGS)
 static int
 mdoc_root_pre(MDOC_ARGS)
 {
-       struct htmlpair  tag[3];
+       struct htmlpair  tag;
        struct tag      *t, *tt;
-       char             b[BUFSIZ], title[BUFSIZ];
-
-       strlcpy(b, meta->vol, BUFSIZ);
+       char            *volume, *title;
 
-       if (meta->arch) {
-               strlcat(b, " (", BUFSIZ);
-               strlcat(b, meta->arch, BUFSIZ);
-               strlcat(b, ")", BUFSIZ);
-       }
+       if (NULL == meta->arch)
+               volume = mandoc_strdup(meta->vol);
+       else
+               mandoc_asprintf(&volume, "%s (%s)",
+                   meta->vol, meta->arch);
 
-       snprintf(title, BUFSIZ - 1, "%s(%s)", meta->title, meta->msec);
+       if (NULL == meta->msec)
+               title = mandoc_strdup(meta->title);
+       else
+               mandoc_asprintf(&title, "%s(%s)",
+                   meta->title, meta->msec);
 
-       PAIR_SUMMARY_INIT(&tag[0], "Document Header");
-       PAIR_CLASS_INIT(&tag[1], "head");
-       PAIR_INIT(&tag[2], ATTR_WIDTH, "100%");
-       t = print_otag(h, TAG_TABLE, 3, tag);
-       PAIR_INIT(&tag[0], ATTR_WIDTH, "30%");
-       print_otag(h, TAG_COL, 1, tag);
-       print_otag(h, TAG_COL, 1, tag);
-       print_otag(h, TAG_COL, 1, tag);
+       PAIR_CLASS_INIT(&tag, "head");
+       t = print_otag(h, TAG_TABLE, 1, &tag);
 
        print_otag(h, TAG_TBODY, 0, NULL);
 
        tt = print_otag(h, TAG_TR, 0, NULL);
 
-       PAIR_CLASS_INIT(&tag[0], "head-ltitle");
-       print_otag(h, TAG_TD, 1, tag);
+       PAIR_CLASS_INIT(&tag, "head-ltitle");
+       print_otag(h, TAG_TD, 1, &tag);
        print_text(h, title);
        print_stagq(h, tt);
 
-       PAIR_CLASS_INIT(&tag[0], "head-vol");
-       PAIR_INIT(&tag[1], ATTR_ALIGN, "center");
-       print_otag(h, TAG_TD, 2, tag);
-       print_text(h, b);
+       PAIR_CLASS_INIT(&tag, "head-vol");
+       print_otag(h, TAG_TD, 1, &tag);
+       print_text(h, volume);
        print_stagq(h, tt);
 
-       PAIR_CLASS_INIT(&tag[0], "head-rtitle");
-       PAIR_INIT(&tag[1], ATTR_ALIGN, "right");
-       print_otag(h, TAG_TD, 2, tag);
+       PAIR_CLASS_INIT(&tag, "head-rtitle");
+       print_otag(h, TAG_TD, 1, &tag);
        print_text(h, title);
        print_tagq(h, t);
+
+       free(title);
+       free(volume);
        return(1);
 }
 
@@ -565,12 +556,18 @@ mdoc_sh_pre(MDOC_ARGS)
 {
        struct htmlpair  tag;
 
-       if (MDOC_BLOCK == n->type) {
+       switch (n->type) {
+       case MDOC_BLOCK:
                PAIR_CLASS_INIT(&tag, "section");
                print_otag(h, TAG_DIV, 1, &tag);
                return(1);
-       } else if (MDOC_BODY == n->type)
+       case MDOC_BODY:
+               if (n->sec == SEC_AUTHORS)
+                       h->flags &= ~(HTML_SPLIT|HTML_NOSPLIT);
                return(1);
+       default:
+               break;
+       }
 
        bufinit(h);
        bufcat(h, "x");
@@ -635,9 +632,10 @@ mdoc_fl_pre(MDOC_ARGS)
 
        print_text(h, "\\-");
 
-       if (n->child)
-               h->flags |= HTML_NOSPACE;
-       else if (n->next && n->next->line == n->line)
+       if ( ! (n->nchild == 0 &&
+           (n->next == NULL ||
+            n->next->type == MDOC_TEXT ||
+            n->next->flags & MDOC_LINE)))
                h->flags |= HTML_NOSPACE;
 
        return(1);
@@ -697,7 +695,7 @@ mdoc_nm_pre(MDOC_ARGS)
        if (0 == len && meta->name)
                len = html_strlen(meta->name);
 
-       SCALE_HS_INIT(&su, (double)len);
+       SCALE_HS_INIT(&su, len);
        bufinit(h);
        bufcat_su(h, "width", &su);
        PAIR_STYLE_INIT(&tag, h);
@@ -989,8 +987,8 @@ mdoc_bl_pre(MDOC_ARGS)
        PAIR_STYLE_INIT(&tag[0], h);
 
        assert(lists[n->norm->Bl.type]);
-       strlcpy(buf, "list ", BUFSIZ);
-       strlcat(buf, lists[n->norm->Bl.type], BUFSIZ);
+       (void)strlcpy(buf, "list ", BUFSIZ);
+       (void)strlcat(buf, lists[n->norm->Bl.type], BUFSIZ);
        PAIR_INIT(&tag[1], ATTR_CLASS, buf);
 
        /* Set the block's left-hand margin. */
@@ -1067,11 +1065,11 @@ mdoc_ex_pre(MDOC_ARGS)
        }
 
        if (nchild > 1)
-               print_text(h, "utilities exit");
+               print_text(h, "utilities exit\\~0");
        else
-               print_text(h, "utility exits");
+               print_text(h, "utility exits\\~0");
 
-       print_text(h, "0 on success, and >0 if an error occurs.");
+       print_text(h, "on success, and\\~>0 if an error occurs.");
        return(0);
 }
 
@@ -1158,7 +1156,7 @@ mdoc_bd_pre(MDOC_ARGS)
                                break;
                }
                if ( ! comp)
-                       print_otag(h, TAG_P, 0, NULL);
+                       print_paragraph(h);
                return(1);
        }
 
@@ -1252,7 +1250,25 @@ mdoc_an_pre(MDOC_ARGS)
 {
        struct htmlpair tag;
 
-       /* TODO: -split and -nosplit (see termp_an_pre()). */
+       if (n->norm->An.auth == AUTH_split) {
+               h->flags &= ~HTML_NOSPLIT;
+               h->flags |= HTML_SPLIT;
+               return(0);
+       }
+       if (n->norm->An.auth == AUTH_nosplit) {
+               h->flags &= ~HTML_SPLIT;
+               h->flags |= HTML_NOSPLIT;
+               return(0);
+       }
+
+       if (n->child == NULL)
+               return(0);
+
+       if (h->flags & HTML_SPLIT)
+               print_otag(h, TAG_BR, 0, NULL);
+
+       if (n->sec == SEC_AUTHORS && ! (h->flags & HTML_NOSPLIT))
+               h->flags |= HTML_SPLIT;
 
        PAIR_CLASS_INIT(&tag, "author");
        print_otag(h, TAG_SPAN, 1, &tag);
@@ -1359,6 +1375,15 @@ mdoc_fd_pre(MDOC_ARGS)
 
        if (NULL != (n = n->next)) {
                assert(MDOC_TEXT == n->type);
+
+               /*
+                * XXX This is broken and not easy to fix.
+                * When using -Oincludes, truncation may occur.
+                * Dynamic allocation wouldn't help because
+                * passing long strings to buffmt_includes()
+                * does not work either.
+                */
+
                strlcpy(buf, '<' == *n->string || '"' == *n->string ?
                    n->string + 1 : n->string, BUFSIZ);
 
@@ -1471,10 +1496,8 @@ mdoc_fn_pre(MDOC_ARGS)
 
        t = print_otag(h, TAG_B, 1, tag);
 
-       if (sp) {
-               strlcpy(nbuf, sp, BUFSIZ);
-               print_text(h, nbuf);
-       }
+       if (sp)
+               print_text(h, sp);
 
        print_tagq(h, t);
 
@@ -1515,28 +1538,21 @@ static int
 mdoc_sm_pre(MDOC_ARGS)
 {
 
-       assert(n->child && MDOC_TEXT == n->child->type);
-       if (0 == strcmp("on", n->child->string)) {
-               /*
-                * FIXME: no p->col to check.  Thus, if we have
-                *  .Bd -literal
-                *  .Sm off
-                *  1 2
-                *  .Sm on
-                *  3
-                *  .Ed
-                * the "3" is preceded by a space.
-                */
-               h->flags &= ~HTML_NOSPACE;
+       if (NULL == n->child)
+               h->flags ^= HTML_NONOSPACE;
+       else if (0 == strcmp("on", n->child->string))
                h->flags &= ~HTML_NONOSPACE;
-       else
+       else
                h->flags |= HTML_NONOSPACE;
 
+       if ( ! (HTML_NONOSPACE & h->flags))
+               h->flags &= ~HTML_NOSPACE;
+
        return(0);
 }
 
 static int
-mdoc_ll_pre(MDOC_ARGS)
+mdoc_skip_pre(MDOC_ARGS)
 {
 
        return(0);
@@ -1546,7 +1562,7 @@ static int
 mdoc_pp_pre(MDOC_ARGS)
 {
 
-       print_otag(h, TAG_P, 0, NULL);
+       print_paragraph(h);
        return(0);
 }
 
@@ -1563,7 +1579,7 @@ mdoc_sp_pre(MDOC_ARGS)
                        if ( ! a2roffsu(n->string, &su, SCALE_VS))
                                SCALE_VS_INIT(&su, atoi(n->string));
        } else
-               su.scale = 0;
+               su.scale = 0.0;
 
        bufinit(h);
        bufcat_su(h, "height", &su);
@@ -1742,35 +1758,41 @@ mdoc_rv_pre(MDOC_ARGS)
 
        PAIR_CLASS_INIT(&tag, "fname");
 
-       print_text(h, "The");
-
        nchild = n->nchild;
-       for (n = n->child; n; n = n->next) {
-               assert(MDOC_TEXT == n->type);
+       if (nchild > 0) {
+               print_text(h, "The");
 
-               t = print_otag(h, TAG_B, 1, &tag);
-               print_text(h, n->string);
-               print_tagq(h, t);
+               for (n = n->child; n; n = n->next) {
+                       t = print_otag(h, TAG_B, 1, &tag);
+                       print_text(h, n->string);
+                       print_tagq(h, t);
 
-               h->flags |= HTML_NOSPACE;
-               print_text(h, "()");
-
-               if (nchild > 2 && n->next) {
                        h->flags |= HTML_NOSPACE;
-                       print_text(h, ",");
+                       print_text(h, "()");
+
+                       if (n->next == NULL)
+                               continue;
+
+                       if (nchild > 2) {
+                               h->flags |= HTML_NOSPACE;
+                               print_text(h, ",");
+                       }
+                       if (n->next->next == NULL)
+                               print_text(h, "and");
                }
 
-               if (n->next && NULL == n->next->next)
-                       print_text(h, "and");
-       }
+               if (nchild > 1)
+                       print_text(h, "functions return");
+               else
+                       print_text(h, "function returns");
 
-       if (nchild > 1)
-               print_text(h, "functions return");
-       else
-               print_text(h, "function returns");
+               print_text(h, "the value\\~0 if successful;");
+       } else
+               print_text(h, "Upon successful completion,"
+                    " the value\\~0 is returned;");
 
-       print_text(h, "the value 0 if successful; otherwise the "
-           "value -1 is returned and the global variable");
+       print_text(h, "otherwise the value\\~\\-1 is returned"
+          " and the global variable");
 
        PAIR_CLASS_INIT(&tag, "var");
        t = print_otag(h, TAG_B, 1, &tag);
@@ -1868,7 +1890,7 @@ mdoc_rs_pre(MDOC_ARGS)
                return(1);
 
        if (n->prev && SEC_SEE_ALSO == n->sec)
-               print_otag(h, TAG_P, 0, NULL);
+               print_paragraph(h);
 
        PAIR_CLASS_INIT(&tag, "ref");
        print_otag(h, TAG_SPAN, 1, &tag);
@@ -2076,6 +2098,12 @@ mdoc_quote_pre(MDOC_ARGS)
                PAIR_CLASS_INIT(&tag, "opt");
                print_otag(h, TAG_SPAN, 1, &tag);
                break;
+       case MDOC_En:
+               if (NULL == n->norm->Es ||
+                   NULL == n->norm->Es->child)
+                       return(1);
+               print_text(h, n->norm->Es->child->string);
+               break;
        case MDOC_Eo:
                break;
        case MDOC_Do:
@@ -2119,7 +2147,8 @@ mdoc_quote_post(MDOC_ARGS)
        if (MDOC_BODY != n->type)
                return;
 
-       h->flags |= HTML_NOSPACE;
+       if (MDOC_En != n->tok)
+               h->flags |= HTML_NOSPACE;
 
        switch (n->tok) {
        case MDOC_Ao:
@@ -2141,6 +2170,14 @@ mdoc_quote_post(MDOC_ARGS)
        case MDOC_Bq:
                print_text(h, "\\(rB");
                break;
+       case MDOC_En:
+               if (NULL != n->norm->Es &&
+                   NULL != n->norm->Es->child &&
+                   NULL != n->norm->Es->child->next) {
+                       h->flags |= HTML_NOSPACE;
+                       print_text(h, n->norm->Es->child->next->string);
+               }
+               break;
        case MDOC_Eo:
                break;
        case MDOC_Qo: