Skip to content

Instantly share code, notes, and snippets.

@protist
Created March 9, 2015 10:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save protist/820bb6a25722083a2edd to your computer and use it in GitHub Desktop.
Save protist/820bb6a25722083a2edd to your computer and use it in GitHub Desktop.
Create a linkified vimwiki tree of the specified directory and subdirectories
#!/usr/bin/env bash
# Create a linkified vimwiki tree of the specified directory and subdirectories.
# It takes one argument: the base directory of the vimwiki.
# For the links to make sense, place the output of this script into a file located in this base directory.
# Print header
echo '= Wiki contents ='
echo
cd "$1"
while read -r line; do
# Assuming there are no files with ── in their name.
filename="$(<<<"$line" sed -r 's/.*── (.*)/\1/' )"
treetrunk="$(<<<"$line" sed -r 's/(.*── ).*/\1/')"
# Calculate depth of current file, where 1 is current directory.
depth=$(( $(<<<"$treetrunk" wc -m ) / 4 ))
# Work out path
path_array[$depth]="$filename"
path_formatted="$(printf "/%s" "${path_array[@]:1:$depth}")"
path_formatted="${path_formatted:1}"
# Print out line
echo -n "$treetrunk"
if $(<<<"$filename" grep -q '.wiki$'); then
echo "[[${path_formatted%.wiki}|${filename%.wiki}]]"
else
echo "$filename"
fi
done <<< "$(tree | head -n -2 | tail -n +2)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment