builder/syntax

50 lines
897 B
Bash
Executable File

#!/usr/bin/env bash
_highlight() {
declare -n syn=$1
if [[ "$syn" ]]; then
exec highlight --force --inline-css -f -I -O xhtml -S "$syn" 2>/dev/null
else
# echo " :cgit: No syntax type provided."
exec cat
fi
}
file_name=$1
file_extension="${file_name##*.}"
if ! [[ "$file_name" == "$file_extension" ]]; then
case "$file_extension" in
(md) exec cmark;;
(bash|zsh) syntax='sh';;
(*) syntax=$file_extension;;
esac
fi
case "$file_name" in
(Makefile) syntax='makefile';;
(PKGBUILD) syntax='sh';;
esac
# Read and output the first line
read -r
# Set syntax if the first line is a shebang
if [[ "$REPLY" =~ ^'#!' ]]; then
case "$REPLY" in
(*sh) syntax='sh';;
(*ruby*) syntax='ruby';;
esac
fi
{
# Print the first line
printf '%s\n' "$REPLY"
# Read and output everything
while read -r; do
printf '%s\n' "$REPLY"
done
} | _highlight syntax