Skip to content

Instantly share code, notes, and snippets.

@augustohp
Last active March 8, 2016 04:28
Show Gist options
  • Save augustohp/f5ab3f14541a86249c9a to your computer and use it in GitHub Desktop.
Save augustohp/f5ab3f14541a86249c9a to your computer and use it in GitHub Desktop.
Wrapper to `highlight` tool, with sensible defaults and ease to use on directories
#!/usr/bin/env bash
#
# vim: et ts=4 sw=4 ft=sh:
declare -r SCRIPT_NAME=$0
declare -r THEME_LIGHT="Seashell"
declare -r THEME_DARK="Zenburn"
declare -r TAB_SPACES=4
declare -r LINE_NUMBER_COLUMN_LENGTH=4
declare -r OUTPUT_ENCODING=UTF-8
declare -r OUTPUT_FORMAT=rtf
VERBOSE_IS_ON=false
THEME=$THEME_DARK
SHOW_LINES_ON_OUTPUT=true
OUTPUT_DIRECTORY=
function exit_with_error
{
echo $1
exit 2
}
function debug_message
{
[[ $VERBOSE_IS_ON ]] || exit 0;
echo $1 >&2
}
function outout_line_numbers_option()
{
[[ $SHOW_LINES_ON_OUTPUT ]] && echo --line-numbers --line-number-length=$LINE_NUMBER_COLUMN_LENGTH
}
function highligh_using_config
{
declare -r source=$1
declare -r destiny=$2
[[ -z "$source" ]] && exit_with_error 'A "source" file is required to highlight.'
[[ -z "$destiny" ]] && exit_with_error 'A "destiny" file is required to put highlighted code into.'
debug_message "Highlighting $source -> $destiny"
highlight --out-format $OUTPUT_FORMAT -u $OUTPUT_ENCODING -i "$source" -o "$destiny" --style $THEME --replace-tabs $TAB_SPACES $(outout_line_numbers_option)
}
function create_destination_file()
{
declare -r source=$1
declare -r desintation="${source}.rtf"
[[ ! -f $source ]] && exit_with_error 'Source is invalid'
[[ -f $desintation ]] && rm $desintation
echo $desintation
}
function fetch_list_of_files_to_highlight
{
declare -r source=$1
[[ -z "$source" ]] && exit_with_error 'A "source" is required to fetch files to highlight.'
debug_message "Identifying source: $source"
if [ -d $source ]; then
debug_message "Source is a directory"
ls -d $source/*
else
debug_message "Source is a single file"
echo $source
fi
}
function display_usage_information
{
cat <<- _EOT_
Usage: $SCRIPT_NAME -hv [-o <output directory>] <source>
Uses 'highlight' to convert a code file into a RTF file, which
can be used in presentations, for example. This is just a crude
wrapper to the 'highlight' binary making easier things like:
- Highlighting an entire directory
- Great default configurations, no more looking for help every time
you need to create those highlights again.
- Switch between dark and light themes.
Options:
-v Verbose mode, display script execution information.
-h This help message.
-d Uses the dark theme (this is default).
-l Uses the light theme.
-n Do not show line numbers in output, defaults to show
them using 4 columns, starting at 0 (zero).
-o dir Output RTF files into this directory instead of using
the same directory of the sources.
_EOT_
exit 1;
}
function main
{
declare -r from_source=$1
[[ -z "$from_source" ]] && display_usage_information
for from_file in $(fetch_list_of_files_to_highlight $from_source); do
to_file=$(create_destination_file $from_file)
highligh_using_config $from_file $to_file
done
exit 0;
}
while getopts "vhldno:" OPTION; do
case $OPTION in
v)
VERBOSE_IS_ON=true
;;
h)
display_usage_information
;;
o)
exit_with_error 'Custom output diretctory option not implemented.'
[[ -z "$OPTARG" ]] && exit_with_error 'An output directory is required for option "-o <output directory>".'
[[ ! -d "$OPTARG" ]] && exit_with_error 'Output directory being used is not a directory.'
OUTPUT_DIRECTORY=$OPTARG
;;
d)
THEME=$THEME_DARK
;;
l)
THEME=$THEME_LIGHT
;;
n)
SHOW_LINES_ON_OUTPUT=false
;;
esac
done
# Sets the action as the last parameter avaiable (Dirty hack)
for INPUT; do true; done
main $INPUT
exit 0;
@augustohp
Copy link
Author

Note to self: I love the fact the I wrote this less than the fact that I wrote that help message. 😍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment