forked from chenkaie/Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvim2html.bash
executable file
·149 lines (113 loc) · 3.45 KB
/
vim2html.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/bin/bash
# Filename: vim2html.bash
# Description: Convert source code to html using vim
# Maintainer: Jeremy Cantrell <[email protected]>
# Last Modified: Sun 2009-03-08 00:57:35 (-0500)
# REQUIREMENTS: bash-utils
# Vim is really good at recognizing filetypes and highlighting the syntax in a
# myriad of colorschemes. Vim is also good at turning that highlighted code
# into HTML. The only missing piece was making this process parameter driven
# and scriptable. That's what this script is intended to solve.
# FUNCTIONS {{{1
cleanup() #{{{2
{
rm -f "$TMP_FILE"
}
usage_exit() #{{{2
{
cat >&2 <<-EOF
Usage: $SCRIPT_NAME [options] input_file [output_file]
Creates HTML exports of source code via vim.
-h Display this help message.
-i Interactive. Prompt for certain actions.
-v Be verbose.
-f FILETYPE Use FILETYPE as vim filetype.
-n Output line numbers.
-t Use tidy to cleanup HTML.
-c COLORSCHEME Use COLORSCHEME as vim colorscheme.
-l Use light background.
-L List available vim colorschemes.
EOF
exit ${1:-0}
}
error_exit() #{{{2
{
msg-error "$1"
exit ${2:-1}
}
colorscheme_list() #{{{2
{
(
find "$VIM_HOME/colors" -name "*.vim" 2>/dev/null
find "$HOME/.vim/colors" -name "*.vim" 2>/dev/null
) | awk -F'/' '{print $NF}' | sed 's/\.vim$//' | sort | uniq
}
# VARIABLES {{{1
SCRIPT_NAME=$(basename "$0" .bash)
VIM_CMD="vim -n -u NONE"
VIM_HOME=/usr/share/vim/vimcurrent
COLORSCHEME=inkpot
BACKGROUND=dark
TMP_FILE=$(mktemp)
# COMMAND-LINE OPTIONS {{{1
while getopts ":hifvqf:ntc:lL" options; do
case $options in
i) export INTERACTIVE=1 ;;
f) export INTERACTIVE=0 ;;
v) export VERBOSE=1 ;;
q) export VERBOSE=0 ;;
f) FILETYPE=$OPTARG ;;
n) LINE_NUMBERS=1 ;;
t) TIDY=1 ;;
c) COLORSCHEME=$OPTARG ;;
l) BACKGROUND=light ;;
L) colorscheme_list; exit 0 ;;
h) usage_exit 0 ;;
*) usage_exit 1 ;;
esac
done && shift $(($OPTIND - 1))
trap cleanup INT TERM EXIT
# ERROR-CHECKING {{{1
[[ -d $VIM_HOME ]] ||
VIM_HOME="$(find /usr/share/vim -mindepth 1 -maxdepth 1 \
-type d -name "vim*" 2>/dev/null | sort -r | head -n1)"
[[ "$VIM_HOME" ]] || error_exit "Could not find Vim home directory"
INFILE=$1
[[ $INFILE ]] || error_exit "Input file not provided"
[[ -f $INFILE ]] || error_exit "Input file does not exist or is not a regular file"
OUTFILE=$2
[[ $OUTFILE ]] || OUTFILE=${INFILE##*/}.html
if interactive && [[ -f $OUTFILE ]]; then
question "Overwrite '$OUTFILE'?" || exit 1
fi
# BUILD CONVERSION SCRIPT {{{1
: >$TMP_FILE
cat >>$TMP_FILE<<-EOF
set nocompatible
syntax on
set t_Co=256
set expandtabs
set tabstop=4
set background=$BACKGROUND
let html_ignore_folding = 1
let html_use_css = 0
EOF
(
[[ $FILETYPE ]] && echo "set ft=$FILETYPE"
[[ $LINE_NUMBERS ]] && echo "set nu"
[[ $COLORSCHEME ]] && echo "colorscheme $COLORSCHEME"
) >>$TMP_FILE
cat >>$TMP_FILE<<-EOF
runtime! syntax/2html.vim
w! $OUTFILE
q!
q!
EOF
#}}}
verbose && msg-info "Converting file '$INFILE' to '$OUTFILE'"
$VIM_CMD -S "$TMP_FILE" "$INFILE" >/dev/null 2>&1
sed -i "s%<title>.*</title>%<title>$(basename "$INFILE")</title>%" "$OUTFILE"
if [[ $TIDY ]]; then
verbose && msg-info "Cleaning HTML file '$OUTFILE'"
tidy --tidy-mark no -utf8 -f /dev/null -clean -asxhtml -o "$OUTFILE" "$OUTFILE"
fi