summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAli Fardan <raiz@stellarbound.space>2020-11-06 00:45:33 +0300
committerAli Fardan <raiz@stellarbound.space>2020-11-06 00:45:33 +0300
commitef0e7ee27849017e12bd9d651ec0d20fabeba511 (patch)
tree3c212251c087afae377abef6150aafcf0edf077d
downloadgmitohtml-master.tar.gz
initial commit HEAD master
-rw-r--r--Makefile25
-rw-r--r--gmitohtml.142
-rw-r--r--gmitohtml.c68
3 files changed, 135 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..b56b19b
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,25 @@
+CC = cc
+CFLAGS = -static -O2 -Wall -Wextra
+LIBS = -lgemtext
+OUT = gmitohtml
+
+PREFIX = /usr
+
+OBJS = gmitohtml.o
+MANPAGES = gmitohtml.1
+
+all: $(OUT)
+
+$(OUT): $(OBJS)
+	$(CC) $(CFLAGS) -o $(OUT) $(OBJS) $(LIBS)
+
+install: all
+	mkdir -p $(PREFIX)/bin
+	cp -f $(OUT) $(PREFIX)/bin
+	mkdir -p $(PREFIX)/share/man/man1
+	cp $(MANPAGES) $(PREFIX)/share/man/man1
+
+clean:
+	rm -f $(OUT) $(OBJS)
+
+.PHONY: all clean
diff --git a/gmitohtml.1 b/gmitohtml.1
new file mode 100644
index 0000000..8ebf271
--- /dev/null
+++ b/gmitohtml.1
@@ -0,0 +1,42 @@
+.Dd November 6, 2020
+.Dt GMITOHTML 1
+.Sh NAME
+.Nm gmitohtml
+.Nd convert gemtext to HTML
+.Sh SYNOPSIS
+.Nm gmitohtml
+.Op Fl h
+.Op Fl i Ar input
+.Op Fl o Ar output
+.Sh DESCRIPTION
+.Nm gmitohtml
+reads gemtext from stdin and converts it to HTML written to stdout.
+.Pp
+Options are as follows:
+.Bl -tag -width Ds
+.It Fl o
+Print usage help message.
+.It Fl i
+Specify input file instead of stdin.
+.It Fl o
+Specify output file instead of stdout.
+.El
+.Sh EXAMPLES
+Read from stdin, output to stdout:
+.Bd -literal -offset indent
+$ gmitohtml < index.gmi > index.html
+.Ed
+.Pp
+Use alternative input/output:
+.Bd -literal -offset indent
+$ gmitohtml -i index.gmi -o index.html
+.Ed
+.Pp
+Use only alternative input then redirect stdout output:
+.Bd -literal -offset indent
+$ gmitohtml -i index.gmi > output.html
+.Ed
+.Sh CAVEATS
+This tool processes input and output in one chunk, meaning that streaming from and into pipes is not an option.
+.Sh AUTHOR
+.An Ali Fardan Aq Mt raiz@stellarbound.space
diff --git a/gmitohtml.c b/gmitohtml.c
new file mode 100644
index 0000000..611a96e
--- /dev/null
+++ b/gmitohtml.c
@@ -0,0 +1,68 @@
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <gemtext.h>
+
+#define HEADER "<!DOCTYPE html><html><body>"
+#define FOOTER "</body></html>"
+
+int
+main(int argc, char *argv[])
+{
+	int ch;
+	int infd;
+	int outfd;
+	char *in;
+	char *out;
+	struct gemtext **list;
+
+	in = "/dev/stdin";
+	out = "/dev/stdout";
+
+	while ((ch = getopt(argc, argv, "i:o:h")) != -1) {
+		switch (ch) {
+		case 'i':
+			in = optarg;
+			break;
+		case 'o':
+			out = optarg;
+			break;
+		case 'h':
+			fprintf(stderr, "Usage: %s [-h] [-i input] [-o output]\n", argv[0]);
+			return 0;
+		default:
+			fprintf(stderr, "Usage: %s [-h] [-i input] [-o output]\n", argv[0]);
+			return 1;
+		}
+	}
+
+	infd = open(in, O_RDONLY);
+	if (infd == -1)
+		err(1, "open");
+
+	outfd = open(out, O_WRONLY|O_CREAT|O_TRUNC);
+	if (outfd == -1)
+		err(1, "open");
+
+	list = gemtext_list_decode_fd(infd);
+	if (list == NULL)
+		err(1, "gemtext_list_decode_fd");
+	close(infd);
+
+	if (write(outfd, HEADER, strlen(HEADER)) == -1)
+		err(1, "write");
+	if (gemtext_list_encode_to_html_fd(list, outfd) == -1)
+		err(1, "gemtext_list_encode_to_html_fd");
+	if (write(outfd, FOOTER, strlen(FOOTER)) == -1)
+		err(1, "write");
+
+	close(outfd);
+	gemtext_list_free(list);
+	return 0;
+}