summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--mandocdb.c28
-rw-r--r--mdoc.c40
-rw-r--r--mdoc.h1
3 files changed, 44 insertions, 25 deletions
diff --git a/mandocdb.c b/mandocdb.c
index e2b73b84..b2da7633 100644
--- a/mandocdb.c
+++ b/mandocdb.c
@@ -1506,32 +1506,10 @@ parse_mdoc_Xr(struct mpage *mpage, const struct mdoc_node *n)
static int
parse_mdoc_Nd(struct mpage *mpage, const struct mdoc_node *n)
{
- size_t sz;
-
- if (MDOC_BODY != n->type)
- return(0);
- /*
- * Special-case the `Nd' because we need to put the description
- * into the document table.
- */
-
- for (n = n->child; NULL != n; n = n->next) {
- if (MDOC_TEXT == n->type) {
- if (NULL != mpage->desc) {
- sz = strlen(mpage->desc) +
- strlen(n->string) + 2;
- mpage->desc = mandoc_realloc(
- mpage->desc, sz);
- strlcat(mpage->desc, " ", sz);
- strlcat(mpage->desc, n->string, sz);
- } else
- mpage->desc = mandoc_strdup(n->string);
- }
- if (NULL != n->child)
- parse_mdoc_Nd(mpage, n);
- }
- return(1);
+ if (MDOC_BODY == n->type)
+ mdoc_deroff(&mpage->desc, n);
+ return(0);
}
static int
diff --git a/mdoc.c b/mdoc.c
index 814e43b5..c3f84c4a 100644
--- a/mdoc.c
+++ b/mdoc.c
@@ -22,6 +22,7 @@
#include <sys/types.h>
#include <assert.h>
+#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
@@ -1020,3 +1021,42 @@ mdoc_isdelim(const char *p)
return(DELIM_NONE);
}
+
+void
+mdoc_deroff(char **dest, const struct mdoc_node *n)
+{
+ char *cp;
+ size_t sz;
+
+ if (MDOC_TEXT != n->type) {
+ for (n = n->child; n; n = n->next)
+ mdoc_deroff(dest, n);
+ return;
+ }
+
+ /* Skip leading whitespace. */
+
+ for (cp = n->string; '\0' != *cp; cp++)
+ if (0 == isspace((unsigned char)*cp))
+ break;
+
+ /* Skip trailing whitespace. */
+
+ for (sz = strlen(cp); sz; sz--)
+ if (0 == isspace((unsigned char)cp[sz-1]))
+ break;
+
+ /* Skip empty strings. */
+
+ if (0 == sz)
+ return;
+
+ if (NULL == *dest) {
+ *dest = mandoc_strndup(cp, sz);
+ return;
+ }
+
+ mandoc_asprintf(&cp, "%s %*s", *dest, (int)sz, cp);
+ free(*dest);
+ *dest = cp;
+}
diff --git a/mdoc.h b/mdoc.h
index 7bc8819e..86da0334 100644
--- a/mdoc.h
+++ b/mdoc.h
@@ -389,6 +389,7 @@ struct mdoc;
const struct mdoc_node *mdoc_node(const struct mdoc *);
const struct mdoc_meta *mdoc_meta(const struct mdoc *);
+void mdoc_deroff(char **, const struct mdoc_node *);
__END_DECLS