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

Diff for /mandoc/html.c between version 1.24 and 1.196

version 1.24, 2008/12/10 13:41:58 version 1.196, 2017/01/18 19:22:21
Line 1 
Line 1 
 /* $Id$ */  /*      $Id$ */
 /*  /*
  * Copyright (c) 2008 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   * purpose with or without fee is hereby granted, provided that the above
  * above copyright notice and this permission notice appear in all   * copyright notice and this permission notice appear in all copies.
  * copies.  
  *   *
  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL   * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED   * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE   * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL   * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR   * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER   * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR   * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  * PERFORMANCE OF THIS SOFTWARE.  
  */   */
 #include <sys/param.h>  #include "config.h"
 #include <sys/stat.h>  
   
   #include <sys/types.h>
   
 #include <assert.h>  #include <assert.h>
 #include <err.h>  #include <ctype.h>
 #include <fcntl.h>  #include <stdarg.h>
 #include <stdlib.h>  
 #include <stdio.h>  #include <stdio.h>
   #include <stdint.h>
   #include <stdlib.h>
 #include <string.h>  #include <string.h>
 #include <unistd.h>  #include <unistd.h>
   
   #include "mandoc.h"
   #include "mandoc_aux.h"
   #include "out.h"
 #include "html.h"  #include "html.h"
 #include "ml.h"  #include "manconf.h"
   #include "main.h"
   
 /* TODO: allow head/tail-less invocations (just "div" start). */  struct  htmldata {
           const char       *name;
           int               flags;
   #define HTML_NOSTACK     (1 << 0)
   #define HTML_AUTOCLOSE   (1 << 1)
   #define HTML_NLBEFORE    (1 << 2)
   #define HTML_NLBEGIN     (1 << 3)
   #define HTML_NLEND       (1 << 4)
   #define HTML_NLAFTER     (1 << 5)
   #define HTML_NLAROUND    (HTML_NLBEFORE | HTML_NLAFTER)
   #define HTML_NLINSIDE    (HTML_NLBEGIN | HTML_NLEND)
   #define HTML_NLALL       (HTML_NLAROUND | HTML_NLINSIDE)
   #define HTML_INDENT      (1 << 6)
   #define HTML_NOINDENT    (1 << 7)
   };
   
 struct  htmlnode {  static  const struct htmldata htmltags[TAG_MAX] = {
         int              tok;          {"html",        HTML_NLALL},
         enum md_ns       ns;          {"head",        HTML_NLALL | HTML_INDENT},
         int              argc[ROFF_MAXLINEARG];          {"body",        HTML_NLALL},
         char            *argv[ROFF_MAXLINEARG];          {"meta",        HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
         struct htmlnode *parent;          {"title",       HTML_NLAROUND},
           {"div",         HTML_NLAROUND},
           {"h1",          HTML_NLAROUND},
           {"h2",          HTML_NLAROUND},
           {"span",        0},
           {"link",        HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
           {"br",          HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
           {"a",           0},
           {"table",       HTML_NLALL | HTML_INDENT},
           {"tbody",       HTML_NLALL | HTML_INDENT},
           {"col",         HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
           {"tr",          HTML_NLALL | HTML_INDENT},
           {"td",          HTML_NLAROUND},
           {"li",          HTML_NLAROUND | HTML_INDENT},
           {"ul",          HTML_NLALL | HTML_INDENT},
           {"ol",          HTML_NLALL | HTML_INDENT},
           {"dl",          HTML_NLALL | HTML_INDENT},
           {"dt",          HTML_NLAROUND},
           {"dd",          HTML_NLAROUND | HTML_INDENT},
           {"blockquote",  HTML_NLALL | HTML_INDENT},
           {"pre",         HTML_NLALL | HTML_NOINDENT},
           {"b",           0},
           {"i",           0},
           {"code",        0},
           {"small",       0},
           {"style",       HTML_NLALL | HTML_INDENT},
           {"math",        HTML_NLALL | HTML_INDENT},
           {"mrow",        0},
           {"mi",          0},
           {"mo",          0},
           {"msup",        0},
           {"msub",        0},
           {"msubsup",     0},
           {"mfrac",       0},
           {"msqrt",       0},
           {"mfenced",     0},
           {"mtable",      0},
           {"mtr",         0},
           {"mtd",         0},
           {"munderover",  0},
           {"munder",      0},
           {"mover",       0},
 };  };
   
 struct  htmlq {  static  const char      *const roffscales[SCALE_MAX] = {
         struct htmlnode *last;          "cm", /* SCALE_CM */
           "in", /* SCALE_IN */
           "pc", /* SCALE_PC */
           "pt", /* SCALE_PT */
           "em", /* SCALE_EM */
           "em", /* SCALE_MM */
           "ex", /* SCALE_EN */
           "ex", /* SCALE_BU */
           "em", /* SCALE_VS */
           "ex", /* SCALE_FS */
 };  };
   
   static  void     a2width(const char *, struct roffsu *);
   static  void     html_endline(struct html *);
   static  void     html_indent(struct html *);
   static  void     print_ctag(struct html *, struct tag *);
   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);
   
 static  int             html_loadcss(struct md_mbuf *,  
                                 const char *);  
 static  int             html_alloc(void **);  
 static  void            html_free(void *);  
 static  ssize_t         html_endtag(struct md_mbuf *, void *,  
                                 const struct md_args *,  
                                 enum md_ns, int);  
 static  ssize_t         html_beginstring(struct md_mbuf *,  
                                 const struct md_args *,  
                                 const char *, size_t);  
 static  ssize_t         html_beginhttp(struct md_mbuf *,  
                                 const struct md_args *,  
                                 const char *, size_t);  
 static  ssize_t         html_endstring(struct md_mbuf *,  
                                 const struct md_args *,  
                                 const char *, size_t);  
 static  ssize_t         html_endhttp(struct md_mbuf *,  
                                 const struct md_args *,  
                                 const char *, size_t);  
 static  ssize_t         html_begintag(struct md_mbuf *, void *,  
                                 const struct md_args *,  
                                 enum md_ns, int,  
                                 const int *, const char **);  
 static  int             html_begin(struct md_mbuf *,  
                                 const struct md_args *,  
                                 const struct tm *,  
                                 const char *, const char *,  
                                 enum roffmsec, enum roffvol);  
 static  int             html_printargs(struct md_mbuf *, int,  
                                 const char *, const int *,  
                                 const char **, size_t *);  
 static  int             html_end(struct md_mbuf *,  
                                 const struct md_args *,  
                                 const struct tm *,  
                                 const char *, const char *,  
                                 enum roffmsec, enum roffvol);  
 static  int             html_blocktagname(struct md_mbuf *,  
                                 const struct md_args *, int,  
                                 struct htmlq *, const int *,  
                                 const char **, size_t *);  
 static  int             html_blocktagargs(struct md_mbuf *,  
                                 const struct md_args *, int,  
                                 const int *, const char **, size_t *);  
 static  int             html_headtagname(struct md_mbuf *,  
                                 const struct md_args *, int,  
                                 struct htmlq *, const int *,  
                                 const char **, size_t *);  
 static  int             html_headtagargs(struct md_mbuf *,  
                                 const struct md_args *, int,  
                                 const int *, const char **, size_t *);  
 static  int             html_bodytagname(struct md_mbuf *,  
                                 const struct md_args *,  
                                 int, struct htmlq *, const int *,  
                                 const char **, size_t *);  
 static  int             html_bodytagargs(struct md_mbuf *,  
                                 const struct md_args *, int,  
                                 const int *, const char **, size_t *);  
 static  int             html_inlinetagname(struct md_mbuf *,  
                                 const struct md_args *, int, size_t *);  
 static  int             html_inlinetagargs(struct md_mbuf *,  
                                 const struct md_args *, int,  
                                 const int *, const char **, size_t *);  
 static  int             html_Bl_bodytagname(struct md_mbuf *,  
                                 struct htmlq *, const int *,  
                                 const char **, size_t *);  
 static  int             html_It_blocktagname(struct md_mbuf *,  
                                 struct htmlq *, const int *,  
                                 const char **, size_t *);  
 static  int             html_It_headtagname(struct md_mbuf *,  
                                 struct htmlq *, const int *,  
                                 const char **, size_t *);  
 static  int             html_It_bodytagname(struct md_mbuf *,  
                                 struct htmlq *, const int *,  
                                 const char **, size_t *);  
 static  int             html_tputln(struct md_mbuf *,  
                                 enum ml_scope, int, enum html_tag);  
 static  int             html_aputln(struct md_mbuf *, enum ml_scope,  
                                 int, enum html_tag,  
                                 int, const struct html_pair *);  
   
   void *
 /* ARGSUSED */  html_alloc(const struct manoutput *outopts)
 static int  
 html_It_headtagname(struct md_mbuf *mbuf, struct htmlq *q,  
                 const int *argc, const char **argv, size_t *res)  
 {  {
         struct htmlnode *n;          struct html     *h;
         int              i;  
   
         for (n = q->last; n; n = n->parent)          h = mandoc_calloc(1, sizeof(struct html));
                 if (n->tok == ROFF_Bl)  
                         break;  
   
         assert(n);          h->tags.head = NULL;
           h->style = outopts->style;
           h->base_man = outopts->man;
           h->base_includes = outopts->includes;
           if (outopts->fragment)
                   h->oflags |= HTML_FRAGMENT;
   
         /* LINTED */          return h;
         for (i = 0; ROFF_ARGMAX != n->argc[i] &&  }
                         i < ROFF_MAXLINEARG; i++) {  
                 switch (n->argc[i]) {  void
                 case (ROFF_Ohang):  html_free(void *p)
                         return(html_stput(mbuf, HTML_TAG_DIV, res));  {
                 case (ROFF_Tag):          struct tag      *tag;
                         /* FALLTHROUGH */          struct html     *h;
                 case (ROFF_Column):  
                         return(html_stput(mbuf, HTML_TAG_TD, res));          h = (struct html *)p;
                 default:  
                         break;          while ((tag = h->tags.head) != NULL) {
                 }                  h->tags.head = tag->next;
                   free(tag);
         }          }
   
         return(0);          free(h);
 }  }
   
   void
 /* ARGSUSED */  print_gen_head(struct html *h)
 static int  
 html_It_bodytagname(struct md_mbuf *mbuf, struct htmlq *q,  
                 const int *argc, const char **argv, size_t *res)  
 {  {
         struct htmlnode *n;          struct tag      *t;
         int              i;  
   
         for (n = q->last; n; n = n->parent)          print_otag(h, TAG_META, "?", "charset", "utf-8");
                 if (n->tok == ROFF_Bl)  
                         break;  
   
         assert(n);          /*
            * Print a default style-sheet.
            */
   
         /* LINTED */          t = print_otag(h, TAG_STYLE, "");
         for (i = 0; ROFF_ARGMAX != n->argc[i] &&          print_text(h, "table.head, table.foot { width: 100%; }");
                         i < ROFF_MAXLINEARG; i++) {          html_endline(h);
                 switch (n->argc[i]) {          print_text(h, "td.head-rtitle, td.foot-os { text-align: right; }");
                 case (ROFF_Enum):          html_endline(h);
                         /* FALLTHROUGH */          print_text(h, "td.head-vol { text-align: center; }");
                 case (ROFF_Bullet):          html_endline(h);
                         /* FALLTHROUGH */          print_text(h, "table.foot td { width: 50%; }");
                 case (ROFF_Dash):          html_endline(h);
                         /* FALLTHROUGH */          print_text(h, "table.head td { width: 33%; }");
                 case (ROFF_Hyphen):          html_endline(h);
                         /* FALLTHROUGH */          print_text(h, "div.spacer { margin: 1em 0; }");
                 case (ROFF_Item):          print_tagq(h, t);
                         /* FALLTHROUGH */  
                 case (ROFF_Diag):  
                         /* FALLTHROUGH */  
                 case (ROFF_Hang):  
                         /* FALLTHROUGH */  
                 case (ROFF_Ohang):  
                         /* FALLTHROUGH */  
                 case (ROFF_Inset):  
                         return(html_stput(mbuf, HTML_TAG_DIV, res));  
                 case (ROFF_Tag):  
                         /* FALLTHROUGH */  
                 case (ROFF_Column):  
                         return(html_stput(mbuf, HTML_TAG_TD, res));  
                 default:  
                         break;  
                 }  
         }  
   
         assert(i != ROFF_MAXLINEARG);          if (h->style)
         return(0);                  print_otag(h, TAG_LINK, "?h??", "rel", "stylesheet",
                       h->style, "type", "text/css", "media", "all");
 }  }
   
   static void
 /* ARGSUSED */  print_metaf(struct html *h, enum mandoc_esc deco)
 static int  
 html_Bl_bodytagname(struct md_mbuf *mbuf, struct htmlq *q,  
                 const int *argc, const char **argv, size_t *res)  
 {  {
         int              i;          enum htmlfont    font;
   
         for (i = 0; ROFF_ARGMAX != argc[i]          switch (deco) {
                         && i < ROFF_MAXLINEARG; i++) {          case ESCAPE_FONTPREV:
                 switch (argc[i]) {                  font = h->metal;
                 case (ROFF_Enum):                  break;
                         return(html_stput(mbuf, HTML_TAG_OL, res));          case ESCAPE_FONTITALIC:
                 case (ROFF_Bullet):                  font = HTMLFONT_ITALIC;
                         /* FALLTHROUGH */                  break;
                 case (ROFF_Dash):          case ESCAPE_FONTBOLD:
                         /* FALLTHROUGH */                  font = HTMLFONT_BOLD;
                 case (ROFF_Hyphen):                  break;
                         /* FALLTHROUGH */          case ESCAPE_FONTBI:
                 case (ROFF_Item):                  font = HTMLFONT_BI;
                         /* FALLTHROUGH */                  break;
                 case (ROFF_Diag):          case ESCAPE_FONT:
                         /* FALLTHROUGH */          case ESCAPE_FONTROMAN:
                 case (ROFF_Hang):                  font = HTMLFONT_NONE;
                         /* FALLTHROUGH */                  break;
                 case (ROFF_Ohang):          default:
                         /* FALLTHROUGH */                  abort();
                 case (ROFF_Inset):  
                         return(html_stput(mbuf, HTML_TAG_UL, res));  
                 case (ROFF_Tag):  
                         /* FALLTHROUGH */  
                 case (ROFF_Column):  
                         return(html_stput(mbuf, HTML_TAG_TABLE, res));  
                 default:  
                         break;  
                 }  
         }          }
   
         assert(i != ROFF_MAXLINEARG);          if (h->metaf) {
         return(0);                  print_tagq(h, h->metaf);
 }                  h->metaf = NULL;
           }
   
           h->metal = h->metac;
           h->metac = font;
   
 /* ARGSUSED */          switch (font) {
 static int          case HTMLFONT_ITALIC:
 html_It_blocktagname(struct md_mbuf *mbuf, struct htmlq *q,                  h->metaf = print_otag(h, TAG_I, "");
                 const int *argc, const char **argv, size_t *res)                  break;
           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
   html_strlen(const char *cp)
 {  {
         struct htmlnode *n;          size_t           rsz;
         int              i;          int              skip, sz;
   
         for (n = q->last; n; n = n->parent)          /*
                 if (n->tok == ROFF_Bl)           * Account for escaped sequences within string length
                         break;           * 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.
            */
   
         assert(n);          sz = 0;
           skip = 0;
         /* LINTED */          while (1) {
         for (i = 0; ROFF_ARGMAX != n->argc[i] &&                  rsz = strcspn(cp, "\\");
                         i < ROFF_MAXLINEARG; i++) {                  if (rsz) {
                 switch (n->argc[i]) {                          cp += rsz;
                 case (ROFF_Enum):                          if (skip) {
                         /* FALLTHROUGH */                                  skip = 0;
                 case (ROFF_Bullet):                                  rsz--;
                         /* FALLTHROUGH */                          }
                 case (ROFF_Dash):                          sz += rsz;
                         /* FALLTHROUGH */                  }
                 case (ROFF_Hyphen):                  if ('\0' == *cp)
                         /* FALLTHROUGH */                          break;
                 case (ROFF_Item):                  cp++;
                         /* FALLTHROUGH */                  switch (mandoc_escape(&cp, NULL, NULL)) {
                 case (ROFF_Diag):                  case ESCAPE_ERROR:
                         /* FALLTHROUGH */                          return sz;
                 case (ROFF_Hang):                  case ESCAPE_UNICODE:
                         /* FALLTHROUGH */                  case ESCAPE_NUMBERED:
                 case (ROFF_Ohang):                  case ESCAPE_SPECIAL:
                         /* FALLTHROUGH */                  case ESCAPE_OVERSTRIKE:
                 case (ROFF_Inset):                          if (skip)
                         return(html_stput(mbuf, HTML_TAG_LI, res));                                  skip = 0;
                 case (ROFF_Tag):                          else
                         /* FALLTHROUGH */                                  sz++;
                 case (ROFF_Column):                          break;
                         return(html_stput(mbuf, HTML_TAG_TR, res));                  case ESCAPE_SKIPCHAR:
                           skip = 1;
                           break;
                 default:                  default:
                         break;                          break;
                 }                  }
         }          }
           return sz;
         assert(i != ROFF_MAXLINEARG);  
         return(0);  
 }  }
   
   
 static int  static int
 html_loadcss(struct md_mbuf *mbuf, const char *css)  print_escape(char c)
 {  {
         size_t           res, bufsz;  
         char            *buf;  
         struct stat      st;  
         int              fd, c;  
         ssize_t          ssz;  
   
         c = 0;          switch (c) {
         res = 0;          case '<':
         buf = NULL;                  printf("&lt;");
                   break;
         if (-1 == (fd = open(css, O_RDONLY, 0))) {          case '>':
                 warn("%s", css);                  printf("&gt;");
                 return(0);                  break;
         }          case '&':
                   printf("&amp;");
         if (-1 == fstat(fd, &st)) {                  break;
                 warn("%s", css);          case '"':
                 goto out;                  printf("&quot;");
                   break;
           case ASCII_NBRSP:
                   printf("&nbsp;");
                   break;
           case ASCII_HYPH:
                   putchar('-');
                   break;
           case ASCII_BREAK:
                   break;
           default:
                   return 0;
         }          }
           return 1;
         bufsz = MAX(st.st_blksize, BUFSIZ);  
         if (NULL == (buf = malloc(bufsz))) {  
                 warn("malloc");  
                 goto out;  
         }  
   
         for (;;) {  
                 if (-1 == (ssz = read(fd, buf, bufsz))) {  
                         warn("%s", css);  
                         goto out;  
                 } else if (0 == ssz)  
                         break;  
                 if ( ! ml_nputs(mbuf, buf, (size_t)ssz, &res))  
                         goto out;  
         }  
   
         c = 1;  
   
 out:  
         if (-1 == close(fd)) {  
                 warn("%s", css);  
                 c = 0;  
         }  
   
         if (buf)  
                 free(buf);  
   
         return(c);  
 }  }
   
   
 static int  static int
 html_tputln(struct md_mbuf *mbuf, enum ml_scope scope,  print_encode(struct html *h, const char *p, const char *pend, int norecurse)
                 int i, enum html_tag tag)  
 {  {
           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 ( ! ml_putchars(mbuf, ' ', INDENT(i) * INDENT_SZ, NULL))          if (pend == NULL)
                 return(0);                  pend = strchr(p, '\0');
         if ( ! html_tput(mbuf, scope, tag, NULL))  
                 return(0);  
         return(ml_nputs(mbuf, "\n", 1, NULL));  
 }  
   
           nospace = 0;
   
 static int          while (p < pend) {
 html_aputln(struct md_mbuf *mbuf, enum ml_scope scope, int i,                  if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
                 enum html_tag tag, int sz, const struct html_pair *p)                          h->flags &= ~HTML_SKIPCHAR;
 {                          p++;
                           continue;
                   }
   
         if ( ! ml_putchars(mbuf, ' ', INDENT(i) * INDENT_SZ, NULL))                  sz = strcspn(p, rejs);
                 return(0);                  if (p + sz > pend)
         if ( ! html_aput(mbuf, scope, tag, NULL, sz, p))                          sz = pend - p;
                 return(0);  
         return(ml_nputs(mbuf, "\n", 1, NULL));  
 }  
   
                   fwrite(p, 1, sz, stdout);
                   p += (int)sz;
   
 /* ARGSUSED */                  if (p >= pend)
 static int                          break;
 html_begin(struct md_mbuf *mbuf, const struct md_args *args,  
                 const struct tm *tm, const char *os,  
                 const char *name, enum roffmsec msec, enum roffvol vol)  
 {  
         enum roffvol     bvol;  
         struct html_pair attr[4];  
         char             ts[32], title[64];  
         int              i;  
   
         (void)snprintf(ts, sizeof(ts), "%s(%s)",                  if (print_escape(*p++))
                         name, roff_msecname(msec));                          continue;
   
         if (vol >= ROFF_ARCH_START) {                  esc = mandoc_escape(&p, &seq, &len);
                 switch (msec) {                  if (ESCAPE_ERROR == esc)
                 case(ROFF_MSEC_1):  
                         /* FALLTHROUGH */  
                 case(ROFF_MSEC_6):  
                         /* FALLTHROUGH */  
                 case(ROFF_MSEC_7):  
                         bvol = ROFF_VOL_URM;  
                         break;                          break;
                 case(ROFF_MSEC_2):  
                         /* FALLTHROUGH */                  switch (esc) {
                 case(ROFF_MSEC_3):                  case ESCAPE_FONT:
                         /* FALLTHROUGH */                  case ESCAPE_FONTPREV:
                 case(ROFF_MSEC_3p):                  case ESCAPE_FONTBOLD:
                         /* FALLTHROUGH */                  case ESCAPE_FONTITALIC:
                 case(ROFF_MSEC_4):                  case ESCAPE_FONTBI:
                         /* FALLTHROUGH */                  case ESCAPE_FONTROMAN:
                 case(ROFF_MSEC_5):                          if (0 == norecurse)
                         bvol = ROFF_VOL_PRM;                                  print_metaf(h, esc);
                           continue;
                   case ESCAPE_SKIPCHAR:
                           h->flags |= HTML_SKIPCHAR;
                           continue;
                   default:
                         break;                          break;
                 case(ROFF_MSEC_8):                  }
                         bvol = ROFF_VOL_PRM;  
                   if (h->flags & HTML_SKIPCHAR) {
                           h->flags &= ~HTML_SKIPCHAR;
                           continue;
                   }
   
                   switch (esc) {
                   case ESCAPE_UNICODE:
                           /* Skip past "u" header. */
                           c = mchars_num2uc(seq + 1, len - 1);
                         break;                          break;
                 case(ROFF_MSEC_9):                  case ESCAPE_NUMBERED:
                         bvol = ROFF_VOL_KM;                          c = mchars_num2char(seq, len);
                           if (c < 0)
                                   continue;
                         break;                          break;
                 case(ROFF_MSEC_UNASS):                  case ESCAPE_SPECIAL:
                         /* FALLTHROUGH */                          c = mchars_spec2cp(seq, len);
                 case(ROFF_MSEC_DRAFT):                          if (c <= 0)
                         /* FALLTHROUGH */                                  continue;
                 case(ROFF_MSEC_PAPER):  
                         bvol = ROFF_VOL_NONE;  
                         break;                          break;
                   case ESCAPE_NOSPACE:
                           if ('\0' == *p)
                                   nospace = 1;
                           continue;
                   case ESCAPE_OVERSTRIKE:
                           if (len == 0)
                                   continue;
                           c = seq[len - 1];
                           break;
                 default:                  default:
                         abort();                          continue;
                         /* NOTREACHED */  
                 }                  }
                   if ((c < 0x20 && c != 0x09) ||
                 (void)snprintf(title, sizeof(title), "%s (%s)",                      (c > 0x7E && c < 0xA0))
                                 roff_volname(bvol), roff_volname(vol));                          c = 0xFFFD;
         } else                  if (c > 0x7E)
                 (void)snprintf(title, sizeof(title), "%s", roff_volname(vol));                          printf("&#%d;", c);
                   else if ( ! print_escape(c))
                           putchar(c);
         i = 0;  
   
         if ( ! html_typeput(mbuf, HTML_TYPE_4_01_STRICT, NULL))  
                 return(0);  
         if ( ! html_tputln(mbuf, ML_OPEN, i, HTML_TAG_HTML))  
                 return(0);  
         if ( ! html_tputln(mbuf, ML_OPEN, i++, HTML_TAG_HEAD))  
                 return(0);  
   
         attr[0].attr = HTML_ATTR_HTTP_EQUIV;  
         attr[0].val = "content-type";  
         attr[1].attr = HTML_ATTR_CONTENT;  
         attr[1].val = "text/html;charset=utf-8";  
   
         if ( ! html_aputln(mbuf, ML_OPEN, i, HTML_TAG_META, 2, attr))  
                 return(0);  
   
         attr[0].attr = HTML_ATTR_NAME;  
         attr[0].val = "resource-type";  
         attr[1].attr = HTML_ATTR_CONTENT;  
         attr[1].val = "document";  
   
         if ( ! html_aputln(mbuf, ML_OPEN, i, HTML_TAG_META, 2, attr))  
                 return(0);  
   
         if ( ! html_tputln(mbuf, ML_OPEN, i, HTML_TAG_TITLE))  
                 return(0);  
         if ( ! ml_putstring(mbuf, ts, NULL))  
                 return(0);  
         if ( ! html_tputln(mbuf, ML_CLOSE, i, HTML_TAG_TITLE))  
                 return(0);  
   
         if (HTML_CSS_EMBED & args->params.html.flags) {  
                 attr[0].attr = HTML_ATTR_TYPE;  
                 attr[0].val = "text/css";  
   
                 if ( ! html_aputln(mbuf, ML_OPEN, i,  
                                         HTML_TAG_STYLE, 1, attr))  
                         return(0);  
                 if ( ! html_commentput(mbuf, ML_OPEN, NULL))  
                         return(0);  
   
                 if ( ! html_loadcss(mbuf, args->params.html.css))  
                         return(0);  
   
                 if ( ! html_commentput(mbuf, ML_CLOSE, NULL))  
                         return(0);  
                 if ( ! html_tputln(mbuf, ML_CLOSE, i, HTML_TAG_STYLE))  
                         return(0);  
         } else {  
                 attr[0].attr = HTML_ATTR_REL;  
                 attr[0].val = "stylesheet";  
                 attr[1].attr = HTML_ATTR_TYPE;  
                 attr[1].val = "text/css";  
                 attr[2].attr = HTML_ATTR_HREF;  
                 attr[2].val = args->params.html.css;  
   
                 if ( ! html_aputln(mbuf, ML_OPEN, i,  
                                         HTML_TAG_LINK, 3, attr))  
                         return(0);  
         }          }
   
         if ( ! html_tputln(mbuf, ML_CLOSE, --i, HTML_TAG_HEAD))          return nospace;
                 return(0);  
         if ( ! html_tputln(mbuf, ML_OPEN, i, HTML_TAG_BODY))  
                 return(0);  
   
         attr[0].attr = HTML_ATTR_CLASS;  
         attr[0].val = "mdoc";  
   
         if ( ! html_aputln(mbuf, ML_OPEN, i, HTML_TAG_DIV, 1, attr))  
                 return(0);  
   
         attr[0].attr = HTML_ATTR_WIDTH;  
         attr[0].val = "100%";  
         attr[1].attr = HTML_ATTR_CLASS;  
         attr[1].val = "header-table";  
   
         if ( ! html_aputln(mbuf, ML_OPEN, i++, HTML_TAG_TABLE, 2, attr))  
                 return(0);  
         if ( ! html_tputln(mbuf, ML_OPEN, i++, HTML_TAG_TR))  
                 return(0);  
   
         attr[0].attr = HTML_ATTR_ALIGN;  
         attr[0].val = "left";  
         attr[1].attr = HTML_ATTR_CLASS;  
         attr[1].val = "header-section";  
   
         if ( ! html_aputln(mbuf, ML_OPEN, i, HTML_TAG_TD, 2, attr))  
                 return(0);  
         if ( ! ml_putstring(mbuf, ts, NULL))  
                 return(0);  
         if ( ! html_tputln(mbuf, ML_CLOSE, i, HTML_TAG_TD))  
                 return(0);  
   
         attr[0].attr = HTML_ATTR_ALIGN;  
         attr[0].val = "center";  
         attr[1].attr = HTML_ATTR_CLASS;  
         attr[1].val = "header-volume";  
   
         if ( ! html_aputln(mbuf, ML_OPEN, i, HTML_TAG_TD, 2, attr))  
                 return(0);  
         if ( ! ml_putstring(mbuf, title, NULL))  
                 return(0);  
         if ( ! html_tputln(mbuf, ML_CLOSE, i, HTML_TAG_TD))  
                 return(0);  
   
         attr[0].attr = HTML_ATTR_ALIGN;  
         attr[0].val = "right";  
         attr[1].attr = HTML_ATTR_CLASS;  
         attr[1].val = "header-section";  
   
         if ( ! html_aputln(mbuf, ML_OPEN, i, HTML_TAG_TD, 2, attr))  
                 return(0);  
         if ( ! ml_putstring(mbuf, ts, NULL))  
                 return(0);  
         if ( ! html_tputln(mbuf, ML_CLOSE, i, HTML_TAG_TD))  
                 return(0);  
   
         if ( ! html_tputln(mbuf, ML_CLOSE, --i, HTML_TAG_TR))  
                 return(0);  
         return(html_tputln(mbuf, ML_CLOSE, --i, HTML_TAG_TABLE));  
 }  }
   
   static void
 /* ARGSUSED */  print_href(struct html *h, const char *name, const char *sec, int man)
 static int  
 html_end(struct md_mbuf *mbuf, const struct md_args *args,  
                 const struct tm *tm, const char *os,  
                 const char *name, enum roffmsec msec, enum roffvol vol)  
 {  {
         struct html_pair attr[4];          const char      *p, *pp;
         int              i;  
         char             ts[64];  
   
         if (0 == strftime(ts, sizeof(ts), "%B %d, %Y", tm)) {          pp = man ? h->base_man : h->base_includes;
                 warn("strftime");          while ((p = strchr(pp, '%')) != NULL) {
                 return(0);                  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')
         i = 0;                  print_encode(h, pp, NULL, 1);
   
         attr[0].attr = HTML_ATTR_WIDTH;  
         attr[0].val = "100%";  
         attr[1].attr = HTML_ATTR_CLASS;  
         attr[1].val = "header-footer";  
   
         if ( ! html_aputln(mbuf, ML_OPEN, i++, HTML_TAG_TABLE, 2, attr))  
                 return(0);  
         if ( ! html_tputln(mbuf, ML_OPEN, i++, HTML_TAG_TR))  
                 return(0);  
   
         attr[0].attr = HTML_ATTR_ALIGN;  
         attr[0].val = "left";  
         attr[1].attr = HTML_ATTR_CLASS;  
         attr[1].val = "footer-os";  
   
         if ( ! html_aputln(mbuf, ML_OPEN, i, HTML_TAG_TD, 2, attr))  
                 return(0);  
         if ( ! ml_putstring(mbuf, os, NULL))  
                 return(0);  
         if ( ! html_tputln(mbuf, ML_CLOSE, i, HTML_TAG_TD))  
                 return(0);  
   
         attr[0].attr = HTML_ATTR_ALIGN;  
         attr[0].val = "right";  
         attr[1].attr = HTML_ATTR_CLASS;  
         attr[1].val = "footer-date";  
   
         if ( ! html_aputln(mbuf, ML_OPEN, i, HTML_TAG_TD, 2, attr))  
                 return(0);  
         if ( ! ml_putstring(mbuf, ts, NULL))  
                 return(0);  
         if ( ! html_tputln(mbuf, ML_CLOSE, i, HTML_TAG_TD))  
                 return(0);  
   
         if ( ! html_tputln(mbuf, ML_CLOSE, --i, HTML_TAG_TR))  
                 return(0);  
         if ( ! html_tputln(mbuf, ML_CLOSE, --i, HTML_TAG_TABLE))  
                 return(0);  
   
         if ( ! html_tputln(mbuf, ML_CLOSE, 0, HTML_TAG_DIV))  
                 return(0);  
         if ( ! html_tputln(mbuf, ML_CLOSE, 0, HTML_TAG_BODY))  
                 return(0);  
         return(html_tputln(mbuf, ML_CLOSE, 0, HTML_TAG_HTML));  
 }  }
   
   struct tag *
 /* ARGSUSED */  print_otag(struct html *h, enum htmltag tag, const char *fmt, ...)
 static int  
 html_bodytagname(struct md_mbuf *mbuf,  
                 const struct md_args *args, int tok, struct htmlq *q,  
                 const int *argc, const char **argv, size_t *res)  
 {  {
           va_list          ap;
           struct roffsu    mysu, *su;
           struct tag      *t;
           const char      *attr;
           char            *s;
           double           v;
           int              i, have_style, tflags;
   
         switch (tok) {          tflags = htmltags[tag].flags;
         case (ROFF_Bl):  
                 return(html_Bl_bodytagname(mbuf, q, argc, argv, res));  
         case (ROFF_Fo):  
                 return(html_stput(mbuf, HTML_TAG_SPAN, res));  
         case (ROFF_It):  
                 return(html_It_bodytagname(mbuf, q, argc, argv, res));  
         case (ROFF_Oo):  
                 return(html_stput(mbuf, HTML_TAG_SPAN, res));  
         default:  
                 break;  
         }  
   
         return(html_stput(mbuf, HTML_TAG_DIV, res));          /* Push this tags onto the stack of open scopes. */
 }  
   
           if ((tflags & HTML_NOSTACK) == 0) {
                   t = mandoc_malloc(sizeof(struct tag));
                   t->tag = tag;
                   t->next = h->tags.head;
                   h->tags.head = t;
           } else
                   t = NULL;
   
 /* ARGSUSED */          if (tflags & HTML_NLBEFORE)
 static int                  html_endline(h);
 html_headtagname(struct md_mbuf *mbuf,          if (h->flags & HTML_NLDONE)
                 const struct md_args *args, int tok, struct htmlq *q,                  html_indent(h);
                 const int *argc, const char **argv, size_t *res)          else if ((h->flags & HTML_NOSPACE) == 0) {
 {                  if (h->flags & HTML_KEEP)
                           printf("&#160;");
         switch (tok) {                  else {
         case (ROFF_It):                          if (h->flags & HTML_PREKEEP)
                 return(html_It_headtagname(mbuf, q, argc, argv, res));                                  h->flags |= HTML_KEEP;
         case (ROFF_Fo):                          putchar(' ');
                 /* FALLTHROUGH */                  }
         case (ROFF_Oo):  
                 return(html_stput(mbuf, HTML_TAG_SPAN, res));  
         case (ROFF_Sh):  
                 return(html_stput(mbuf, HTML_TAG_H1, res));  
         case (ROFF_Ss):  
                 return(html_stput(mbuf, HTML_TAG_H2, res));  
         default:  
                 break;  
         }          }
   
         return(html_stput(mbuf, HTML_TAG_DIV, res));          if ( ! (h->flags & HTML_NONOSPACE))
 }                  h->flags &= ~HTML_NOSPACE;
           else
                   h->flags |= HTML_NOSPACE;
   
           /* Print out the tag name and attributes. */
   
 /* ARGSUSED */          printf("<%s", htmltags[tag].name);
 static int  
 html_blocktagname(struct md_mbuf *mbuf, const struct md_args *args,  
                 int tok, struct htmlq *q, const int *argc,  
                 const char **argv, size_t *res)  
 {  
   
         switch (tok) {          va_start(ap, fmt);
         case (ROFF_Fo):  
                 /* FALLTHROUGH */          have_style = 0;
         case (ROFF_Oo):          while (*fmt != '\0') {
                 return(html_stput(mbuf, HTML_TAG_SPAN, res));                  if (*fmt == 's') {
         case (ROFF_It):                          printf(" style=\"");
                 return(html_It_blocktagname(mbuf, q, argc, argv, res));                          have_style = 1;
         default:                          fmt++;
                 break;                          break;
                   }
                   s = va_arg(ap, char *);
                   switch (*fmt++) {
                   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('"');
         }          }
   
         return(html_stput(mbuf, HTML_TAG_DIV, res));          /* Print out styles. */
 }  
   
           s = NULL;
           su = &mysu;
           while (*fmt != '\0') {
   
 /* ARGSUSED */                  /* First letter: input argument type. */
 static int  
 html_printargs(struct md_mbuf *mbuf, int tok, const char *ns,  
                 const int *argc, const char **argv, size_t *res)  
 {  
   
         /* FIXME: use API in ml.h. */                  switch (*fmt++) {
                   case 'h':
                           i = va_arg(ap, int);
                           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();
                   }
   
         if ( ! ml_puts(mbuf, " class=\"", res))                  /* Second letter: style name. */
                 return(0);  
         if ( ! ml_puts(mbuf, ns, res))  
                 return(0);  
         if ( ! ml_puts(mbuf, "-", res))  
                 return(0);  
         if ( ! ml_puts(mbuf, toknames[tok], res))  
                 return(0);  
         return(ml_puts(mbuf, "\"", res));  
 }  
   
                   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('"');
   
 /* ARGSUSED */          va_end(ap);
 static int  
 html_headtagargs(struct md_mbuf *mbuf,  
                 const struct md_args *args, int tok,  
                 const int *argc, const char **argv, size_t *res)  
 {  
   
         return(html_printargs(mbuf, tok, "head", argc, argv, res));          /* Accommodate for "well-formed" singleton escaping. */
 }  
   
           if (HTML_AUTOCLOSE & htmltags[tag].flags)
                   putchar('/');
   
 /* ARGSUSED */          putchar('>');
 static int  
 html_bodytagargs(struct md_mbuf *mbuf,  
                 const struct md_args *args, int tok,  
                 const int *argc, const char **argv, size_t *res)  
 {  
   
         return(html_printargs(mbuf, tok, "body", argc, argv, res));          if (tflags & HTML_NLBEGIN)
 }                  html_endline(h);
           else
                   h->flags |= HTML_NOSPACE;
   
           if (tflags & HTML_INDENT)
                   h->indent++;
           if (tflags & HTML_NOINDENT)
                   h->noindent++;
   
 /* ARGSUSED */          return t;
 static int  
 html_blocktagargs(struct md_mbuf *mbuf,  
                 const struct md_args *args, int tok,  
                 const int *argc, const char **argv, size_t *res)  
 {  
   
         return(html_printargs(mbuf, tok, "block", argc, argv, res));  
 }  }
   
   static void
 /* ARGSUSED */  print_ctag(struct html *h, struct tag *tag)
 static int  
 html_inlinetagargs(struct md_mbuf *mbuf,  
                 const struct md_args *args, int tok,  
                 const int *argc, const char **argv, size_t *res)  
 {  {
           int      tflags;
   
         if ( ! html_printargs(mbuf, tok, "inline", argc, argv, res))          /*
                 return(0);           * 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;
   
         switch (tok) {          tflags = htmltags[tag->tag].flags;
         case (ROFF_Sh):  
   
                 /* FIXME: use API in ml.h. */          if (tflags & HTML_INDENT)
                   h->indent--;
           if (tflags & HTML_NOINDENT)
                   h->noindent--;
           if (tflags & HTML_NLEND)
                   html_endline(h);
           html_indent(h);
           printf("</%s>", htmltags[tag->tag].name);
           if (tflags & HTML_NLAFTER)
                   html_endline(h);
   
                 assert(*argv);          h->tags.head = tag->next;
                 if ( ! ml_nputs(mbuf, " name=\"", 7, res))          free(tag);
                         return(0);  
                 if ( ! ml_putstring(mbuf, *argv++, res))  
                         return(0);  
                 while (*argv) {  
                         if ( ! ml_putstring(mbuf, "_", res))  
                                 return(0);  
                         if ( ! ml_putstring(mbuf, *argv++, res))  
                                 return(0);  
                 }  
                 if ( ! ml_nputs(mbuf, "\"", 1, res))  
                         return(0);  
                 break;  
   
         case (ROFF_Sx):  
   
                 /* FIXME: use API in ml.h. */  
   
                 assert(*argv);  
                 if ( ! ml_nputs(mbuf, " href=\"#", 8, res))  
                         return(0);  
                 if ( ! ml_putstring(mbuf, *argv++, res))  
                         return(0);  
                 while (*argv) {  
                         if ( ! ml_putstring(mbuf, "_", res))  
                                 return(0);  
                         if ( ! ml_putstring(mbuf, *argv++, res))  
                                 return(0);  
                 }  
                 if ( ! ml_nputs(mbuf, "\"", 1, res))  
                         return(0);  
   
                 break;  
         default:  
                 break;  
         }  
   
         return(1);  
 }  }
   
   void
 /* ARGSUSED */  print_gen_decls(struct html *h)
 static int  
 html_inlinetagname(struct md_mbuf *mbuf,  
                 const struct md_args *args, int tok, size_t *res)  
 {  {
   
         switch (tok) {          puts("<!DOCTYPE html>");
         case (ROFF_Sh):          h->flags |= HTML_NLDONE;
                 return(html_stput(mbuf, HTML_TAG_A, res));  
         case (ROFF_Pp):  
                 return(html_stput(mbuf, HTML_TAG_DIV, res));  
         case (ROFF_Sx):  
                 return(html_stput(mbuf, HTML_TAG_A, res));  
         default:  
                 break;  
         }  
   
         return(html_stput(mbuf, HTML_TAG_SPAN, res));  
 }  }
   
   void
 static ssize_t  print_text(struct html *h, const char *word)
 html_begintag(struct md_mbuf *mbuf, void *data,  
                 const struct md_args *args, enum md_ns ns,  
                 int tok, const int *argc, const char **argv)  
 {  {
         size_t           res;          if ((h->flags & (HTML_NLDONE | HTML_NOSPACE)) == 0) {
         struct htmlq    *q;                  if ( ! (HTML_KEEP & h->flags)) {
         struct htmlnode *node;                          if (HTML_PREKEEP & h->flags)
         int              i;                                  h->flags |= HTML_KEEP;
                           putchar(' ');
         assert(ns != MD_NS_DEFAULT);                  } else
         res = 0;                          printf("&#160;");
   
         assert(data);  
         q = (struct htmlq *)data;  
   
         if (NULL == (node = calloc(1, sizeof(struct htmlnode)))) {  
                 warn("calloc");  
                 return(-1);  
         }          }
   
         node->parent = q->last;          assert(NULL == h->metaf);
         node->tok = tok;          switch (h->metac) {
         node->ns = ns;          case HTMLFONT_ITALIC:
                   h->metaf = print_otag(h, TAG_I, "");
         if (argc)  {  
                 /* TODO: argv. */  
   
                 assert(argv);  
                 /* LINTED */  
                 for (i = 0; ROFF_ARGMAX != argc[i]  
                                 && i < ROFF_MAXLINEARG; i++)  
                         node->argc[i] = argc[i];  
                 assert(i != ROFF_MAXLINEARG);  
         }  
   
   
         q->last = node;  
   
         switch (ns) {  
         case (MD_NS_BLOCK):  
                 if ( ! html_blocktagname(mbuf, args, tok,  
                                         q, argc, argv, &res))  
                         return(-1);  
                 if ( ! html_blocktagargs(mbuf, args, tok,  
                                         argc, argv, &res))  
                         return(-1);  
                 break;                  break;
         case (MD_NS_BODY):          case HTMLFONT_BOLD:
                 if ( ! html_bodytagname(mbuf, args, tok,                  h->metaf = print_otag(h, TAG_B, "");
                                         q, argc, argv, &res))  
                         return(-1);  
                 if ( ! html_bodytagargs(mbuf, args, tok,  
                                         argc, argv, &res))  
                         return(-1);  
                 break;                  break;
         case (MD_NS_HEAD):          case HTMLFONT_BI:
                 if ( ! html_headtagname(mbuf, args, tok, q,                  h->metaf = print_otag(h, TAG_B, "");
                                         argc, argv, &res))                  print_otag(h, TAG_I, "");
                         return(-1);  
                 if ( ! html_headtagargs(mbuf, args, tok,  
                                         argc, argv, &res))  
                         return(-1);  
                 break;                  break;
         default:          default:
                 if ( ! html_inlinetagname(mbuf, args, tok, &res))                  html_indent(h);
                         return(-1);  
                 if ( ! html_inlinetagargs(mbuf, args, tok,  
                                         argc, argv, &res))  
                         return(-1);  
                 break;                  break;
         }          }
   
         return((ssize_t)res);          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) {
 static ssize_t                  print_tagq(h, h->metaf);
 html_endtag(struct md_mbuf *mbuf, void *data,                  h->metaf = NULL;
                 const struct md_args *args, enum md_ns ns, int tok)  
 {  
         size_t           res;  
         struct htmlq    *q;  
         struct htmlnode *node;  
   
         assert(ns != MD_NS_DEFAULT);  
         res = 0;  
   
         assert(data);  
         q = (struct htmlq *)data;  
         node = q->last;  
   
         switch (ns) {  
         case (MD_NS_BLOCK):  
                 if ( ! html_blocktagname(mbuf, args, tok,  
                                         q, node->argc,  
                                         (const char **)node->argv, &res))  
                         return(-1);  
                 break;  
         case (MD_NS_BODY):  
                 if ( ! html_bodytagname(mbuf, args, tok,  
                                         q, node->argc,  
                                         (const char **)node->argv, &res))  
                         return(-1);  
                 break;  
         case (MD_NS_HEAD):  
                 if ( ! html_headtagname(mbuf, args, tok,  
                                         q, node->argc,  
                                         (const char **)node->argv, &res))  
                         return(-1);  
                 break;  
         default:  
                 if ( ! html_inlinetagname(mbuf, args, tok, &res))  
                         return(-1);  
                 break;  
         }          }
   
         q->last = node->parent;          h->flags &= ~HTML_IGNDELIM;
   
         free(node);  
   
         return((ssize_t)res);  
 }  }
   
   void
 static int  print_tagq(struct html *h, const struct tag *until)
 html_alloc(void **p)  
 {  {
           struct tag      *tag;
   
         if (NULL == (*p = calloc(1, sizeof(struct htmlq)))) {          while ((tag = h->tags.head) != NULL) {
                 warn("calloc");                  print_ctag(h, tag);
                 return(0);                  if (until && tag == until)
                           return;
         }          }
         return(1);  
 }  }
   
   void
 static void  print_stagq(struct html *h, const struct tag *suntil)
 html_free(void *p)  
 {  {
         struct htmlq    *q;          struct tag      *tag;
         struct htmlnode *n;  
   
         assert(p);          while ((tag = h->tags.head) != NULL) {
         q = (struct htmlq *)p;                  if (suntil && tag == suntil)
                           return;
         /* LINTED */                  print_ctag(h, tag);
         while ((n = q->last)) {  
                 q->last = n->parent;  
                 free(n);  
         }          }
   
         free(q);  
 }  }
   
   void
 /* ARGSUSED */  print_paragraph(struct html *h)
 static ssize_t  
 html_beginhttp(struct md_mbuf *mbuf,  
                 const struct md_args *args,  
                 const char *buf, size_t sz)  
 {  {
         size_t           res;          struct tag      *t;
         struct html_pair pair;  
   
         res = 0;          t = print_otag(h, TAG_DIV, "c", "spacer");
         pair.attr = HTML_ATTR_HREF;          print_tagq(h, t);
         pair.val = (char *)buf;  
   
         if ( ! html_aput(mbuf, ML_OPEN, HTML_TAG_A, &res, 1, &pair))  
                 return(-1);  
         return((ssize_t)res);  
 }  }
   
   /*
 /* ARGSUSED */   * If something was printed on the current output line, end it.
 static ssize_t   * Not to be called right after html_indent().
 html_endhttp(struct md_mbuf *mbuf,   */
                 const struct md_args *args,  static void
                 const char *buf, size_t sz)  html_endline(struct html *h)
 {  {
         size_t           res;          if (h->flags & HTML_NLDONE)
                   return;
   
         res = 0;          putchar('\n');
         if ( ! html_tput(mbuf, ML_CLOSE, HTML_TAG_A, &res))          h->flags |= HTML_NLDONE | HTML_NOSPACE;
                 return(-1);  
         return((ssize_t)res);  
 }  }
   
   /*
 /* ARGSUSED */   * If at the beginning of a new output line,
 static ssize_t   * perform indentation and mark the line as containing output.
 html_beginstring(struct md_mbuf *mbuf,   * Make sure to really produce some output right afterwards,
                 const struct md_args *args,   * but do not use print_otag() for producing it.
                 const char *buf, size_t sz)   */
   static void
   html_indent(struct html *h)
 {  {
           int      i;
   
         if (0 == strncmp(buf, "http://", 7))          if ((h->flags & HTML_NLDONE) == 0)
                 return(html_beginhttp(mbuf, args, buf, sz));                  return;
   
         return(0);          if (h->noindent == 0)
                   for (i = 0; i < h->indent * 2; i++)
                           putchar(' ');
           h->flags &= ~(HTML_NLDONE | HTML_NOSPACE);
 }  }
   
   /*
 /* ARGSUSED */   * Calculate the scaling unit passed in a `-width' argument.  This uses
 static ssize_t   * either a native scaling unit (e.g., 1i, 2m) or the string length of
 html_endstring(struct md_mbuf *mbuf,   * the value.
                 const struct md_args *args,   */
                 const char *buf, size_t sz)  static void
   a2width(const char *p, struct roffsu *su)
 {  {
           if (a2roffsu(p, su, SCALE_MAX) < 2) {
         if (0 == strncmp(buf, "http://", 7))                  su->unit = SCALE_EN;
                 return(html_endhttp(mbuf, args, buf, sz));                  su->scale = html_strlen(p);
           } else if (su->scale < 0.0)
         return(0);                  su->scale = 0.0;
 }  
   
   
 int  
 md_line_html(void *data, char *buf)  
 {  
   
         return(mlg_line((struct md_mlg *)data, buf));  
 }  
   
   
 int  
 md_exit_html(void *data, int flush)  
 {  
   
         return(mlg_exit((struct md_mlg *)data, flush));  
 }  
   
   
 void *  
 md_init_html(const struct md_args *args,  
                 struct md_mbuf *mbuf, const struct md_rbuf *rbuf)  
 {  
         struct ml_cbs    cbs;  
   
         cbs.ml_alloc = html_alloc;  
         cbs.ml_free = html_free;  
         cbs.ml_begintag = html_begintag;  
         cbs.ml_endtag = html_endtag;  
         cbs.ml_begin = html_begin;  
         cbs.ml_end = html_end;  
         cbs.ml_beginstring = html_beginstring;  
         cbs.ml_endstring = html_endstring;  
   
         return(mlg_alloc(args, rbuf, mbuf, &cbs));  
 }  }

Legend:
Removed from v.1.24  
changed lines
  Added in v.1.196

CVSweb