#!/usr/bin/env bash

# $Id: vim2html,v 1.46 2005/03/07 00:03:51 chipster Exp $

#############################################################################
# vim2html                                                                  #
#   a utility to convert a file into a virtual Vim file in HTML format.     #
# REQUIRED: Vim with 2html syntax file.                                     #
# OPTIONAL: HTMLtidy                                                        #
#                                                                           #
# Copyright (C) 2004 Chip Cuccio <http://norlug.org/~chipster/vim2html>     #
# NOTE: Don't forget to edit/check user settings below!!!                   #
# ------------------------------------------------------------------------- #
# This program is GPL'd Free Software.  Please see the COPYING file!        #
#############################################################################

#### begin user settings ####

VIM_HOME="`find /opt/local/share/vim /Applications/MacPorts/Vim/Vim.app/Contents/Resources/vim/ /usr/share/vim -type d -mindepth 1 -maxdepth 1|sort|tail -1`"
    # normally, no need to change this

SYNTAX="$VIM_HOME/syntax/2html.vim"
    # full path for '2html.vim' syntax file

COLORSCHEME='ron'
    # default color scheme to use

####  end user settings ####

#
# Set global vars.
#
TIDY=
FILE_TYPE=
PROGRAM_NAME="$(basename ${0})"
VIM_CMD="vim -n +f"
AUTHOR="Chip Cuccio"
COPYRIGHT="2003-2004"
CONTACT="http:\/\/norlug.org\/~chipster\/finger"
URL="http:\/\/norlug.org\/~chipster\/vim2html"
USAGE="Usage: ${PROGRAM_NAME} [OPTIONS]... file"
HELP="Try '${PROGRAM_NAME} --help' for more information."
TIDYLOC=`which tidy`
VIM2HTMLVERSION='$Revision: 1.46 $- $Date: 2005/03/07 00:03:51 $'
SYNTAX_ERROR="Cannot locate Vim syntax file (\"2html.vim\").
Please chack and adjust the \"VIM_HOME\" and \"SYNTAX\" variables
within the ${PROGRAM_NAME} program."
DESCRIPTION="Converts Vim files (with colors) to HTML - with optional tidying
(tidying requires HTMLtidy to be present). Output file will be
original filename (including path) with \".html\" appended to it.

Options:
  -t, --tidy                 Tidy up HTML/Apply CSS and XHTML (when possible)
  -l, --list-schemes         List available color schemes
  -s, --color-scheme SCHEME  Use specified color SCHEME
  -n, --line-numbers         Display line-numbers in output file
  -f, --file-type FILETYPE   Force Vim to use file type FILETYPE
  -h, --help                 Print this message
  -v, --version              Print ${PROGRAM_NAME} version information"
NOTIDYMSG="Cannot find the HTMLtidy binary! You have 2 options:
1) Install HTMLtidy!
2) Re-run ${PROGRAM_NAME} without the \"-t\" option."

#
# Make certain '2html.vim' syntax file is found
#
if [ ! -r "$SYNTAX" ]
then
    echo
    echo "${SYNTAX_ERROR}"
    echo
    exit 1
fi

#
# get opts.
#
while test "${1}"; do
    case "${1}" in
        --help | -h)
            echo "${USAGE}"
            echo
            echo "${DESCRIPTION}"
            exit
            ;;
        -f | --file-type)
            shift
            FILE_TYPE=$1
            ;;
        -n | --line-numbers)
            LINE_NUMBERS=true
            ;;
        -t | --tidy)
            TIDY=true
            ;;
        -s | --color-scheme)
        shift
            COLORSCHEME=$1
            ;;
        -l | --list-schemes)
	    echo "available color schemes:"
	    echo
	    ( cd "$VIM_HOME/colors/" && ls -1d *.vim | sed 's/.vim$//' )
	    exit
            ;;
	--version | -v)
	    echo
	    echo "${PROGRAM_NAME} - by ${AUTHOR} <${CONTACT}>" | sed 's/\\//g'
	    echo ${VIM2HTMLVERSION} | sed 's/\$//g' | sed 's/Revision/Version/g'
        echo "<${URL}>" | sed 's/\\//g'
        echo
	    exit
	    ;;
        --)
            shift
            break
            ;;
        -*|--*)
            echo "Unrecognized option ${1}" >&2
            echo "${HELP}" >&2
            exit 1
            ;;
        *)
            break
            ;;
    esac
    shift
