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

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

version 1.7, 2008/12/05 11:28:16 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"
   
 struct  htmlnode {  enum    htmltag {
         int              type;          TAG_HTML,
         int             *argc[ROFF_MAXLINEARG];          TAG_HEAD,
         char            *argv[ROFF_MAXLINEARG];          TAG_BODY,
         struct htmlnode *parent;          TAG_META,
           TAG_TITLE,
           TAG_DIV,
           TAG_H1,
           TAG_H2,
           TAG_P,
           TAG_SPAN,
           TAG_LINK,
           TAG_MAX
 };  };
   
   enum    htmlattr {
           ATTR_HTTPEQUIV,
           ATTR_CONTENT,
           ATTR_NAME,
           ATTR_REL,
           ATTR_HREF,
           ATTR_TYPE,
           ATTR_MEDIA,
           ATTR_CLASS,
           ATTR_MAX
   };
   
 struct  htmlq {  struct  htmldata {
         struct htmlnode *last;          char             *name;
           int               flags;
   #define HTML_BLOCK       (1 << 0)
 };  };
   
   static  const struct htmldata htmltags[TAG_MAX] = {
           {"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  int             html_loadcss(struct md_mbuf *, const char *);  static  const char       *const htmlattrs[ATTR_MAX] = {
           "http-equiv",
           "content",
           "name",
           "rel",
           "href",
           "type",
           "media",
           "class"
   };
   
 static  ssize_t         html_endtag(struct md_mbuf *,  struct  htmlpair {
                                 const struct md_args *,          enum htmlattr     key;
                                 enum md_ns, int);          char             *val;
 static  ssize_t         html_begintag(struct md_mbuf *,  };
                                 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, size_t *);  
 static  int             html_blocktagargs(struct md_mbuf *,  
                                 const struct md_args *, int,  
                                 const int *, const char **, size_t *);  
 static  int             html_blockheadtagname(struct md_mbuf *,  
                                 const struct md_args *, int, size_t *);  
 static  int             html_blockheadtagargs(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, 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 *);  
   
   struct  html {
           int               flags;
   #define HTML_NOSPACE     (1 << 0)
   };
   
 static int  #define MDOC_ARGS         const struct mdoc_meta *m, \
 html_loadcss(struct md_mbuf *mbuf, const char *css)                            const struct mdoc_node *n, \
 {                            struct html *h
         size_t           res, bufsz;  #define MAN_ARGS          const struct man_meta *m, \
         char            *buf;                            const struct man_node *n, \
         struct stat      st;                            struct html *h
         int              fd, c;  struct  htmlmdoc {
         ssize_t          ssz;          int             (*pre)(MDOC_ARGS);
           void            (*post)(MDOC_ARGS);
   };
   
         c = 0;  static  void              print_gen_doctype(struct html *);
         res = 0;  static  void              print_gen_head(struct html *);
         buf = NULL;  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);
   
         if (-1 == (fd = open(css, O_RDONLY, 0))) {  static  int               mdoc_nd_pre(MDOC_ARGS);
                 warn("%s", css);  static  int               mdoc_nm_pre(MDOC_ARGS);
                 return(0);  static  void              mdoc_nm_post(MDOC_ARGS);
         }  static  int               mdoc_pp_pre(MDOC_ARGS);
   static  int               mdoc_sh_pre(MDOC_ARGS);
         if (-1 == fstat(fd, &st)) {  static  void              mdoc_sh_post(MDOC_ARGS);
                 warn("%s", css);  static  int               mdoc_ss_pre(MDOC_ARGS);
                 goto out;  static  void              mdoc_ss_post(MDOC_ARGS);
         }  
   
         bufsz = MAX(st.st_blksize, BUFSIZ);  static  const struct htmlmdoc mdocs[MDOC_MAX] = {
         if (NULL == (buf = malloc(bufsz))) {          {NULL, NULL}, /* Ap */
                 warn("malloc");          {NULL, NULL}, /* Dd */
                 goto out;          {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 */
   };
   
         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;  int
   html_mdoc(void *arg, const struct mdoc *m)
   {
           struct html     *h;
   
 out:          h = (struct html *)arg;
         if (-1 == close(fd)) {  
                 warn("%s", css);  
                 c = 0;  
         }  
   
         if (buf)          print_gen_doctype(h);
                 free(buf);          print_otag(h, TAG_HTML, 0, NULL);
           print_mdoc(mdoc_meta(m), mdoc_node(m), h);
         return(c);          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 int  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];
   
         return(ml_puts(mbuf, "div", res));          meta0[0].key = ATTR_HTTPEQUIV;
           meta0[0].val = "Content-Type";
           meta0[1].key = ATTR_CONTENT;
           meta0[1].val = "text/html; charest-utf-8";
   
           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 int  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;
   
         return(ml_puts(mbuf, "div", res));          div.key = ATTR_CLASS;
           div.val = "body";
   
           print_otag(h, TAG_DIV, 1, &div);
           return(1);
 }  }
   
   
 /* ARGSUSED */  static void
 static int  mdoc_root_post(MDOC_ARGS)
 html_blocktagname(struct md_mbuf *mbuf,  
                 const struct md_args *args, int tok, size_t *res)  
 {  {
   
         return(ml_puts(mbuf, "div", res));          print_ctag(h, TAG_DIV);
 }  }
   
   
 static int  static int
 html_printargs(struct md_mbuf *mbuf, int tok, const char *ns,  mdoc_ss_pre(MDOC_ARGS)
                 const int *argc, const char **argv, size_t *res)  
 {  {
         int              i, c;  
   
         if ( ! ml_puts(mbuf, " class=\"", res))          if (MDOC_BODY == n->type)
                 return(0);                  print_otag(h, TAG_P, 0, NULL);
         if ( ! ml_puts(mbuf, ns, res))          if (MDOC_HEAD == n->type)
                 return(0);                  print_otag(h, TAG_H2, 0, NULL);
         if ( ! ml_puts(mbuf, "-", res))          return(1);
                 return(0);  }
         if ( ! ml_puts(mbuf, toknames[tok], res))  
                 return(0);  
         if ( ! ml_puts(mbuf, "\"", res))  
                 return(0);  
   
         if (NULL == argv || NULL == argc)  
                 return(1);  
         assert(argv && argc);  
   
         /* FIXME: ignores values. */  
   
         for (i = 0; ROFF_ARGMAX != (c = argc[i]); i++) {  static void
                 if (argv[i])  mdoc_ss_post(MDOC_ARGS)
                         continue;  {
                 if ( ! ml_puts(mbuf, " class=\"", res))  
                         return(0);  
                 if ( ! ml_puts(mbuf, ns, res))  
                         return(0);  
                 if ( ! ml_puts(mbuf, "-", res))  
                         return(0);  
                 if ( ! ml_puts(mbuf, toknames[tok], res))  
                         return(0);  
                 if ( ! ml_puts(mbuf, "-", res))  
                         return(0);  
                 if ( ! ml_puts(mbuf, tokargnames[c], res))  
                         return(0);  
                 if ( ! ml_puts(mbuf, "\"", res))  
                         return(0);  
         }  
   
         return(1);          if (MDOC_BODY == n->type)
                   print_ctag(h, TAG_P);
           if (MDOC_HEAD == n->type)
                   print_ctag(h, TAG_H2);
 }  }
   
   
 /* ARGSUSED */  
 static int  static int
 html_blockheadtagargs(struct md_mbuf *mbuf,  mdoc_pp_pre(MDOC_ARGS)
                 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));          print_otag(h, TAG_P, 0, NULL);
           return(0);
 }  }
   
   
 /* ARGSUSED */  
 static int  static int
 html_blockbodytagargs(struct md_mbuf *mbuf,  mdoc_nd_pre(MDOC_ARGS)
                 const struct md_args *args, int tok,  
                 const int *argc, const char **argv, size_t *res)  
 {  {
   
         return(html_printargs(mbuf, tok, "body", argc, argv, res));          if (MDOC_BODY == n->type)
                   print_text(h, "--");
           return(1);
 }  }
   
   
 /* ARGSUSED */  
 static int  static int
 html_blocktagargs(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, "block", 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);
   
           return(1);
 }  }
   
   
 /* ARGSUSED */  static void
 static int  mdoc_nm_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)  
 {  {
   
         return(html_printargs(mbuf, tok, "inline", argc, argv, res));          print_ctag(h, TAG_SPAN);
 }  }
   
   
 /* ARGSUSED */  
 static int  static int
 html_inlinetagname(struct md_mbuf *mbuf,  mdoc_sh_pre(MDOC_ARGS)
                 const struct md_args *args, int tok, size_t *res)  
 {  {
   
         switch (tok) {          if (MDOC_BODY == n->type)
         case (ROFF_Pp):                  print_otag(h, TAG_P, 0, NULL);
                 return(ml_puts(mbuf, "div", res));          if (MDOC_HEAD == n->type)
         default:                  print_otag(h, TAG_H1, 0, NULL);
                 return(ml_puts(mbuf, "span", res));  
         }  
         return(1);          return(1);
 }  }
   
   
 static ssize_t  static void
 html_begintag(struct md_mbuf *mbuf, const struct md_args *args,  mdoc_sh_post(MDOC_ARGS)
                 enum md_ns ns, int tok,  
                 const int *argc, const char **argv)  
 {  {
         size_t           res;  
   
         assert(ns != MD_NS_DEFAULT);          if (MDOC_BODY == n->type)
         res = 0;                  print_ctag(h, TAG_P);
           if (MDOC_HEAD == n->type)
                   print_ctag(h, TAG_H1);
   }
   
         switch (ns) {  
         case (MD_NS_BLOCK):  static void
                 if ( ! html_blocktagname(mbuf, args, tok, &res))  print_mdoc_node(MDOC_ARGS)
                         return(-1);  {
                 if ( ! html_blocktagargs(mbuf, args, tok,          int              child;
                                         argc, argv, &res))  
                         return(-1);          child = 1;
   
           switch (n->type) {
           case (MDOC_ROOT):
                   child = mdoc_root_pre(m, n, h);
                 break;                  break;
         case (MD_NS_BODY):          case (MDOC_TEXT):
                 if ( ! html_blockbodytagname(mbuf, args, tok, &res))                  print_text(h, n->string);
                         return(-1);  
                 if ( ! html_blockbodytagargs(mbuf, args, tok,  
                                         argc, argv, &res))  
                         return(-1);  
                 break;                  break;
         case (MD_NS_HEAD):          default:
                 if ( ! html_blockheadtagname(mbuf, args, tok, &res))                  if (mdocs[n->tok].pre)
                         return(-1);                          child = (*mdocs[n->tok].pre)(m, n, h);
                 if ( ! html_blockheadtagargs(mbuf, args, tok,  
                                         argc, argv, &res))  
                         return(-1);  
                 break;                  break;
           }
   
           if (child && n->child)
                   print_mdoc_node(m, n->child, h);
   
           switch (n->type) {
           case (MDOC_ROOT):
                   mdoc_root_post(m, n, h);
                   break;
           case (MDOC_TEXT):
                   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, const struct md_args *args,  print_man(MAN_ARGS)
                 enum md_ns ns, int tok)  
 {  {
         size_t           res;  
   
         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);
   }
   
         switch (ns) {  
         case (MD_NS_BLOCK):  
                 if ( ! html_blocktagname(mbuf, args, tok, &res))  
                         return(-1);  
                 break;  
         case (MD_NS_BODY):  
                 if ( ! html_blockbodytagname(mbuf, args, tok, &res))  
                         return(-1);  
                 break;  
         case (MD_NS_HEAD):  
                 if ( ! html_blockheadtagname(mbuf, args, tok, &res))  
                         return(-1);  
                 break;  
         default:  
                 if ( ! html_inlinetagname(mbuf, args, tok, &res))  
                         return(-1);  
                 break;  
         }  
   
         return((ssize_t)res);  static void
   print_man_head(MAN_ARGS)
   {
   
           print_gen_head(h);
           print_otag(h, TAG_TITLE, 0, NULL);
           print_encode(m->title);
           print_ctag(h, TAG_TITLE);
 }  }
   
   
 int  static void
 md_line_html(void *data, char *buf)  print_man_body(MAN_ARGS)
 {  {
   }
   
         return(mlg_line((struct md_mlg *)data, buf));  
   static void
   print_encode(const char *p)
   {
   
           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.7  
changed lines
  Added in v.1.29

CVSweb