source@mandoc.bsd.lv
 help / color / mirror / Atom feed
* pod2mdoc: If a function is listed in the SYNOPSIS and its name occurs
@ 2015-02-13  0:44 schwarze
  0 siblings, 0 replies; only message in thread
From: schwarze @ 2015-02-13  0:44 UTC (permalink / raw)
  To: source

Log Message:
-----------
If a function is listed in the SYNOPSIS and its name occurs later in the
text, followed by "()", mark it up with `Fn'.  This is the first of
a number of features assigning markup by looking at earlier content.
Portability glue will be added later.

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

Added Files:
-----------
    pod2mdoc:
        dict.c
        dict.h

Revision Data
-------------
--- /dev/null
+++ dict.h
@@ -0,0 +1,26 @@
+/*	$Id: dict.h,v 1.1 2015/02/13 00:44:16 schwarze Exp $	*/
+/*
+ * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+enum mdoc_type {
+	MDOC_Fo = 0,
+	MDOC_MAX
+};
+
+void		 dict_init(void);
+enum mdoc_type	 dict_get(const char *, size_t);
+void		 dict_put(const char *, enum mdoc_type);
+void		 dict_destroy(void);
Index: pod2mdoc.c
===================================================================
RCS file: /home/cvs/mdocml/pod2mdoc/pod2mdoc.c,v
retrieving revision 1.36
retrieving revision 1.37
diff -Lpod2mdoc.c -Lpod2mdoc.c -u -p -r1.36 -r1.37
--- pod2mdoc.c
+++ pod2mdoc.c
@@ -1,6 +1,7 @@
-/*	$Id$ */
+/*	$Id$	*/
 /*
  * Copyright (c) 2014 Kristaps Dzonsons <kristaps@bsd.lv>
+ * Copyright (c) 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
  *
  * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
@@ -26,6 +27,8 @@
 #include <string.h>
 #include <unistd.h>
 
+#include "dict.h"
+
 /*
  * In what section can we find Perl module manuals?
  * Sometimes (Mac OS X) it's 3pm, sometimes (OpenBSD, etc.) 3p.
@@ -1107,6 +1110,7 @@ again:
 			putchar('\n');
 			buf[ifa++] = '\0';
 			printf(".Fo %s\n", buf + ifo);
+			dict_put(buf + ifo, MDOC_Fo);
 			buf[ifc++] = '\0';
 			for (;;) {
 				cp = strchr(buf + ifa, ',');
@@ -1333,12 +1337,26 @@ ordinary(struct state *st, const char *b
 			 */
 
 			last = buf[start++];
-			if (' ' == last) {
-				outbuf_flush(st);
-				putchar(' ');
-				st->wantws = 1;
-			} else
+			if (' ' != last) {
 				outbuf_addchar(st);
+				continue;
+			}
+
+			if ( ! strcmp(st->outbuf + st->outbuflen - 2, "()") &&
+			    dict_get(st->outbuf, st->outbuflen - 2) ==
+			    MDOC_Fo) {
+				st->outbuflen -= 2;
+				st->outbuf[st->outbuflen] = '\0';
+				mdoc_newln(st);
+				fputs(".Fn ", stdout);
+				outbuf_flush(st);
+				mdoc_newln(st);
+				continue;
+			}
+
+			outbuf_flush(st);
+			putchar(' ');
+			st->wantws = 1;
 		}
 
 		if (start < end - 1 && '<' == buf[start + 1] &&
@@ -1504,6 +1522,7 @@ dofile(const struct args *args, const ch
 
 	free(title);
 
+	dict_init();
 	memset(&st, 0, sizeof(struct state));
 	st.oust = OUST_NL;
 	st.wantws = 1;
@@ -1531,6 +1550,7 @@ dofile(const struct args *args, const ch
 		dopar(&st, buf, cur, end);
 		cur = sup;
 	}
+	dict_destroy();
 }
 
 /*
Index: pod2mdoc.1
===================================================================
RCS file: /home/cvs/mdocml/pod2mdoc/pod2mdoc.1,v
retrieving revision 1.13
retrieving revision 1.14
diff -Lpod2mdoc.1 -Lpod2mdoc.1 -u -p -r1.13 -r1.14
--- pod2mdoc.1
+++ pod2mdoc.1
@@ -50,7 +50,7 @@ For compatibility with
 Ignored.
 .It Fl d Ar date
 Set the document date
-.Pq Sq Dd
+.Pq Sq \&Dd
 to
 .Ar date
 .Po
@@ -64,7 +64,7 @@ uses the file modification date or the c
 standard input.
 .It Fl n Ar title
 Set the document title
-.Pq Sq Dt
+.Pq Sq \&Dt
 to
 .Ar title .
 If unspecified,
@@ -77,7 +77,7 @@ if reading from standard input
 .Pq you probably don't want that .
 .It Fl s Ar section
 Set the document section
-.Pq Sq Dt
+.Pq Sq \&Dt
 to
 .Ar section .
 If unspecified,
@@ -141,7 +141,7 @@ Thus, the input
 .Li B<foo> [B<-bar baz>]
 is rendered as follows:
 .Bd -literal
-\&.Nm foo 
+\&.Nm foo
 \&.Oo
 \&.Fl bar Ar baz
 \&.Oc
@@ -192,6 +192,11 @@ If only a section appears, such as in
 .Li </section> ,
 the link is rendered with
 .Sq \&Sx .
+.Pp
+Words followed by
+.Qq Pq
+that match function names listed in the SYNOPSIS section are marked up with
+.Sq \&Fn .
 .Sh EXIT STATUS
 .Ex -std
 .Sh EXAMPLES
@@ -235,8 +240,7 @@ does, append a POD ERRORS section in the
 .Sh AUTHORS
 .Nm
 was written by
-.Ar Kristaps Dzonsons ,
-.Mt kristaps@bsd.lv .
+.An Kristaps Dzonsons Aq Mt kristaps@bsd.lv .
 .Sh CAVEATS
 By way of being a presentational language, POD is not well-represented
 by
--- /dev/null
+++ dict.c
@@ -0,0 +1,126 @@
+/*	$Id: dict.c,v 1.1 2015/02/13 00:44:16 schwarze Exp $	*/
+/*
+ * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+#include <sys/types.h>
+
+#include <stddef.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ohash.h>
+
+#include "dict.h"
+
+struct dict_entry {
+	enum mdoc_type	 t;
+	char		 s[];
+};
+
+static void	*dict_malloc(size_t, void *);
+static void	*dict_calloc(size_t, size_t, void *);
+static void	 dict_free(void *, void *);
+
+static struct ohash dict_data;
+
+
+void
+dict_init(void)
+{
+	struct ohash_info	 dict_info;
+
+	dict_info.key_offset = offsetof(struct dict_entry, s);
+	dict_info.data = NULL;
+	dict_info.alloc = dict_malloc;
+	dict_info.calloc = dict_calloc;
+	dict_info.free = dict_free;
+
+	ohash_init(&dict_data, 4, &dict_info);
+}
+
+enum mdoc_type
+dict_get(const char *s, size_t len)
+{
+	struct dict_entry	*entry;
+	const char		*end;
+	unsigned int		 slot;
+
+	if (len == 0)
+		len = strlen(s);
+	end = s + len;
+	slot = ohash_qlookupi(&dict_data, s, &end);
+	entry = ohash_find(&dict_data, slot);
+	return(entry == NULL ? MDOC_MAX : entry->t);
+}
+
+void
+dict_put(const char *s, enum mdoc_type t)
+{
+	struct dict_entry	*entry;
+	const char		*end;
+	size_t			 len;
+	unsigned int		 slot;
+
+	len = strlen(s);
+	end = s + len;
+	slot = ohash_qlookupi(&dict_data, s, &end);
+	entry = ohash_find(&dict_data, slot);
+	if (entry == NULL) {
+		entry = malloc(sizeof(*entry) + len + 1);
+		if (entry == NULL) {
+			perror(NULL);
+			exit(1);
+		}
+		memcpy(entry->s, s, len + 1);
+		ohash_insert(&dict_data, slot, entry);
+	}
+	entry->t = t;
+}
+
+void
+dict_destroy(void)
+{
+	struct dict_entry	*entry;
+	unsigned int		 slot;
+
+	entry = ohash_first(&dict_data, &slot);
+	while (entry != NULL) {
+		free(entry);
+		entry = ohash_next(&dict_data, &slot);
+	}
+	ohash_delete(&dict_data);
+}
+
+static void *
+dict_malloc(size_t size, void *dummy)
+{
+
+	return(malloc(size));
+}
+
+static void *
+dict_calloc(size_t nmemb, size_t size, void *dummy)
+{
+
+	return(calloc(nmemb, size));
+}
+
+static void
+dict_free(void *ptr, void *dummy)
+{
+
+	free(ptr);
+}
Index: Makefile
===================================================================
RCS file: /home/cvs/mdocml/pod2mdoc/Makefile,v
retrieving revision 1.12
retrieving revision 1.13
diff -LMakefile -LMakefile -u -p -r1.12 -r1.13
--- Makefile
+++ Makefile
@@ -2,8 +2,8 @@ VERSION = 0.1
 CFLAGS += -g -W -Wall -Wstrict-prototypes -Wno-unused-parameter -Wwrite-strings 
 WWWPREFIX = /usr/vhosts/mdocml.bsd.lv/www/htdocs/pod2mdoc
 
-pod2mdoc: pod2mdoc.o
-	$(CC) -o $@ pod2mdoc.o
+pod2mdoc: dict.o pod2mdoc.o
+	$(CC) $(LDFLAGS) -o $@ dict.o pod2mdoc.o -lutil
 
 www: index.html pod2mdoc.1.html pod2mdoc-$(VERSION).tgz
 
@@ -29,6 +29,6 @@ pod2mdoc.1.html: pod2mdoc.1
 	mandoc -Thtml pod2mdoc.1 >$@
 
 clean:
-	rm -f pod2mdoc pod2mdoc.o pod2mdoc-$(VERSION).tgz \
+	rm -f pod2mdoc dict.o pod2mdoc.o pod2mdoc-$(VERSION).tgz \
 		index.html pod2mdoc.1.html
 	rm -rf pod2mdoc.dSYM
--
 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:[~2015-02-13  0:44 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-02-13  0:44 pod2mdoc: If a function is listed in the SYNOPSIS and its name occurs 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).