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

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

version 1.10, 2008/12/05 22:34:30 version 1.29, 2009/09/16 09:41:24
Line 1 
Line 1 
 /* $Id$ */  /*      $Id$ */
 /*  /*
  * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>   * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
  *   *
  * 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>  
 #include <sys/stat.h>  
   
 #include <assert.h>  #include <assert.h>
 #include <err.h>  #include <err.h>
 #include <fcntl.h>  
 #include <stdlib.h>  
 #include <stdio.h>  #include <stdio.h>
 #include <string.h>  #include <stdlib.h>
 #include <unistd.h>  
   
 #include "libmdocml.h"  #include "mdoc.h"
 #include "private.h"  #include "man.h"
 #include "ml.h"  
   
   #define DOCTYPE         "-//W3C//DTD HTML 4.01//EN"
   #define DTD             "http://www.w3.org/TR/html4/strict.dtd"
   
 /* TODO: allow head/tail-less invocations (just "div" start). */  enum    htmltag {
           TAG_HTML,
           TAG_HEAD,
           TAG_BODY,
           TAG_META,
           TAG_TITLE,
           TAG_DIV,
           TAG_H1,
           TAG_H2,
           TAG_P,
           TAG_SPAN,
           TAG_LINK,
           TAG_MAX
   };
   
 struct  htmlnode {  enum    htmlattr {
         int              tok;          ATTR_HTTPEQUIV,
         enum md_ns       ns;          ATTR_CONTENT,
         int              argc[ROFF_MAXLINEARG];          ATTR_NAME,
         char            *argv[ROFF_MAXLINEARG];          ATTR_REL,
         struct htmlnode *parent;          ATTR_HREF,
           ATTR_TYPE,
           ATTR_MEDIA,
           ATTR_CLASS,
           ATTR_MAX
 };  };
   
   struct  htmldata {
           char             *name;
           int               flags;
   #define HTML_BLOCK       (1 << 0)
   };
   
 struct  htmlq {  static  const struct htmldata htmltags[TAG_MAX] = {
         struct htmlnode *last;          {"html",        HTML_BLOCK}, /* TAG_HTML */
           {"head",        HTML_BLOCK}, /* TAG_HEAD */
           {"body",        HTML_BLOCK}, /* TAG_BODY */
           {"meta",        HTML_BLOCK}, /* TAG_META */
           {"title",       HTML_BLOCK}, /* TAG_TITLE */
           {"div",         HTML_BLOCK}, /* TAG_DIV */
           {"h1",          0}, /* TAG_H1 */
           {"h2",          0}, /* TAG_H2 */
           {"p",           HTML_BLOCK}, /* TAG_P */
           {"span",        0}, /* TAG_SPAN */
           {"link",        HTML_BLOCK}, /* TAG_LINK */
 };  };
   
   static  const char       *const htmlattrs[ATTR_MAX] = {
           "http-equiv",
           "content",
           "name",
           "rel",
           "href",
           "type",
           "media",
           "class"
   };
   
 static  int             html_loadcss(struct md_mbuf *, const char *);  struct  htmlpair {
           enum htmlattr     key;
           char             *val;
   };
   
 static  int             html_alloc(void **);  struct  html {
 static  void            html_free(void *);          int               flags;
 static  ssize_t         html_endtag(struct md_mbuf *, void *,  #define HTML_NOSPACE     (1 << 0)
                                 const struct md_args *,  };
                                 enum md_ns, int);  
 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 *);  
 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 *);  
   
   #define MDOC_ARGS         const struct mdoc_meta *m, \
                             const struct mdoc_node *n, \
                             struct html *h
   #define MAN_ARGS          const struct man_meta *m, \
                             const struct man_node *n, \
                             struct html *h
   struct  htmlmdoc {
           int             (*pre)(MDOC_ARGS);
           void            (*post)(MDOC_ARGS);
   };
   
 /* ARGSUSED */  static  void              print_gen_doctype(struct html *);
 static int  static  void              print_gen_head(struct html *);
 html_It_headtagname(struct md_mbuf *mbuf, struct htmlq *q,  static  void              print_mdoc(MDOC_ARGS);
                 const int *argc, const char **argv, size_t *res)  static  void              print_mdoc_head(MDOC_ARGS);
 {  static  void              print_mdoc_node(MDOC_ARGS);
         struct htmlnode *n;  static  void              print_man(MAN_ARGS);
         int              i, c;  static  void              print_man_head(MAN_ARGS);
   static  void              print_man_body(MAN_ARGS);
   static  void              print_otag(struct html *, enum htmltag,
                                   int, const struct htmlpair *);
   static  void              print_ctag(struct html *, enum htmltag);
   static  void              print_encode(const char *);
   static  void              print_text(struct html *, const char *);
   static  int               mdoc_root_pre(MDOC_ARGS);
   static  void              mdoc_root_post(MDOC_ARGS);
   
         for (n = q->last; n; n = n->parent)  static  int               mdoc_nd_pre(MDOC_ARGS);
                 if (n->tok == ROFF_Bl)  static  int               mdoc_nm_pre(MDOC_ARGS);
                         break;  static  void              mdoc_nm_post(MDOC_ARGS);
   static  int               mdoc_pp_pre(MDOC_ARGS);
   static  int               mdoc_sh_pre(MDOC_ARGS);
   static  void              mdoc_sh_post(MDOC_ARGS);
   static  int               mdoc_ss_pre(MDOC_ARGS);
   static  void              mdoc_ss_post(MDOC_ARGS);
   
         assert(n);  static  const struct htmlmdoc mdocs[MDOC_MAX] = {
         for (i = 0; ROFF_ARGMAX != (c = n->argc[i]) &&          {NULL, NULL}, /* Ap */
                         i < ROFF_MAXLINEARG; i++) {          {NULL, NULL}, /* Dd */
                 switch (n->argc[i]) {          {NULL, NULL}, /* Dt */
                 case (ROFF_Tag):          {NULL, NULL}, /* Os */
                         /* FALLTHROUGH */          {mdoc_sh_pre, mdoc_sh_post }, /* Sh */
                 case (ROFF_Column):          {mdoc_ss_pre, mdoc_ss_post }, /* Ss */
                         return(ml_nputs(mbuf, "td", 2, res));          {mdoc_pp_pre, NULL}, /* Pp */
                 default:          {NULL, NULL}, /* D1 */
                         break;          {NULL, NULL}, /* Dl */
                 }          {NULL, NULL}, /* Bd */
         }          {NULL, NULL}, /* Ed */
           {NULL, NULL}, /* Bl */
           {NULL, NULL}, /* El */
           {NULL, NULL}, /* It */
           {NULL, NULL}, /* Ad */
           {NULL, NULL}, /* An */
           {NULL, NULL}, /* Ar */
           {NULL, NULL}, /* Cd */
           {NULL, NULL}, /* Cm */
           {NULL, NULL}, /* Dv */
           {NULL, NULL}, /* Er */
           {NULL, NULL}, /* Ev */
           {NULL, NULL}, /* Ex */
           {NULL, NULL}, /* Fa */
           {NULL, NULL}, /* Fd */
           {NULL, NULL}, /* Fl */
           {NULL, NULL}, /* Fn */
           {NULL, NULL}, /* Ft */
           {NULL, NULL}, /* Ic */
           {NULL, NULL}, /* In */
           {NULL, NULL}, /* Li */
           {mdoc_nd_pre, NULL}, /* Nd */
           {mdoc_nm_pre, mdoc_nm_post}, /* Nm */
           {NULL, NULL}, /* Op */
           {NULL, NULL}, /* Ot */
           {NULL, NULL}, /* Pa */
           {NULL, NULL}, /* Rv */
           {NULL, NULL}, /* St */
           {NULL, NULL}, /* Va */
           {NULL, NULL}, /* Vt */
           {NULL, NULL}, /* Xr */
           {NULL, NULL}, /* %A */
           {NULL, NULL}, /* %B */
           {NULL, NULL}, /* %D */
           {NULL, NULL}, /* %I */
           {NULL, NULL}, /* %J */
           {NULL, NULL}, /* %N */
           {NULL, NULL}, /* %O */
           {NULL, NULL}, /* %P */
           {NULL, NULL}, /* %R */
           {NULL, NULL}, /* %T */
           {NULL, NULL}, /* %V */
           {NULL, NULL}, /* Ac */
           {NULL, NULL}, /* Ao */
           {NULL, NULL}, /* Aq */
           {NULL, NULL}, /* At */
           {NULL, NULL}, /* Bc */
           {NULL, NULL}, /* Bf */
           {NULL, NULL}, /* Bo */
           {NULL, NULL}, /* Bq */
           {NULL, NULL}, /* Bsx */
           {NULL, NULL}, /* Bx */
           {NULL, NULL}, /* Db */
           {NULL, NULL}, /* Dc */
           {NULL, NULL}, /* Do */
           {NULL, NULL}, /* Dq */
           {NULL, NULL}, /* Ec */
           {NULL, NULL}, /* Ef */
           {NULL, NULL}, /* Em */
           {NULL, NULL}, /* Eo */
           {NULL, NULL}, /* Fx */
           {NULL, NULL}, /* Ms */
           {NULL, NULL}, /* No */
           {NULL, NULL}, /* Ns */
           {NULL, NULL}, /* Nx */
           {NULL, NULL}, /* Ox */
           {NULL, NULL}, /* Pc */
           {NULL, NULL}, /* Pf */
           {NULL, NULL}, /* Po */
           {NULL, NULL}, /* Pq */
           {NULL, NULL}, /* Qc */
           {NULL, NULL}, /* Ql */
           {NULL, NULL}, /* Qo */
           {NULL, NULL}, /* Qq */
           {NULL, NULL}, /* Re */
           {NULL, NULL}, /* Rs */
           {NULL, NULL}, /* Sc */
           {NULL, NULL}, /* So */
           {NULL, NULL}, /* Sq */
           {NULL, NULL}, /* Sm */
           {NULL, NULL}, /* Sx */
           {NULL, NULL}, /* Sy */
           {NULL, NULL}, /* Tn */
           {NULL, NULL}, /* Ux */
           {NULL, NULL}, /* Xc */
           {NULL, NULL}, /* Xo */
           {NULL, NULL}, /* Fo */
           {NULL, NULL}, /* Fc */
           {NULL, NULL}, /* Oo */
           {NULL, NULL}, /* Oc */
           {NULL, NULL}, /* Bk */
           {NULL, NULL}, /* Ek */
           {NULL, NULL}, /* Bt */
           {NULL, NULL}, /* Hf */
           {NULL, NULL}, /* Fr */
           {NULL, NULL}, /* Ud */
           {NULL, NULL}, /* Lb */
           {NULL, NULL}, /* Lp */
           {NULL, NULL}, /* Lk */
           {NULL, NULL}, /* Mt */
           {NULL, NULL}, /* Brq */
           {NULL, NULL}, /* Bro */
           {NULL, NULL}, /* Brc */
           {NULL, NULL}, /* %C */
           {NULL, NULL}, /* Es */
           {NULL, NULL}, /* En */
           {NULL, NULL}, /* Dx */
           {NULL, NULL}, /* %Q */
           {NULL, NULL}, /* br */
           {NULL, NULL}, /* sp */
   };
   
         assert(i != ROFF_MAXLINEARG);  
         abort();  
         /* NOTREACHED */  
   
   int
   html_mdoc(void *arg, const struct mdoc *m)
   {
           struct html     *h;
   
           h = (struct html *)arg;
   
           print_gen_doctype(h);
           print_otag(h, TAG_HTML, 0, NULL);
           print_mdoc(mdoc_meta(m), mdoc_node(m), h);
           print_ctag(h, TAG_HTML);
           printf("\n");
         return(1);          return(1);
 }  }
   
   
 /* ARGSUSED */  int
 static int  html_man(void *arg, const struct man *m)
 html_It_bodytagname(struct md_mbuf *mbuf, struct htmlq *q,  
                 const int *argc, const char **argv, size_t *res)  
 {  {
         struct htmlnode *n;          struct html     *h;
         int              i, c;  
   
         for (n = q->last; n; n = n->parent)          h = (struct html *)arg;
                 if (n->tok == ROFF_Bl)  
                         break;  
   
         assert(n);          print_gen_doctype(h);
         for (i = 0; ROFF_ARGMAX != (c = n->argc[i]) &&          print_otag(h, TAG_HTML, 0, NULL);
                         i < ROFF_MAXLINEARG; i++) {          print_man(man_meta(m), man_node(m), h);
                 switch (n->argc[i]) {          print_ctag(h, TAG_HTML);
                 case (ROFF_Enum):          printf("\n");
                         /* 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);  
         abort();  
         /* NOTREACHED */  
   
         return(1);          return(1);
 }  }
   
   
 /* ARGSUSED */  void *
 static int  html_alloc(void)
 html_Bl_bodytagname(struct md_mbuf *mbuf, struct htmlq *q,  
                 const int *argc, const char **argv, size_t *res)  
 {  {
         int              c, i;  
   
         for (i = 0; ROFF_ARGMAX != (c = argc[i])          return(calloc(1, sizeof(struct html)));
                         && 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;  
         int              i, c;  
   
         for (n = q->last; n; n = n->parent)          free(p);
                 if (n->tok == ROFF_Bl)  
                         break;  
   
         assert(n);  
         for (i = 0; ROFF_ARGMAX != (c = n->argc[i]) &&  
                         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, "li", 2, res));  
                 case (ROFF_Tag):  
                         /* FALLTHROUGH */  
                 case (ROFF_Column):  
                         return(ml_nputs(mbuf, "tr", 2, res));  
                 default:  
                         break;  
                 }  
         }  
   
         assert(i != ROFF_MAXLINEARG);  
         abort();  
         /* NOTREACHED */  
 }  }
   
   
 static int  static void
 html_loadcss(struct md_mbuf *mbuf, const char *css)  print_mdoc(MDOC_ARGS)
 {  {
         size_t           res, bufsz;  
         char            *buf;  
         struct stat      st;  
         int              fd, c;  
         ssize_t          ssz;  
   
         c = 0;          print_otag(h, TAG_HEAD, 0, NULL);
         res = 0;          print_mdoc_head(m, n, h);
         buf = NULL;          print_ctag(h, TAG_HEAD);
           print_otag(h, TAG_BODY, 0, NULL);
           print_mdoc_node(m, n, h);
           print_ctag(h, TAG_BODY);
   }
   
         if (-1 == (fd = open(css, O_RDONLY, 0))) {  
                 warn("%s", css);  
                 return(0);  
         }  
   
         if (-1 == fstat(fd, &st)) {  
                 warn("%s", css);  
                 goto out;  
         }  
   
         bufsz = MAX(st.st_blksize, BUFSIZ);  static void
         if (NULL == (buf = malloc(bufsz))) {  print_gen_head(struct html *h)
                 warn("malloc");  {
                 goto out;          struct htmlpair  meta0[2];
         }          struct htmlpair  meta1[2];
           struct htmlpair  link[4];
   
         for (;;) {          meta0[0].key = ATTR_HTTPEQUIV;
                 if (-1 == (ssz = read(fd, buf, bufsz))) {          meta0[0].val = "Content-Type";
                         warn("%s", css);          meta0[1].key = ATTR_CONTENT;
                         goto out;          meta0[1].val = "text/html; charest-utf-8";
                 } else if (0 == ssz)  
                         break;  
                 if ( ! ml_nputs(mbuf, buf, (size_t)ssz, &res))  
                         goto out;  
         }  
   
         c = 1;          meta1[0].key = ATTR_NAME;
           meta1[0].val = "resource-type";
           meta1[1].key = ATTR_CONTENT;
           meta1[1].val = "document";
   
 out:          link[0].key = ATTR_REL;
         if (-1 == close(fd)) {          link[0].val = "stylesheet";
                 warn("%s", css);          link[1].key = ATTR_HREF;
                 c = 0;          link[1].val = "style.css";
         }          link[2].key = ATTR_TYPE;
           link[2].val = "text/css";
           link[3].key = ATTR_MEDIA;
           link[3].val = "all";
   
         if (buf)          print_otag(h, TAG_META, 2, meta0);
                 free(buf);          print_otag(h, TAG_META, 2, meta1);
           print_otag(h, TAG_LINK, 4, link);
         return(c);  
 }  }
   
   
 /* ARGSUSED */  static void
 static int  print_mdoc_head(MDOC_ARGS)
 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 =          print_gen_head(h);
         "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n"          print_otag(h, TAG_TITLE, 0, NULL);
         "    \"http://www.w3.org/TR/html4/strict.dtd\">\n"          print_encode(m->title);
         "<html>\n"          print_ctag(h, TAG_TITLE);
         "<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 =  
         "    <link rel=\"stylesheet\" type=\"text/css\"\n"  
         "         href=\"%s\">\n";  
         trail =  
         "</head>\n"  
         "<body>\n"  
         "<div class=\"mdoc\">";  
   
         res = 0;  static int
   mdoc_root_pre(MDOC_ARGS)
   {
           struct htmlpair  div;
   
         (void)snprintf(buf, sizeof(buf) - 1,          div.key = ATTR_CLASS;
                         preamble, title, section);          div.val = "body";
   
         if ( ! ml_puts(mbuf, buf, &res))          print_otag(h, TAG_DIV, 1, &div);
                 return(0);  
   
         assert(args->params.html.css);  
         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))  
                 return(0);  
   
         return(1);          return(1);
 }  }
   
   
 /* ARGSUSED */  static void
 static int  mdoc_root_post(MDOC_ARGS)
 html_end(struct md_mbuf *mbuf, const struct md_args *args)  
 {  {
   
         return(ml_puts(mbuf, "</div></body>\n</html>", NULL));          print_ctag(h, TAG_DIV);
 }  }
   
   
 /* ARGSUSED */  
 static int  static int
 html_blockbodytagname(struct md_mbuf *mbuf,  mdoc_ss_pre(MDOC_ARGS)
                 const struct md_args *args, int tok, struct htmlq *q,  
                 const int *argc, const char **argv, size_t *res)  
 {  {
   
         switch (tok) {          if (MDOC_BODY == n->type)
         case (ROFF_Bl):                  print_otag(h, TAG_P, 0, NULL);
                 return(html_Bl_bodytagname(mbuf, q, argc, argv, res));          if (MDOC_HEAD == n->type)
         case (ROFF_It):                  print_otag(h, TAG_H2, 0, NULL);
                 return(html_It_bodytagname(mbuf, q, argc, argv, res));          return(1);
         default:  
                 break;  
         }  
   
         return(ml_puts(mbuf, "div", res));  
 }  }
   
   
 /* ARGSUSED */  static void
 static int  mdoc_ss_post(MDOC_ARGS)
 html_headtagname(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) {          if (MDOC_BODY == n->type)
         case (ROFF_It):                  print_ctag(h, TAG_P);
                 return(html_It_headtagname(mbuf, q, argc, argv, res));          if (MDOC_HEAD == n->type)
         case (ROFF_Sh):                  print_ctag(h, TAG_H2);
                 return(ml_puts(mbuf, "h1", res));  
         case (ROFF_Ss):  
                 return(ml_puts(mbuf, "h2", res));  
         default:  
                 break;  
         }  
   
         return(ml_puts(mbuf, "div", res));  
 }  }
   
   
 /* ARGSUSED */  
 static int  static int
 html_blocktagname(struct md_mbuf *mbuf, const struct md_args *args,  mdoc_pp_pre(MDOC_ARGS)
                 int tok, struct htmlq *q, const int *argc,  
                 const char **argv, size_t *res)  
 {  {
   
         switch (tok) {          print_otag(h, TAG_P, 0, NULL);
         case (ROFF_It):          return(0);
                 return(html_It_blocktagname(mbuf, q, argc, argv, res));  
         default:  
                 break;  
         }  
   
         return(ml_puts(mbuf, "div", res));  
 }  }
   
   
 /* ARGSUSED */  
 static int  static int
 html_printargs(struct md_mbuf *mbuf, int tok, const char *ns,  mdoc_nd_pre(MDOC_ARGS)
                 const int *argc, const char **argv, size_t *res)  
 {  {
   
         if ( ! ml_puts(mbuf, " class=\"", res))          if (MDOC_BODY == n->type)
                 return(0);                  print_text(h, "--");
         if ( ! ml_puts(mbuf, ns, res))          return(1);
                 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 int  static int
 html_headtagargs(struct md_mbuf *mbuf,  mdoc_nm_pre(MDOC_ARGS)
                 const struct md_args *args, int tok,  
                 const int *argc, const char **argv, size_t *res)  
 {  {
           struct htmlpair class;
   
         return(html_printargs(mbuf, tok, "head", argc, argv, res));          class.key = ATTR_CLASS;
 }          class.val = "name";
   
           print_otag(h, TAG_SPAN, 1, &class);
           if (NULL == n->child)
                   print_text(h, m->name);
   
 /* ARGSUSED */          return(1);
 static int  
 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));  
 }  }
   
   
 /* ARGSUSED */  static void
 static int  mdoc_nm_post(MDOC_ARGS)
 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));          print_ctag(h, TAG_SPAN);
 }  }
   
   
 /* ARGSUSED */  
 static int  static int
 html_inlinetagargs(struct md_mbuf *mbuf,  mdoc_sh_pre(MDOC_ARGS)
                 const struct md_args *args, int tok,  
                 const int *argc, const char **argv, size_t *res)  
 {  {
   
         return(html_printargs(mbuf, tok, "inline", argc, argv, res));          if (MDOC_BODY == n->type)
                   print_otag(h, TAG_P, 0, NULL);
           if (MDOC_HEAD == n->type)
                   print_otag(h, TAG_H1, 0, NULL);
           return(1);
 }  }
   
   
 /* ARGSUSED */  static void
 static int  mdoc_sh_post(MDOC_ARGS)
 html_inlinetagname(struct md_mbuf *mbuf,  
                 const struct md_args *args, int tok, size_t *res)  
 {  {
   
         switch (tok) {          if (MDOC_BODY == n->type)
         case (ROFF_Pp):                  print_ctag(h, TAG_P);
                 return(ml_puts(mbuf, "div", res));          if (MDOC_HEAD == n->type)
         default:                  print_ctag(h, TAG_H1);
                 break;  
         }  
   
         return(ml_puts(mbuf, "span", res));  
 }  }
   
   
 static ssize_t  static void
 html_begintag(struct md_mbuf *mbuf, void *data,  print_mdoc_node(MDOC_ARGS)
                 const struct md_args *args, enum md_ns ns,  
                 int tok, const int *argc, const char **argv)  
 {  {
         size_t           res;          int              child;
         struct htmlq    *q;  
         struct htmlnode *node;  
         int              i, c;  
   
         assert(ns != MD_NS_DEFAULT);          child = 1;
         res = 0;  
   
         assert(data);          switch (n->type) {
         q = (struct htmlq *)data;          case (MDOC_ROOT):
                   child = mdoc_root_pre(m, n, h);
         if (NULL == (node = calloc(1, sizeof(struct htmlnode)))) {                  break;
                 warn("calloc");          case (MDOC_TEXT):
                 return(-1);                  print_text(h, n->string);
                   break;
           default:
                   if (mdocs[n->tok].pre)
                           child = (*mdocs[n->tok].pre)(m, n, h);
                   break;
         }          }
   
         node->parent = q->last;          if (child && n->child)
         node->tok = tok;                  print_mdoc_node(m, n->child, h);
         node->ns = ns;  
   
         if (argc)  {          switch (n->type) {
                 /* TODO: argv. */          case (MDOC_ROOT):
                   mdoc_root_post(m, n, h);
                 assert(argv);  
                 for (i = 0; ROFF_ARGMAX != (c = 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 (MDOC_TEXT):
                 if ( ! html_blockbodytagname(mbuf, args, tok,  
                                         q, argc, argv, &res))  
                         return(-1);  
                 if ( ! html_blockbodytagargs(mbuf, args, tok,  
                                         argc, argv, &res))  
                         return(-1);  
                 break;                  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:          default:
                 if ( ! html_inlinetagname(mbuf, args, tok, &res))                  if (mdocs[n->tok].post)
                         return(-1);                          (*mdocs[n->tok].post)(m, n, h);
                 if ( ! html_inlinetagargs(mbuf, args, tok,  
                                         argc, argv, &res))  
                         return(-1);  
                 break;                  break;
         }          }
   
         return((ssize_t)res);          if (n->next)
                   print_mdoc_node(m, n->next, h);
 }  }
   
   
 static ssize_t  static void
 html_endtag(struct md_mbuf *mbuf, void *data,  print_man(MAN_ARGS)
                 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);          print_otag(h, TAG_HEAD, 0, NULL);
         res = 0;          print_man_head(m, n, h);
           print_ctag(h, TAG_HEAD);
           print_otag(h, TAG_BODY, 0, NULL);
           print_man_body(m, n, h);
           print_ctag(h, TAG_BODY);
   }
   
         assert(data);  
         q = (struct htmlq *)data;  
         node = q->last;  
   
         switch (ns) {  static void
         case (MD_NS_BLOCK):  print_man_head(MAN_ARGS)
                 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;          print_gen_head(h);
           print_otag(h, TAG_TITLE, 0, NULL);
           print_encode(m->title);
           print_ctag(h, TAG_TITLE);
   }
   
         free(node);  
   
         return((ssize_t)res);  static void
   print_man_body(MAN_ARGS)
   {
 }  }
   
   
 static int  static void
 html_alloc(void **p)  print_encode(const char *p)
 {  {
   
         if (NULL == (*p = calloc(1, sizeof(struct htmlq)))) {          printf("%s", p); /* XXX */
                 warn("calloc");  
                 return(0);  
         }  
         return(1);  
 }  }
   
   
 static void  static void
 html_free(void *p)  print_otag(struct html *h, enum htmltag tag,
                   int sz, const struct htmlpair *p)
 {  {
         struct htmlq    *q;          int              i;
         struct htmlnode *n;  
   
         assert(p);          if ( ! (HTML_NOSPACE & h->flags))
         q = (struct htmlq *)p;                  if ( ! (HTML_BLOCK & htmltags[tag].flags))
                           printf(" ");
   
         while ((n = q->last)) {          printf("<%s", htmltags[tag].name);
                 q->last = n->parent;          for (i = 0; i < sz; i++) {
                 free(n);                  printf(" %s=\"", htmlattrs[p[i].key]);
                   assert(p->val);
                   print_encode(p[i].val);
                   printf("\"");
         }          }
           printf(">");
   
         free(q);          h->flags |= HTML_NOSPACE;
   
 }  }
   
   
 int  /* ARGSUSED */
 md_line_html(void *data, char *buf)  static void
   print_ctag(struct html *h, enum htmltag tag)
 {  {
   
         return(mlg_line((struct md_mlg *)data, buf));          printf("</%s>", htmltags[tag].name);
           if (HTML_BLOCK & htmltags[tag].flags)
                   h->flags |= HTML_NOSPACE;
 }  }
   
   
 int  /* ARGSUSED */
 md_exit_html(void *data, int flush)  static void
   print_gen_doctype(struct html *h)
 {  {
   
         return(mlg_exit((struct md_mlg *)data, flush));          printf("<!DOCTYPE HTML PUBLIC \"%s\" \"%s\">\n", DOCTYPE, DTD);
 }  }
   
   
 void *  static void
 md_init_html(const struct md_args *args,  print_text(struct html *h, const char *p)
                 struct md_mbuf *mbuf, const struct md_rbuf *rbuf)  
 {  {
         struct ml_cbs    cbs;  
   
         cbs.ml_alloc = html_alloc;          if (*p && 0 == *(p + 1))
         cbs.ml_free = html_free;                  switch (*p) {
         cbs.ml_begintag = html_begintag;                  case('.'):
         cbs.ml_endtag = html_endtag;                          /* FALLTHROUGH */
         cbs.ml_begin = html_begin;                  case(','):
         cbs.ml_end = html_end;                          /* FALLTHROUGH */
                   case(';'):
                           /* FALLTHROUGH */
                   case(':'):
                           /* FALLTHROUGH */
                   case('?'):
                           /* FALLTHROUGH */
                   case('!'):
                           /* FALLTHROUGH */
                   case(')'):
                           /* FALLTHROUGH */
                   case(']'):
                           /* FALLTHROUGH */
                   case('}'):
                           h->flags |= HTML_NOSPACE;
                   default:
                           break;
                   }
   
         return(mlg_alloc(args, rbuf, mbuf, &cbs));          if ( ! (h->flags & HTML_NOSPACE))
 }                  printf(" ");
           h->flags &= ~HTML_NOSPACE;
   
           if (p)
                   print_encode(p);
   
           if (*p && 0 == *(p + 1))
                   switch (*p) {
                   case('('):
                           /* FALLTHROUGH */
                   case('['):
                           /* FALLTHROUGH */
                   case('{'):
                           h->flags |= HTML_NOSPACE;
                   default:
                           break;
                   }
   }

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

CVSweb