source@mandoc.bsd.lv
 help / color / mirror / Atom feed
From: schwarze@mandoc.bsd.lv
To: source@mandoc.bsd.lv
Subject: docbook2mdoc: factor out new function pnode_getattr_raw()
Date: Fri, 22 Mar 2019 11:56:14 -0500 (EST)	[thread overview]
Message-ID: <e3fc931347ef6ffd@fantadrom.bsd.lv> (raw)

Log Message:
-----------
factor out new function pnode_getattr_raw()

Modified Files:
--------------
    docbook2mdoc:
        docbook2mdoc.c

Revision Data
-------------
Index: docbook2mdoc.c
===================================================================
RCS file: /home/cvs/mdocml/docbook2mdoc/docbook2mdoc.c,v
retrieving revision 1.58
retrieving revision 1.59
diff -Ldocbook2mdoc.c -Ldocbook2mdoc.c -u -p -r1.58 -r1.59
--- docbook2mdoc.c
+++ docbook2mdoc.c
@@ -491,6 +491,22 @@ pnode_unlinksub(struct pnode *pn)
 }
 
 /*
+ * Retrieve an attribute string from a node.
+ * Return defval if the node has no such attribute.
+ */
+const char *
+pnode_getattr_raw(struct pnode *pn, enum attrkey key, const char *defval)
+{
+	struct pattr	*ap;
+
+	TAILQ_FOREACH(ap, &pn->attrq, child)
+		if (ap->key == key)
+			return ATTRVAL__MAX == ap->val ? ap->rawval :
+			    attrvals[ap->val];
+	return(defval);
+}
+
+/*
  * Reset the lookaside buffer.
  */
 static void
@@ -911,15 +927,8 @@ static void
 pnode_printmathfenced(struct parse *p, struct pnode *pn)
 {
 	struct pnode	*pp;
-	struct pattr	*ap;
 
-	TAILQ_FOREACH(ap, &pn->attrq, child)
-		if (ATTRKEY_OPEN == ap->key) {
-			printf("left %s ", ap->rawval);
-			break;
-		}
-	if (NULL == ap)
-		printf("left ( ");
+	printf("left %s ", pnode_getattr_raw(pn, ATTRKEY_OPEN, "("));
 
 	pp = TAILQ_FIRST(&pn->childq);
 	pnode_print(p, pp);
@@ -929,13 +938,7 @@ pnode_printmathfenced(struct parse *p, s
 		pnode_print(p, pp);
 	}
 
-	TAILQ_FOREACH(ap, &pn->attrq, child)
-		if (ATTRKEY_CLOSE == ap->key) {
-			printf("right %s ", ap->rawval);
-			break;
-		}
-	if (NULL == ap)
-		printf("right ) ");
+	printf("right %s ", pnode_getattr_raw(pn, ATTRKEY_CLOSE, ")"));
 }
 
 /*
@@ -1090,8 +1093,6 @@ static void
 pnode_printprologue(struct parse *p, struct pnode *pn)
 {
 	struct pnode	*pp;
-	struct pattr	*ap;
-	const char	*name;
 
 	pp = NULL == p->root ? NULL :
 		pnode_findfirst(p->root, NODE_REFMETA);
@@ -1100,16 +1101,9 @@ pnode_printprologue(struct parse *p, str
 	if (NULL != pp) {
 		pnode_printrefmeta(p, pp);
 		pnode_unlink(pp);
-	} else {
-		name = "UNKNOWN";
-		TAILQ_FOREACH(ap, &p->root->attrq, child) {
-			if (ATTRKEY_ID == ap->key) {
-				name = ap->rawval;
-				break;
-			}
-		}
-		printf(".Dt %s 1\n", name);
-	}
+	} else
+		printf(".Dt %s 1\n",
+		    pnode_getattr_raw(p->root, ATTRKEY_ID, "UNKNOWN"));
 	puts(".Os");
 
 	if (PARSE_EQN & p->flags) {
@@ -1255,7 +1249,7 @@ static void
 pnode_print(struct parse *p, struct pnode *pn)
 {
 	struct pnode	*pp;
-	struct pattr	*ap;
+	const char	*ccp;
 	char		*cp;
 	int		 last, sv;
 
@@ -1364,13 +1358,11 @@ pnode_print(struct parse *p, struct pnod
 		puts(".Sh LEGAL NOTICE");
 		break;
 	case (NODE_LINK):
-		TAILQ_FOREACH(ap, &pn->attrq, child)
-			if (ATTRKEY_LINKEND == ap->key)
-				break;
-		if (ap == NULL)
+		ccp = pnode_getattr_raw(pn, ATTRKEY_LINKEND, NULL);
+		if (NULL == ccp)
 			break;
 		pnode_printmopen(p);
-		printf("Sx %s\n", ap->rawval);
+		printf("Sx %s\n", ccp);
 		p->newln = 1;
 		return;
 	case (NODE_LITERAL):
--
 To unsubscribe send an email to source+unsubscribe@mandoc.bsd.lv

                 reply	other threads:[~2019-03-22 16:56 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e3fc931347ef6ffd@fantadrom.bsd.lv \
    --to=schwarze@mandoc.bsd.lv \
    --cc=source@mandoc.bsd.lv \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).