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

Diff for /mandoc/html.c between version 1.12 and 1.107

version 1.12, 2008/12/06 19:41:41 version 1.107, 2010/07/16 22:33:30
Line 1 
Line 1 
 /* $Id$ */  /*      $Id$ */
 /*  /*
  * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>   * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
  *   *
  * 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 AUTHOR DISCLAIMS 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 AUTHOR 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>  #ifdef HAVE_CONFIG_H
 #include <sys/stat.h>  #include "config.h"
   #endif
   
   #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 "out.h"
 #include "ml.h"  #include "chars.h"
   #include "html.h"
   #include "main.h"
   
   struct  htmldata {
           const char       *name;
           int               flags;
   #define HTML_CLRLINE     (1 << 0)
   #define HTML_NOSTACK     (1 << 1)
   #define HTML_AUTOCLOSE   (1 << 2) /* Tag has auto-closure. */
   };
   
 /* TODO: allow head/tail-less invocations (just "div" start). */  static  const struct htmldata htmltags[TAG_MAX] = {
           {"html",        HTML_CLRLINE}, /* TAG_HTML */
           {"head",        HTML_CLRLINE}, /* TAG_HEAD */
           {"body",        HTML_CLRLINE}, /* TAG_BODY */
           {"meta",        HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_META */
           {"title",       HTML_CLRLINE}, /* TAG_TITLE */
           {"div",         HTML_CLRLINE}, /* TAG_DIV */
           {"h1",          0}, /* TAG_H1 */
           {"h2",          0}, /* TAG_H2 */
           {"span",        0}, /* TAG_SPAN */
           {"link",        HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_LINK */
           {"br",          HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_BR */
           {"a",           0}, /* TAG_A */
           {"table",       HTML_CLRLINE}, /* TAG_TABLE */
           {"col",         HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_COL */
           {"tr",          HTML_CLRLINE}, /* TAG_TR */
           {"td",          HTML_CLRLINE}, /* TAG_TD */
           {"li",          HTML_CLRLINE}, /* TAG_LI */
           {"ul",          HTML_CLRLINE}, /* TAG_UL */
           {"ol",          HTML_CLRLINE}, /* TAG_OL */
   };
   
 struct  htmlnode {  static  const char      *const htmlfonts[HTMLFONT_MAX] = {
         int              tok;          "roman",
         enum md_ns       ns;          "bold",
         int              argc[ROFF_MAXLINEARG];          "italic"
         char            *argv[ROFF_MAXLINEARG];  
         struct htmlnode *parent;  
 };  };
   
   static  const char      *const htmlattrs[ATTR_MAX] = {
 struct  htmlq {          "http-equiv",
         struct htmlnode *last;          "content",
           "name",
           "rel",
           "href",
           "type",
           "media",
           "class",
           "style",
           "width",
           "valign",
           "target",
           "id",
           "summary",
 };  };
   
   static  void              print_spec(struct html *, const char *, size_t);
 static  int             html_loadcss(struct md_mbuf *, const char *);  static  void              print_res(struct html *, const char *, size_t);
   static  void              print_ctag(struct html *, enum htmltag);
 static  int             html_alloc(void **);  static  void              print_doctype(struct html *);
 static  void            html_free(void *);  static  void              print_xmltype(struct html *);
 static  ssize_t         html_endtag(struct md_mbuf *, void *,  static  int               print_encode(struct html *, const char *, int);
                                 const struct md_args *,  static  void              print_metaf(struct html *, enum roffdeco);
                                 enum md_ns, int);  static  void              print_attr(struct html *,
 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 *,  
                                 const char *, const char *);                                  const char *, const char *);
 static  int             html_printargs(struct md_mbuf *, int,  static  void             *ml_alloc(char *, enum htmltype);
                                 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_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 *);  
   
   
 /* ARGSUSED */  static void *
 static int  ml_alloc(char *outopts, enum htmltype type)
 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;          const char      *toks[4];
           char            *v;
   
         for (n = q->last; n; n = n->parent)          toks[0] = "style";
                 if (n->tok == ROFF_Bl)          toks[1] = "man";
                         break;          toks[2] = "includes";
           toks[3] = NULL;
   
         assert(n);          h = calloc(1, sizeof(struct html));
         for (i = 0; ROFF_ARGMAX != n->argc[i] &&          if (NULL == h) {
                         i < ROFF_MAXLINEARG; i++) {                  perror(NULL);
                 switch (n->argc[i]) {                  exit(EXIT_FAILURE);
                 case (ROFF_Tag):  
                         /* FALLTHROUGH */  
                 case (ROFF_Column):  
                         return(ml_nputs(mbuf, "td", 2, res));  
                 default:  
                         break;  
                 }  
         }          }
   
         assert(i != ROFF_MAXLINEARG);          h->type = type;
         abort();          h->tags.head = NULL;
         /* NOTREACHED */          h->ords.head = NULL;
           h->symtab = chars_init(CHARS_HTML);
   
         return(1);          while (outopts && *outopts)
 }                  switch (getsubopt(&outopts, UNCONST(toks), &v)) {
                   case (0):
                           h->style = v;
 /* ARGSUSED */  
 static int  
 html_It_bodytagname(struct md_mbuf *mbuf, struct htmlq *q,  
                 const int *argc, const char **argv, size_t *res)  
 {  
         struct htmlnode *n;  
         int              i;  
   
         for (n = q->last; n; n = n->parent)  
                 if (n->tok == ROFF_Bl)  
                         break;                          break;
                   case (1):
         assert(n);                          h->base_man = v;
         for (i = 0; ROFF_ARGMAX != n->argc[i] &&                          break;
                         i < ROFF_MAXLINEARG; i++) {                  case (2):
                 switch (n->argc[i]) {                          h->base_includes = v;
                 case (ROFF_Enum):                          break;
                         /* 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:                  default:
                         break;                          break;
                 }                  }
         }  
   
         assert(i != ROFF_MAXLINEARG);          return(h);
         abort();  }
         /* NOTREACHED */  
   
         return(1);  void *
   html_alloc(char *outopts)
   {
   
           return(ml_alloc(outopts, HTML_HTML_4_01_STRICT));
 }  }
   
   
 /* ARGSUSED */  void *
 static int  xhtml_alloc(char *outopts)
 html_Bl_bodytagname(struct md_mbuf *mbuf, struct htmlq *q,  
                 const int *argc, const char **argv, size_t *res)  
 {  {
         int              i;  
   
         for (i = 0; ROFF_ARGMAX != argc[i]          return(ml_alloc(outopts, HTML_XHTML_1_0_STRICT));
                         && i < ROFF_MAXLINEARG; i++) {  
                 switch (argc[i]) {  
                 case (ROFF_Enum):  
                         return(ml_nputs(mbuf, "ol", 2, res));  
                 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, "ul", 2, res));  
                 case (ROFF_Tag):  
                         /* FALLTHROUGH */  
                 case (ROFF_Column):  
                         return(ml_nputs(mbuf, "table", 5, res));  
                 default:  
                         break;  
                 }  
         }  
   
         assert(i != ROFF_MAXLINEARG);  
         abort();  
         /* NOTREACHED */  
 }  }
   
   
 /* ARGSUSED */  void
 static int  html_free(void *p)
 html_It_blocktagname(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 ord      *ord;
           struct html     *h;
   
         for (n = q->last; n; n = n->parent)          h = (struct html *)p;
                 if (n->tok == ROFF_Bl)  
                         break;  
   
         assert(n);          while ((ord = h->ords.head) != NULL) {
         for (i = 0; ROFF_ARGMAX != n->argc[i] &&                  h->ords.head = ord->next;
                         i < ROFF_MAXLINEARG; i++) {                  free(ord);
                 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, "li", 2, res));  
                 case (ROFF_Tag):  
                         /* FALLTHROUGH */  
                 case (ROFF_Column):  
                         return(ml_nputs(mbuf, "tr", 2, res));  
                 default:  
                         break;  
                 }  
         }          }
   
         assert(i != ROFF_MAXLINEARG);          while ((tag = h->tags.head) != NULL) {
         abort();                  h->tags.head = tag->next;
         /* NOTREACHED */                  free(tag);
           }
   
           if (h->symtab)
                   chars_free(h->symtab);
   
           free(h);
 }  }
   
   
 static int  void
 html_loadcss(struct md_mbuf *mbuf, const char *css)  print_gen_head(struct html *h)
 {  {
         size_t           res, bufsz;          struct htmlpair  tag[4];
         char            *buf;  
         struct stat      st;  
         int              fd, c;  
         ssize_t          ssz;  
   
         c = 0;          tag[0].key = ATTR_HTTPEQUIV;
         res = 0;          tag[0].val = "Content-Type";
         buf = NULL;          tag[1].key = ATTR_CONTENT;
           tag[1].val = "text/html; charset=utf-8";
           print_otag(h, TAG_META, 2, tag);
   
         if (-1 == (fd = open(css, O_RDONLY, 0))) {          tag[0].key = ATTR_NAME;
                 warn("%s", css);          tag[0].val = "resource-type";
                 return(0);          tag[1].key = ATTR_CONTENT;
         }          tag[1].val = "document";
           print_otag(h, TAG_META, 2, tag);
         if (-1 == fstat(fd, &st)) {  
                 warn("%s", css);  
                 goto out;  
         }  
   
         bufsz = MAX(st.st_blksize, BUFSIZ);          if (h->style) {
         if (NULL == (buf = malloc(bufsz))) {                  tag[0].key = ATTR_REL;
                 warn("malloc");                  tag[0].val = "stylesheet";
                 goto out;                  tag[1].key = ATTR_HREF;
                   tag[1].val = h->style;
                   tag[2].key = ATTR_TYPE;
                   tag[2].val = "text/css";
                   tag[3].key = ATTR_MEDIA;
                   tag[3].val = "all";
                   print_otag(h, TAG_LINK, 4, tag);
         }          }
   }
   
         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;  static void
   print_spec(struct html *h, const char *p, size_t len)
   {
           int              cp;
           const char      *rhs;
           size_t           sz;
   
 out:          if ((cp = chars_spec2cp(h->symtab, p, len)) > 0) {
         if (-1 == close(fd)) {                  printf("&#%d;", cp);
                 warn("%s", css);                  return;
                 c = 0;          } else if (-1 == cp)
         }                  return;
   
         if (buf)          if (NULL != (rhs = chars_spec2str(h->symtab, p, len, &sz)))
                 free(buf);                  fwrite(rhs, 1, sz, stdout);
   
         return(c);  
 }  }
   
   
 /* ARGSUSED */  static void
 static int  print_res(struct html *h, const char *p, size_t len)
 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;          int              cp;
         char             buf[512];          const char      *rhs;
         size_t           res;          size_t           sz;
   
         preamble =          if ((cp = chars_res2cp(h->symtab, p, len)) > 0) {
         "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n"                  printf("&#%d;", cp);
         "    \"http://www.w3.org/TR/html4/strict.dtd\">\n"                  return;
         "<html>\n"          } else if (-1 == cp)
         "<head>\n"                  return;
         "    <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 =          if (NULL != (rhs = chars_res2str(h->symtab, p, len, &sz)))
         "    <link rel=\"stylesheet\" type=\"text/css\"\n"                  fwrite(rhs, 1, sz, stdout);
         "         href=\"%s\">\n";  }
         trail =  
         "</head>\n"  
         "<body>\n"  
         "<div class=\"mdoc\">";  
   
         res = 0;  
   
         (void)snprintf(buf, sizeof(buf) - 1,  struct tag *
                         preamble, title, section);  print_ofont(struct html *h, enum htmlfont font)
   {
           struct htmlpair  tag;
   
         if ( ! ml_puts(mbuf, buf, &res))          h->metal = h->metac;
                 return(0);          h->metac = font;
   
         assert(args->params.html.css);          /* FIXME: DECO_ROMAN should just close out preexisting. */
         if (HTML_CSS_EMBED & args->params.html.flags) {  
                 if ( ! ml_puts(mbuf, "    <style type=\"text/css\"><!--\n", &res))  
                         return(0);  
                 if ( ! html_loadcss(mbuf, args->params.html.css))  
                         return(0);  
                 if ( ! ml_puts(mbuf, "    --!></style>\n", &res))  
                         return(0);  
         } else {  
                 (void)snprintf(buf, sizeof(buf) - 1, css,  
                                 args->params.html.css);  
                 if ( ! ml_puts(mbuf, buf, &res))  
                         return(0);  
         }  
   
         if ( ! ml_puts(mbuf, trail, &res))          if (h->metaf && h->tags.head == h->metaf)
                 return(0);                  print_tagq(h, h->metaf);
   
         return(1);          PAIR_CLASS_INIT(&tag, htmlfonts[font]);
           h->metaf = print_otag(h, TAG_SPAN, 1, &tag);
           return(h->metaf);
 }  }
   
   
 /* ARGSUSED */  static void
 static int  print_metaf(struct html *h, enum roffdeco deco)
 html_end(struct md_mbuf *mbuf, const struct md_args *args)  
 {  {
           enum htmlfont    font;
   
         return(ml_puts(mbuf, "</div></body>\n</html>", NULL));          switch (deco) {
           case (DECO_PREVIOUS):
                   font = h->metal;
                   break;
           case (DECO_ITALIC):
                   font = HTMLFONT_ITALIC;
                   break;
           case (DECO_BOLD):
                   font = HTMLFONT_BOLD;
                   break;
           case (DECO_ROMAN):
                   font = HTMLFONT_NONE;
                   break;
           default:
                   abort();
                   /* NOTREACHED */
           }
   
           (void)print_ofont(h, font);
 }  }
   
   
 /* ARGSUSED */  
 static int  static int
 html_bodytagname(struct md_mbuf *mbuf,  print_encode(struct html *h, const char *p, int norecurse)
                 const struct md_args *args, int tok, struct htmlq *q,  
                 const int *argc, const char **argv, size_t *res)  
 {  {
           size_t           sz;
           int              len, nospace;
           const char      *seq;
           enum roffdeco    deco;
           static const char rejs[6] = { '\\', '<', '>', '&', ASCII_HYPH, '\0' };
   
         switch (tok) {          nospace = 0;
         case (ROFF_Bl):  
                 return(html_Bl_bodytagname(mbuf, q, argc, argv, res));  
         case (ROFF_Fo):  
                 return(ml_nputs(mbuf, "span", 4, res));  
         case (ROFF_It):  
                 return(html_It_bodytagname(mbuf, q, argc, argv, res));  
         case (ROFF_Oo):  
                 return(ml_nputs(mbuf, "span", 4, res));  
         default:  
                 break;  
         }  
   
         return(ml_puts(mbuf, "div", res));          for (; *p; p++) {
 }                  sz = strcspn(p, rejs);
   
                   fwrite(p, 1, sz, stdout);
                   p += /* LINTED */
                           sz;
   
 /* ARGSUSED */                  if ('<' == *p) {
 static int                          printf("&lt;");
 html_headtagname(struct md_mbuf *mbuf,                          continue;
                 const struct md_args *args, int tok, struct htmlq *q,                  } else if ('>' == *p) {
                 const int *argc, const char **argv, size_t *res)                          printf("&gt;");
 {                          continue;
                   } else if ('&' == *p) {
                           printf("&amp;");
                           continue;
                   } else if (ASCII_HYPH == *p) {
                           /*
                            * Note: "soft hyphens" aren't graphically
                            * displayed when not breaking the text; we want
                            * them to be displayed.
                            */
                           /*printf("&#173;");*/
                           putchar('-');
                           continue;
                   } else if ('\0' == *p)
                           break;
   
         switch (tok) {                  seq = ++p;
         case (ROFF_It):                  len = a2roffdeco(&deco, &seq, &sz);
                 return(html_It_headtagname(mbuf, q, argc, argv, res));  
         case (ROFF_Fo):                  switch (deco) {
                 return(ml_nputs(mbuf, "span", 4, res));                  case (DECO_RESERVED):
         case (ROFF_Oo):                          print_res(h, seq, sz);
                 return(ml_nputs(mbuf, "span", 4, res));                          break;
         case (ROFF_Sh):                  case (DECO_SPECIAL):
                 return(ml_nputs(mbuf, "h1", 2, res));                          print_spec(h, seq, sz);
         case (ROFF_Ss):                          break;
                 return(ml_nputs(mbuf, "h2", 2, res));                  case (DECO_PREVIOUS):
         default:                          /* FALLTHROUGH */
                 break;                  case (DECO_BOLD):
                           /* FALLTHROUGH */
                   case (DECO_ITALIC):
                           /* FALLTHROUGH */
                   case (DECO_ROMAN):
                           if (norecurse)
                                   break;
                           print_metaf(h, deco);
                           break;
                   default:
                           break;
                   }
   
                   p += len - 1;
   
                   if (DECO_NOSPACE == deco && '\0' == *(p + 1))
                           nospace = 1;
         }          }
   
         return(ml_nputs(mbuf, "div", 3, res));          return(nospace);
 }  }
   
   
 /* ARGSUSED */  static void
 static int  print_attr(struct html *h, const char *key, const char *val)
 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)  
 {  {
           printf(" %s=\"", key);
           (void)print_encode(h, val, 1);
           putchar('\"');
   }
   
         switch (tok) {  
         case (ROFF_Fo):  struct tag *
                 return(ml_nputs(mbuf, "span", 4, res));  print_otag(struct html *h, enum htmltag tag,
         case (ROFF_Oo):                  int sz, const struct htmlpair *p)
                 return(ml_nputs(mbuf, "span", 4, res));  {
         case (ROFF_It):          int              i;
                 return(html_It_blocktagname(mbuf, q, argc, argv, res));          struct tag      *t;
         default:  
                 break;          /* Push this tags onto the stack of open scopes. */
   
           if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
                   t = malloc(sizeof(struct tag));
                   if (NULL == t) {
                           perror(NULL);
                           exit(EXIT_FAILURE);
                   }
                   t->tag = tag;
                   t->next = h->tags.head;
                   h->tags.head = t;
           } else
                   t = NULL;
   
           if ( ! (HTML_NOSPACE & h->flags))
                   if ( ! (HTML_CLRLINE & htmltags[tag].flags)) {
                           /* Manage keeps! */
                           if ( ! (HTML_KEEP & h->flags)) {
                                   if (HTML_PREKEEP & h->flags)
                                           h->flags |= HTML_KEEP;
                                   putchar(' ');
                           } else
                                   printf("&#160;");
                   }
   
           /* Print out the tag name and attributes. */
   
           printf("<%s", htmltags[tag].name);
           for (i = 0; i < sz; i++)
                   print_attr(h, htmlattrs[p[i].key], p[i].val);
   
           /* Add non-overridable attributes. */
   
           if (TAG_HTML == tag && HTML_XHTML_1_0_STRICT == h->type) {
                   print_attr(h, "xmlns", "http://www.w3.org/1999/xhtml");
                   print_attr(h, "xml:lang", "en");
                   print_attr(h, "lang", "en");
         }          }
   
         return(ml_puts(mbuf, "div", res));          /* Accomodate for XML "well-formed" singleton escaping. */
 }  
   
           if (HTML_AUTOCLOSE & htmltags[tag].flags)
                   switch (h->type) {
                   case (HTML_XHTML_1_0_STRICT):
                           putchar('/');
                           break;
                   default:
                           break;
                   }
   
 /* ARGSUSED */          putchar('>');
 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))          h->flags |= HTML_NOSPACE;
                 return(0);          return(t);
         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));  
 }  }
   
   
 /* ARGSUSED */  static void
 static int  print_ctag(struct html *h, enum htmltag tag)
 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));          printf("</%s>", htmltags[tag].name);
           if (HTML_CLRLINE & htmltags[tag].flags) {
                   h->flags |= HTML_NOSPACE;
                   putchar('\n');
           }
 }  }
   
   
 /* ARGSUSED */  void
 static int  print_gen_decls(struct html *h)
 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));          print_xmltype(h);
           print_doctype(h);
 }  }
   
   
 /* ARGSUSED */  static void
 static int  print_xmltype(struct html *h)
 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));          if (HTML_XHTML_1_0_STRICT == h->type)
                   printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
 }  }
   
   
 /* ARGSUSED */  static void
 static int  print_doctype(struct html *h)
 html_inlinetagargs(struct md_mbuf *mbuf,  
                 const struct md_args *args, int tok,  
                 const int *argc, const char **argv, size_t *res)  
 {  {
           const char      *doctype;
           const char      *dtd;
           const char      *name;
   
         if ( ! html_printargs(mbuf, tok, "inline", argc, argv, res))          switch (h->type) {
                 return(0);          case (HTML_HTML_4_01_STRICT):
                   name = "HTML";
         switch (tok) {                  doctype = "-//W3C//DTD HTML 4.01//EN";
         case (ROFF_Sx):                  dtd = "http://www.w3.org/TR/html4/strict.dtd";
                 assert(*argv);                  break;
                 if ( ! ml_nputs(mbuf, " href=\"#", 8, res))  
                         return(0);  
                 if ( ! ml_putstring(mbuf, *argv, res))  
                         return(0);  
                 if ( ! ml_nputs(mbuf, "\"", 1, res))  
                         return(0);  
         default:          default:
                   name = "html";
                   doctype = "-//W3C//DTD XHTML 1.0 Strict//EN";
                   dtd = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";
                 break;                  break;
         }          }
   
         return(1);          printf("<!DOCTYPE %s PUBLIC \"%s\" \"%s\">\n",
                           name, doctype, dtd);
 }  }
   
   
 /* ARGSUSED */  void
 static int  print_text(struct html *h, const char *word)
 html_inlinetagname(struct md_mbuf *mbuf,  
                 const struct md_args *args, int tok, size_t *res)  
 {  {
   
         switch (tok) {          if (word[0] && '\0' == word[1])
         case (ROFF_Pp):                  switch (word[0]) {
                 return(ml_nputs(mbuf, "div", 3, res));                  case('.'):
         case (ROFF_Sx):                          /* FALLTHROUGH */
                 return(ml_nputs(mbuf, "a", 1, res));                  case(','):
         default:                          /* FALLTHROUGH */
                 break;                  case(';'):
                           /* FALLTHROUGH */
                   case(':'):
                           /* FALLTHROUGH */
                   case('?'):
                           /* FALLTHROUGH */
                   case('!'):
                           /* FALLTHROUGH */
                   case(')'):
                           /* FALLTHROUGH */
                   case(']'):
                           if ( ! (HTML_IGNDELIM & h->flags))
                                   h->flags |= HTML_NOSPACE;
                           break;
                   default:
                           break;
                   }
   
           if ( ! (HTML_NOSPACE & h->flags)) {
                   /* Manage keeps! */
                   if ( ! (HTML_KEEP & h->flags)) {
                           if (HTML_PREKEEP & h->flags)
                                   h->flags |= HTML_KEEP;
                           putchar(' ');
                   } else
                           printf("&#160;");
         }          }
   
         return(ml_puts(mbuf, "span", res));          assert(word);
           if ( ! print_encode(h, word, 0))
                   h->flags &= ~HTML_NOSPACE;
   
           /*
            * Note that we don't process the pipe: the parser sees it as
            * punctuation, but we don't in terms of typography.
            */
           if (word[0] && '\0' == word[1])
                   switch (word[0]) {
                   case('('):
                           /* FALLTHROUGH */
                   case('['):
                           h->flags |= HTML_NOSPACE;
                           break;
                   default:
                           break;
                   }
 }  }
   
   
 static ssize_t  void
 html_begintag(struct md_mbuf *mbuf, void *data,  print_tagq(struct html *h, const struct tag *until)
                 const struct md_args *args, enum md_ns ns,  
                 int tok, const int *argc, const char **argv)  
 {  {
         size_t           res;          struct tag      *tag;
         struct htmlq    *q;  
         struct htmlnode *node;  
         int              i;  
   
         assert(ns != MD_NS_DEFAULT);          while ((tag = h->tags.head) != NULL) {
         res = 0;                  if (tag == h->metaf)
                           h->metaf = NULL;
                   print_ctag(h, tag->tag);
                   h->tags.head = tag->next;
                   free(tag);
                   if (until && tag == until)
                           return;
           }
   }
   
         assert(data);  
         q = (struct htmlq *)data;  
   
         if (NULL == (node = calloc(1, sizeof(struct htmlnode)))) {  void
                 warn("calloc");  print_stagq(struct html *h, const struct tag *suntil)
                 return(-1);  {
           struct tag      *tag;
   
           while ((tag = h->tags.head) != NULL) {
                   if (suntil && tag == suntil)
                           return;
                   if (tag == h->metaf)
                           h->metaf = NULL;
                   print_ctag(h, tag->tag);
                   h->tags.head = tag->next;
                   free(tag);
         }          }
   }
   
         node->parent = q->last;  
         node->tok = tok;  
         node->ns = ns;  
   
         if (argc)  {  void
                 /* TODO: argv. */  bufinit(struct html *h)
   {
   
                 assert(argv);          h->buf[0] = '\0';
                 for (i = 0; ROFF_ARGMAX != argc[i]          h->buflen = 0;
                                 && i < ROFF_MAXLINEARG; i++)  }
                         node->argc[i] = argc[i];  
                 assert(i != ROFF_MAXLINEARG);  
         }  
   
   
         q->last = node;  void
   bufcat_style(struct html *h, const char *key, const char *val)
   {
   
         switch (ns) {          bufcat(h, key);
         case (MD_NS_BLOCK):          bufncat(h, ":", 1);
                 if ( ! html_blocktagname(mbuf, args, tok,          bufcat(h, val);
                                         q, argc, argv, &res))          bufncat(h, ";", 1);
                         return(-1);  
                 if ( ! html_blocktagargs(mbuf, args, tok,  
                                         argc, argv, &res))  
                         return(-1);  
                 break;  
         case (MD_NS_BODY):  
                 if ( ! html_bodytagname(mbuf, args, tok,  
                                         q, argc, argv, &res))  
                         return(-1);  
                 if ( ! html_bodytagargs(mbuf, args, tok,  
                                         argc, argv, &res))  
                         return(-1);  
                 break;  
         case (MD_NS_HEAD):  
                 if ( ! html_headtagname(mbuf, args, tok, q,  
                                         argc, argv, &res))  
                         return(-1);  
                 if ( ! html_headtagargs(mbuf, args, tok,  
                                         argc, argv, &res))  
                         return(-1);  
                 break;  
         default:  
                 if ( ! html_inlinetagname(mbuf, args, tok, &res))  
                         return(-1);  
                 if ( ! html_inlinetagargs(mbuf, args, tok,  
                                         argc, argv, &res))  
                         return(-1);  
                 break;  
         }  
   
         return((ssize_t)res);  
 }  }
   
   
 static ssize_t  void
 html_endtag(struct md_mbuf *mbuf, void *data,  bufcat(struct html *h, const char *p)
                 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);          bufncat(h, p, strlen(p));
         res = 0;  }
   
         assert(data);  
         q = (struct htmlq *)data;  
         node = q->last;  
   
         switch (ns) {  void
         case (MD_NS_BLOCK):  buffmt(struct html *h, const char *fmt, ...)
                 if ( ! html_blocktagname(mbuf, args, tok,  {
                                         q, node->argc,          va_list          ap;
                                         (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;          va_start(ap, fmt);
           (void)vsnprintf(h->buf + (int)h->buflen,
                           BUFSIZ - h->buflen - 1, fmt, ap);
           va_end(ap);
           h->buflen = strlen(h->buf);
   }
   
         free(node);  
   
         return((ssize_t)res);  void
   bufncat(struct html *h, const char *p, size_t sz)
   {
   
           if (h->buflen + sz > BUFSIZ - 1)
                   sz = BUFSIZ - 1 - h->buflen;
   
           (void)strncat(h->buf, p, sz);
           h->buflen += sz;
 }  }
   
   
 static int  void
 html_alloc(void **p)  buffmt_includes(struct html *h, const char *name)
 {  {
           const char      *p, *pp;
   
         if (NULL == (*p = calloc(1, sizeof(struct htmlq)))) {          pp = h->base_includes;
                 warn("calloc");  
                 return(0);          while (NULL != (p = strchr(pp, '%'))) {
                   bufncat(h, pp, (size_t)(p - pp));
                   switch (*(p + 1)) {
                   case('I'):
                           bufcat(h, name);
                           break;
                   default:
                           bufncat(h, p, 2);
                           break;
                   }
                   pp = p + 2;
         }          }
         return(1);          if (pp)
                   bufcat(h, pp);
 }  }
   
   
 static void  void
 html_free(void *p)  buffmt_man(struct html *h,
                   const char *name, const char *sec)
 {  {
         struct htmlq    *q;          const char      *p, *pp;
         struct htmlnode *n;  
   
         assert(p);          pp = h->base_man;
         q = (struct htmlq *)p;  
           /* LINTED */
         while ((n = q->last)) {          while (NULL != (p = strchr(pp, '%'))) {
                 q->last = n->parent;                  bufncat(h, pp, (size_t)(p - pp));
                 free(n);                  switch (*(p + 1)) {
                   case('S'):
                           bufcat(h, sec ? sec : "1");
                           break;
                   case('N'):
                           buffmt(h, name);
                           break;
                   default:
                           bufncat(h, p, 2);
                           break;
                   }
                   pp = p + 2;
         }          }
           if (pp)
         free(q);                  bufcat(h, pp);
 }  }
   
   
 int  void
 md_line_html(void *data, char *buf)  bufcat_su(struct html *h, const char *p, const struct roffsu *su)
 {  {
           double           v;
           const char      *u;
   
         return(mlg_line((struct md_mlg *)data, buf));          v = su->scale;
   
           switch (su->unit) {
           case (SCALE_CM):
                   u = "cm";
                   break;
           case (SCALE_IN):
                   u = "in";
                   break;
           case (SCALE_PC):
                   u = "pc";
                   break;
           case (SCALE_PT):
                   u = "pt";
                   break;
           case (SCALE_EM):
                   u = "em";
                   break;
           case (SCALE_MM):
                   if (0 == (v /= 100))
                           v = 1;
                   u = "em";
                   break;
           case (SCALE_EN):
                   u = "ex";
                   break;
           case (SCALE_BU):
                   u = "ex";
                   break;
           case (SCALE_VS):
                   u = "em";
                   break;
           default:
                   u = "ex";
                   break;
           }
   
           /*
            * XXX: the CSS spec isn't clear as to which types accept
            * integer or real numbers, so we just make them all decimals.
            */
           buffmt(h, "%s: %.2f%s;", p, v, u);
 }  }
   
   
 int  void
 md_exit_html(void *data, int flush)  html_idcat(char *dst, const char *src, int sz)
 {  {
           int              ssz;
   
         return(mlg_exit((struct md_mlg *)data, flush));          assert(sz);
 }  
   
           /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
   
 void *          for ( ; *dst != '\0' && sz; dst++, sz--)
 md_init_html(const struct md_args *args,                  /* Jump to end. */ ;
                 struct md_mbuf *mbuf, const struct md_rbuf *rbuf)  
 {  
         struct ml_cbs    cbs;  
   
         cbs.ml_alloc = html_alloc;          assert(sz > 2);
         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;  
   
         return(mlg_alloc(args, rbuf, mbuf, &cbs));          /* We can't start with a number (bah). */
 }  
   
           *dst++ = 'x';
           *dst = '\0';
           sz--;
   
           for ( ; *src != '\0' && sz > 1; src++) {
                   ssz = snprintf(dst, (size_t)sz, "%.2x", *src);
                   sz -= ssz;
                   dst += ssz;
           }
   }

Legend:
Removed from v.1.12  
changed lines
  Added in v.1.107

CVSweb