mirror of
https://github.com/ivuorinen/dotfiles.git
synced 2026-01-26 11:14:08 +00:00
Feat: Pure Bash Bible and TLDR in cheat
This commit is contained in:
54
scripts/install-cheat-purebashbible.sh
Executable file
54
scripts/install-cheat-purebashbible.sh
Executable file
@@ -0,0 +1,54 @@
|
||||
#!/usr/bin/env bash
|
||||
# shellcheck disable=SC2231,SC2034,SC2181,SC2068
|
||||
# shellcheck source=shared.sh
|
||||
source "$HOME/.dotfiles/scripts/shared.sh"
|
||||
|
||||
PBB_REQUIRED_TOOLS=(basename git mkdir cheat ls grep head awk cp echo rm)
|
||||
for t in ${PBB_REQUIRED_TOOLS[@]}; do
|
||||
! have "$t" && echo "(!) $t is missing, can't continue..." && exit 1
|
||||
done
|
||||
|
||||
PBB_GIT="https://github.com/dylanaraps/pure-bash-bible.git"
|
||||
PBB_SOURCE="source: $PBB_GIT"
|
||||
PBB_SYNTAX="syntax: bash"
|
||||
PBB_TAGS="tags: [bash]"
|
||||
|
||||
PBB_TEMP_PREFIX=$(basename "$0")
|
||||
PBB_TEMP_DIR="/tmp/pbb-$(rnd)"
|
||||
|
||||
# If there's no .git, clone the folder
|
||||
if [ ! -d "$PBB_TEMP_DIR/.git" ]; then
|
||||
git clone "$PBB_GIT" "$PBB_TEMP_DIR"
|
||||
fi
|
||||
|
||||
PBB_CHAPTERS=$(ls -1v "$PBB_TEMP_DIR"/manuscript/chapter*)
|
||||
CHEAT_DEST="$(cheat -d | grep pure-bash-bible | head -1 | awk '{print $2}')"
|
||||
|
||||
if [ ! -d "$CHEAT_DEST" ]; then
|
||||
mkdir -p "$CHEAT_DEST"
|
||||
fi
|
||||
|
||||
for f in ${PBB_CHAPTERS[@]}; do
|
||||
# get all headers, take the first one, strip the # and return the first word in lowercase
|
||||
HEADER=$(grep -e '^[#] ' "$f" | head -1 | awk '{print tolower($2)}')
|
||||
CHEAT_FILE="$CHEAT_DEST/${HEADER}"
|
||||
echo "(*) $CHEAT_FILE"
|
||||
|
||||
if [ ! -f "$CHEAT_FILE" ]; then
|
||||
cp "$f" "$CHEAT_FILE"
|
||||
fi
|
||||
|
||||
LC_ALL=C perl -pi.bak -e 's/\<\!-- CHAPTER END --\>//' "$CHEAT_FILE"
|
||||
rm "$CHEAT_FILE.bak"
|
||||
|
||||
# add tags if the file doesn't have them
|
||||
if [ '---' != "$(head -1 < "$CHEAT_FILE")" ]; then
|
||||
T="$PBB_SYNTAX\n$PBB_TAGS\n$PBB_SOURCE\n"
|
||||
echo -e "---\n$T---\n$(cat "$CHEAT_FILE")" > "$CHEAT_FILE"
|
||||
fi
|
||||
done
|
||||
|
||||
# Cleanup
|
||||
if [ -d "$PBB_TEMP_DIR" ]; then
|
||||
rm -rf "$PBB_TEMP_DIR"
|
||||
fi
|
||||
73
scripts/install-cheat-tldr.sh
Executable file
73
scripts/install-cheat-tldr.sh
Executable file
@@ -0,0 +1,73 @@
|
||||
#!/usr/bin/env bash
|
||||
# shellcheck disable=SC2231,SC2034,SC2181,SC2068
|
||||
# shellcheck source=shared.sh
|
||||
source "$HOME/.dotfiles/scripts/shared.sh"
|
||||
|
||||
TLDR_REQUIRED_TOOLS=(basename git mkdir cheat ls grep head awk cp echo rm)
|
||||
for t in ${TLDR_REQUIRED_TOOLS[@]}; do
|
||||
! have "$t" && echo "(!) $t is missing, can't continue..." && exit 1
|
||||
done
|
||||
|
||||
TLDR_GIT="https://github.com/tldr-pages/tldr.git"
|
||||
TLDR_SOURCE="source: $TLDR_GIT"
|
||||
TLDR_SYNTAX="syntax: markdown"
|
||||
TLDR_TAGS="tags: [bash]"
|
||||
|
||||
TLDR_TEMP_PREFIX=$(basename "$0")
|
||||
TLDR_TEMP_DIR="/tmp/cheat-tldr-$(rnd)"
|
||||
|
||||
# If there's no .git, clone the folder
|
||||
if [ ! -d "$TLDR_TEMP_DIR/.git" ]; then
|
||||
git clone "$TLDR_GIT" "$TLDR_TEMP_DIR"
|
||||
fi
|
||||
|
||||
# Fetch the destination directory from cheat defined directories.
|
||||
TLDR_CHEAT_DEST="$(cheat -d | grep tldr | head -1 | awk '{print $2}')"
|
||||
|
||||
[ "$TLDR_CHEAT_DEST" = "" ] && msg_err "cheat doesn't know about the destination" && exit 1
|
||||
|
||||
if [ ! -d "$TLDR_CHEAT_DEST" ]; then
|
||||
mkdir -p "$TLDR_CHEAT_DEST"
|
||||
fi
|
||||
|
||||
for d in "$TLDR_TEMP_DIR"/pages/*; do
|
||||
DIRNAME=$(basename "$d")
|
||||
# echo "-> $DIRNAME ($d)"
|
||||
|
||||
case "$DIRNAME" in
|
||||
*common)
|
||||
SECTION_DIR="${TLDR_CHEAT_DEST}"
|
||||
TLDR_TAGS="tags: [common]"
|
||||
;;
|
||||
*)
|
||||
SECTION_DIR="${TLDR_CHEAT_DEST}/$DIRNAME"
|
||||
TLDR_TAGS="tags: [$DIRNAME]"
|
||||
;;
|
||||
esac
|
||||
|
||||
if [ ! -d "$SECTION_DIR" ]; then
|
||||
mkdir -p "$SECTION_DIR"
|
||||
fi
|
||||
|
||||
for FILE in $d/*.md; do
|
||||
BASENAME=$(basename "$FILE" .md)
|
||||
FILENAME="${BASENAME%%.*}"
|
||||
# echo "-> $FILE = $FILENAME"
|
||||
TLDR_FILE="$SECTION_DIR/${FILENAME}"
|
||||
# echo "-> dest: $TLDR_FILE"
|
||||
|
||||
if [ ! -f "$TLDR_FILE" ]; then
|
||||
cp "$FILE" "$TLDR_FILE"
|
||||
fi
|
||||
|
||||
if [ -f "$TLDR_FILE" ] && [ '---' != "$(head -1 < "$TLDR_FILE")" ]; then
|
||||
echo -e "---\n$TLDR_SYNTAX\n$TLDR_TAGS\n$TLDR_SOURCE\n---\n$(cat "$TLDR_FILE")" > "$TLDR_FILE"
|
||||
fi
|
||||
|
||||
done
|
||||
done
|
||||
|
||||
# Cleanup
|
||||
if [ -d "$TLDR_TEMP_DIR" ]; then
|
||||
rm -rf "$TLDR_TEMP_DIR"
|
||||
fi
|
||||
@@ -224,3 +224,9 @@ function path_prepend
|
||||
path_remove "$1"
|
||||
PATH="$1${PATH:+":$PATH"}"
|
||||
}
|
||||
|
||||
# Creates a random string
|
||||
rnd()
|
||||
{
|
||||
echo $RANDOM | md5sum | head -c 20
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user