Browse code

Add implementation

Robert Cranston authored on 25/04/2020 14:27:05
Showing 1 changed files

1 1
new file mode 100755
... ...
@@ -0,0 +1,39 @@
1
+#!/bin/sh
2
+set -euC
3
+
4
+# Bail if pandoc is not available.
5
+if ! command -v pandoc > "/dev/null"
6
+then
7
+    exit
8
+fi
9
+
10
+# Find readme.
11
+file="$(
12
+    git ls-tree HEAD \
13
+    | sed 's/^[^\t]\+\t//' \
14
+    | grep -i '^readme' \
15
+    | head -n 1
16
+)"
17
+if [ -z "$file" ]
18
+then
19
+    exit
20
+fi
21
+
22
+# Write the readme to a temporary directory so that pandoc's automatic filetype
23
+# detection works.
24
+tmp_dir="$(mktemp -d)"
25
+trap 'rm -rf "$tmp_dir"' EXIT
26
+in="$tmp_dir/$(basename "$file")"
27
+git cat-file blob "HEAD:$file" > "$in"
28
+
29
+# `--from` overrides.
30
+ext="$(printf "%s\n" "${file##*.}" | tr '[:upper:]' '[:lower*]')"
31
+case "$ext" in
32
+    *.md|*mkd|*.markdown)
33
+        from='--from gfm'
34
+        ;;
35
+esac
36
+
37
+# Run pandoc.
38
+out="${GIT_DIR:-"$(git rev-parse --git-dir)"}/README.html"
39
+pandoc ${from:-} --output "$out" "$in"