source@mandoc.bsd.lv
 help / color / mirror / Atom feed
* pod2mdoc: Pick out the first B<> as an Nm when in SYNOPSIS.
@ 2014-04-02 14:50 kristaps
  0 siblings, 0 replies; only message in thread
From: kristaps @ 2014-04-02 14:50 UTC (permalink / raw)
  To: source

Log Message:
-----------
Pick out the first B<> as an Nm when in SYNOPSIS.
Document all of these tricks in the manpage.

Modified Files:
--------------
    pod2mdoc:
        pod2mdoc.1
        pod2mdoc.c

Revision Data
-------------
Index: pod2mdoc.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/pod2mdoc/pod2mdoc.c,v
retrieving revision 1.14
retrieving revision 1.15
diff -Lpod2mdoc.c -Lpod2mdoc.c -u -p -r1.14 -r1.15
--- pod2mdoc.c
+++ pod2mdoc.c
@@ -239,7 +239,6 @@ trylink(const char *buf, size_t *start, 
 	return(1);
 }
 
-
 /*
  * Doclifting: if we're a bold "-xx" and we're in the SYNOPSIS section,
  * then it's likely that we're a flag.
@@ -315,13 +314,16 @@ again:
  * been printed to the current line.
  * If "nomacro", then we don't print any macros, just contained data
  * (e.g., following "Sh" or "Nm").
+ * "pos" is only significant in SYNOPSIS, and should be 0 when invoked
+ * as the first format code on a line (for decoration as an "Nm"),
+ * non-zero otherwise.
  * Return whether we've printed a macro or not--in other words, whether
  * this should trigger a subsequent newline (this should be ignored when
  * reentrant).
  */
 static int
-formatcode(struct state *st, const char *buf, 
-	size_t *start, size_t end, int reentrant, int nomacro)
+formatcode(struct state *st, const char *buf, size_t *start, 
+	size_t end, int reentrant, int nomacro, int pos)
 {
 	enum fmt	 fmt;
 	size_t		 i, j, dsz;
@@ -430,6 +432,8 @@ formatcode(struct state *st, const char 
 			if (SECT_SYNOPSIS == st->sect) { 
 				if (1 == dsz && '-' == buf[*start])
 					dosynopsisfl(buf, start, end);
+				else if (0 == pos)
+					printf("Nm ");
 				else
 					printf("Ar ");
 				break;
@@ -482,7 +486,7 @@ formatcode(struct state *st, const char 
 			}
 		}
 		if (*start + 1 < end && '<' == buf[*start + 1]) {
-			formatcode(st, buf, start, end, 1, nomacro);
+			formatcode(st, buf, start, end, 1, nomacro, 1);
 			continue;
 		}
 
@@ -544,7 +548,7 @@ formatcodeln(struct state *st, const cha
 	last = ' ';
 	while (*start < end)  {
 		if (*start + 1 < end && '<' == buf[*start + 1]) {
-			formatcode(st, buf, start, end, 1, nomacro);
+			formatcode(st, buf, start, end, 1, nomacro, 1);
 			continue;
 		}
 		/*
@@ -873,6 +877,7 @@ static void
 ordinary(struct state *st, const char *buf, size_t start, size_t end)
 {
 	size_t		i, j, opstack;
+	int		seq;
 
 	if ( ! st->parsing || st->paused)
 		return;
@@ -884,8 +889,8 @@ ordinary(struct state *st, const char *b
 	 * To wit, print out a "Nm" and "Nd" in that format.
 	 */
 	if (SECT_NAME == st->sect) {
-		for (i = end - 1; i > start; i--)
-			if ('-' == buf[i])
+		for (i = end - 2; i > start; i--)
+			if ('-' == buf[i] && ' ' == buf[i + 1])
 				break;
 		if ('-' == buf[i]) {
 			j = i;
@@ -893,11 +898,11 @@ ordinary(struct state *st, const char *b
 			for ( ; i > start; i--)
 				if ('-' != buf[i])
 					break;
-			printf(".Nm ");
+			fputs(".Nm ", stdout);
 			formatcodeln(st, buf, &start, i + 1, 1);
 			putchar('\n');
 			start = j + 1;
-			printf(".Nd ");
+			fputs(".Nd ", stdout);
 			formatcodeln(st, buf, &start, end, 1);
 			putchar('\n');
 			return;
@@ -911,7 +916,7 @@ ordinary(struct state *st, const char *b
 	last = '\n';
 	opstack = 0;
 
-	while (start < end) {
+	for (seq = 0; start < end; seq++) {
 		/* 
 		 * Loop til we get either to a newline or escape. 
 		 * Escape initial control characters.
@@ -951,7 +956,7 @@ ordinary(struct state *st, const char *b
 			 * Consume all whitespace so we don't
 			 * accidentally start an implicit literal line.
 			 */
-			if (formatcode(st, buf, &start, end, 0, 0)) {
+			if (formatcode(st, buf, &start, end, 0, 0, seq)) {
 				putchar(last = '\n');
 				while (start < end && ' ' == buf[start])
 					start++;
Index: pod2mdoc.1
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/pod2mdoc/pod2mdoc.1,v
retrieving revision 1.6
retrieving revision 1.7
diff -Lpod2mdoc.1 -Lpod2mdoc.1 -u -p -r1.6 -r1.7
--- pod2mdoc.1
+++ pod2mdoc.1
@@ -89,6 +89,73 @@ or, if the input file suffix is
 uses
 .Ar 3p .
 .El
+.Ss Smarts
+Since
+.Xr mdoc 7
+is semantic and
+.Xr perlpod 1
+is not,
+.Nm
+tries to figure out semantic context for some terms.
+Specifically, within each paragraph of the SYNOPSIS section, the
+following occur:
+.Bl -bullet
+.It
+An initial
+.Li B<>
+format code is rendered as
+.Sq \&Nm .
+.It
+Subsequent
+.Li B<>
+format codes are rendered as
+.Sq \&Ar .
+However, if the leading character of a
+.Li B<>
+format code is
+.Sq - ,
+it is rendered as
+.Sq \&Fl .
+Subsequent space-separated terms without leading hyphens, e.g.,
+.Li B<-foo bar> ,
+are rendered as
+.Sq \&Ar .
+.It
+Matching
+.Li \&[
+and
+.Li \&]
+pairs are rendered as
+.Sq \&Oo
+and
+.Sq \&Oc .
+.El
+.Pp
+Thus, the input
+.Li B<foo> [B<-bar baz>]
+is rendered as follows:
+.Bd -literal
+\&.Nm foo 
+\&.Oo
+\&.Fl bar Ar baz
+\&.Oc
+.Ed
+.Pp
+In the NAME section, an
+.Sq \&Nm
+and
+.Sq \&Nd
+macro are inferred from text leading and trailing the last hyphen
+followed by a space (there may be any number of hyphens preceding the
+space).
+The space may occur on either side of the hyphen.
+Thus,
+.Li B<foo> - bar
+will be rendered as follows:
+.Bd -literal
+\&.Nm foo
+\&.Nd bar
+.Ed
 .Sh EXIT STATUS
 .Ex -std
 .Sh EXAMPLES
--
 To unsubscribe send an email to source+unsubscribe@mdocml.bsd.lv

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2014-04-02 14:50 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-04-02 14:50 pod2mdoc: Pick out the first B<> as an Nm when in SYNOPSIS kristaps

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).