=================================================================== RCS file: /cvs/mandoc/mdoc_html.c,v retrieving revision 1.206 retrieving revision 1.225 diff -u -p -r1.206 -r1.225 --- mandoc/mdoc_html.c 2014/09/27 10:56:18 1.206 +++ mandoc/mdoc_html.c 2015/02/12 12:24:33 1.225 @@ -1,7 +1,7 @@ -/* $Id: mdoc_html.c,v 1.206 2014/09/27 10:56:18 kristaps Exp $ */ +/* $Id: mdoc_html.c,v 1.225 2015/02/12 12:24:33 schwarze Exp $ */ /* - * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons - * Copyright (c) 2014 Ingo Schwarze + * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons + * Copyright (c) 2014, 2015 Ingo Schwarze * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -26,17 +26,16 @@ #include #include -#include "mandoc.h" #include "mandoc_aux.h" +#include "mdoc.h" #include "out.h" #include "html.h" -#include "mdoc.h" #include "main.h" #define INDENT 5 #define MDOC_ARGS const struct mdoc_meta *meta, \ - const struct mdoc_node *n, \ + struct mdoc_node *n, \ struct html *h #ifndef MIN @@ -56,7 +55,6 @@ static void synopsis_pre(struct html *, const struct mdoc_node *); static void a2width(const char *, struct roffsu *); -static void a2offs(const char *, struct roffsu *); static void mdoc_root_post(MDOC_ARGS); static int mdoc_root_pre(MDOC_ARGS); @@ -83,6 +81,8 @@ static int mdoc_fl_pre(MDOC_ARGS); static int mdoc_fn_pre(MDOC_ARGS); static int mdoc_ft_pre(MDOC_ARGS); static int mdoc_em_pre(MDOC_ARGS); +static void mdoc_eo_post(MDOC_ARGS); +static int mdoc_eo_pre(MDOC_ARGS); static int mdoc_er_pre(MDOC_ARGS); static int mdoc_ev_pre(MDOC_ARGS); static int mdoc_ex_pre(MDOC_ARGS); @@ -99,6 +99,7 @@ static int mdoc_mt_pre(MDOC_ARGS); static int mdoc_ms_pre(MDOC_ARGS); static int mdoc_nd_pre(MDOC_ARGS); static int mdoc_nm_pre(MDOC_ARGS); +static int mdoc_no_pre(MDOC_ARGS); static int mdoc_ns_pre(MDOC_ARGS); static int mdoc_pa_pre(MDOC_ARGS); static void mdoc_pf_post(MDOC_ARGS); @@ -183,17 +184,17 @@ static const struct htmlmdoc mdocs[MDOC_MAX] = { {mdoc_quote_pre, mdoc_quote_post}, /* Bq */ {mdoc_xx_pre, NULL}, /* Bsx */ {mdoc_bx_pre, NULL}, /* Bx */ - {NULL, NULL}, /* Db */ + {mdoc_skip_pre, NULL}, /* Db */ {NULL, NULL}, /* Dc */ {mdoc_quote_pre, mdoc_quote_post}, /* Do */ {mdoc_quote_pre, mdoc_quote_post}, /* Dq */ {NULL, NULL}, /* Ec */ /* FIXME: no space */ {NULL, NULL}, /* Ef */ {mdoc_em_pre, NULL}, /* Em */ - {mdoc_quote_pre, mdoc_quote_post}, /* Eo */ + {mdoc_eo_pre, mdoc_eo_post}, /* Eo */ {mdoc_xx_pre, NULL}, /* Fx */ {mdoc_ms_pre, NULL}, /* Ms */ - {mdoc_igndelim_pre, NULL}, /* No */ + {mdoc_no_pre, NULL}, /* No */ {mdoc_ns_pre, NULL}, /* Ns */ {mdoc_xx_pre, NULL}, /* Nx */ {mdoc_xx_pre, NULL}, /* Ox */ @@ -266,7 +267,7 @@ void html_mdoc(void *arg, const struct mdoc *mdoc) { - print_mdoc(mdoc_meta(mdoc), mdoc_node(mdoc), + print_mdoc(mdoc_meta(mdoc), mdoc_node(mdoc)->child, (struct html *)arg); putchar('\n'); } @@ -280,10 +281,11 @@ static void a2width(const char *p, struct roffsu *su) { - if ( ! a2roffsu(p, su, SCALE_MAX)) { - su->unit = SCALE_BU; + if (a2roffsu(p, su, SCALE_MAX) < 2) { + su->unit = SCALE_EN; su->scale = html_strlen(p); - } + } else if (su->scale < 0.0) + su->scale = 0.0; } /* @@ -328,28 +330,7 @@ synopsis_pre(struct html *h, const struct mdoc_node *n } } -/* - * Calculate the scaling unit passed in an `-offset' argument. This - * uses either a native scaling unit (e.g., 1i, 2m), one of a set of - * predefined strings (indent, etc.), or the string length of the value. - */ static void -a2offs(const char *p, struct roffsu *su) -{ - - /* FIXME: "right"? */ - - if (0 == strcmp(p, "left")) - SCALE_HS_INIT(su, 0); - else if (0 == strcmp(p, "indent")) - SCALE_HS_INIT(su, INDENT); - else if (0 == strcmp(p, "indent-two")) - SCALE_HS_INIT(su, INDENT * 2); - else if ( ! a2roffsu(p, su, SCALE_MAX)) - SCALE_HS_INIT(su, html_strlen(p)); -} - -static void print_mdoc(MDOC_ARGS) { struct tag *t, *tt; @@ -392,9 +373,10 @@ static void print_mdoc_nodelist(MDOC_ARGS) { - print_mdoc_node(meta, n, h); - if (n->next) - print_mdoc_nodelist(meta, n->next, h); + while (n != NULL) { + print_mdoc_node(meta, n, h); + n = n->next; + } } static void @@ -405,6 +387,7 @@ print_mdoc_node(MDOC_ARGS) child = 1; t = h->tags.head; + n->flags &= ~MDOC_ENDED; switch (n->type) { case MDOC_ROOT: @@ -444,23 +427,19 @@ print_mdoc_node(MDOC_ARGS) * the "meta" table state. This will be reopened on the * next table element. */ - if (h->tblt) { + if (h->tblt != NULL) { print_tblclose(h); t = h->tags.head; } - - assert(NULL == h->tblt); - if (mdocs[n->tok].pre && ENDBODY_NOT == n->end) + assert(h->tblt == NULL); + if (mdocs[n->tok].pre && (n->end == ENDBODY_NOT || n->child)) child = (*mdocs[n->tok].pre)(meta, n, h); break; } - if (HTML_KEEP & h->flags) { - if (n->prev ? (n->prev->lastline != n->line) : - (n->parent && n->parent->line != n->line)) { - h->flags &= ~HTML_KEEP; - h->flags |= HTML_PREKEEP; - } + if (h->flags & HTML_KEEP && n->flags & MDOC_LINE) { + h->flags &= ~HTML_KEEP; + h->flags |= HTML_PREKEEP; } if (child && n->child) @@ -475,8 +454,13 @@ print_mdoc_node(MDOC_ARGS) case MDOC_EQN: break; default: - if (mdocs[n->tok].post && ENDBODY_NOT == n->end) - (*mdocs[n->tok].post)(meta, n, h); + if ( ! mdocs[n->tok].post || n->flags & MDOC_ENDED) + break; + (*mdocs[n->tok].post)(meta, n, h); + if (n->end != ENDBODY_NOT) + n->body->flags |= MDOC_ENDED; + if (n->end == ENDBODY_NOSPACE) + h->flags |= HTML_NOSPACE; break; } } @@ -994,7 +978,7 @@ mdoc_bl_pre(MDOC_ARGS) /* Set the block's left-hand margin. */ if (n->norm->Bl.offs) { - a2offs(n->norm->Bl.offs, &su); + a2width(n->norm->Bl.offs, &su); bufcat_su(h, "margin-left", &su); } @@ -1139,7 +1123,7 @@ mdoc_bd_pre(MDOC_ARGS) { struct htmlpair tag[2]; int comp, sv; - const struct mdoc_node *nn; + struct mdoc_node *nn; struct roffsu su; if (MDOC_HEAD == n->type) @@ -1160,10 +1144,18 @@ mdoc_bd_pre(MDOC_ARGS) return(1); } - SCALE_HS_INIT(&su, 0); - if (n->norm->Bd.offs) - a2offs(n->norm->Bd.offs, &su); + /* Handle the -offset argument. */ + if (n->norm->Bd.offs == NULL || + ! strcmp(n->norm->Bd.offs, "left")) + SCALE_HS_INIT(&su, 0); + else if ( ! strcmp(n->norm->Bd.offs, "indent")) + SCALE_HS_INIT(&su, INDENT); + else if ( ! strcmp(n->norm->Bd.offs, "indent-two")) + SCALE_HS_INIT(&su, INDENT * 2); + else + a2width(n->norm->Bd.offs, &su); + bufinit(h); bufcat_su(h, "margin-left", &su); PAIR_STYLE_INIT(&tag[0], h); @@ -1211,7 +1203,8 @@ mdoc_bd_pre(MDOC_ARGS) default: break; } - if (nn->next && nn->next->line == nn->line) + if (h->flags & HTML_NONEWLINE || + (nn->next && ! (nn->next->flags & MDOC_LINE))) continue; else if (nn->next) print_text(h, "\n"); @@ -1261,9 +1254,6 @@ mdoc_an_pre(MDOC_ARGS) return(0); } - if (n->child == NULL) - return(0); - if (h->flags & HTML_SPLIT) print_otag(h, TAG_BR, 0, NULL); @@ -1575,9 +1565,12 @@ mdoc_sp_pre(MDOC_ARGS) SCALE_VS_INIT(&su, 1); if (MDOC_sp == n->tok) { - if (NULL != (n = n->child)) + if (NULL != (n = n->child)) { if ( ! a2roffsu(n->string, &su, SCALE_VS)) - SCALE_VS_INIT(&su, atoi(n->string)); + su.scale = 1.0; + else if (su.scale < 0.0) + su.scale = 0.0; + } } else su.scale = 0.0; @@ -1878,7 +1871,8 @@ static void mdoc_pf_post(MDOC_ARGS) { - h->flags |= HTML_NOSPACE; + if ( ! (n->next == NULL || n->next->flags & MDOC_LINE)) + h->flags |= HTML_NOSPACE; } static int @@ -1898,6 +1892,16 @@ mdoc_rs_pre(MDOC_ARGS) } static int +mdoc_no_pre(MDOC_ARGS) +{ + struct htmlpair tag; + + PAIR_CLASS_INIT(&tag, "none"); + print_otag(h, TAG_CODE, 1, &tag); + return(1); +} + +static int mdoc_li_pre(MDOC_ARGS) { struct htmlpair tag; @@ -2078,7 +2082,8 @@ mdoc_quote_pre(MDOC_ARGS) case MDOC_Ao: /* FALLTHROUGH */ case MDOC_Aq: - print_text(h, "\\(la"); + print_text(h, n->nchild == 1 && + n->child->tok == MDOC_Mt ? "<" : "\\(la"); break; case MDOC_Bro: /* FALLTHROUGH */ @@ -2104,8 +2109,6 @@ mdoc_quote_pre(MDOC_ARGS) return(1); print_text(h, n->norm->Es->child->string); break; - case MDOC_Eo: - break; case MDOC_Do: /* FALLTHROUGH */ case MDOC_Dq: @@ -2144,17 +2147,17 @@ static void mdoc_quote_post(MDOC_ARGS) { - if (MDOC_BODY != n->type) + if (n->type != MDOC_BODY && n->type != MDOC_ELEM) return; - if (MDOC_En != n->tok) - h->flags |= HTML_NOSPACE; + h->flags |= HTML_NOSPACE; switch (n->tok) { case MDOC_Ao: /* FALLTHROUGH */ case MDOC_Aq: - print_text(h, "\\(ra"); + print_text(h, n->nchild == 1 && + n->child->tok == MDOC_Mt ? ">" : "\\(ra"); break; case MDOC_Bro: /* FALLTHROUGH */ @@ -2171,15 +2174,13 @@ mdoc_quote_post(MDOC_ARGS) 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; + if (n->norm->Es == NULL || + n->norm->Es->child == NULL || + n->norm->Es->child->next == NULL) + h->flags &= ~HTML_NOSPACE; + else print_text(h, n->norm->Es->child->next->string); - } break; - case MDOC_Eo: - break; case MDOC_Qo: /* FALLTHROUGH */ case MDOC_Qq: @@ -2205,4 +2206,45 @@ mdoc_quote_post(MDOC_ARGS) abort(); /* NOTREACHED */ } +} + +static int +mdoc_eo_pre(MDOC_ARGS) +{ + + if (n->type != MDOC_BODY) + return(1); + + if (n->end == ENDBODY_NOT && + n->parent->head->child == NULL && + n->child != NULL && + n->child->end != ENDBODY_NOT) + print_text(h, "\\&"); + else if (n->end != ENDBODY_NOT ? n->child != NULL : + n->parent->head->child != NULL && (n->child != NULL || + (n->parent->tail != NULL && n->parent->tail->child != NULL))) + h->flags |= HTML_NOSPACE; + return(1); +} + +static void +mdoc_eo_post(MDOC_ARGS) +{ + int body, tail; + + if (n->type != MDOC_BODY) + return; + + if (n->end != ENDBODY_NOT) { + h->flags &= ~HTML_NOSPACE; + return; + } + + body = n->child != NULL || n->parent->head->child != NULL; + tail = n->parent->tail != NULL && n->parent->tail->child != NULL; + + if (body && tail) + h->flags |= HTML_NOSPACE; + else if ( ! tail) + h->flags &= ~HTML_NOSPACE; }