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

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

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

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

CVSweb