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

Diff for /mandoc/html.c between version 1.143 and 1.155

version 1.143, 2011/05/17 11:38:18 version 1.155, 2014/03/23 11:25:26
Line 1 
Line 1 
 /*      $Id$ */  /*      $Id$ */
 /*  /*
  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>   * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
  * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>   * Copyright (c) 2011, 2012, 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
  *   *
  * 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 above   * purpose with or without fee is hereby granted, provided that the above
Line 31 
Line 31 
 #include <unistd.h>  #include <unistd.h>
   
 #include "mandoc.h"  #include "mandoc.h"
   #include "mandoc_aux.h"
 #include "libmandoc.h"  #include "libmandoc.h"
 #include "out.h"  #include "out.h"
 #include "html.h"  #include "html.h"
Line 108  static const char *const roffscales[SCALE_MAX] = {
Line 109  static const char *const roffscales[SCALE_MAX] = {
 };  };
   
 static  void     bufncat(struct html *, const char *, size_t);  static  void     bufncat(struct html *, const char *, size_t);
 static  void     print_spec(struct html *, const char *, size_t);  
 static  void     print_res(struct html *, const char *, size_t);  
 static  void     print_ctag(struct html *, enum htmltag);  static  void     print_ctag(struct html *, enum htmltag);
 static  int      print_encode(struct html *, const char *, int);  static  int      print_encode(struct html *, const char *, int);
 static  void     print_metaf(struct html *, enum mandoc_esc);  static  void     print_metaf(struct html *, enum mandoc_esc);
Line 120  static void *
Line 119  static void *
 ml_alloc(char *outopts, enum htmltype type)  ml_alloc(char *outopts, enum htmltype type)
 {  {
         struct html     *h;          struct html     *h;
         const char      *toks[4];          const char      *toks[5];
         char            *v;          char            *v;
   
         toks[0] = "style";          toks[0] = "style";
         toks[1] = "man";          toks[1] = "man";
         toks[2] = "includes";          toks[2] = "includes";
         toks[3] = NULL;          toks[3] = "fragment";
           toks[4] = NULL;
   
         h = mandoc_calloc(1, sizeof(struct html));          h = mandoc_calloc(1, sizeof(struct html));
   
Line 145  ml_alloc(char *outopts, enum htmltype type)
Line 145  ml_alloc(char *outopts, enum htmltype type)
                 case (2):                  case (2):
                         h->base_includes = v;                          h->base_includes = v;
                         break;                          break;
                   case (3):
                           h->oflags |= HTML_FRAGMENT;
                           break;
                 default:                  default:
                         break;                          break;
                 }                  }
Line 219  print_gen_head(struct html *h)
Line 222  print_gen_head(struct html *h)
 }  }
   
 static void  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 272  print_metaf(struct html *h, enum mandoc_esc deco)
Line 236  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_FONTBI):
                   font = HTMLFONT_BI;
                   break;
           case (ESCAPE_FONT):
                   /* FALLTHROUGH */
         case (ESCAPE_FONTROMAN):          case (ESCAPE_FONTROMAN):
                 font = HTMLFONT_NONE;                  font = HTMLFONT_NONE;
                 break;                  break;
