Skip to content

Instantly share code, notes, and snippets.

@cemkeylan
Last active June 13, 2022 22:18
Show Gist options
  • Save cemkeylan/4d8a03d65b7744365aaef88f7877cd22 to your computer and use it in GitHub Desktop.
Save cemkeylan/4d8a03d65b7744365aaef88f7877cd22 to your computer and use it in GitHub Desktop.
#!/bin/sh
# a static html index generator
#
# This will output a single line index page to stdout.
# If printf and [] is a built-in (it usually is), this
# script doesn't make use of an external process.
#
# If you want to generate indexes for a full site
# directory, you can do the following
#
# ROOT=$PWD
# find . -type d | while -read -r dir; do (
# cd $dir
# lsindex $ROOT > index.html
# ) done
#
#
# Why I use this?
# ---------------
#
# I have an nginx server that I don't fully control, and
# it doesn't generate index pages where index.html doesn't
# exist. So, this script basically does it for me.
#
# This script is in the public domain.
# If root directory is not set, make it the current directory.
# Print usage output if '--help' is given. If the argument is
# not a full patch cd into the directory and retrieve the full
# path.
case "$1" in
'') set -- "$PWD" ;;
--help|-h)
printf '%s\n' "usage: ${0##*/} [rootdir]" "" \
"Create an index of the current directory" >&2
exit 0 ;;
/*) ;;
*) set -- "$(cd "$1" || exit 1; printf "$PWD")"
esac
# Get the relative path of the current directory to the site root
curdir="${PWD#$1}" title="index of ${curdir:=/}"
# Unless we are on the root directory, add a parent directory to
# the listing.
[ "$curdir" = / ] || { parentdir=${curdir%/*}; : "${parentdir:=/}" ;}
# Use the current directory name for the page title and header
printf "<html><head><title>$title</title></head><body><h1>$title</h1><ul>"
# If there is a parent directory, print it to the top of the list.
[ "$parentdir" ] && printf "<li><a href=$parentdir>Parent Directory</a></li>"
# Skip index.html for the directory list and append a '/' to the name
# if that's a directory instead of a regular file.
for file in *; do
[ "$file" = index.html ] && continue
[ -d "$file" ] && file="$file/"
printf '<li><a href="%s">%s</a></li>' "$file" "$file"
done
# End the file by closing open tags and print a newline character.
printf '%s\n' "</ul></body></html>"
#!/bin/sh
# a static html index generator
#
# This will output a single line index page to stdout.
# If printf and [ ] is a built-in (it usually is),
# this script doesn't make use of an external process.
#
# If you want to generate indexes for a full site
# directory, you can do the following
#
# ROOT=$PWD
# find . -type d | while -read -r dir; do (
# cd $dir
# lsindex $ROOT > index.html
# ) done
#
#
# Why I use this?
# ---------------
#
# I have an nginx server that I don't fully control,
# and it doesn't generate index pages where index.html
# doesn't exist. So, this script basically does it for
# me.
#
# This script is in the public domain.
# If root directory is not set, make it the current directory.
# Print usage output if '--help' is given.
# If the argument is not a full patch cd into the directory and
# retrieve the full path.
case "$1" in
'') set -- "$PWD" ;;
--help|-h) printf 'usage: %s [rootdir]\n\nCreate an instance of the current directory\n' "${0##*/}" >&2; exit 0 ;;
/*) ;;
*) set -- "$(cd "$1" || exit 1; printf "$PWD")"
esac
# Get the relative path of the current directory to the site root
curdir="${PWD#$1}" title="index of ${curdir:=/}"
# Unless we are on the root directory, add a parent directory to
# the listing.
[ "$curdir" = / ] || { parentdir=${curdir%/*}; : "${parentdir:=/}" ;}
printf '%s' "<html><head><title>$title</title></head><body><h1>$title</h1><ul>"
[ "$parentdir" ] && printf '<li><a href="%s">Parent Directory</a></li>' "$parentdir"
for file in *; do
[ -d "$file" ] && file="$file/"
printf '<li><a href="%s">%s</a></li>' "$file" "$file"
done
printf '%s\n' "</ul></body></html>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment