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

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

version 1.11, 2008/12/05 22:54:44 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 "libmdocml.h"  #include "mandoc.h"
 #include "private.h"  #include "mandoc_aux.h"
 #include "ml.h"  #include "out.h"
   #include "html.h"
   #include "manconf.h"
   #include "main.h"
   
   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)
   };
   
 /* TODO: allow head/tail-less invocations (just "div" start). */  static  const struct htmldata htmltags[TAG_MAX] = {
           {"html",        HTML_NLALL},
           {"head",        HTML_NLALL | HTML_INDENT},
           {"body",        HTML_NLALL},
           {"meta",        HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
           {"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  htmlnode {  static  const char      *const roffscales[SCALE_MAX] = {
         int              tok;          "cm", /* SCALE_CM */
         enum md_ns       ns;          "in", /* SCALE_IN */
         int              argc[ROFF_MAXLINEARG];          "pc", /* SCALE_PC */
         char            *argv[ROFF_MAXLINEARG];          "pt", /* SCALE_PT */
         struct htmlnode *parent;          "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);
   
 struct  htmlq {  
         struct htmlnode *last;  
 };  
   
   void *
   html_alloc(const struct manoutput *outopts)
   {
           struct html     *h;
   
 static  int             html_loadcss(struct md_mbuf *, const char *);          h = mandoc_calloc(1, sizeof(struct html));
   
 static  int             html_alloc(void **);          h->tags.head = NULL;
 static  void            html_free(void *);          h->style = outopts->style;
 static  ssize_t         html_endtag(struct md_mbuf *, void *,          h->base_man = outopts->man;
                                 const struct md_args *,          h->base_includes = outopts->includes;
                                 enum md_ns, int);          if (outopts->fragment)
 static  ssize_t         html_begintag(struct md_mbuf *, void *,                  h->oflags |= HTML_FRAGMENT;
                                 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 *,  
                                 const char *, const char *);  
 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 *);  
 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_blockbodytagname(struct md_mbuf *,  
                                 const struct md_args *,  
                                 int, struct htmlq *, const int *,  
                                 const char **, size_t *);  
 static  int             html_blockbodytagargs(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 *);  
   
           return h;
   }
   
 /* ARGSUSED */  void
 static int  html_free(void *p)
 html_It_headtagname(struct md_mbuf *mbuf, struct htmlq *q,  
                 const int *argc, const char **argv, size_t *res)  
 {  {
         struct htmlnode *n;          struct tag      *tag;
         int              i;          struct html     *h;
   
         for (n = q->last; n; n = n->parent)          h = (struct html *)p;
                 if (n->tok == ROFF_Bl)  
                         break;  
   
         assert(n);          while ((tag = h->tags.head) != NULL) {
         for (i = 0; ROFF_ARGMAX != n->argc[i] &&                  h->tags.head = tag->next;
                         i < ROFF_MAXLINEARG; i++) {                  free(tag);
                 switch (n->argc[i]) {  
                 case (ROFF_Tag):  
                         /* FALLTHROUGH */  
                 case (ROFF_Column):  
                         return(ml_nputs(mbuf, "td", 2, res));  
                 default:  
                         break;  
                 }  
         }          }
   
         assert(i != ROFF_MAXLINEARG);          free(h);
         abort();  
         /* NOTREACHED */  
   
         return(1);  
 }  }
   
   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);          /*
         for (i = 0; ROFF_ARGMAX != n->argc[i] &&           * Print a default style-sheet.
                         i < ROFF_MAXLINEARG; i++) {           */
                 switch (n->argc[i]) {  
                 case (ROFF_Enum):  
                         /* FALLTHROUGH */  
                 case (ROFF_Bullet):  
                         /* FALLTHROUGH */  
                 case (ROFF_Dash):  
                         /* FALLTHROUGH */  
                 case (ROFF_Hyphen):  
                         /* FALLTHROUGH */  
                 case (ROFF_Item):  
                         /* FALLTHROUGH */  
                 case (ROFF_Diag):  
                         /* FALLTHROUGH */  
                 case (ROFF_Hang):  
                         /* FALLTHROUGH */  
                 case (ROFF_Ohang):  
                         /* FALLTHROUGH */  
                 case (ROFF_Inset):  
                         return(ml_nputs(mbuf, "div", 3, res));  
                 case (ROFF_Tag):  
                         /* FALLTHROUGH */  
                 case (ROFF_Column):  
                         return(ml_nputs(mbuf, "td", 2, res));  
                 default:  
                         break;  
                 }  
         }  
   
         assert(i != ROFF_MAXLINEARG);          t = print_otag(h, TAG_STYLE, "");
         abort();          print_text(h, "table.head, table.foot { width: 100%; }");
         /* NOTREACHED */          html_endline(h);
           print_text(h, "td.head-rtitle, td.foot-os { text-align: right; }");
           html_endline(h);
           print_text(h, "td.head-vol { text-align: center; }");
           html_endline(h);
           print_text(h, "table.foot td { width: 50%; }");
           html_endline(h);
           print_text(h, "table.head td { width: 33%; }");
           html_endline(h);
           print_text(h, "div.spacer { margin: 1em 0; }");
           print_tagq(h, t);
   
         return(1);          if (h->style)
                   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(ml_nputs(mbuf, "ol", 2, 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(ml_nputs(mbuf, "ul", 2, res));  
                 case (ROFF_Tag):  
                         /* FALLTHROUGH */  
                 case (ROFF_Column):  
                         return(ml_nputs(mbuf, "table", 5, res));  
                 default:  
                         break;  
                 }  
         }          }
   
         assert(i != ROFF_MAXLINEARG);          if (h->metaf) {
         abort();                  print_tagq(h, h->metaf);
         /* NOTREACHED */                  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;
         for (i = 0; ROFF_ARGMAX != n->argc[i] &&          skip = 0;
                         i < ROFF_MAXLINEARG; i++) {          while (1) {
                 switch (n->argc[i]) {                  rsz = strcspn(cp, "\\");
                 case (ROFF_Enum):                  if (rsz) {
                         /* FALLTHROUGH */                          cp += rsz;
                 case (ROFF_Bullet):                          if (skip) {
                         /* FALLTHROUGH */                                  skip = 0;
                 case (ROFF_Dash):                                  rsz--;
                         /* FALLTHROUGH */                          }
                 case (ROFF_Hyphen):                          sz += rsz;
                         /* FALLTHROUGH */                  }
                 case (ROFF_Item):                  if ('\0' == *cp)
                         /* FALLTHROUGH */                          break;
                 case (ROFF_Diag):                  cp++;
                         /* FALLTHROUGH */                  switch (mandoc_escape(&cp, NULL, NULL)) {
                 case (ROFF_Hang):                  case ESCAPE_ERROR:
                         /* FALLTHROUGH */                          return sz;
                 case (ROFF_Ohang):                  case ESCAPE_UNICODE:
                         /* FALLTHROUGH */                  case ESCAPE_NUMBERED:
                 case (ROFF_Inset):                  case ESCAPE_SPECIAL:
                         return(ml_nputs(mbuf, "li", 2, res));                  case ESCAPE_OVERSTRIKE:
                 case (ROFF_Tag):                          if (skip)
                         /* FALLTHROUGH */                                  skip = 0;
                 case (ROFF_Column):                          else
                         return(ml_nputs(mbuf, "tr", 2, res));                                  sz++;
                           break;
                   case ESCAPE_SKIPCHAR:
                           skip = 1;
                           break;
                 default:                  default:
                         break;                          break;
                 }                  }
         }          }
           return sz;
         assert(i != ROFF_MAXLINEARG);  
         abort();  
         /* NOTREACHED */  
 }  }
   
   
 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);  static int
         if (NULL == (buf = malloc(bufsz))) {  print_encode(struct html *h, const char *p, const char *pend, int norecurse)
                 warn("malloc");  {
                 goto out;          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' };
   
         for (;;) {          if (pend == NULL)
                 if (-1 == (ssz = read(fd, buf, bufsz))) {                  pend = strchr(p, '\0');
                         warn("%s", css);  
                         goto out;  
                 } else if (0 == ssz)  
                         break;  
                 if ( ! ml_nputs(mbuf, buf, (size_t)ssz, &res))  
                         goto out;  
         }  
   
         c = 1;          nospace = 0;
   
 out:          while (p < pend) {
         if (-1 == close(fd)) {                  if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
                 warn("%s", css);                          h->flags &= ~HTML_SKIPCHAR;
                 c = 0;                          p++;
         }                          continue;
                   }
   
         if (buf)                  sz = strcspn(p, rejs);
                 free(buf);                  if (p + sz > pend)
                           sz = pend - p;
   
         return(c);                  fwrite(p, 1, sz, stdout);
 }                  p += (int)sz;
   
                   if (p >= pend)
                           break;
   
 /* ARGSUSED */                  if (print_escape(*p++))
 static int                          continue;
 html_begin(struct md_mbuf *mbuf, const struct md_args *args,  
                 const struct tm *tm, const char *os,  
                 const char *title, const char *section,  
                 const char *vol)  
 {  
         const char      *preamble, *css, *trail;  
         char             buf[512];  
         size_t           res;  
   
         preamble =                  esc = mandoc_escape(&p, &seq, &len);
         "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n"                  if (ESCAPE_ERROR == esc)
         "    \"http://www.w3.org/TR/html4/strict.dtd\">\n"                          break;
         "<html>\n"  
         "<head>\n"  
         "    <meta http-equiv=\"Content-Type\"\n"  
         "         content=\"text/html;charset=utf-8\">\n"  
         "    <meta name=\"resource-type\" content=\"document\">\n"  
         "    <title>Manual Page for %s(%s)</title>\n";  
   
         css =                  switch (esc) {
         "    <link rel=\"stylesheet\" type=\"text/css\"\n"                  case ESCAPE_FONT:
         "         href=\"%s\">\n";                  case ESCAPE_FONTPREV:
         trail =                  case ESCAPE_FONTBOLD:
         "</head>\n"                  case ESCAPE_FONTITALIC:
         "<body>\n"                  case ESCAPE_FONTBI:
         "<div class=\"mdoc\">";                  case ESCAPE_FONTROMAN:
                           if (0 == norecurse)
                                   print_metaf(h, esc);
                           continue;
                   case ESCAPE_SKIPCHAR:
                           h->flags |= HTML_SKIPCHAR;
                           continue;
                   default:
                           break;
                   }
   
         res = 0;                  if (h->flags & HTML_SKIPCHAR) {
                           h->flags &= ~HTML_SKIPCHAR;
                           continue;
                   }
   
         (void)snprintf(buf, sizeof(buf) - 1,                  switch (esc) {
                         preamble, title, section);                  case ESCAPE_UNICODE:
                           /* Skip past "u" header. */
         if ( ! ml_puts(mbuf, buf, &res))                          c = mchars_num2uc(seq + 1, len - 1);
                 return(0);                          break;
                   case ESCAPE_NUMBERED:
         assert(args->params.html.css);                          c = mchars_num2char(seq, len);
         if (HTML_CSS_EMBED & args->params.html.flags) {                          if (c < 0)
                 if ( ! ml_puts(mbuf, "    <style type=\"text/css\"><!--\n", &res))                                  continue;
                         return(0);                          break;
                 if ( ! html_loadcss(mbuf, args->params.html.css))                  case ESCAPE_SPECIAL:
                         return(0);                          c = mchars_spec2cp(seq, len);
                 if ( ! ml_puts(mbuf, "    --!></style>\n", &res))                          if (c <= 0)
                         return(0);                                  continue;
         } else {                          break;
                 (void)snprintf(buf, sizeof(buf) - 1, css,                  case ESCAPE_NOSPACE:
                                 args->params.html.css);                          if ('\0' == *p)
                 if ( ! ml_puts(mbuf, buf, &res))                                  nospace = 1;
                         return(0);                          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);
         }          }
   
         if ( ! ml_puts(mbuf, trail, &res))          return nospace;
                 return(0);  
   
         return(1);  
 }  }
   
   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 char      *p, *pp;
   
         return(ml_puts(mbuf, "</div></body>\n</html>", NULL));          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 *
 /* ARGSUSED */  print_otag(struct html *h, enum htmltag tag, const char *fmt, ...)
 static int  
 html_blockbodytagname(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_It):  
                 return(html_It_bodytagname(mbuf, q, argc, argv, res));  
         default:  
                 break;  
         }  
   
         return(ml_puts(mbuf, "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_Sh):                          putchar(' ');
                 return(ml_puts(mbuf, "h1", res));                  }
         case (ROFF_Ss):  
                 return(ml_puts(mbuf, "h2", res));  
         default:  
                 break;  
         }          }
   
         return(ml_puts(mbuf, "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_It):  
                 return(html_It_blocktagname(mbuf, q, argc, argv, res));          have_style = 0;
         default:          while (*fmt != '\0') {
                 break;                  if (*fmt == 's') {
                           printf(" style=\"");
                           have_style = 1;
                           fmt++;
                           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(ml_puts(mbuf, "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)  
 {  
   
         if ( ! ml_puts(mbuf, " class=\"", res))                  switch (*fmt++) {
                 return(0);                  case 'h':
         if ( ! ml_puts(mbuf, ns, res))                          i = va_arg(ap, int);
                 return(0);                          SCALE_HS_INIT(su, i);
         if ( ! ml_puts(mbuf, "-", res))                          break;
                 return(0);                  case 's':
         if ( ! ml_puts(mbuf, toknames[tok], res))                          s = va_arg(ap, char *);
                 return(0);                          break;
         return(ml_puts(mbuf, "\"", res));                  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. */
   
 /* ARGSUSED */                  switch (*fmt++) {
 static int                  case 'b':
 html_headtagargs(struct md_mbuf *mbuf,                          attr = "margin-bottom";
                 const struct md_args *args, int tok,                          break;
                 const int *argc, const char **argv, size_t *res)                  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('"');
   
         return(html_printargs(mbuf, tok, "head", argc, argv, res));          va_end(ap);
 }  
   
           /* Accommodate for "well-formed" singleton escaping. */
   
 /* ARGSUSED */          if (HTML_AUTOCLOSE & htmltags[tag].flags)
 static int                  putchar('/');
 html_blockbodytagargs(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));          putchar('>');
 }  
   
           if (tflags & HTML_NLBEGIN)
                   html_endline(h);
           else
                   h->flags |= HTML_NOSPACE;
   
 /* ARGSUSED */          if (tflags & HTML_INDENT)
 static int                  h->indent++;
 html_blocktagargs(struct md_mbuf *mbuf,          if (tflags & HTML_NOINDENT)
                 const struct md_args *args, int tok,                  h->noindent++;
                 const int *argc, const char **argv, size_t *res)  
 {  
   
         return(html_printargs(mbuf, tok, "block", argc, argv, res));          return t;
 }  }
   
   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;
   
         return(html_printargs(mbuf, tok, "inline", argc, argv, res));          /*
 }           * 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;
   
           tflags = htmltags[tag->tag].flags;
   
 /* ARGSUSED */          if (tflags & HTML_INDENT)
 static int                  h->indent--;
 html_inlinetagname(struct md_mbuf *mbuf,          if (tflags & HTML_NOINDENT)
                 const struct md_args *args, int tok, size_t *res)                  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);
   
         switch (tok) {          h->tags.head = tag->next;
         case (ROFF_Pp):          free(tag);
                 return(ml_puts(mbuf, "div", res));  
         default:  
                 break;  
         }  
   
         return(ml_puts(mbuf, "span", res));  
 }  }
   
   void
 static ssize_t  print_gen_decls(struct html *h)
 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;  
         struct htmlq    *q;  
         struct htmlnode *node;  
         int              i;  
   
         assert(ns != MD_NS_DEFAULT);          puts("<!DOCTYPE html>");
         res = 0;          h->flags |= HTML_NLDONE;
   }
   
         assert(data);  void
         q = (struct htmlq *)data;  print_text(struct html *h, const char *word)
   {
         if (NULL == (node = calloc(1, sizeof(struct htmlnode)))) {          if ((h->flags & (HTML_NLDONE | HTML_NOSPACE)) == 0) {
                 warn("calloc");                  if ( ! (HTML_KEEP & h->flags)) {
                 return(-1);                          if (HTML_PREKEEP & h->flags)
                                   h->flags |= HTML_KEEP;
                           putchar(' ');
                   } else
                           printf("&#160;");
         }          }
   
         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);  
                 for (i = 0; ROFF_ARGMAX != argc[i]  
                                 && i < ROFF_MAXLINEARG; i++)  
                         node->argc[i] = argc[i];  
                 assert(i != ROFF_MAXLINEARG);  
         } else  
                 assert(NULL == argv);  
   
   
         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_blockbodytagname(mbuf, args, tok,                  h->metaf = print_otag(h, TAG_B, "");
                                         q, argc, argv, &res))  
                         return(-1);  
                 if ( ! html_blockbodytagargs(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_blockbodytagname(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;
         while ((n = q->last)) {                  print_ctag(h, tag);
                 q->last = n->parent;  
                 free(n);  
         }          }
   
         free(q);  
 }  }
   
   void
 int  print_paragraph(struct html *h)
 md_line_html(void *data, char *buf)  
 {  {
           struct tag      *t;
   
         return(mlg_line((struct md_mlg *)data, buf));          t = print_otag(h, TAG_DIV, "c", "spacer");
           print_tagq(h, t);
 }  }
   
   /*
 int   * If something was printed on the current output line, end it.
 md_exit_html(void *data, int flush)   * Not to be called right after html_indent().
    */
   static void
   html_endline(struct html *h)
 {  {
           if (h->flags & HTML_NLDONE)
                   return;
   
         return(mlg_exit((struct md_mlg *)data, flush));          putchar('\n');
           h->flags |= HTML_NLDONE | HTML_NOSPACE;
 }  }
   
   /*
 void *   * If at the beginning of a new output line,
 md_init_html(const struct md_args *args,   * perform indentation and mark the line as containing output.
                 struct md_mbuf *mbuf, const struct md_rbuf *rbuf)   * Make sure to really produce some output right afterwards,
    * but do not use print_otag() for producing it.
    */
   static void
   html_indent(struct html *h)
 {  {
         struct ml_cbs    cbs;          int      i;
   
         cbs.ml_alloc = html_alloc;          if ((h->flags & HTML_NLDONE) == 0)
         cbs.ml_free = html_free;                  return;
         cbs.ml_begintag = html_begintag;  
         cbs.ml_endtag = html_endtag;  
         cbs.ml_begin = html_begin;  
         cbs.ml_end = html_end;  
   
         return(mlg_alloc(args, rbuf, mbuf, &cbs));          if (h->noindent == 0)
                   for (i = 0; i < h->indent * 2; i++)
                           putchar(' ');
           h->flags &= ~(HTML_NLDONE | HTML_NOSPACE);
 }  }
   
   /*
    * 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
    * the value.
    */
   static void
   a2width(const char *p, struct roffsu *su)
   {
           if (a2roffsu(p, su, SCALE_MAX) < 2) {
                   su->unit = SCALE_EN;
                   su->scale = html_strlen(p);
           } else if (su->scale < 0.0)
                   su->scale = 0.0;
   }

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

CVSweb