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" | sort)
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 exit 0
44}
45
46link() {
47 # FIXME: this needs to be reworked
48 # post A, and post B
49 a="$(noext "$1")"
50 a="${a#"$WIKI_PATH"}"
51 b="$(noext "$2")"
52 b="${a#"$WIKI_PATH"}"
53
54 printf '%s' "- [$a](/wiki/$a)" >> "$2"
55 printf '%s' "- [$b](/wiki/$b)" >> "$1"
56}
57
58if [ "$#" -eq 0 ]; then
59 printf '%s' "---
60title: the wiki
61subtitle: Ideas, thoughts and strongy held opinions.
62date: $(date +'%Y-%m-%d')
63template: page.html
64---
65
66" > "$ROOT_INDEX_MD"
67 generate_index >> "$ROOT_INDEX_MD"
68else
69 case "$1" in
70 "topic")
71 shift
72 topic "$1"
73 ;;
74 esac
75fi