Skip to content

Instantly share code, notes, and snippets.

@debo
Last active January 30, 2019 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save debo/09b91d4a7564cd0a6585 to your computer and use it in GitHub Desktop.
Save debo/09b91d4a7564cd0a6585 to your computer and use it in GitHub Desktop.
#!/usr/bin/env zsh
#
# Wikify by Mario "Kuroir" Ricalde
#
function wikify() {
# gnu ls has natural number sorting, useful for sorting 1, 10, 2 into 1, 2, 10
function gls_or_ls() {
if hash gls 2>/dev/null; then
gls -v "$@"
else
ls "$@"
fi
}
# Filter some files and directories.
function continue_if_filtered() {
if [[ "$@" =~ "images$" ]]; then
continue
elif [[ "$@" =~ "(_sidebar|_footer|_header)" ]]; then
continue
elif [[ "$@" == "." ]]; then
continue
fi
}
function directory_depth() {
if [[ $initial_depth -ne 0 ]]; then
new_depth=$(echo $@ | grep -o / | wc -l)
# The minus 1, is for the parent subject directory, we don't count it.
echo $(($new_depth - $initial_depth - 1 ))
else
echo $@ | grep -o / | wc -l
fi
}
# 4 Spaces per level, this is the markdown standard for lists.
function calculate_spaces() {
_spaces=""
if [[ $@ -gt 0 ]]; then
i=1
while [[ $i -le $@ ]]; do
_spaces="$_spaces "
(( i++ ))
done
fi
echo $_spaces
}
function depth_and_space() {
depth=$(directory_depth $@)
space=$(calculate_spaces $depth)
}
function to_parent_subject() {
echo "$@" | sed -e 's/\/[^\/]*$//'
}
function subject_name() {
echo "$@" | sed -e 's/.*\/\([^\/]*\).md$/\1/'
}
function string_to_name_and_id() {
if [[ "$(file -ib $1)" =~ "directory" ]]; then
1="$1.md"
fi
identifier=$(echo "$1" | sed -e 's/.*\/\([^\/]*\).md$/\1/')
name=$(echo "$identifier" | sed -e 's/^[0-9\.]*\-//' | sed -e 's/\-/ /g')
}
function subject_to_list() {
string_to_name_and_id $1
if [[ ! -s $file ]]; then
flag=" ⚑"
fi
echo "${space}0. [[$name|$identifier]]$flag"
}
function id_to_string() {
echo "${@/-/ }"
}
function subject_to_header() {
string_to_name_and_id $1
echo "\n${space}### [[☰|$identifier]] $(id_to_string $identifier)"
}
function recursive_write() {
if [[ "$reverse_pointer_depth" -eq "0" ]]; then
pointer_file=$(to_parent_subject $@)
else
pointer_file=$@
fi
if [[ "$@" != "$subjects" ]]; then
if [[ "$reverse_pointer_depth" -ne "0" ]]; then
space=$(calculate_spaces $(($reverse_pointer_depth - 1)))
echo "$(subject_to_list $file)" >> "$pointer_file.md"
fi
(( reverse_pointer_depth++ ))
recursive_write $(to_parent_subject $@)
# if current - 1 = home. Then we're at the bottom!
if [[ "$(to_parent_subject $@)" == "$subjects" ]]; then
echo "$(subject_to_list $file)" >> "$@/_sidebar.md"
fi
else
space=$(calculate_spaces $(($reverse_pointer_depth - 2)))
echo "$(subject_to_list $file)" >> "$subjects/Home.md"
reverse_pointer_depth=0
fi
}
function parse_subject() {
depth_and_space $@
echo "${space}Directory: $@"
# Directory write
if [[ "$depth" -eq "0" ]]; then
echo "$(subject_to_header $@.md)" >> "$subjects/Home.md"
echo "$(subject_to_header $@.md)\n---" > "$@/_sidebar.md"
echo "$@.md" >> /tmp/wikify.txt
echo "$@/_sidebar.md" >> /tmp/wikify.txt
fi
for file in $(gls_or_ls -l -v $@ | awk '{ print $9 }'); do;
continue_if_filtered $file
file="$@/$file"
if [[ "$(file -ib $file)" =~ "directory" ]]; then
echo "$file.md" >> /tmp/wikify.txt
recursive_write $file
parse_subject $file
else
depth_and_space $file
echo "${space}File: $file"
echo "$file" >> /tmp/wikify.txt
recursive_write $file
fi
done
}
LANG=C
subjects=$PWD
initial_depth=0
initial_depth=$(directory_depth $subjects)
reverse_pointer_depth=0
rm -f /tmp/wikify.txt; touch /tmp/wikify.txt
# Empty Home.md
home_file="$subjects/Home.md"
rm $home_file
# Cleanup Previous Generated Files.
for subject in $(find . -type d ! \( -path './.*' -o -path '.' \)); do;
rm "$subject.md"
done
# Begin doing the magic
for subject in $(gls_or_ls -l -d $PWD/* | egrep '^d' | awk '{ print $9}'); do;
continue_if_filtered $subject
parse_subject $subject
done
echo "$subjects/Home.md" >> /tmp/wikify.txt
while read line
do
[ -f $line ] && git add $line
done < /tmp/wikify.txt
echo "**Important**: The Tables of Content are generated. Any change will be overridden on the next update.<br>For more information: [GitHub Wikifier](https://github.com/hybridgroup/GitHub-Wikifier)" > "$subjects/_footer.md"
subjects="$PWD." # hack to restore ps1
}
wikify .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment