aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile12
-rw-r--r--README.gmi30
-rw-r--r--tango.144
-rw-r--r--tango.c121
-rw-r--r--test.gmi15
5 files changed, 222 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..9ae3c18
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,12 @@
+CC = cc
+CFLAGS = -Wall -g
+
+.PHONY: all clean install
+
+all: tango
+
+tango: tango.o
+ ${CC} -o tango tango.o ${LDFLAGS}
+
+clean:
+ rm -f *.o tango
diff --git a/README.gmi b/README.gmi
new file mode 100644
index 0000000..e7bae6e
--- /dev/null
+++ b/README.gmi
@@ -0,0 +1,30 @@
+# tango
+
+> tangle gemini files
+
+tango is an utility to tangle literate programs written in text/gemini files. It’s able to export bocks all blocks of preformatted text, or only a subset of them matching a regular expression, and export them.
+
+### Usage
+
+```
+tango [-r regex] [-o out] [file]
+```
+
+tango will extract all the code blocks from file (standard input if not specified) to out (standard output if not specified), optionally limiting the blocks to those who match the provided regular expression.
+
+You’ll find a test.gmi file that distributed with the sources. It’s a silly text/gemini file with different example of “Hello World” in different programming languages (elisp, c, common-lisp). You can export the common-lisp with
+```sh
+tango -r common-lisp test.gmi
+```
+
+To export the C code you may run
+```sh
+tango -r c test.gmi
+```
+but ‘c’ matches both C and common-lisp, so you have to run
+```sh
+tango -r 'c$' test.gmi
+```
+instead.
+
+See the manpage for further details.
diff --git a/tango.1 b/tango.1
new file mode 100644
index 0000000..ac2163f
--- /dev/null
+++ b/tango.1
@@ -0,0 +1,44 @@
+.\" Copyright (c) 2020 Omar Polo <op@omarpolo.com>
+.\"
+.\" 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 AUTHOR DISCLAIMS ALL WARRANTIES
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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.
+.Dd $Mdocdate: October 2 2020$
+.Dt TANGO 1
+.Os
+.Sh NAME
+.Nm tango
+.Nd tagle gemini files
+.Sh SYNOPSIS
+.Nm
+.Bk -words
+.Op Fl r Ar regex
+.Op Fl o Ar out
+.Op files...
+.Ek
+.Sh DESCRIPTION
+.Nm
+exports blocks of code from text/gemini files.
+.Pp
+.Nm
+is able to export only the preformatted blocks whose alternative text
+matches
+.Ar regex .
+.Pp
+By default
+.Nm
+reads from standard input and writes to standard output, if no
+.Ar out
+or
+.Ar file
+are given.
+.Pp
+The regex is matched ignoring the case.
diff --git a/tango.c b/tango.c
new file mode 100644
index 0000000..2b53bbe
--- /dev/null
+++ b/tango.c
@@ -0,0 +1,121 @@
+/*
+ * Copyright (c) 2020 Omar Polo <op@omarpolo.com>
+ *
+ * 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 AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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 <err.h>
+#include <regex.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#ifndef __OpenBSD__
+# define pledge(a, b) 0
+#endif
+
+int
+is_pre(char *line)
+{
+ return *line == '`'
+ && *(line+1) == '`'
+ && *(line+2) == '`';
+}
+
+int
+process_file(regex_t *re, const char *path, FILE *out)
+{
+ FILE *in;
+ char *line = NULL;
+ size_t linesize = 0;
+ ssize_t linelen;
+ int in_pre = 0, tangle;
+
+ if ((in = fopen(path, "r")) == NULL)
+ err(1, "open: %s", path);
+
+ tangle = re == NULL;
+ while ((linelen = getline(&line, &linesize, in)) != -1) {
+ if (is_pre(line)) {
+ in_pre = !in_pre;
+ if (tangle && !in_pre)
+ fprintf(out, "\n");
+ if (in_pre && re != NULL)
+ tangle = regexec(re, line+3, 0, NULL, 0) != REG_NOMATCH;
+ } else if (in_pre && tangle)
+ fwrite(line, linelen, 1, out);
+ }
+
+ free(line);
+ if (ferror(in))
+ err(1, "getline");
+ fclose(in);
+ return 1;
+}
+
+__dead void
+usage(const char *me)
+{
+ fprintf(stderr, "USAGE: %s [-r regexp] [-o out] [file...]\n", me);
+ exit(1);
+}
+
+int
+main(int argc, char **argv)
+{
+ FILE *out;
+ int ch, has_reg, i;
+ regex_t r;
+
+ out = stdout;
+ has_reg = 0;
+ while ((ch = getopt(argc, argv, "o:r:")) != -1) {
+ switch (ch) {
+ case 'o':
+ if (out != stdout)
+ errx(1, "-o can only be provided once");
+ if ((out = fopen(optarg, "r")) == NULL)
+ err(1, "open: %s", optarg);
+ break;
+
+ case 'r':
+ if (has_reg)
+ errx(1, "-r can only be provided once");
+ has_reg = 1;
+ if (regcomp(&r, optarg, REG_ICASE | REG_NOSUB | REG_NEWLINE))
+ errx(1, "cannot compile regex: %s", optarg);
+ break;
+
+ default:
+ usage(*argv);
+ }
+ }
+ argc -= optind;
+ argv += optind;
+
+ if (pledge("stdio rpath", NULL) == -1)
+ err(1, "pledge");
+
+ for (i = 0; i < argc; ++i)
+ process_file(has_reg ? &r : NULL, argv[i], out);
+
+ if (has_reg)
+ regfree(&r);
+
+ if (out != stdout)
+ fclose(out);
+
+ return 0;
+}
diff --git a/test.gmi b/test.gmi
new file mode 100644
index 0000000..883a6a2
--- /dev/null
+++ b/test.gmi
@@ -0,0 +1,15 @@
+# Hello world
+
+Here’s different examples of “hello world” programs in different programming languages
+
+```elisp
+(message "Hello, world!")
+```
+
+```c
+printf("Hello, world!");
+```
+
+```common-lisp
+(format t "Hello, world!")
+```