Line 288  print_metaf(struct html *h, enum mandoc_esc deco)
Line 257  print_metaf(struct html *h, enum mandoc_esc deco)
         h->metal = h->metac;          h->metal = h->metac;
         h->metac = font;          h->metac = font;
   
         if (HTMLFONT_NONE != font)          switch (font) {
                 h->metaf = HTMLFONT_BOLD == font ?          case (HTMLFONT_ITALIC):
                         print_otag(h, TAG_B, 0, NULL) :                  h->metaf = print_otag(h, TAG_I, 0, NULL);
                         print_otag(h, TAG_I, 0, NULL);                  break;
           case (HTMLFONT_BOLD):
                   h->metaf = print_otag(h, TAG_B, 0, NULL);
                   break;
           case (HTMLFONT_BI):
                   h->metaf = print_otag(h, TAG_B, 0, NULL);
                   print_otag(h, TAG_I, 0, NULL);
                   break;
           default:
                   break;
           }
 }  }
   
 int  int
 html_strlen(const char *cp)  html_strlen(const char *cp)
 {  {
         int              ssz, sz;          size_t           rsz;
         const char      *seq, *p;          int              skip, sz;
   
         /*          /*
          * Account for escaped sequences within string length           * Account for escaped sequences within string length
Line 309  html_strlen(const char *cp)
Line 288  html_strlen(const char *cp)
          */           */
   
         sz = 0;          sz = 0;
         while (NULL != (p = strchr(cp, '\\'))) {          skip = 0;
                 sz += (int)(p - cp);          while (1) {
                 ++cp;                  rsz = strcspn(cp, "\\");
                 switch (mandoc_escape(&cp, &seq, &ssz)) {                  if (rsz) {
                           cp += rsz;
                           if (skip) {
                                   skip = 0;
                                   rsz--;
                           }
                           sz += rsz;
                   }
                   if ('\0' == *cp)
                           break;
                   cp++;
                   switch (mandoc_escape(&cp, NULL, NULL)) {
                 case (ESCAPE_ERROR):                  case (ESCAPE_ERROR):
                         return(sz);                          return(sz);
                   case (ESCAPE_UNICODE):
                           /* FALLTHROUGH */
                 case (ESCAPE_NUMBERED):                  case (ESCAPE_NUMBERED):
                         /* FALLTHROUGH */                          /* FALLTHROUGH */
                 case (ESCAPE_PREDEF):  
                         /* FALLTHROUGH */  
                 case (ESCAPE_SPECIAL):                  case (ESCAPE_SPECIAL):
                         sz++;                          if (skip)
                                   skip = 0;
                           else
                                   sz++;
                         break;                          break;
                   case (ESCAPE_SKIPCHAR):
                           skip = 1;
                           break;
                 default:                  default:
                         break;                          break;
                 }                  }
         }          }
           return(sz);
         assert(sz >= 0);  
         return(sz + strlen(cp));  
 }  }
   
 static int  static int