done

#
# get file opts.
# 
FILE="${1}"

#
# strip path and slashes from FILE
#
FILE_NAME=`echo $FILE | tr '/' '\n' | tail -1`

#
# sanity checks
#
if [ -z "${FILE}" ]; then
    echo "${USAGE}"
    echo
    echo "${PROGRAM_NAME}: You must specify a file!"
    echo "${HELP}"
    exit 1
elif [ ! -e "${FILE}" ]; then
    echo "${FILE} does not exist!"
    exit 1
elif [ ! -f "${FILE}" ]; then
    echo "${FILE} is not a regular file!"
    exit 1
elif [ -f "${FILE}.html" ]; then
    echo "${FILE}.html already exists! Do you wish to overwrite it? (y/N)"
    read OK
    if [ " $OK" = " y" -o " $OK" = " Y" ]; then
        rm -rf "${FILE}.html"
    else
        echo "Aborted! - File not overwritten."
        exit 1
    fi
fi

#
# HTMLtidy chack
#
if [ "X${TIDY}" = "Xtrue" ]; then
    if [ ! -e "${TIDYLOC}" ]; then
        echo
        echo "$NOTIDYMSG"
        echo
        exit 1
    fi
fi

#
# cheesy status msg.
#
echo ""
echo "Converting file \"${FILE}\""
echo ""

#
# Here's the meat ;-)
#
# Line numbers option selected
if [ -n "$LINE_NUMBERS" ]; then
    # Forced file-type
    if [ -n "$FILE_TYPE" ]; then
        $VIM_CMD +"set ft=$FILE_TYPE" +"set nu" +"syntax on" \
          +"colorscheme $COLORSCHEME" +"TOhtml" +"wq" +"q" "$FILE" \
          >/dev/null 2>/dev/null
    else
        # Auto file-type
        $VIM_CMD +"syntax on" +"set nu" +"colorscheme $COLORSCHEME" \
          +"TOhtml" +"wq" +"q" "$FILE" >/dev/null 2>/dev/null
    fi
# No line numbers
else
    # Forced file-type
    if [ -n "$FILE_TYPE" ]; then
        $VIM_CMD +"set ft=$FILE_TYPE" +"syntax on" +"colorscheme $COLORSCHEME" \
          +"TOhtml" +"wq" +"q" "$FILE" >/dev/null 2>/dev/null
    else
        # Auto file-type
        $VIM_CMD +"syntax on" +"colorscheme $COLORSCHEME" \
          +"TOhtml" +"wq" +"q" "$FILE" >/dev/null 2>/dev/null
    fi
fi

#
# Fix odd '2html.vim' title
#
sed "s/<title>.*<\/title>/<title>$PROGRAM_NAME output of \"$FILE_NAME\"<\/title>/g" \
    <$FILE.html > $FILE.new
    mv $FILE.new $FILE.html

#
# Generator footer message
#
sed "s/<\/body>/<hr \/>\\
<address>Generated with \\
<tt><strong>\\
<a href=\"$URL\">$PROGRAM_NAME<\/a>\\
<\/strong><\/tt><br \/>\\
Copyright \&copy\; $COPYRIGHT by $AUTHOR \\
<a href=\"$CONTACT\">\\
<tt>\&lt\;$CONTACT\&gt\;<\/tt><\/a><\/address>\\
<\/body>/g" \
<$FILE.html > $FILE.new
mv $FILE.new $FILE.html

#
# Generator meta-tag
#
sed "s/content=\"Vim.*/content=\"$PROGRAM_NAME ($URL)\" \/>/g" \
<$FILE.html > $FILE.new
mv $FILE.new $FILE.html

#
# Tidy it up!
#
# Tidy option invoked
if [ "X${TIDY}" = "Xtrue" ]; then
    tidy --tidy-mark no -f /dev/null -clean -asxhtml -m "$FILE.html"
    sed "s/<body>/<body>\\
    <h2><tt>$PROGRAM_NAME<\/tt> output of \&\#8220\;$FILE\&\#8221\;<\/h2>\\
    <hr \/>/g" \
    <$FILE.html > $FILE.new
    mv $FILE.new $FILE.html
fi

#
# YACSM (Yet another cheesy status message ;-)
#
echo
echo "All done!"
echo "File output is \"${FILE}.html\""
echo

exit 0

#EOF

