source@mandoc.bsd.lv
 help / color / mirror / Atom feed
* docbook2mdoc: Reorganize printing of .Pp macro lines, much improved
@ 2019-04-16 14:26 schwarze
  0 siblings, 0 replies; only message in thread
From: schwarze @ 2019-04-16 14:26 UTC (permalink / raw)
  To: source

Log Message:
-----------
Reorganize printing of .Pp macro lines, much improved formatting:
Refrain from manual tree inspection.
Instead, let <para> and other nodes keep paragraph state
and automatically emit .Pp when needed before printing text or macros.

Modified Files:
--------------
    docbook2mdoc:
        docbook2mdoc.c
        macro.c
        macro.h

Revision Data
-------------
Index: macro.c
===================================================================
RCS file: /home/cvs/mdocml/docbook2mdoc/macro.c,v
retrieving revision 1.13
retrieving revision 1.14
diff -Lmacro.c -Lmacro.c -u -p -r1.13 -r1.14
--- macro.c
+++ macro.c
@@ -30,6 +30,13 @@
 void
 macro_open(struct format *f, const char *name)
 {
+	if (f->parastate == PARA_WANT) {
+		if (f->linestate != LINE_NEW) {
+			putchar('\n');
+			f->linestate = LINE_NEW;
+		}
+		puts(".Pp");
+	}
 	switch (f->linestate) {
 	case LINE_MACRO:
 		if (f->flags & FMT_NOSPC) {
@@ -53,6 +60,7 @@ macro_open(struct format *f, const char 
 	fputs(name, stdout);
 	f->flags &= FMT_IMPL;
 	f->flags |= FMT_ARG;
+	f->parastate = PARA_MID;
 }
 
 void
@@ -138,6 +146,7 @@ macro_addarg(struct format *f, const cha
 	}
 	if (quote_now)
 		putchar('"');
+	f->parastate = PARA_MID;
 }
 
 void
@@ -170,6 +179,7 @@ macro_addnode(struct format *f, struct p
 
 	if (n->node == NODE_TEXT || n->node == NODE_ESCAPE) {
 		macro_addarg(f, n->b, flags);
+		f->parastate = PARA_MID;
 		return;
 	}
 
@@ -204,6 +214,7 @@ macro_addnode(struct format *f, struct p
 	}
 	if (quote_now)
 		putchar('"');
+	f->parastate = PARA_MID;
 }
 
 void
@@ -222,6 +233,13 @@ macro_nodeline(struct format *f, const c
 void
 print_text(struct format *f, const char *word, int flags)
 {
+	if (f->parastate == PARA_WANT) {
+		if (f->linestate != LINE_NEW) {
+			putchar('\n');
+			f->linestate = LINE_NEW;
+		}
+		puts(".Pp");
+	}
 	switch (f->linestate) {
 	case LINE_NEW:
 		break;
@@ -241,6 +259,7 @@ print_text(struct format *f, const char 
 			putchar('e');
 	}
 	f->linestate = LINE_TEXT;
+	f->parastate = PARA_MID;
 	f->flags = 0;
 }
 
Index: macro.h
===================================================================
RCS file: /home/cvs/mdocml/docbook2mdoc/macro.h,v
retrieving revision 1.4
retrieving revision 1.5
diff -Lmacro.h -Lmacro.h -u -p -r1.4 -r1.5
--- macro.h
+++ macro.h
@@ -26,6 +26,12 @@ enum	linestate {
 	LINE_MACRO	/* In the middle of a macro line. */
 };
 
+enum	parastate {
+	PARA_HAVE,	/* Just printed .Pp or equivalent. */
+	PARA_MID,	/* In the middle of a paragraph. */
+	PARA_WANT 	/* Need .Pp before printing anything else. */
+};
+
 struct	format {
 	int		 level;      /* Header level, starting at 1. */
 	int		 flags;
@@ -34,6 +40,7 @@ struct	format {
 #define	FMT_CHILD	 (1 << 2)    /* Expect a single child macro. */
 #define	FMT_IMPL	 (1 << 3)    /* Partial implicit block is open. */
 	enum linestate	 linestate;
+	enum parastate	 parastate;
 };
 
 #define	ARG_SPACE	1  /* Insert whitespace before this argument. */
Index: docbook2mdoc.c
===================================================================
RCS file: /home/cvs/mdocml/docbook2mdoc/docbook2mdoc.c,v
retrieving revision 1.129
retrieving revision 1.130
diff -Ldocbook2mdoc.c -Ldocbook2mdoc.c -u -p -r1.129 -r1.130
--- docbook2mdoc.c
+++ docbook2mdoc.c
@@ -141,45 +141,14 @@ pnode_printimagedata(struct format *f, s
 }
 
 static void
-pnode_printpara(struct format *f, struct pnode *n)
-{
-	struct pnode	*np;
-
-	if (n->parent == NULL)
-		return;
-
-	if ((np = TAILQ_PREV(n, pnodeq, child)) == NULL)
-	    np = n->parent;
-
-	f->flags = 0;
-
-	switch (np->node) {
-	case NODE_ENTRY:
-	case NODE_FOOTNOTE:
-	case NODE_GLOSSTERM:
-	case NODE_LISTITEM:
-	case NODE_TERM:
-		return;
-	case NODE_APPENDIX:
-	case NODE_LEGALNOTICE:
-	case NODE_PREFACE:
-	case NODE_SECTION:
-		if (f->level < 3)
-			return;
-		break;
-	default:
-		break;
-	}
-	macro_line(f, "Pp");
-}
-
-static void
 pnode_printrefnamediv(struct format *f, struct pnode *n)
 {
 	struct pnode	*nc, *nn;
 	int		 comma;
 
+	f->parastate = PARA_HAVE;
 	macro_line(f, "Sh NAME");
+	f->parastate = PARA_HAVE;
 	comma = 0;
 	TAILQ_FOREACH_SAFE(nc, &n->childq, child, nn) {
 		if (nc->node != NODE_REFNAME)
@@ -206,7 +175,9 @@ pnode_printrefsynopsisdiv(struct format 
 		if (nc->node == NODE_TITLE)
 			pnode_unlink(nc);
 
+	f->parastate = PARA_HAVE;
 	macro_line(f, "Sh SYNOPSIS");
+	f->parastate = PARA_HAVE;
 }
 
 /*
@@ -280,14 +251,17 @@ pnode_printsection(struct format *f, str
 	switch (level) {
 	case 1:
 		macro_close(f);
+		f->parastate = PARA_HAVE;
 		macro_open(f, "Sh");
 		break;
 	case 2:
 		macro_close(f);
+		f->parastate = PARA_HAVE;
 		macro_open(f, "Ss");
 		break;
 	default:
-		pnode_printpara(f, n);
+		if (f->parastate == PARA_MID)
+			f->parastate = PARA_WANT;
 		macro_open(f, "Sy");
 		break;
 	}
@@ -304,12 +278,14 @@ pnode_printsection(struct format *f, str
 	 */
 
 	if (nc != NULL) {
-		ncc = TAILQ_FIRST(&nc->childq);
-		if (ncc != NULL && ncc->node == NODE_TEXT &&
+		if (level == 1 &&
+		    (ncc = TAILQ_FIRST(&nc->childq)) != NULL &&
+		    ncc->node == NODE_TEXT &&
 		    strcasecmp(ncc->b, "AUTHORS") == 0)
 			macro_line(f, "An -nosplit");
 		pnode_unlink(nc);
 	}
+	f->parastate = level > 2 ? PARA_WANT : PARA_HAVE;
 }
 
 /*
@@ -785,6 +761,7 @@ pnode_printprologue(struct format *f, st
 	pnode_unlink(name);
 	pnode_unlink(vol);
 	pnode_unlink(descr);
+	f->parastate = PARA_HAVE;
 }
 
 static void
@@ -928,6 +905,7 @@ pnode_printvarlistentry(struct format *f
 	int		 first = 1;
 
 	macro_open(f, "It");
+	f->parastate = PARA_HAVE;
 	f->flags |= FMT_IMPL;
 	TAILQ_FOREACH_SAFE(nc, &n->childq, child, nn) {
 		if (nc->node != NODE_TERM && nc->node != NODE_GLOSSTERM)
@@ -944,16 +922,19 @@ pnode_printvarlistentry(struct format *f
 				break;
 			}
 		}
+		f->parastate = PARA_HAVE;
 		pnode_print(f, nc);
 		pnode_unlink(nc);
 		first = 0;
 	}
 	macro_close(f);
+	f->parastate = PARA_HAVE;
 	while ((nc = TAILQ_FIRST(&n->childq)) != NULL) {
 		pnode_print(f, nc);
 		pnode_unlink(nc);
 	}
 	macro_close(f);
+	f->parastate = PARA_HAVE;
 }
 
 static void
@@ -963,8 +944,9 @@ pnode_printtitle(struct format *f, struc
 
 	TAILQ_FOREACH_SAFE(nc, &n->childq, child, nn) {
 		if (nc->node == NODE_TITLE) {
-			pnode_printpara(f, nc);
-			pnode_print(f, nc);
+			if (f->parastate == PARA_MID)
+				f->parastate = PARA_WANT;
+			macro_nodeline(f, "Sy", nc, 0);
 			pnode_unlink(nc);
 		}
 	}
@@ -992,7 +974,9 @@ pnode_printtgroup1(struct format *f, str
 	macro_line(f, "Bl -bullet -compact");
 	while ((nc = pnode_findfirst(n, NODE_ENTRY)) != NULL) {
 		macro_line(f, "It");
+		f->parastate = PARA_HAVE;
 		pnode_print(f, nc);
+		f->parastate = PARA_HAVE;
 		pnode_unlink(nc);
 	}
 	macro_line(f, "El");
@@ -1004,16 +988,20 @@ pnode_printtgroup2(struct format *f, str
 {
 	struct pnode	*nr, *ne;
 
+	f->parastate = PARA_HAVE;
 	macro_line(f, "Bl -tag -width Ds");
 	while ((nr = pnode_findfirst(n, NODE_ROW)) != NULL) {
 		if ((ne = pnode_findfirst(n, NODE_ENTRY)) == NULL)
 			break;
 		macro_open(f, "It");
 		f->flags |= FMT_IMPL;
+		f->parastate = PARA_HAVE;
 		pnode_print(f, ne);
 		macro_close(f);
 		pnode_unlink(ne);
+		f->parastate = PARA_HAVE;
 		pnode_print(f, nr);
+		f->parastate = PARA_HAVE;
 		pnode_unlink(nr);
 	}
 	macro_line(f, "El");
@@ -1036,6 +1024,7 @@ pnode_printtgroup(struct format *f, stru
 		break;
 	}
 
+	f->parastate = PARA_HAVE;
 	macro_line(f, "Bl -ohang");
 	while ((nc = pnode_findfirst(n, NODE_ROW)) != NULL) {
 		macro_line(f, "It Table Row");
@@ -1051,11 +1040,14 @@ pnode_printlist(struct format *f, struct
 	struct pnode	*nc;
 
 	pnode_printtitle(f, n);
+	f->parastate = PARA_HAVE;
 	macro_argline(f, "Bl",
 	    n->node == NODE_ORDEREDLIST ? "-enum" : "-bullet");
 	TAILQ_FOREACH(nc, &n->childq, child) {
 		macro_line(f, "It");
+		f->parastate = PARA_HAVE;
 		pnode_print(f, nc);
+		f->parastate = PARA_HAVE;
 	}
 	macro_line(f, "El");
 	pnode_unlinksub(n);
@@ -1067,6 +1059,7 @@ pnode_printvariablelist(struct format *f
 	struct pnode	*nc;
 
 	pnode_printtitle(f, n);
+	f->parastate = PARA_HAVE;
 	macro_line(f, "Bl -tag -width Ds");
 	TAILQ_FOREACH(nc, &n->childq, child) {
 		if (nc->node == NODE_VARLISTENTRY)
@@ -1110,7 +1103,9 @@ pnode_print(struct format *f, struct pno
 		macro_line(f, "An -split");
 		break;
 	case NODE_BLOCKQUOTE:
+		f->parastate = PARA_HAVE;
 		macro_line(f, "Bd -ragged -offset indent");
+		f->parastate = PARA_HAVE;
 		break;
 	case NODE_CITEREFENTRY:
 		pnode_printciterefentry(f, n);
@@ -1146,6 +1141,8 @@ pnode_print(struct format *f, struct pno
 		if ((nc = TAILQ_FIRST(&n->childq)) != NULL &&
 		    pnode_class(nc->node) < CLASS_LINE)
 			macro_open(f, "Em");
+		if (n->node == NODE_GLOSSTERM)
+			f->parastate = PARA_HAVE;
 		break;
 	case NODE_ENVAR:
 		macro_open(f, "Ev");
@@ -1158,6 +1155,7 @@ pnode_print(struct format *f, struct pno
 		break;
 	case NODE_FOOTNOTE:
 		macro_line(f, "Bo");
+		f->parastate = PARA_HAVE;
 		break;
 	case NODE_FUNCTION:
 		macro_open(f, "Fn");
@@ -1172,7 +1170,9 @@ pnode_print(struct format *f, struct pno
 		pnode_printimagedata(f, n);
 		break;
 	case NODE_INFORMALEQUATION:
+		f->parastate = PARA_HAVE;
 		macro_line(f, "Bd -ragged -offset indent");
+		f->parastate = PARA_HAVE;
 		/* FALLTHROUGH */
 	case NODE_INLINEEQUATION:
 		macro_line(f, "EQ");
@@ -1201,8 +1201,10 @@ pnode_print(struct format *f, struct pno
 		break;
 	case NODE_LITERALLAYOUT:
 		macro_close(f);
+		f->parastate = PARA_HAVE;
 		macro_argline(f, "Bd", pnode_getattr(n, ATTRKEY_CLASS) ==
 		    ATTRVAL_MONOSPACED ? "-literal" : "-unfilled");
+		f->parastate = PARA_HAVE;
 		break;
 	case NODE_MARKUP:
 		macro_open(f, "Ic");
@@ -1235,7 +1237,8 @@ pnode_print(struct format *f, struct pno
 		pnode_printlist(f, n);
 		break;
 	case NODE_PARA:
-		pnode_printpara(f, n);
+		if (f->parastate == PARA_MID)
+			f->parastate = PARA_WANT;
 		break;
 	case NODE_PARAMDEF:
 	case NODE_PARAMETER:
@@ -1260,7 +1263,9 @@ pnode_print(struct format *f, struct pno
 	case NODE_PROGRAMLISTING:
 	case NODE_SCREEN:
 	case NODE_SYNOPSIS:
+		f->parastate = PARA_HAVE;
 		macro_line(f, "Bd -literal");
+		f->parastate = PARA_HAVE;
 		break;
 	case NODE_SYSTEMITEM:
 		pnode_printsystemitem(f, n);
@@ -1296,7 +1301,8 @@ pnode_print(struct format *f, struct pno
 		macro_open(f, "Ar");
 		break;
 	case NODE_SBR:
-		macro_line(f, "br");
+		if (f->parastate == PARA_MID)
+			macro_line(f, "br");
 		break;
 	case NODE_SUBSCRIPT:
 		if (f->linestate == LINE_MACRO)
@@ -1320,7 +1326,8 @@ pnode_print(struct format *f, struct pno
 		break;
 	case NODE_TITLE:
 	case NODE_SUBTITLE:
-		pnode_printpara(f, n);
+		if (f->parastate == PARA_MID)
+			f->parastate = PARA_WANT;
 		macro_nodeline(f, "Sy", n, 0);
 		pnode_unlinksub(n);
 		break;
@@ -1357,8 +1364,12 @@ pnode_print(struct format *f, struct pno
 		/* Accept more arguments to the previous macro. */
 		return;
 	case NODE_FOOTNOTE:
+		f->parastate = PARA_HAVE;
 		macro_line(f, "Bc");
 		break;
+	case NODE_GLOSSTERM:
+		f->parastate = PARA_HAVE;
+		break;
 	case NODE_INFORMALEQUATION:
 		macro_line(f, "EN");
 		macro_line(f, "Ed");
@@ -1401,6 +1412,10 @@ pnode_print(struct format *f, struct pno
 			break;
 		fputs(" } ", stdout);
 		break;
+	case NODE_PARA:
+		if (f->parastate == PARA_MID)
+			f->parastate = PARA_WANT;
+		break;
 	case NODE_QUOTE:
 		if ((nc = TAILQ_FIRST(&n->childq)) != NULL &&
 		    nc->node == NODE_FILENAME &&
@@ -1427,8 +1442,13 @@ pnode_print(struct format *f, struct pno
 	case NODE_PROGRAMLISTING:
 	case NODE_SCREEN:
 	case NODE_SYNOPSIS:
+		f->parastate = PARA_HAVE;
 		macro_line(f, "Ed");
 		break;
+	case NODE_TITLE:
+	case NODE_SUBTITLE:
+		f->parastate = PARA_WANT;
+		break;
 	default:
 		break;
 	}
@@ -1442,6 +1462,7 @@ ptree_print_mdoc(struct ptree *tree)
 
 	formatter.level = 0;
 	formatter.linestate = LINE_NEW;
+	formatter.parastate = PARA_HAVE;
 	pnode_printprologue(&formatter, tree->root);
 	pnode_print(&formatter, tree->root);
 	if (formatter.linestate != LINE_NEW)
--
 To unsubscribe send an email to source+unsubscribe@mandoc.bsd.lv

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

only message in thread, other threads:[~2019-04-16 14:26 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-16 14:26 docbook2mdoc: Reorganize printing of .Pp macro lines, much improved schwarze

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).