Line 338  print_encode(struct html *h, const char *p, int norecu
Line 332  print_encode(struct html *h, const char *p, int norecu
         int              c, 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[8] = { '\\', '<', '>', '&',
                   ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' };
   
         nospace = 0;          nospace = 0;
   
         while ('\0' != *p) {          while ('\0' != *p) {
                   if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
                           h->flags &= ~HTML_SKIPCHAR;
                           p++;
                           continue;
                   }
   
                 sz = strcspn(p, rejs);                  sz = strcspn(p, rejs);
   
                 fwrite(p, 1, sz, stdout);                  fwrite(p, 1, sz, stdout);
Line 361  print_encode(struct html *h, const char *p, int norecu
Line 362  print_encode(struct html *h, const char *p, int norecu
                 case ('&'):                  case ('&'):
                         printf("&amp;");                          printf("&amp;");
                         continue;                          continue;
                   case (ASCII_NBRSP):
                           putchar('-');
                           continue;
                 case (ASCII_HYPH):                  case (ASCII_HYPH):
                         putchar('-');                          putchar('-');
                           /* FALLTHROUGH */
                   case (ASCII_BREAK):
                         continue;                          continue;
                 default:                  default:
                         break;                          break;
Line 373  print_encode(struct html *h, const char *p, int norecu
Line 379  print_encode(struct html *h, const char *p, int norecu
                         break;                          break;
   
                 switch (esc) {                  switch (esc) {
                 case (ESCAPE_NUMBERED):                  case (ESCAPE_FONT):
                         c = mchars_num2char(seq, len);                          /* FALLTHROUGH */
                         if ('\0' != c)  
                                 putchar(c);  
                         break;  
                 case (ESCAPE_PREDEF):  
                         print_res(h, seq, len);  
                         break;  
                 case (ESCAPE_SPECIAL):  
                         print_spec(h, seq, len);  
                         break;  
                 case (ESCAPE_FONTPREV):                  case (ESCAPE_FONTPREV):
                         /* FALLTHROUGH */                          /* FALLTHROUGH */
                 case (ESCAPE_FONTBOLD):                  case (ESCAPE_FONTBOLD):
                         /* FALLTHROUGH */                          /* FALLTHROUGH */
                 case (ESCAPE_FONTITALIC):                  case (ESCAPE_FONTITALIC):
                         /* FALLTHROUGH */                          /* FALLTHROUGH */
                   case (ESCAPE_FONTBI):
                           /* FALLTHROUGH */
                 case (ESCAPE_FONTROMAN):                  case (ESCAPE_FONTROMAN):
                         if (norecurse)                          if (0 == norecurse)
                                 break;                                  print_metaf(h, esc);
                         print_metaf(h, esc);                          continue;
                   case (ESCAPE_SKIPCHAR):
                           h->flags |= HTML_SKIPCHAR;
                           continue;
                   default:
                         break;                          break;
                   }
   
                   if (h->flags & HTML_SKIPCHAR) {
                           h->flags &= ~HTML_SKIPCHAR;
                           continue;
                   }
   
                   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):
                           c = mchars_num2char(seq, len);
                           if ('\0' != c)
                                   putchar(c);
                           break;
                   case (ESCAPE_SPECIAL):
                           c = mchars_spec2cp(h->symtab, seq, len);
                           if (c > 0)
                                   printf("&#%d;", c);
                           else if (-1 == c && 1 == len)
                                   putchar((int)*seq);
                           break;
                 case (ESCAPE_NOSPACE):                  case (ESCAPE_NOSPACE):
                         if ('\0' == *p)                          if ('\0' == *p)
                                 nospace = 1;                                  nospace = 1;
Line 537  print_text(struct html *h, const char *word)
Line 566  print_text(struct html *h, const char *word)
         }          }
   
         assert(NULL == h->metaf);          assert(NULL == h->metaf);
         if (HTMLFONT_NONE != h->metac)          switch (h->metac) {
                 h->metaf = HTMLFONT_BOLD == h->metac ?          case (HTMLFONT_ITALIC):
                         print_otag(h, TAG_B, 0, NULL) :                  h->metaf = print_otag(h, TAG_I, 0, NULL);
                         print_otag(h, TAG_I, 0, NULL);                  break;
           case (HTMLFONT_BOLD):
                   h->metaf = print_otag(h, TAG_B, 0, NULL);
                   break;
           case (HTMLFONT_BI):
                   h->metaf = print_otag(h, TAG_B, 0, NULL);
                   print_otag(h, TAG_I, 0, NULL);
                   break;
           default:
                   break;
           }
   
         assert(word);          assert(word);
         if ( ! print_encode(h, word, 0))          if ( ! print_encode(h, word, 0)) {
                 if ( ! (h->flags & HTML_NONOSPACE))                  if ( ! (h->flags & HTML_NONOSPACE))
                         h->flags &= ~HTML_NOSPACE;                          h->flags &= ~HTML_NOSPACE;
           } else
                   h->flags |= HTML_NOSPACE;
   
         if (h->metaf) {          if (h->metaf) {
                 print_tagq(h, h->metaf);                  print_tagq(h, h->metaf);
Line 625  bufcat(struct html *h, const char *p)
Line 666  bufcat(struct html *h, const char *p)
   
         h->buflen = strlcat(h->buf, p, BUFSIZ);          h->buflen = strlcat(h->buf, p, BUFSIZ);
         assert(h->buflen < BUFSIZ);          assert(h->buflen < BUFSIZ);
         h->buflen--;  
 }  }
   
 void  void
Line 689  buffmt_man(struct html *h, 
Line 729  buffmt_man(struct html *h, 
                         bufcat(h, sec ? sec : "1");                          bufcat(h, sec ? sec : "1");
                         break;                          break;
                 case('N'):                  case('N'):
                         bufcat_fmt(h, name);                          bufcat_fmt(h, "%s", name);
                         break;                          break;
                 default:                  default:
                         bufncat(h, p, 2);                          bufncat(h, p, 2);

Legend:
Removed from v.1.143  
changed lines
  Added in v.1.155

CVSweb