=================================================================== RCS file: /cvs/pod2mdoc/pod2mdoc.c,v retrieving revision 1.2 retrieving revision 1.5 diff -u -p -r1.2 -r1.5 --- pod2mdoc/pod2mdoc.c 2014/03/20 15:15:32 1.2 +++ pod2mdoc/pod2mdoc.c 2014/03/23 13:00:24 1.5 @@ -1,4 +1,4 @@ -/* $Id: pod2mdoc.c,v 1.2 2014/03/20 15:15:32 schwarze Exp $ */ +/* $Id: pod2mdoc.c,v 1.5 2014/03/23 13:00:24 kristaps Exp $ */ /* * Copyright (c) 2014 Kristaps Dzonsons * @@ -32,12 +32,22 @@ struct args { const char *section; /* override "Dt" section */ }; +enum list { + LIST_BULLET = 0, + LIST_ENUM, + LIST_TAG, + LIST__MAX +}; + struct state { int parsing; /* after =cut of before command */ int paused; /* in =begin and before =end */ int haspar; /* in paragraph: do we need Pp? */ int isname; /* are we the NAME section? */ 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 { @@ -144,12 +154,15 @@ formatescape(const char *buf, size_t *start, size_t en /* * Skip space characters. */ -static void +static int skipspace(const char *buf, size_t *start, size_t end) { + size_t sv = *start; while (*start < end && ' ' == buf[*start]) (*start)++; + + return(*start > sv); } /* @@ -168,10 +181,26 @@ formatcode(const char *buf, size_t *start, size_t end, int reentrant, int last, int nomacro) { enum fmt fmt; + size_t i, j, dsz; assert(*start + 1 < end); assert('<' == buf[*start + 1]); + /* + * Determine whether we're overriding our delimiter. + * According to POD, if we have more than one '<' followed by a + * space, then we need a space followed by matching '>' to close + * the expression. + * Otherwise we use the usual '<' and '>' matched pair. + */ + i = *start + 1; + while (i < end && '<' == buf[i]) + i++; + assert(i > *start + 1); + dsz = i - (*start + 1); + if (dsz > 1 && (i >= end || ' ' != buf[i])) + dsz = 1; + for (fmt = 0; fmt < FMT__MAX; fmt++) if (buf[*start] == fmts[fmt]) break; @@ -184,7 +213,8 @@ formatcode(const char *buf, size_t *start, return(0); } - *start += 2; + /* Remember, if dsz>1, to jump the trailing space. */ + *start += dsz + 1 + (dsz > 1 ? 1 : 0); /* * Escapes don't print macro sequences, so just output them like @@ -194,11 +224,28 @@ formatcode(const char *buf, size_t *start, formatescape(buf, start, end); return(0); } else if (FMT_NULL == fmt || FMT_INDEX == fmt) { - /* For indices and nulls, just consume. */ - while (*start < end && '>' != buf[*start]) - (*start)++; - if (*start < end) - (*start)++; + /* + * For indices and nulls, just consume. + * Be wary of encountering custom delimiters (dsz>1), + * which require special handling. + */ + for ( ; *start < end; (*start)++) { + if ('>' != buf[*start]) + continue; + else if (dsz == 1) + break; + assert(*start > 0); + if (' ' != buf[*start - 1]) + continue; + i = *start; + for (j = 0; i < end && j < dsz; j++) + if ('>' != buf[i++]) + break; + if (dsz != j) + continue; + (*start) += dsz; + break; + } return(0); } @@ -249,34 +296,57 @@ formatcode(const char *buf, size_t *start, } /* - * Read until we reach the end market ('>') or until we find a - * nested format code. + * Read until we reach the end market (e.g., '>') or until we + * find a nested format code. * Don't emit any newlines: since we're on a macro line, we * don't want to break the line. */ while (*start < end) { - if ('>' == buf[*start]) { + if ('>' == buf[*start] && 1 == dsz) { (*start)++; break; + } else if ('>' == buf[*start] && + ' ' == buf[*start - 1]) { + /* + * Handle custom delimiters. + * These require a certain number of + * space-preceded carrots before we're really at + * the end. + */ + i = *start; + for (j = 0; i < end && j < dsz; j++) + if ('>' != buf[i++]) + break; + if (dsz == j) { + *start += dsz; + break; + } } if (*start + 1 < end && '<' == buf[*start + 1]) { formatcode(buf, start, end, 1, last, nomacro); continue; } - if ('\n' != buf[*start]) { - /* - * Make sure that any macro-like words (or - * really any word starting with a capital - * letter) is assumed to be a macro that must be - * escaped. - * XXX: should this be isalpha()? - */ - if ((' ' == last || '\n' == last) && - isupper(buf[*start])) - printf("\\&"); - putchar(last = buf[*start]); - } - (*start)++; + + /* + * Make sure that any macro-like words (or + * really any word starting with a capital + * letter) is assumed to be a macro that must be + * escaped. + * This matches "Xx " and "XxEOLN". + */ + if ((' ' == last || '\n' == last) && + end - *start > 1 && + isupper((int)buf[*start]) && + islower((int)buf[*start + 1]) && + (end - *start == 2 || + ' ' == buf[*start + 2])) + printf("\\&"); + + /* Suppress newline. */ + if ('\n' == (last = buf[(*start)++])) + last = ' '; + + putchar(last); } if ( ! nomacro && FMT_CODE == fmt) @@ -285,12 +355,15 @@ formatcode(const char *buf, size_t *start, if (reentrant) return(1); + /* FIXME: with the "Qc", this doens't work good. */ + /* * If we're not reentrant, we want to put ending punctuation on * the macro line so that it's properly handled by being * smooshed against the terminal word. */ skipspace(buf, start, end); + if (',' != buf[*start] && '.' != buf[*start] && '!' != buf[*start] && '?' != buf[*start] && ')' != buf[*start]) @@ -318,19 +391,57 @@ formatcodeln(const char *buf, size_t *start, size_t en { int last; - last = '\n'; + last = ' '; while (*start < end) { if (*start + 1 < end && '<' == buf[*start + 1]) { formatcode(buf, start, end, 1, last, nomacro); 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]) putchar(last = buf[*start]); + else + putchar(last = ' '); (*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 * that we should do something, optionally with some text to print as * well. @@ -407,21 +518,70 @@ command(struct state *st, const char *buf, size_t star st->haspar = 1; break; case (CMD_OVER): - /* - * TODO: we should be doing this after we process the - * first =item to see whether we'll do an -enum, - * -bullet, or something else. + /* + * If we have an existing list that hasn't had an =item + * yet, then make sure that we open it now. + * 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; case (CMD_ITEM): - printf(".It "); - formatcodeln(buf, &start, end, 0); - putchar('\n'); + assert(st->lpos > 0); + /* + * 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; break; case (CMD_BACK): - puts(".El"); + /* Make sure we don't back over the stack. */ + if (st->lpos > 0) { + st->lpos--; + puts(".El"); + } break; case (CMD_BEGIN): /* @@ -497,10 +657,13 @@ ordinary(struct state *st, const char *buf, size_t sta for ( ; i > start; i--) if ('-' != buf[i]) break; - printf(".Nm %.*s\n", - (int)((i + 1) - start), &buf[start]); - printf(".Nd %.*s\n", - (int)(end - (j + 1)), &buf[j + 1]); + printf(".Nm "); + formatcodeln(buf, &start, i + 1, 1); + putchar('\n'); + start = j + 1; + printf(".Nd "); + formatcodeln(buf, &start, end, 1); + putchar('\n'); return; } }