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

Diff for /mandoc/html.c between version 1.135 and 1.148

version 1.135, 2011/04/30 22:14:02 version 1.148, 2011/07/04 09:42:38
Line 31 
Line 31 
 #include <unistd.h>  #include <unistd.h>
   
 #include "mandoc.h"  #include "mandoc.h"
   #include "libmandoc.h"
 #include "out.h"  #include "out.h"
 #include "html.h"  #include "html.h"
 #include "main.h"  #include "main.h"
Line 93  static const char *const htmlattrs[ATTR_MAX] = {
Line 94  static const char *const htmlattrs[ATTR_MAX] = {
         "colspan", /* ATTR_COLSPAN */          "colspan", /* ATTR_COLSPAN */
 };  };
   
 static  void              print_num(struct html *, const char *, size_t);  static  const char      *const roffscales[SCALE_MAX] = {
 static  void              print_spec(struct html *, const char *, size_t);          "cm", /* SCALE_CM */
 static  void              print_res(struct html *, const char *, size_t);          "in", /* SCALE_IN */
 static  void              print_ctag(struct html *, enum htmltag);          "pc", /* SCALE_PC */
 static  void              print_doctype(struct html *);          "pt", /* SCALE_PT */
 static  void              print_xmltype(struct html *);          "em", /* SCALE_EM */
 static  int               print_encode(struct html *, const char *, int);          "em", /* SCALE_MM */
 static  void              print_metaf(struct html *, enum mandoc_esc);          "ex", /* SCALE_EN */
 static  void              print_attr(struct html *,          "ex", /* SCALE_BU */
                                 const char *, const char *);          "em", /* SCALE_VS */
 static  void             *ml_alloc(char *, enum htmltype);          "ex", /* SCALE_FS */
   };
   
   static  void     bufncat(struct html *, const char *, size_t);
   static  void     print_ctag(struct html *, enum htmltag);
   static  int      print_encode(struct html *, const char *, int);
   static  void     print_metaf(struct html *, enum mandoc_esc);
   static  void     print_attr(struct html *, const char *, const char *);
   static  void     *ml_alloc(char *, enum htmltype);
   
 static void *  static void *
 ml_alloc(char *outopts, enum htmltype type)  ml_alloc(char *outopts, enum htmltype type)
