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

Diff for /pod2mdoc/pod2mdoc.c between version 1.3 and 1.4

version 1.3, 2014/03/20 15:18:56 version 1.4, 2014/03/20 15:29:57
Line 32  struct args {
Line 32  struct args {
         const char      *section; /* override "Dt" section */          const char      *section; /* override "Dt" section */
 };  };
   
   enum    list {
           LIST_BULLET = 0,
           LIST_ENUM,
           LIST_TAG,
           LIST__MAX
   };
   
 struct  state {  struct  state {
         int              parsing; /* after =cut of before command */          int              parsing; /* after =cut of before command */
         int              paused; /* in =begin and before =end */          int              paused; /* in =begin and before =end */
         int              haspar; /* in paragraph: do we need Pp? */          int              haspar; /* in paragraph: do we need Pp? */
         int              isname; /* are we the NAME section? */          int              isname; /* are we the NAME section? */
         const char      *fname; /* file being parsed */          const char      *fname; /* file being parsed */
   #define LIST_STACKSZ     128
           enum list        lstack[LIST_STACKSZ]; /* open lists */
           size_t           lpos; /* where in list stack */
 };  };
   
 enum    fmt {  enum    fmt {
Line 264  formatcode(const char *buf, size_t *start, 
Line 274  formatcode(const char *buf, size_t *start, 
                         continue;                          continue;
                 }                  }
   
                         /*                  /*
                          * Make sure that any macro-like words (or                   * Make sure that any macro-like words (or
                          * really any word starting with a capital                   * really any word starting with a capital
                          * letter) is assumed to be a macro that must be                   * letter) is assumed to be a macro that must be
                          * escaped.                   * escaped.
                          * XXX: should this be isalpha()?                   * This matches "Xx " and "XxEOLN".
                          */                   */
                         if ((' ' == last || '\n' == last) &&                  if ((' ' == last || '\n' == last) &&
                                 isupper(buf[*start]))                                  end - *start > 1 &&
                                 printf("\\&");                                  isupper((int)buf[*start]) &&
                                   islower((int)buf[*start + 1]) &&
                                   (end - *start == 2 ||
                                    ' ' == buf[*start + 2]))
                           printf("\\&");
   
                 last = buf[*start];                  /* Suppress newline. */
                 if ('\n' == last)                  if ('\n' == (last = buf[(*start)++]))
                         last = ' ';                          last = ' ';
                 putchar(last);  
   
                 (*start)++;                  putchar(last);
         }          }
   
         if ( ! nomacro && FMT_CODE == fmt)          if ( ! nomacro && FMT_CODE == fmt)
