bin/wiki.sh (view raw)
1#!/usr/bin/env bash
2
3# wiki.sh -- helper script to generate a wiki
4# does all kinds of weird shit. this is NOT how you engineer software, people.
5
6WIKI_PATH="pages/wiki"
7ROOT_INDEX_MD="pages/wiki/_index.md"
8
9noext() {
10 printf '%s' "${1%%'.md'}"
11}
12
13topic() {
14 entry="$1"
15 mkdir "$WIKI_PATH"/entry
16 printf '%s' "---
17title: $entry
18subtitle:
19date: $(date +'%Y-%m-%d')
20template: page.html
21---" > "$WIKI_PATH"/entry/_index.md
22
23}
24
25generate_index() {
26 mapfile -t entries < <(find "$WIKI_PATH" ! -path "$WIKI_PATH")
27 prevdir=''
28 for r in "${entries[@]}"; do
29 path="$(basename "$r")"
30 [ "$path" != "_index.md" ] && {
31 if [ -d "$r" ]; then
32 printf '%s\n' "- [$path](/wiki/$path)"
33 prevdir="$path"
34 elif [ "$(basename "$(dirname "$r")")" == "$prevdir" ]; then
35 noext="$(noext "$path")"
36 printf ' %s\n' "- [$noext](/wiki/$prevdir/$noext)"
37 else
38 noext="$(noext "$path")"
39 printf '%s\n' "- [$noext](/wiki/$noext)"
40 fi
41 }
42 done
43}
44
45link() {
46 # FIXME: this needs to be reworked
47 # post A, and post B
48 a="$(noext "$1")"
49 a="${a#"$WIKI_PATH"}"
50 b="$(noext "$2")"
51 b="${a#"$WIKI_PATH"}"
52
53 printf '%s' "- [$a](/wiki/$a)" >> "$2"
54 printf '%s' "- [$b](/wiki/$b)" >> "$1"
55}
56
57if [ "$#" -eq 0 ]; then
58 printf '%s' "---
59title: Wiki
60subtitle: Ideas, beliefs and thoughts.
61date: $(date +'%Y-%m-%d')
62template: page.html
63---
64
65" > "$ROOT_INDEX_MD"
66 generate_index >> "$ROOT_INDEX_MD"
67else
68 case "$1" in
69 "topic")
70 shift
71 topic "$1"
72 ;;
73 esac
74fi