Line 122  ml_alloc(char *outopts, enum htmltype type)
Line 130  ml_alloc(char *outopts, enum htmltype type)
   
         h->type = type;          h->type = type;
         h->tags.head = NULL;          h->tags.head = NULL;
         h->symtab = mchars_init();          h->symtab = mchars_alloc();
   
         while (outopts && *outopts)          while (outopts && *outopts)
                 switch (getsubopt(&outopts, UNCONST(toks), &v)) {                  switch (getsubopt(&outopts, UNCONST(toks), &v)) {
Line 208  print_gen_head(struct html *h)
Line 216  print_gen_head(struct html *h)
         }          }
 }  }
   
 /* ARGSUSED */  
 static void  static void
 print_num(struct html *h, const char *p, size_t len)  
 {  
         const char      *rhs;  
   
         rhs = mchars_num2char(p, len);  
         if (rhs)  
                 putchar((int)*rhs);  
 }  
   
 static void  
 print_spec(struct html *h, const char *p, size_t len)  
 {  
         int              cp;  
         const char      *rhs;  
         size_t           sz;  
   
         if ((cp = mchars_spec2cp(h->symtab, p, len)) > 0) {  
                 printf("&#%d;", cp);  
                 return;  
         } else if (-1 == cp && 1 == len) {  
                 fwrite(p, 1, len, stdout);  
                 return;  
         } else if (-1 == cp)  
                 return;  
   
         if (NULL != (rhs = mchars_spec2str(h->symtab, p, len, &sz)))  
                 fwrite(rhs, 1, sz, stdout);  
 }  
   
   
 static void  
 print_res(struct html *h, const char *p, size_t len)  
 {  
         int              cp;  
         const char      *rhs;  
         size_t           sz;  
   
         if ((cp = mchars_res2cp(h->symtab, p, len)) > 0) {  
                 printf("&#%d;", cp);  
                 return;  
         } else if (-1 == cp)  
                 return;  
   
         if (NULL != (rhs = mchars_res2str(h->symtab, p, len, &sz)))  
                 fwrite(rhs, 1, sz, stdout);  
 }  
   
   
 static void  
 print_metaf(struct html *h, enum mandoc_esc deco)  print_metaf(struct html *h, enum mandoc_esc deco)
 {  {
         enum htmlfont    font;          enum htmlfont    font;
Line 273  print_metaf(struct html *h, enum mandoc_esc deco)
Line 231  print_metaf(struct html *h, enum mandoc_esc deco)
         case (ESCAPE_FONTBOLD):          case (ESCAPE_FONTBOLD):
                 font = HTMLFONT_BOLD;                  font = HTMLFONT_BOLD;
                 break;                  break;
           case (ESCAPE_FONT):
                   /* FALLTHROUGH */
         case (ESCAPE_FONTROMAN):          case (ESCAPE_FONTROMAN):
                 font = HTMLFONT_NONE;                  font = HTMLFONT_NONE;
                 break;                  break;
Line 295  print_metaf(struct html *h, enum mandoc_esc deco)
Line 255  print_metaf(struct html *h, enum mandoc_esc deco)
                         print_otag(h, TAG_I, 0, NULL);                          print_otag(h, TAG_I, 0, NULL);
 }  }
   
   int
   html_strlen(const char *cp)
   {
           int              ssz, sz;
           const char      *seq, *p;
   
           /*
            * Account for escaped sequences within string length
            * calculations.  This follows the logic in term_strlen() as we
            * must calculate the width of produced strings.
            * Assume that characters are always width of "1".  This is
            * hacky, but it gets the job done for approximation of widths.
            */
   
           sz = 0;
           while (NULL != (p = strchr(cp, '\\'))) {
                   sz += (int)(p - cp);
                   ++cp;
                   switch (mandoc_escape(&cp, &seq, &ssz)) {
                   case (ESCAPE_ERROR):
                           return(sz);
                   case (ESCAPE_UNICODE):
                           /* FALLTHROUGH */
                   case (ESCAPE_NUMBERED):
                           /* FALLTHROUGH */
                   case (ESCAPE_SPECIAL):
                           sz++;
                           break;
                   default:
                           break;
                   }
           }
   
           assert(sz >= 0);
           return(sz + strlen(cp));
   }
   
 static int  static int
 print_encode(struct html *h, const char *p, int norecurse)  print_encode(struct html *h, const char *p, int norecurse)
 {  {
         size_t           sz;          size_t           sz;
         int              len, nospace;          int              c, len, nospace;
         const char      *seq;          const char      *seq;
         enum mandoc_esc  esc;          enum mandoc_esc  esc;
         static const char rejs[6] = { '\\', '<', '>', '&', ASCII_HYPH, '\0' };          static const char rejs[6] = { '\\', '<', '>', '&', ASCII_HYPH, '\0' };
Line 338  print_encode(struct html *h, const char *p, int norecu
Line 334  print_encode(struct html *h, const char *p, int norecu
                         break;                          break;
   
                 switch (esc) {                  switch (esc) {
                   case (ESCAPE_UNICODE):
                           /* Skip passed "u" header. */
                           c = mchars_num2uc(seq + 1, len - 1);
                           if ('\0' != c)
                                   printf("&#x%x;", c);
                           break;
                 case (ESCAPE_NUMBERED):                  case (ESCAPE_NUMBERED):
                         print_num(h, seq, len);                          c = mchars_num2char(seq, len);
                           if ('\0' != c)
                                   putchar(c);
                         break;                          break;
                 case (ESCAPE_PREDEF):  
                         print_res(h, seq, len);  
                         break;  
                 case (ESCAPE_SPECIAL):                  case (ESCAPE_SPECIAL):
                         print_spec(h, seq, len);                          c = mchars_spec2cp(h->symtab, seq, len);
                           if (c > 0)
                                   printf("&#%d;", c);
                           else if (-1 == c && 1 == len)
                                   putchar((int)*seq);
                         break;                          break;
                   case (ESCAPE_FONT):
                           /* FALLTHROUGH */
                 case (ESCAPE_FONTPREV):                  case (ESCAPE_FONTPREV):
                         /* FALLTHROUGH */                          /* FALLTHROUGH */
                 case (ESCAPE_FONTBOLD):                  case (ESCAPE_FONTBOLD):
Line 460  print_ctag(struct html *h, enum htmltag tag)
Line 467  print_ctag(struct html *h, enum htmltag tag)
         }          }
 }  }
   
   
 void  void
 print_gen_decls(struct html *h)  print_gen_decls(struct html *h)
 {  {
   
         print_xmltype(h);  
         print_doctype(h);  
 }  
   
   
 static void  
 print_xmltype(struct html *h)  
 {  
   
         if (HTML_XHTML_1_0_STRICT == h->type)  
                 puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");  
 }  
   
   
 static void  
 print_doctype(struct html *h)  
 {  
         const char      *doctype;          const char      *doctype;
         const char      *dtd;          const char      *dtd;
         const char      *name;          const char      *name;
Line 493  print_doctype(struct html *h)
Line 481  print_doctype(struct html *h)
                 dtd = "http://www.w3.org/TR/html4/strict.dtd";                  dtd = "http://www.w3.org/TR/html4/strict.dtd";
                 break;                  break;
         default:          default:
                   puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                 name = "html";                  name = "html";
                 doctype = "-//W3C//DTD XHTML 1.0 Strict//EN";                  doctype = "-//W3C//DTD XHTML 1.0 Strict//EN";
                 dtd = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";                  dtd = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";
Line 582  print_stagq(struct html *h, const struct tag *suntil)
Line 571  print_stagq(struct html *h, const struct tag *suntil)
         }          }
 }  }
   
   
 void  void
 bufinit(struct html *h)  bufinit(struct html *h)
 {  {
Line 591  bufinit(struct html *h)
Line 579  bufinit(struct html *h)
         h->buflen = 0;          h->buflen = 0;
 }  }
   
   
 void  void
 bufcat_style(struct html *h, const char *key, const char *val)  bufcat_style(struct html *h, const char *key, const char *val)
 {  {
   
         bufcat(h, key);          bufcat(h, key);
         bufncat(h, ":", 1);          bufcat(h, ":");
         bufcat(h, val);          bufcat(h, val);
         bufncat(h, ";", 1);          bufcat(h, ";");
 }  }
   
   
 void  void
 bufcat(struct html *h, const char *p)  bufcat(struct html *h, const char *p)
 {  {
   
         bufncat(h, p, strlen(p));          h->buflen = strlcat(h->buf, p, BUFSIZ);
           assert(h->buflen < BUFSIZ);
 }  }
   
   
 void  void
 buffmt(struct html *h, const char *fmt, ...)  bufcat_fmt(struct html *h, const char *fmt, ...)
 {  {
         va_list          ap;          va_list          ap;
   
Line 623  buffmt(struct html *h, const char *fmt, ...)
Line 609  buffmt(struct html *h, const char *fmt, ...)
         h->buflen = strlen(h->buf);          h->buflen = strlen(h->buf);
 }  }
   
   static void
 void  
 bufncat(struct html *h, const char *p, size_t sz)  bufncat(struct html *h, const char *p, size_t sz)
 {  {
   
         if (h->buflen + sz > BUFSIZ - 1)          assert(h->buflen + sz + 1 < BUFSIZ);
                 sz = BUFSIZ - 1 - h->buflen;          strncat(h->buf, p, sz);
   
         (void)strncat(h->buf, p, sz);  
         h->buflen += sz;          h->buflen += sz;
 }  }
   
   
 void  void
 buffmt_includes(struct html *h, const char *name)  buffmt_includes(struct html *h, const char *name)
 {  {
Line 643  buffmt_includes(struct html *h, const char *name)
Line 625  buffmt_includes(struct html *h, const char *name)
   
         pp = h->base_includes;          pp = h->base_includes;
   
           bufinit(h);
         while (NULL != (p = strchr(pp, '%'))) {          while (NULL != (p = strchr(pp, '%'))) {
                 bufncat(h, pp, (size_t)(p - pp));                  bufncat(h, pp, (size_t)(p - pp));
                 switch (*(p + 1)) {                  switch (*(p + 1)) {
Line 659  buffmt_includes(struct html *h, const char *name)
Line 642  buffmt_includes(struct html *h, const char *name)
                 bufcat(h, pp);                  bufcat(h, pp);
 }  }
   
   
 void  void
 buffmt_man(struct html *h,  buffmt_man(struct html *h,
                 const char *name, const char *sec)                  const char *name, const char *sec)
Line 668  buffmt_man(struct html *h, 
Line 650  buffmt_man(struct html *h, 
   
         pp = h->base_man;          pp = h->base_man;
   
         /* LINTED */          bufinit(h);
         while (NULL != (p = strchr(pp, '%'))) {          while (NULL != (p = strchr(pp, '%'))) {
                 bufncat(h, pp, (size_t)(p - pp));                  bufncat(h, pp, (size_t)(p - pp));
                 switch (*(p + 1)) {                  switch (*(p + 1)) {
Line 676  buffmt_man(struct html *h, 
Line 658  buffmt_man(struct html *h, 
                         bufcat(h, sec ? sec : "1");                          bufcat(h, sec ? sec : "1");
                         break;                          break;
                 case('N'):                  case('N'):
                         buffmt(h, name);                          bufcat_fmt(h, name);
                         break;                          break;
                 default:                  default:
                         bufncat(h, p, 2);                          bufncat(h, p, 2);
Line 688  buffmt_man(struct html *h, 
Line 670  buffmt_man(struct html *h, 
                 bufcat(h, pp);                  bufcat(h, pp);
 }  }
   
   
 void  void
 bufcat_su(struct html *h, const char *p, const struct roffsu *su)  bufcat_su(struct html *h, const char *p, const struct roffsu *su)
 {  {
         double           v;          double           v;
         const char      *u;  
   
         v = su->scale;          v = su->scale;
           if (SCALE_MM == su->unit && 0.0 == (v /= 100.0))
                   v = 1.0;
   
         switch (su->unit) {          bufcat_fmt(h, "%s: %.2f%s;", p, v, roffscales[su->unit]);
         case (SCALE_CM):  
                 u = "cm";  
                 break;  
         case (SCALE_IN):  
                 u = "in";  
                 break;  
         case (SCALE_PC):  
                 u = "pc";  
                 break;  
         case (SCALE_PT):  
                 u = "pt";  
                 break;  
         case (SCALE_EM):  
                 u = "em";  
                 break;  
         case (SCALE_MM):  
                 if (0 == (v /= 100))  
                         v = 1;  
                 u = "em";  
                 break;  
         case (SCALE_EN):  
                 u = "ex";  
                 break;  
         case (SCALE_BU):  
                 u = "ex";  
                 break;  
         case (SCALE_VS):  
                 u = "em";  
                 break;  
         default:  
                 u = "ex";  
                 break;  
         }  
   
         /*  
          * XXX: the CSS spec isn't clear as to which types accept  
          * integer or real numbers, so we just make them all decimals.  
          */  
         buffmt(h, "%s: %.2f%s;", p, v, u);  
 }  }
   
   
 void  void
 html_idcat(char *dst, const char *src, int sz)  bufcat_id(struct html *h, const char *src)
 {  {
         int              ssz;  
   
         assert(sz > 2);  
   
         /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */          /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
   
         /* We can't start with a number (bah). */          while ('\0' != *src)
                   bufcat_fmt(h, "%.2x", *src++);
         if ('#' == *dst) {  
                 dst++;  
                 sz--;  
         }  
         if ('\0' == *dst) {  
                 *dst++ = 'x';  
                 *dst = '\0';  
                 sz--;  
         }  
   
         for ( ; *dst != '\0' && sz; dst++, sz--)  
                 /* Jump to end. */ ;  
   
         for ( ; *src != '\0' && sz > 1; src++) {  
                 ssz = snprintf(dst, (size_t)sz, "%.2x", *src);  
                 sz -= ssz;  
                 dst += ssz;  
         }  
 }  }

Legend:
Removed from v.1.135  
changed lines
  Added in v.1.148

CVSweb