[BACK]Return to html.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Diff for /mandoc/html.c between version 1.29 and 1.195

version 1.29, 2009/09/16 09:41:24 version 1.195, 2017/01/17 15:32:43
Line 1 
Line 1 
 /*      $Id$ */  /*      $Id$ */
 /*  /*
  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>   * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
    * Copyright (c) 2011-2015, 2017 Ingo Schwarze <schwarze@openbsd.org>
  *   *
  * Permission to use, copy, modify, and distribute this software for any   * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above   * purpose with or without fee is hereby granted, provided that the above
  * copyright notice and this permission notice appear in all copies.   * copyright notice and this permission notice appear in all copies.
  *   *
  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES   * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF   * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR   * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES   * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN   * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF   * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.   * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */   */
   #include "config.h"
   
   #include <sys/types.h>
   
 #include <assert.h>  #include <assert.h>
 #include <err.h>  #include <ctype.h>
   #include <stdarg.h>
 #include <stdio.h>  #include <stdio.h>
   #include <stdint.h>
 #include <stdlib.h>  #include <stdlib.h>
   #include <string.h>
   #include <unistd.h>
   
 #include "mdoc.h"  #include "mandoc.h"
 #include "man.h"  #include "mandoc_aux.h"
   #include "out.h"
   #include "html.h"
   #include "manconf.h"
   #include "main.h"
   
 #define DOCTYPE         "-//W3C//DTD HTML 4.01//EN"  
 #define DTD             "http://www.w3.org/TR/html4/strict.dtd"  
   
 enum    htmltag {  
         TAG_HTML,  
         TAG_HEAD,  
         TAG_BODY,  
         TAG_META,  
         TAG_TITLE,  
         TAG_DIV,  
         TAG_H1,  
         TAG_H2,  
         TAG_P,  
         TAG_SPAN,  
         TAG_LINK,  
         TAG_MAX  
 };  
   
 enum    htmlattr {  
         ATTR_HTTPEQUIV,  
         ATTR_CONTENT,  
         ATTR_NAME,  
         ATTR_REL,  
         ATTR_HREF,  
         ATTR_TYPE,  
         ATTR_MEDIA,  
         ATTR_CLASS,  
         ATTR_MAX  
 };  
   
 struct  htmldata {  struct  htmldata {
         char             *name;          const char       *name;
         int               flags;          int               flags;
 #define HTML_BLOCK       (1 << 0)  #define HTML_CLRLINE     (1 << 0)
   #define HTML_NOSTACK     (1 << 1)
   #define HTML_AUTOCLOSE   (1 << 2) /* Tag has auto-closure. */
 };  };
   
 static  const struct htmldata htmltags[TAG_MAX] = {  static  const struct htmldata htmltags[TAG_MAX] = {
         {"html",        HTML_BLOCK}, /* TAG_HTML */          {"html",        HTML_CLRLINE}, /* TAG_HTML */
         {"head",        HTML_BLOCK}, /* TAG_HEAD */          {"head",        HTML_CLRLINE}, /* TAG_HEAD */
         {"body",        HTML_BLOCK}, /* TAG_BODY */          {"body",        HTML_CLRLINE}, /* TAG_BODY */
         {"meta",        HTML_BLOCK}, /* TAG_META */          {"meta",        HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_META */
         {"title",       HTML_BLOCK}, /* TAG_TITLE */          {"title",       HTML_CLRLINE}, /* TAG_TITLE */
         {"div",         HTML_BLOCK}, /* TAG_DIV */          {"div",         HTML_CLRLINE}, /* TAG_DIV */
         {"h1",          0}, /* TAG_H1 */          {"h1",          0}, /* TAG_H1 */
         {"h2",          0}, /* TAG_H2 */          {"h2",          0}, /* TAG_H2 */
         {"p",           HTML_BLOCK}, /* TAG_P */  
         {"span",        0}, /* TAG_SPAN */          {"span",        0}, /* TAG_SPAN */
         {"link",        HTML_BLOCK}, /* TAG_LINK */          {"link",        HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_LINK */
           {"br",          HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_BR */
           {"a",           0}, /* TAG_A */
           {"table",       HTML_CLRLINE}, /* TAG_TABLE */
           {"tbody",       HTML_CLRLINE}, /* TAG_TBODY */
           {"col",         HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_COL */
           {"tr",          HTML_CLRLINE}, /* TAG_TR */
           {"td",          HTML_CLRLINE}, /* TAG_TD */
           {"li",          HTML_CLRLINE}, /* TAG_LI */
           {"ul",          HTML_CLRLINE}, /* TAG_UL */
           {"ol",          HTML_CLRLINE}, /* TAG_OL */
           {"dl",          HTML_CLRLINE}, /* TAG_DL */
           {"dt",          HTML_CLRLINE}, /* TAG_DT */
           {"dd",          HTML_CLRLINE}, /* TAG_DD */
           {"blockquote",  HTML_CLRLINE}, /* TAG_BLOCKQUOTE */
           {"pre",         HTML_CLRLINE }, /* TAG_PRE */
           {"b",           0 }, /* TAG_B */
           {"i",           0 }, /* TAG_I */
           {"code",        0 }, /* TAG_CODE */
           {"small",       0 }, /* TAG_SMALL */
           {"style",       HTML_CLRLINE}, /* TAG_STYLE */
           {"math",        HTML_CLRLINE}, /* TAG_MATH */
           {"mrow",        0}, /* TAG_MROW */
           {"mi",          0}, /* TAG_MI */
           {"mo",          0}, /* TAG_MO */
           {"msup",        0}, /* TAG_MSUP */
           {"msub",        0}, /* TAG_MSUB */
           {"msubsup",     0}, /* TAG_MSUBSUP */
           {"mfrac",       0}, /* TAG_MFRAC */
           {"msqrt",       0}, /* TAG_MSQRT */
           {"mfenced",     0}, /* TAG_MFENCED */
           {"mtable",      0}, /* TAG_MTABLE */
           {"mtr",         0}, /* TAG_MTR */
           {"mtd",         0}, /* TAG_MTD */
           {"munderover",  0}, /* TAG_MUNDEROVER */
           {"munder",      0}, /* TAG_MUNDER*/
           {"mover",       0}, /* TAG_MOVER*/
 };  };
   
 static  const char       *const htmlattrs[ATTR_MAX] = {  static  const char      *const roffscales[SCALE_MAX] = {
         "http-equiv",          "cm", /* SCALE_CM */
         "content",          "in", /* SCALE_IN */
         "name",          "pc", /* SCALE_PC */
         "rel",          "pt", /* SCALE_PT */
         "href",          "em", /* SCALE_EM */
         "type",          "em", /* SCALE_MM */
         "media",          "ex", /* SCALE_EN */
         "class"          "ex", /* SCALE_BU */
           "em", /* SCALE_VS */
           "ex", /* SCALE_FS */
 };  };
   
 struct  htmlpair {  static  void     a2width(const char *, struct roffsu *);
         enum htmlattr     key;  static  void     print_ctag(struct html *, struct tag *);
         char             *val;  static  int      print_escape(char);
 };  static  int      print_encode(struct html *, const char *, const char *, int);
   static  void     print_href(struct html *, const char *, const char *, int);
   static  void     print_metaf(struct html *, enum mandoc_esc);
   
 struct  html {  
         int               flags;  
 #define HTML_NOSPACE     (1 << 0)  
 };  
   
 #define MDOC_ARGS         const struct mdoc_meta *m, \  void *
                           const struct mdoc_node *n, \  html_alloc(const struct manoutput *outopts)
                           struct html *h  
 #define MAN_ARGS          const struct man_meta *m, \  
                           const struct man_node *n, \  
                           struct html *h  
 struct  htmlmdoc {  
         int             (*pre)(MDOC_ARGS);  
         void            (*post)(MDOC_ARGS);  
 };  
   
 static  void              print_gen_doctype(struct html *);  
 static  void              print_gen_head(struct html *);  
 static  void              print_mdoc(MDOC_ARGS);  
 static  void              print_mdoc_head(MDOC_ARGS);  
 static  void              print_mdoc_node(MDOC_ARGS);  
 static  void              print_man(MAN_ARGS);  
 static  void              print_man_head(MAN_ARGS);  
 static  void              print_man_body(MAN_ARGS);  
 static  void              print_otag(struct html *, enum htmltag,  
                                 int, const struct htmlpair *);  
 static  void              print_ctag(struct html *, enum htmltag);  
 static  void              print_encode(const char *);  
 static  void              print_text(struct html *, const char *);  
 static  int               mdoc_root_pre(MDOC_ARGS);  
 static  void              mdoc_root_post(MDOC_ARGS);  
   
 static  int               mdoc_nd_pre(MDOC_ARGS);  
 static  int               mdoc_nm_pre(MDOC_ARGS);  
 static  void              mdoc_nm_post(MDOC_ARGS);  
 static  int               mdoc_pp_pre(MDOC_ARGS);  
 static  int               mdoc_sh_pre(MDOC_ARGS);  
 static  void              mdoc_sh_post(MDOC_ARGS);  
 static  int               mdoc_ss_pre(MDOC_ARGS);  
 static  void              mdoc_ss_post(MDOC_ARGS);  
   
 static  const struct htmlmdoc mdocs[MDOC_MAX] = {  
         {NULL, NULL}, /* Ap */  
         {NULL, NULL}, /* Dd */  
         {NULL, NULL}, /* Dt */  
         {NULL, NULL}, /* Os */  
         {mdoc_sh_pre, mdoc_sh_post }, /* Sh */  
         {mdoc_ss_pre, mdoc_ss_post }, /* Ss */  
         {mdoc_pp_pre, NULL}, /* Pp */  
         {NULL, NULL}, /* D1 */  
         {NULL, NULL}, /* Dl */  
         {NULL, NULL}, /* Bd */  
         {NULL, NULL}, /* Ed */  
         {NULL, NULL}, /* Bl */  
         {NULL, NULL}, /* El */  
         {NULL, NULL}, /* It */  
         {NULL, NULL}, /* Ad */  
         {NULL, NULL}, /* An */  
         {NULL, NULL}, /* Ar */  
         {NULL, NULL}, /* Cd */  
         {NULL, NULL}, /* Cm */  
         {NULL, NULL}, /* Dv */  
         {NULL, NULL}, /* Er */  
         {NULL, NULL}, /* Ev */  
         {NULL, NULL}, /* Ex */  
         {NULL, NULL}, /* Fa */  
         {NULL, NULL}, /* Fd */  
         {NULL, NULL}, /* Fl */  
         {NULL, NULL}, /* Fn */  
         {NULL, NULL}, /* Ft */  
         {NULL, NULL}, /* Ic */  
         {NULL, NULL}, /* In */  
         {NULL, NULL}, /* Li */  
         {mdoc_nd_pre, NULL}, /* Nd */  
         {mdoc_nm_pre, mdoc_nm_post}, /* Nm */  
         {NULL, NULL}, /* Op */  
         {NULL, NULL}, /* Ot */  
         {NULL, NULL}, /* Pa */  
         {NULL, NULL}, /* Rv */  
         {NULL, NULL}, /* St */  
         {NULL, NULL}, /* Va */  
         {NULL, NULL}, /* Vt */  
         {NULL, NULL}, /* Xr */  
         {NULL, NULL}, /* %A */  
         {NULL, NULL}, /* %B */  
         {NULL, NULL}, /* %D */  
         {NULL, NULL}, /* %I */  
         {NULL, NULL}, /* %J */  
         {NULL, NULL}, /* %N */  
         {NULL, NULL}, /* %O */  
         {NULL, NULL}, /* %P */  
         {NULL, NULL}, /* %R */  
         {NULL, NULL}, /* %T */  
         {NULL, NULL}, /* %V */  
         {NULL, NULL}, /* Ac */  
         {NULL, NULL}, /* Ao */  
         {NULL, NULL}, /* Aq */  
         {NULL, NULL}, /* At */  
         {NULL, NULL}, /* Bc */  
         {NULL, NULL}, /* Bf */  
         {NULL, NULL}, /* Bo */  
         {NULL, NULL}, /* Bq */  
         {NULL, NULL}, /* Bsx */  
         {NULL, NULL}, /* Bx */  
         {NULL, NULL}, /* Db */  
         {NULL, NULL}, /* Dc */  
         {NULL, NULL}, /* Do */  
         {NULL, NULL}, /* Dq */  
         {NULL, NULL}, /* Ec */  
         {NULL, NULL}, /* Ef */  
         {NULL, NULL}, /* Em */  
         {NULL, NULL}, /* Eo */  
         {NULL, NULL}, /* Fx */  
         {NULL, NULL}, /* Ms */  
         {NULL, NULL}, /* No */  
         {NULL, NULL}, /* Ns */  
         {NULL, NULL}, /* Nx */  
         {NULL, NULL}, /* Ox */  
         {NULL, NULL}, /* Pc */  
         {NULL, NULL}, /* Pf */  
         {NULL, NULL}, /* Po */  
         {NULL, NULL}, /* Pq */  
         {NULL, NULL}, /* Qc */  
         {NULL, NULL}, /* Ql */  
         {NULL, NULL}, /* Qo */  
         {NULL, NULL}, /* Qq */  
         {NULL, NULL}, /* Re */  
         {NULL, NULL}, /* Rs */  
         {NULL, NULL}, /* Sc */  
         {NULL, NULL}, /* So */  
         {NULL, NULL}, /* Sq */  
         {NULL, NULL}, /* Sm */  
         {NULL, NULL}, /* Sx */  
         {NULL, NULL}, /* Sy */  
         {NULL, NULL}, /* Tn */  
         {NULL, NULL}, /* Ux */  
         {NULL, NULL}, /* Xc */  
         {NULL, NULL}, /* Xo */  
         {NULL, NULL}, /* Fo */  
         {NULL, NULL}, /* Fc */  
         {NULL, NULL}, /* Oo */  
         {NULL, NULL}, /* Oc */  
         {NULL, NULL}, /* Bk */  
         {NULL, NULL}, /* Ek */  
         {NULL, NULL}, /* Bt */  
         {NULL, NULL}, /* Hf */  
         {NULL, NULL}, /* Fr */  
         {NULL, NULL}, /* Ud */  
         {NULL, NULL}, /* Lb */  
         {NULL, NULL}, /* Lp */  
         {NULL, NULL}, /* Lk */  
         {NULL, NULL}, /* Mt */  
         {NULL, NULL}, /* Brq */  
         {NULL, NULL}, /* Bro */  
         {NULL, NULL}, /* Brc */  
         {NULL, NULL}, /* %C */  
         {NULL, NULL}, /* Es */  
         {NULL, NULL}, /* En */  
         {NULL, NULL}, /* Dx */  
         {NULL, NULL}, /* %Q */  
         {NULL, NULL}, /* br */  
         {NULL, NULL}, /* sp */  
 };  
   
   
 int  
 html_mdoc(void *arg, const struct mdoc *m)  
 {  {
         struct html     *h;  
   
         h = (struct html *)arg;  
   
         print_gen_doctype(h);  
         print_otag(h, TAG_HTML, 0, NULL);  
         print_mdoc(mdoc_meta(m), mdoc_node(m), h);  
         print_ctag(h, TAG_HTML);  
         printf("\n");  
         return(1);  
 }  
   
   
 int  
 html_man(void *arg, const struct man *m)  
 {  
         struct html     *h;          struct html     *h;
   
         h = (struct html *)arg;          h = mandoc_calloc(1, sizeof(struct html));
   
         print_gen_doctype(h);          h->tags.head = NULL;
         print_otag(h, TAG_HTML, 0, NULL);          h->style = outopts->style;
         print_man(man_meta(m), man_node(m), h);          h->base_man = outopts->man;
         print_ctag(h, TAG_HTML);          h->base_includes = outopts->includes;
         printf("\n");          if (outopts->fragment)
         return(1);                  h->oflags |= HTML_FRAGMENT;
 }  
   
           return h;
 void *  
 html_alloc(void)  
 {  
   
         return(calloc(1, sizeof(struct html)));  
 }  }
   
   
 void  void
 html_free(void *p)  html_free(void *p)
 {  {
           struct tag      *tag;
           struct html     *h;
   
         free(p);          h = (struct html *)p;
 }  
   
           while ((tag = h->tags.head) != NULL) {
                   h->tags.head = tag->next;
                   free(tag);
           }
   
 static void          free(h);
 print_mdoc(MDOC_ARGS)  
 {  
   
         print_otag(h, TAG_HEAD, 0, NULL);  
         print_mdoc_head(m, n, h);  
         print_ctag(h, TAG_HEAD);  
         print_otag(h, TAG_BODY, 0, NULL);  
         print_mdoc_node(m, n, h);  
         print_ctag(h, TAG_BODY);  
 }  }
   
   void
 static void  
 print_gen_head(struct html *h)  print_gen_head(struct html *h)
 {  {
         struct htmlpair  meta0[2];          struct tag      *t;
         struct htmlpair  meta1[2];  
         struct htmlpair  link[4];  
   
         meta0[0].key = ATTR_HTTPEQUIV;          print_otag(h, TAG_META, "?", "charset", "utf-8");
         meta0[0].val = "Content-Type";  
         meta0[1].key = ATTR_CONTENT;  
         meta0[1].val = "text/html; charest-utf-8";  
   
         meta1[0].key = ATTR_NAME;          /*
         meta1[0].val = "resource-type";           * Print a default style-sheet.
         meta1[1].key = ATTR_CONTENT;           */
         meta1[1].val = "document";          t = print_otag(h, TAG_STYLE, "");
           print_text(h, "table.head, table.foot { width: 100%; }\n"
                 "td.head-rtitle, td.foot-os { text-align: right; }\n"
                 "td.head-vol { text-align: center; }\n"
                 "table.foot td { width: 50%; }\n"
                 "table.head td { width: 33%; }\n"
                 "div.spacer { margin: 1em 0; }\n");
           print_tagq(h, t);
   
         link[0].key = ATTR_REL;          if (h->style)
         link[0].val = "stylesheet";                  print_otag(h, TAG_LINK, "?h??", "rel", "stylesheet",
         link[1].key = ATTR_HREF;                      h->style, "type", "text/css", "media", "all");
         link[1].val = "style.css";  
         link[2].key = ATTR_TYPE;  
         link[2].val = "text/css";  
         link[3].key = ATTR_MEDIA;  
         link[3].val = "all";  
   
         print_otag(h, TAG_META, 2, meta0);  
         print_otag(h, TAG_META, 2, meta1);  
         print_otag(h, TAG_LINK, 4, link);  
 }  }
   
   
 static void  static void
 print_mdoc_head(MDOC_ARGS)  print_metaf(struct html *h, enum mandoc_esc deco)
 {  {
           enum htmlfont    font;
   
         print_gen_head(h);          switch (deco) {
         print_otag(h, TAG_TITLE, 0, NULL);          case ESCAPE_FONTPREV:
         print_encode(m->title);                  font = h->metal;
         print_ctag(h, TAG_TITLE);                  break;
 }          case ESCAPE_FONTITALIC:
                   font = HTMLFONT_ITALIC;
                   break;
           case ESCAPE_FONTBOLD:
                   font = HTMLFONT_BOLD;
                   break;
           case ESCAPE_FONTBI:
                   font = HTMLFONT_BI;
                   break;
           case ESCAPE_FONT:
           case ESCAPE_FONTROMAN:
                   font = HTMLFONT_NONE;
                   break;
           default:
                   abort();
           }
   
           if (h->metaf) {
                   print_tagq(h, h->metaf);
                   h->metaf = NULL;
           }
   
 static int          h->metal = h->metac;
 mdoc_root_pre(MDOC_ARGS)          h->metac = font;
 {  
         struct htmlpair  div;  
   
         div.key = ATTR_CLASS;          switch (font) {
         div.val = "body";          case HTMLFONT_ITALIC:
                   h->metaf = print_otag(h, TAG_I, "");
         print_otag(h, TAG_DIV, 1, &div);                  break;
         return(1);          case HTMLFONT_BOLD:
                   h->metaf = print_otag(h, TAG_B, "");
                   break;
           case HTMLFONT_BI:
                   h->metaf = print_otag(h, TAG_B, "");
                   print_otag(h, TAG_I, "");
                   break;
           default:
                   break;
           }
 }  }
   
   int
 static void  html_strlen(const char *cp)
 mdoc_root_post(MDOC_ARGS)  
 {  {
           size_t           rsz;
           int              skip, sz;
   
         print_ctag(h, TAG_DIV);          /*
            * Account for escaped sequences within string length
            * calculations.  This follows the logic in term_strlen() as we
            * must calculate the width of produced strings.
            * Assume that characters are always width of "1".  This is
            * hacky, but it gets the job done for approximation of widths.
            */
   
           sz = 0;
           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:
                   case ESCAPE_NUMBERED:
                   case ESCAPE_SPECIAL:
                   case ESCAPE_OVERSTRIKE:
                           if (skip)
                                   skip = 0;
                           else
                                   sz++;
                           break;
                   case ESCAPE_SKIPCHAR:
                           skip = 1;
                           break;
                   default:
                           break;
                   }
           }
           return sz;
 }  }
   
   
 static int  static int
 mdoc_ss_pre(MDOC_ARGS)  print_escape(char c)
 {  {
   
         if (MDOC_BODY == n->type)          switch (c) {
                 print_otag(h, TAG_P, 0, NULL);          case '<':
         if (MDOC_HEAD == n->type)                  printf("&lt;");
                 print_otag(h, TAG_H2, 0, NULL);                  break;
         return(1);          case '>':
                   printf("&gt;");
                   break;
           case '&':
                   printf("&amp;");
                   break;
           case '"':
                   printf("&quot;");
                   break;
           case ASCII_NBRSP:
                   printf("&nbsp;");
                   break;
           case ASCII_HYPH:
                   putchar('-');
                   break;
           case ASCII_BREAK:
                   break;
           default:
                   return 0;
           }
           return 1;
 }  }
   
   static int
 static void  print_encode(struct html *h, const char *p, const char *pend, int norecurse)
 mdoc_ss_post(MDOC_ARGS)  
 {  {
           size_t           sz;
           int              c, len, nospace;
           const char      *seq;
           enum mandoc_esc  esc;
           static const char rejs[9] = { '\\', '<', '>', '&', '"',
                   ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' };
   
         if (MDOC_BODY == n->type)          if (pend == NULL)
                 print_ctag(h, TAG_P);                  pend = strchr(p, '\0');
         if (MDOC_HEAD == n->type)  
                 print_ctag(h, TAG_H2);  
 }  
   
           nospace = 0;
   
 static int          while (p < pend) {
 mdoc_pp_pre(MDOC_ARGS)                  if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
 {                          h->flags &= ~HTML_SKIPCHAR;
                           p++;
                           continue;
                   }
   
         print_otag(h, TAG_P, 0, NULL);                  sz = strcspn(p, rejs);
         return(0);                  if (p + sz > pend)
 }                          sz = pend - p;
   
                   fwrite(p, 1, sz, stdout);
                   p += (int)sz;
   
 static int                  if (p >= pend)
 mdoc_nd_pre(MDOC_ARGS)                          break;
 {  
   
         if (MDOC_BODY == n->type)                  if (print_escape(*p++))
                 print_text(h, "--");                          continue;
         return(1);  
 }  
   
                   esc = mandoc_escape(&p, &seq, &len);
                   if (ESCAPE_ERROR == esc)
                           break;
   
 static int                  switch (esc) {
 mdoc_nm_pre(MDOC_ARGS)                  case ESCAPE_FONT:
 {                  case ESCAPE_FONTPREV:
         struct htmlpair class;                  case ESCAPE_FONTBOLD:
                   case ESCAPE_FONTITALIC:
                   case ESCAPE_FONTBI:
                   case ESCAPE_FONTROMAN:
                           if (0 == norecurse)
                                   print_metaf(h, esc);
                           continue;
                   case ESCAPE_SKIPCHAR:
                           h->flags |= HTML_SKIPCHAR;
                           continue;
                   default:
                           break;
                   }
   
         class.key = ATTR_CLASS;                  if (h->flags & HTML_SKIPCHAR) {
         class.val = "name";                          h->flags &= ~HTML_SKIPCHAR;
                           continue;
                   }
   
         print_otag(h, TAG_SPAN, 1, &class);                  switch (esc) {
         if (NULL == n->child)                  case ESCAPE_UNICODE:
                 print_text(h, m->name);                          /* Skip past "u" header. */
                           c = mchars_num2uc(seq + 1, len - 1);
                           break;
                   case ESCAPE_NUMBERED:
                           c = mchars_num2char(seq, len);
                           if (c < 0)
                                   continue;
                           break;
                   case ESCAPE_SPECIAL:
                           c = mchars_spec2cp(seq, len);
                           if (c <= 0)
                                   continue;
                           break;
                   case ESCAPE_NOSPACE:
                           if ('\0' == *p)
                                   nospace = 1;
                           continue;
                   case ESCAPE_OVERSTRIKE:
                           if (len == 0)
                                   continue;
                           c = seq[len - 1];
                           break;
                   default:
                           continue;
                   }
                   if ((c < 0x20 && c != 0x09) ||
                       (c > 0x7E && c < 0xA0))
                           c = 0xFFFD;
                   if (c > 0x7E)
                           printf("&#%d;", c);
                   else if ( ! print_escape(c))
                           putchar(c);
           }
   
         return(1);          return nospace;
 }  }
   
   
 static void  static void
 mdoc_nm_post(MDOC_ARGS)  print_href(struct html *h, const char *name, const char *sec, int man)
 {  {
           const char      *p, *pp;
   
         print_ctag(h, TAG_SPAN);          pp = man ? h->base_man : h->base_includes;
           while ((p = strchr(pp, '%')) != NULL) {
                   print_encode(h, pp, p, 1);
                   if (man && p[1] == 'S') {
                           if (sec == NULL)
                                   putchar('1');
                           else
                                   print_encode(h, sec, NULL, 1);
                   } else if ((man && p[1] == 'N') ||
                       (man == 0 && p[1] == 'I'))
                           print_encode(h, name, NULL, 1);
                   else
                           print_encode(h, p, p + 2, 1);
                   pp = p + 2;
           }
           if (*pp != '\0')
                   print_encode(h, pp, NULL, 1);
 }  }
   
   struct tag *
 static int  print_otag(struct html *h, enum htmltag tag, const char *fmt, ...)
 mdoc_sh_pre(MDOC_ARGS)  
 {  {
           va_list          ap;
           struct roffsu    mysu, *su;
           struct tag      *t;
           const char      *attr;
           char            *s;
           double           v;
           int              i, have_style;
   
         if (MDOC_BODY == n->type)          /* Push this tags onto the stack of open scopes. */
                 print_otag(h, TAG_P, 0, NULL);  
         if (MDOC_HEAD == n->type)  
                 print_otag(h, TAG_H1, 0, NULL);  
         return(1);  
 }  
   
           if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
                   t = mandoc_malloc(sizeof(struct tag));
                   t->tag = tag;
                   t->next = h->tags.head;
                   h->tags.head = t;
           } else
                   t = NULL;
   
 static void          if ( ! (HTML_NOSPACE & h->flags))
 mdoc_sh_post(MDOC_ARGS)                  if ( ! (HTML_CLRLINE & htmltags[tag].flags)) {
 {                          /* Manage keeps! */
                           if ( ! (HTML_KEEP & h->flags)) {
                                   if (HTML_PREKEEP & h->flags)
                                           h->flags |= HTML_KEEP;
                                   putchar(' ');
                           } else
                                   printf("&#160;");
                   }
   
         if (MDOC_BODY == n->type)          if ( ! (h->flags & HTML_NONOSPACE))
                 print_ctag(h, TAG_P);                  h->flags &= ~HTML_NOSPACE;
         if (MDOC_HEAD == n->type)          else
                 print_ctag(h, TAG_H1);                  h->flags |= HTML_NOSPACE;
 }  
   
           /* Print out the tag name and attributes. */
   
 static void          printf("<%s", htmltags[tag].name);
 print_mdoc_node(MDOC_ARGS)  
 {  
         int              child;  
   
         child = 1;          va_start(ap, fmt);
   
         switch (n->type) {          have_style = 0;
         case (MDOC_ROOT):          while (*fmt != '\0') {
                 child = mdoc_root_pre(m, n, h);                  if (*fmt == 's') {
                 break;                          printf(" style=\"");
         case (MDOC_TEXT):                          have_style = 1;
                 print_text(h, n->string);                          fmt++;
                 break;                          break;
         default:                  }
                 if (mdocs[n->tok].pre)                  s = va_arg(ap, char *);
                         child = (*mdocs[n->tok].pre)(m, n, h);                  switch (*fmt++) {
                 break;                  case 'c':
                           attr = "class";
                           break;
                   case 'h':
                           attr = "href";
                           break;
                   case 'i':
                           attr = "id";
                           break;
                   case '?':
                           attr = s;
                           s = va_arg(ap, char *);
                           break;
                   default:
                           abort();
                   }
                   printf(" %s=\"", attr);
                   switch (*fmt) {
                   case 'M':
                           print_href(h, s, va_arg(ap, char *), 1);
                           fmt++;
                           break;
                   case 'I':
                           print_href(h, s, NULL, 0);
                           fmt++;
                           break;
                   case 'R':
                           putchar('#');
                           fmt++;
                           /* FALLTHROUGH */
                   default:
                           print_encode(h, s, NULL, 1);
                           break;
                   }
                   putchar('"');
         }          }
   
         if (child && n->child)          /* Print out styles. */
                 print_mdoc_node(m, n->child, h);  
   
         switch (n->type) {          s = NULL;
         case (MDOC_ROOT):          su = &mysu;
                 mdoc_root_post(m, n, h);          while (*fmt != '\0') {
                 break;  
         case (MDOC_TEXT):                  /* First letter: input argument type. */
                 break;  
         default:                  switch (*fmt++) {
                 if (mdocs[n->tok].post)                  case 'h':
                         (*mdocs[n->tok].post)(m, n, h);                          i = va_arg(ap, int);
                 break;                          SCALE_HS_INIT(su, i);
                           break;
                   case 's':
                           s = va_arg(ap, char *);
                           break;
                   case 'u':
                           su = va_arg(ap, struct roffsu *);
                           break;
                   case 'v':
                           i = va_arg(ap, int);
                           SCALE_VS_INIT(su, i);
                           break;
                   case 'w':
                           s = va_arg(ap, char *);
                           a2width(s, su);
                           break;
                   default:
                           abort();
                   }
   
                   /* Second letter: style name. */
   
                   switch (*fmt++) {
                   case 'b':
                           attr = "margin-bottom";
                           break;
                   case 'h':
                           attr = "height";
                           break;
                   case 'i':
                           attr = "text-indent";
                           break;
                   case 'l':
                           attr = "margin-left";
                           break;
                   case 't':
                           attr = "margin-top";
                           break;
                   case 'w':
                           attr = "width";
                           break;
                   case 'W':
                           attr = "min-width";
                           break;
                   case '?':
                           printf("%s: %s;", s, va_arg(ap, char *));
                           continue;
                   default:
                           abort();
                   }
                   v = su->scale;
                   if (su->unit == SCALE_MM && (v /= 100.0) == 0.0)
                           v = 1.0;
                   else if (su->unit == SCALE_BU)
                           v /= 24.0;
                   printf("%s: %.2f%s;", attr, v, roffscales[su->unit]);
         }          }
           if (have_style)
                   putchar('"');
   
         if (n->next)          va_end(ap);
                 print_mdoc_node(m, n->next, h);  
 }  
   
           /* Accommodate for "well-formed" singleton escaping. */
   
 static void          if (HTML_AUTOCLOSE & htmltags[tag].flags)
 print_man(MAN_ARGS)                  putchar('/');
 {  
   
         print_otag(h, TAG_HEAD, 0, NULL);          putchar('>');
         print_man_head(m, n, h);  
         print_ctag(h, TAG_HEAD);  
         print_otag(h, TAG_BODY, 0, NULL);  
         print_man_body(m, n, h);  
         print_ctag(h, TAG_BODY);  
 }  
   
           h->flags |= HTML_NOSPACE;
   
 static void          if ((HTML_AUTOCLOSE | HTML_CLRLINE) & htmltags[tag].flags)
 print_man_head(MAN_ARGS)                  putchar('\n');
 {  
   
         print_gen_head(h);          return t;
         print_otag(h, TAG_TITLE, 0, NULL);  
         print_encode(m->title);  
         print_ctag(h, TAG_TITLE);  
 }  }
   
   
 static void  static void
 print_man_body(MAN_ARGS)  print_ctag(struct html *h, struct tag *tag)
 {  {
 }  
   
           /*
            * Remember to close out and nullify the current
            * meta-font and table, if applicable.
            */
           if (tag == h->metaf)
                   h->metaf = NULL;
           if (tag == h->tblt)
                   h->tblt = NULL;
   
 static void          printf("</%s>", htmltags[tag->tag].name);
 print_encode(const char *p)          if (HTML_CLRLINE & htmltags[tag->tag].flags) {
 {                  h->flags |= HTML_NOSPACE;
                   putchar('\n');
           }
   
         printf("%s", p); /* XXX */          h->tags.head = tag->next;
           free(tag);
 }  }
   
   void
   print_gen_decls(struct html *h)
   {
   
 static void          puts("<!DOCTYPE html>");
 print_otag(struct html *h, enum htmltag tag,  }
                 int sz, const struct htmlpair *p)  
   void
   print_text(struct html *h, const char *word)
 {  {
         int              i;  
   
         if ( ! (HTML_NOSPACE & h->flags))          if ( ! (HTML_NOSPACE & h->flags)) {
                 if ( ! (HTML_BLOCK & htmltags[tag].flags))                  /* Manage keeps! */
                         printf(" ");                  if ( ! (HTML_KEEP & h->flags)) {
                           if (HTML_PREKEEP & h->flags)
                                   h->flags |= HTML_KEEP;
                           putchar(' ');
                   } else
                           printf("&#160;");
           }
   
         printf("<%s", htmltags[tag].name);          assert(NULL == h->metaf);
         for (i = 0; i < sz; i++) {          switch (h->metac) {
                 printf(" %s=\"", htmlattrs[p[i].key]);          case HTMLFONT_ITALIC:
                 assert(p->val);                  h->metaf = print_otag(h, TAG_I, "");
                 print_encode(p[i].val);                  break;
                 printf("\"");          case HTMLFONT_BOLD:
                   h->metaf = print_otag(h, TAG_B, "");
                   break;
           case HTMLFONT_BI:
                   h->metaf = print_otag(h, TAG_B, "");
                   print_otag(h, TAG_I, "");
                   break;
           default:
                   break;
         }          }
         printf(">");  
   
         h->flags |= HTML_NOSPACE;          assert(word);
           if ( ! print_encode(h, word, NULL, 0)) {
                   if ( ! (h->flags & HTML_NONOSPACE))
                           h->flags &= ~HTML_NOSPACE;
                   h->flags &= ~HTML_NONEWLINE;
           } else
                   h->flags |= HTML_NOSPACE | HTML_NONEWLINE;
   
           if (h->metaf) {
                   print_tagq(h, h->metaf);
                   h->metaf = NULL;
           }
   
           h->flags &= ~HTML_IGNDELIM;
 }  }
   
   void
 /* ARGSUSED */  print_tagq(struct html *h, const struct tag *until)
 static void  
 print_ctag(struct html *h, enum htmltag tag)  
 {  {
           struct tag      *tag;
         printf("</%s>", htmltags[tag].name);  
         if (HTML_BLOCK & htmltags[tag].flags)          while ((tag = h->tags.head) != NULL) {
                 h->flags |= HTML_NOSPACE;                  print_ctag(h, tag);
                   if (until && tag == until)
                           return;
           }
 }  }
   
   void
 /* ARGSUSED */  print_stagq(struct html *h, const struct tag *suntil)
 static void  
 print_gen_doctype(struct html *h)  
 {  {
           struct tag      *tag;
         printf("<!DOCTYPE HTML PUBLIC \"%s\" \"%s\">\n", DOCTYPE, DTD);  
           while ((tag = h->tags.head) != NULL) {
                   if (suntil && tag == suntil)
                           return;
                   print_ctag(h, tag);
           }
 }  }
   
   void
 static void  print_paragraph(struct html *h)
 print_text(struct html *h, const char *p)  
 {  {
           struct tag      *t;
   
         if (*p && 0 == *(p + 1))          t = print_otag(h, TAG_DIV, "c", "spacer");
                 switch (*p) {          print_tagq(h, t);
                 case('.'):  }
                         /* FALLTHROUGH */  
                 case(','):  
                         /* FALLTHROUGH */  
                 case(';'):  
                         /* FALLTHROUGH */  
                 case(':'):  
                         /* FALLTHROUGH */  
                 case('?'):  
                         /* FALLTHROUGH */  
                 case('!'):  
                         /* FALLTHROUGH */  
                 case(')'):  
                         /* FALLTHROUGH */  
                 case(']'):  
                         /* FALLTHROUGH */  
                 case('}'):  
                         h->flags |= HTML_NOSPACE;  
                 default:  
                         break;  
                 }  
   
         if ( ! (h->flags & HTML_NOSPACE))  
                 printf(" ");  
         h->flags &= ~HTML_NOSPACE;  
   
         if (p)  /*
                 print_encode(p);   * Calculate the scaling unit passed in a `-width' argument.  This uses
    * either a native scaling unit (e.g., 1i, 2m) or the string length of
         if (*p && 0 == *(p + 1))   * the value.
                 switch (*p) {   */
                 case('('):  static void
                         /* FALLTHROUGH */  a2width(const char *p, struct roffsu *su)
                 case('['):  {
                         /* FALLTHROUGH */          if (a2roffsu(p, su, SCALE_MAX) < 2) {
                 case('{'):                  su->unit = SCALE_EN;
                         h->flags |= HTML_NOSPACE;                  su->scale = html_strlen(p);
                 default:          } else if (su->scale < 0.0)
                         break;                  su->scale = 0.0;
                 }  
 }  }

Legend:
Removed from v.1.29  
changed lines
  Added in v.1.195

CVSweb