Line 322  formatcodeln(const char *buf, size_t *start, size_t en
Line 335  formatcodeln(const char *buf, size_t *start, size_t en
 {  {
         int              last;          int              last;
   
         last = '\n';          last = ' ';
         while (*start < end)  {          while (*start < end)  {
                 if (*start + 1 < end && '<' == buf[*start + 1]) {                  if (*start + 1 < end && '<' == buf[*start + 1]) {
                         formatcode(buf, start, end, 1, last, nomacro);                          formatcode(buf, start, end, 1, last, nomacro);
                         continue;                          continue;
                 }                  }
                   /*
                    * Since we're already on a macro line, we want to make
                    * sure that we don't inadvertently invoke a macro.
                    * We need to do this carefully because section names
                    * are used in troff and we don't want to escape
                    * something that needn't be escaped.
                    */
                   if (' ' == last && end - *start > 1 &&
                                   isupper((int)buf[*start]) &&
                                   islower((int)buf[*start + 1]) &&
                                   (end - *start == 2 ||
                                    ' ' == buf[*start + 2]))
                           printf("\\&");
   
                 if ('\n' != buf[*start])                  if ('\n' != buf[*start])
                         putchar(last = buf[*start]);                          putchar(last = buf[*start]);
                   else
                           putchar(last = ' ');
                 (*start)++;                  (*start)++;
         }          }
 }  }
   
 /*  /*
    * Guess at what kind of list we are.
    * These are taken straight from the POD manual.
    * I don't know what people do in real life.
    */
   static enum list
   listguess(const char *buf, size_t start, size_t end)
   {
           size_t           len = end - start;
   
           assert(end >= start);
   
           if (len == 1 && '*' == buf[start])
                   return(LIST_BULLET);
           if (len == 2 && '1' == buf[start] && '.' == buf[start + 1])
                   return(LIST_ENUM);
           else if (len == 1 && '1' == buf[start])
                   return(LIST_ENUM);
           else
                   return(LIST_TAG);
   }
   
   /*
  * A command paragraph, as noted in the perlpod manual, just indicates   * A command paragraph, as noted in the perlpod manual, just indicates
  * that we should do something, optionally with some text to print as   * that we should do something, optionally with some text to print as
  * well.   * well.
Line 411  command(struct state *st, const char *buf, size_t star
Line 462  command(struct state *st, const char *buf, size_t star
                 st->haspar = 1;                  st->haspar = 1;
                 break;                  break;
         case (CMD_OVER):          case (CMD_OVER):
                 /*                  /*
                  * TODO: we should be doing this after we process the                   * If we have an existing list that hasn't had an =item
                  * first =item to see whether we'll do an -enum,                   * yet, then make sure that we open it now.
                  * -bullet, or something else.                   * We use the default list type, but that can't be
                    * helped (we haven't seen any items yet).
                  */                   */
                 puts(".Bl -tag -width Ds");                  if (st->lpos > 0)
                           if (LIST__MAX == st->lstack[st->lpos - 1]) {
                                   st->lstack[st->lpos - 1] = LIST_TAG;
                                   puts(".Bl -tag -width Ds");
                           }
                   st->lpos++;
                   assert(st->lpos < LIST_STACKSZ);
                   st->lstack[st->lpos - 1] = LIST__MAX;
                 break;                  break;
         case (CMD_ITEM):          case (CMD_ITEM):
                 printf(".It ");                  assert(st->lpos > 0);
                 formatcodeln(buf, &start, end, 0);                  /*
                 putchar('\n');                   * If we're the first =item, guess at what our content
                    * will be: "*" is a bullet list, "1." is a numbered
                    * list, and everything is tagged.
                    */
                   if (LIST__MAX == st->lstack[st->lpos - 1]) {
                           st->lstack[st->lpos - 1] =
                                   listguess(buf, start, end);
                           switch (st->lstack[st->lpos - 1]) {
                           case (LIST_BULLET):
                                   puts(".Bl -bullet");
                                   break;
                           case (LIST_ENUM):
                                   puts(".Bl -enum");
                                   break;
                           default:
                                   puts(".Bl -tag -width Ds");
                                   break;
                           }
                   }
                   switch (st->lstack[st->lpos - 1]) {
                   case (LIST_TAG):
                           printf(".It ");
                           formatcodeln(buf, &start, end, 0);
                           putchar('\n');
                           break;
                   case (LIST_ENUM):
                           /* FALLTHROUGH */
                   case (LIST_BULLET):
                           /*
                            * Abandon the remainder of the paragraph
                            * because we're going to be a bulletted or
                            * numbered list.
                            */
                           puts(".It");
                           break;
                   default:
                           abort();
                   }
                 st->haspar = 1;                  st->haspar = 1;
                 break;                  break;
         case (CMD_BACK):          case (CMD_BACK):
                 puts(".El");                  /* Make sure we don't back over the stack. */
                   if (st->lpos > 0) {
                           st->lpos--;
                           puts(".El");
                   }
                 break;                  break;
         case (CMD_BEGIN):          case (CMD_BEGIN):
                 /*                  /*
Line 501  ordinary(struct state *st, const char *buf, size_t sta
Line 601  ordinary(struct state *st, const char *buf, size_t sta
                         for ( ; i > start; i--)                          for ( ; i > start; i--)
                                 if ('-' != buf[i])                                  if ('-' != buf[i])
                                         break;                                          break;
                           /* FIXME: escape macro-like words etc. */
                         printf(".Nm %.*s\n",                          printf(".Nm %.*s\n",
                                 (int)((i + 1) - start), &buf[start]);                                  (int)((i + 1) - start), &buf[start]);
                         printf(".Nd %.*s\n",                          printf(".Nd %.*s\n",

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.4

CVSweb