org-mode/contrib/scripts/dir2org.zsh

57 lines
1013 B
Bash
Raw Normal View History

2008-02-08 11:12:28 +00:00
#!/usr/bin/env zsh
# desc:
#
# Output an org compatible structure representing the filesystem from
# the point passed on the command line (or . by default).
#
# options:
# none
#
# usage:
2008-03-13 15:34:03 +00:00
# dir2org.zsh [DIR]...
2008-02-08 11:12:28 +00:00
#
# author:
# Phil Jackson (phil@shellarchive.co.uk)
set -e
function headline {
local depth="${1}"
local text="${2}"
printf "%${depth}s %s" "" | tr ' ' '*'
echo " ${text}"
}
function scan_and_populate {
local depth="${1}"
local dir="${2}"
headline ${depth} "${dir}"
2008-02-08 15:05:52 +00:00
# if there is no files in dir then just move on
[[ $(ls "${dir}" | wc -l) -eq 0 ]] && return
2008-02-08 11:12:28 +00:00
(( depth += 1 ))
2008-02-08 15:05:52 +00:00
for f in $(ls -d "${dir}"/*); do
2008-02-08 11:12:28 +00:00
if [ -d "${f}" ]; then
scan_and_populate ${depth} "${f}"
else
headline ${depth} "[[file://${f}][${${f##*/}%.*}]]"
fi
done
2008-02-08 15:05:52 +00:00
(( depth -= 1 ))
2008-02-08 11:12:28 +00:00
}
function main {
local scan_dir="${1:-$(pwd)}"
local depth=0
scan_and_populate ${depth} "${scan_dir}"
}
main "${@}"