all repos — site @ 2c462d201aea890337df3a79d01e41397671b375

source for my site, found at icyphox.sh

bin/old/wiki.sh (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
#!/usr/bin/env bash

# wiki.sh -- helper script to generate a wiki
# does all kinds of weird shit. this is NOT how you engineer software, people.

WIKI_PATH="pages/wiki"
ROOT_INDEX_MD="pages/wiki/_index.md"

noext() {
   printf '%s' "${1%%'.md'}"
}

topic() {
    entry="$1"
    mkdir "$WIKI_PATH/$entry"
    printf '%s' "---
title: $entry
subtitle:
date: $(date +'%Y-%m-%d')
template: page.html
---" > "$WIKI_PATH/$entry/"_index.md

}

generate_index() {
    mapfile -t entries < <(find "$WIKI_PATH" ! -path "$WIKI_PATH" | sort)
    prevdir=''
    for r in "${entries[@]}"; do
        path="$(basename "$r")"
        [ "$path" != "_index.md" ] && {
            if [ -d "$r" ]; then
                printf '%s\n' "- [$path](/wiki/$path)"
                prevdir="$path"
            elif [ "$(basename "$(dirname "$r")")" == "$prevdir" ]; then
                noext="$(noext "$path")"
                printf '  %s\n' "- [$noext](/wiki/$prevdir/$noext)"
            else
                noext="$(noext "$path")"
                printf '%s\n' "- [$noext](/wiki/$noext)"
            fi
        }
    done
    exit 0
}

link() {
    # FIXME: this needs to be reworked
    # post A, and post B
    a="$(noext "$1")"
    a="${a#"$WIKI_PATH"}"
    b="$(noext "$2")"
    b="${a#"$WIKI_PATH"}"

    printf '%s' "- [$a](/wiki/$a)" >> "$2"
    printf '%s' "- [$b](/wiki/$b)" >> "$1"
}

if [ "$#" -eq 0 ]; then
    printf '%s' "---
title: the wiki
subtitle: A collection of notes on various topics.
date: $(date +'%Y-%m-%d')
template: page.html
---

" > "$ROOT_INDEX_MD"
    generate_index >> "$ROOT_INDEX_MD"
else
    case "$1" in
        "topic")
            shift
            topic "$1"
            ;;
    esac
fi