|
| 1 | +#!/bin/bash |
| 2 | +# aosc-git-wiki.sh: gh wiki utils |
| 3 | +# Converts from/to dir layouts and flat GitHub wiki layout. |
| 4 | +# A special line <aosc-git-wiki title=TITLE path=PATH/> should be used. Otherwise we would guess it. |
| 5 | +shopt -s extglob globstar || exit 2 |
| 6 | +: ${ghwiki=.githubwiki} ${docdir=doc} |
| 7 | +die(){ echo "$1"; exit "${2-1}"; } |
| 8 | +[ -e .git ] || die "Git will fail without .git" |
| 9 | + |
| 10 | +declare -A remotes |
| 11 | + |
| 12 | +update_remote(){ |
| 13 | + local IFS=$'\t' |
| 14 | + git remote -v | while read name path; do remotes["$name"]=("${path%(*)}"); done |
| 15 | +} |
| 16 | + |
| 17 | +update_remote |
| 18 | + |
| 19 | +github_origin(){ |
| 20 | + echo "${remote[github]:-${remote[origin]}}" |
| 21 | +} |
| 22 | + |
| 23 | +github_wikidir(){ |
| 24 | + echo "${1%%.git}.wiki" |
| 25 | +} |
| 26 | + |
| 27 | +if [ ! -d "$ghwiki" ]; then |
| 28 | + [ -e "$ghwiki" ] && die "$ghwiki/ Wrong type.." |
| 29 | + git submodule add "$(github_wikidir "$(github_origin)")" || die WTF |
| 30 | +fi |
| 31 | + |
| 32 | +if [ ! -d "$docdir" ]; then |
| 33 | + [ -e "$docdir" ] && die "$docdir/ Wrong type.." |
| 34 | + mkdir -p "$docdir" |
| 35 | +fi |
| 36 | + |
| 37 | +get_mark(){ |
| 38 | + local k="$(tail -n 1 "$1")" || return $? |
| 39 | + [[ "$k" != \<aosc-git-wiki ]] || return 2 |
| 40 | + k="${k/#<aosc-git-wiki }" |
| 41 | + echo "${k%/>*}" |
| 42 | +} |
| 43 | + |
| 44 | +collapse(){ |
| 45 | + cd "$docdir" |
| 46 | + local IFS=$'\n' |
| 47 | + # TODO: pandoc convertion: We write in pandoc md and convert to GH md. |
| 48 | + to_github $(find . -name '*.md') |
| 49 | + cd - |
| 50 | +} |
| 51 | + |
| 52 | +to_github(){ |
| 53 | + local i temp IFS=$' \t\n' temp2 |
| 54 | + for i; do |
| 55 | + title='' |
| 56 | + local "$(get_mark "$i")" |
| 57 | + if [ -n "$title" ]; then |
| 58 | + # Guess it |
| 59 | + head "$i" | while read temp; do case "$temp" in |
| 60 | + (\#*) title="${temp/\#?( )}"; break;; |
| 61 | + ([A-Za-z]*) title="$temp" temp2='wait-hr';; |
| 62 | + (====*) [[ "$temp2" == wait-hr ]] && break;; |
| 63 | + esac |
| 64 | + done |
| 65 | + fi |
| 66 | + # I believe that I should look at the Filename then. |
| 67 | + [ "$title" ] || ! echo "Failed to eat $i since we don't know the title.">&2 || continue |
| 68 | + # And we should do a mv here. |
| 69 | + # And echo >> when there is not such tag or some info is missing. |
| 70 | + done |
| 71 | +} |
| 72 | + |
| 73 | +from_github(){ |
| 74 | + # The reverse |
| 75 | +} |
0 commit comments