2
#description: format file according to https://wiki.ubuntu.com/Testing/TestCaseFormat
3
#save as test-case-format
4
#Make file executable <code>chmod +x test-case-format
5
#move to /urs/local/bin <code>sudo mv test-case-format /usr/local/bin</code>
6
#In order to use this script, both vim and tidy need to be installed.
7
#usage: test-case-format /path/to/files
12
[ -z "${1}" ] && return 1 || _basename_var_name="${1}"
13
[ -z "${2}" ] || _basename_var_suffix="${2}"
14
case "${_basename_var_name}" in
15
/*|*/*) _basename_var_name="$(expr "${_basename_var_name}" : '.*/\([^/]*\)')" ;;
18
if [ -n "${_basename_var_suffix}" ] && [ "${#_basename_var_name}" -gt "${#2}" ]; then
19
if [ X"$(printf "%s" "${_basename_var_name}" | cut -c"$((${#_basename_var_name} - ${#_basename_var_suffix} + 1))"-"${#_basename_var_name}")" \
20
= X"$(printf "%s" "${_basename_var_suffix}")" ]; then
21
_basename_var_name="$(printf "%s" "${_basename_var_name}" | cut -c1-"$((${#_basename_var_name} - ${#_basename_var_suffix}))")"
25
printf "%s\\n" "${_basename_var_name}"
30
printf "%s\\n" "Usage: $(_basename "${0}") testcase ..." >&2
31
printf "%s\\n" "https://wiki.ubuntu.com/Testing/TestCaseFormat" >&2
37
mkdir -p /tmp/"$(_basename "${0}")"/indent
38
#this file can also be copied to ~/.vim/indent/html.vim so
39
#vim indent html files the testcase way <dl>/<dt>/<dd>
40
cat > /tmp/"$(_basename "${0}")"/indent/html.vim << EOF
41
" Description: html indenter
42
" Author: Johannes Zellner <johannes@zellner.org>
43
" Last Change: Mo, 05 Jun 2006 22:32:41 CEST
44
" Restoring 'cpo' and 'ic' added by Bram 2006 May 5
45
" Globals: g:html_indent_tags -- indenting tags
46
" g:html_indent_strict -- inhibit 'O O' elements
47
" g:html_indent_strict_table -- inhibit 'O -' elements
49
" Only load this indent file when no other was loaded.
50
if exists("b:did_indent")
56
" [-- local settings (must come before aborting the script) --]
57
setlocal indentexpr=HtmlIndentGet(v:lnum)
58
setlocal indentkeys=o,O,*<Return>,<>>,{,}
61
if exists('g:html_indent_tags')
62
unlet g:html_indent_tags
65
" [-- helper function to assemble tag list --]
66
fun! <SID>HtmlIndentPush(tag)
67
if exists('g:html_indent_tags')
68
let g:html_indent_tags = g:html_indent_tags.'\|'.a:tag
70
let g:html_indent_tags = a:tag
75
" [-- <ELEMENT ? - - ...> --]
76
call <SID>HtmlIndentPush('a')
77
call <SID>HtmlIndentPush('abbr')
78
call <SID>HtmlIndentPush('acronym')
79
call <SID>HtmlIndentPush('address')
80
call <SID>HtmlIndentPush('b')
81
call <SID>HtmlIndentPush('bdo')
82
call <SID>HtmlIndentPush('big')
83
call <SID>HtmlIndentPush('blockquote')
84
call <SID>HtmlIndentPush('button')
85
call <SID>HtmlIndentPush('caption')
86
call <SID>HtmlIndentPush('center')
87
call <SID>HtmlIndentPush('cite')
88
call <SID>HtmlIndentPush('code')
89
call <SID>HtmlIndentPush('colgroup')
90
call <SID>HtmlIndentPush('dd')
91
call <SID>HtmlIndentPush('del')
92
call <SID>HtmlIndentPush('dfn')
93
call <SID>HtmlIndentPush('dir')
94
call <SID>HtmlIndentPush('div')
95
call <SID>HtmlIndentPush('dl')
96
call <SID>HtmlIndentPush('dt')
97
call <SID>HtmlIndentPush('em')
98
call <SID>HtmlIndentPush('fieldset')
99
call <SID>HtmlIndentPush('font')
100
call <SID>HtmlIndentPush('form')
101
call <SID>HtmlIndentPush('frameset')
102
call <SID>HtmlIndentPush('h1')
103
call <SID>HtmlIndentPush('h2')
104
call <SID>HtmlIndentPush('h3')
105
call <SID>HtmlIndentPush('h4')
106
call <SID>HtmlIndentPush('h5')
107
call <SID>HtmlIndentPush('h6')
108
call <SID>HtmlIndentPush('i')
109
call <SID>HtmlIndentPush('iframe')
110
call <SID>HtmlIndentPush('ins')
111
call <SID>HtmlIndentPush('kbd')
112
call <SID>HtmlIndentPush('label')
113
call <SID>HtmlIndentPush('legend')
114
call <SID>HtmlIndentPush('map')
115
call <SID>HtmlIndentPush('menu')
116
call <SID>HtmlIndentPush('noframes')
117
call <SID>HtmlIndentPush('noscript')
118
call <SID>HtmlIndentPush('object')
119
call <SID>HtmlIndentPush('ol')
120
call <SID>HtmlIndentPush('optgroup')
121
" call <SID>HtmlIndentPush('pre')
122
call <SID>HtmlIndentPush('q')
123
call <SID>HtmlIndentPush('s')
124
call <SID>HtmlIndentPush('samp')
125
call <SID>HtmlIndentPush('script')
126
call <SID>HtmlIndentPush('select')
127
call <SID>HtmlIndentPush('small')
128
call <SID>HtmlIndentPush('span')
129
call <SID>HtmlIndentPush('strong')
130
call <SID>HtmlIndentPush('style')
131
call <SID>HtmlIndentPush('sub')
132
call <SID>HtmlIndentPush('sup')
133
call <SID>HtmlIndentPush('table')
134
call <SID>HtmlIndentPush('textarea')
135
call <SID>HtmlIndentPush('title')
136
call <SID>HtmlIndentPush('tt')
137
call <SID>HtmlIndentPush('u')
138
call <SID>HtmlIndentPush('ul')
139
call <SID>HtmlIndentPush('var')
142
" [-- <ELEMENT ? O O ...> --]
143
if !exists('g:html_indent_strict')
144
call <SID>HtmlIndentPush('body')
145
call <SID>HtmlIndentPush('head')
146
call <SID>HtmlIndentPush('html')
147
call <SID>HtmlIndentPush('tbody')
151
" [-- <ELEMENT ? O - ...> --]
152
if !exists('g:html_indent_strict_table')
153
call <SID>HtmlIndentPush('th')
154
call <SID>HtmlIndentPush('td')
155
call <SID>HtmlIndentPush('tr')
156
call <SID>HtmlIndentPush('tfoot')
157
call <SID>HtmlIndentPush('thead')
160
delfun <SID>HtmlIndentPush
162
let s:cpo_save = &cpo
165
" [-- count indent-increasing tags of line a:lnum --]
166
fun! <SID>HtmlIndentOpen(lnum, pattern)
167
let s = substitute('x'.getline(a:lnum),
168
\ '.\{-}\(\(<\)\('.a:pattern.'\)\>\)', "\1", 'g')
169
let s = substitute(s, "[^\1].*$", '', '')
173
" [-- count indent-decreasing tags of line a:lnum --]
174
fun! <SID>HtmlIndentClose(lnum, pattern)
175
let s = substitute('x'.getline(a:lnum),
176
\ '.\{-}\(\(<\)/\('.a:pattern.'\)\>>\)', "\1", 'g')
177
let s = substitute(s, "[^\1].*$", '', '')
181
" [-- count indent-increasing '{' of (java|css) line a:lnum --]
182
fun! <SID>HtmlIndentOpenAlt(lnum)
183
return strlen(substitute(getline(a:lnum), '[^{]\+', '', 'g'))
186
" [-- count indent-decreasing '}' of (java|css) line a:lnum --]
187
fun! <SID>HtmlIndentCloseAlt(lnum)
188
return strlen(substitute(getline(a:lnum), '[^}]\+', '', 'g'))
191
" [-- return the sum of indents respecting the syntax of a:lnum --]
192
fun! <SID>HtmlIndentSum(lnum, style)
193
if a:style == match(getline(a:lnum), '^\s*</')
194
if a:style == match(getline(a:lnum), '^\s*</\<\('.g:html_indent_tags.'\)\>')
195
let open = <SID>HtmlIndentOpen(a:lnum, g:html_indent_tags)
196
let close = <SID>HtmlIndentClose(a:lnum, g:html_indent_tags)
197
if 0 != open || 0 != close
203
\ synIDattr(synID(a:lnum, 1, 1), 'name') =~ '\(css\|java\).*' &&
204
\ synIDattr(synID(a:lnum, strlen(getline(a:lnum)), 1), 'name')
205
\ =~ '\(css\|java\).*'
206
if a:style == match(getline(a:lnum), '^\s*}')
207
return <SID>HtmlIndentOpenAlt(a:lnum) - <SID>HtmlIndentCloseAlt(a:lnum)
213
fun! HtmlIndentGet(lnum)
214
" Find a non-empty line above the current line.
215
let lnum = prevnonblank(a:lnum - 1)
217
" Hit the start of the file, use zero indent.
222
" [-- special handling for <dd> --]
223
if getline(a:lnum-1) =~ '\c</dt>'
224
if getline(a:lnum) =~ '\c<dd>'
225
return indent(lnum) + (&sw)
229
" [-- special handling for <dd>/<dt>: --]
230
if getline(a:lnum-1) =~ '\c</dd>'
231
if getline(a:lnum) =~ '\c</dd>'
234
if getline(a:lnum) =~ '\c<dt>'
235
return indent(lnum) - (&sw)
241
setlocal ic " ignore case
243
" [-- special handling for <pre>: no indenting --]
244
if getline(a:lnum) =~ '\c</pre>'
245
\ || 0 < searchpair('\c<pre>', '', '\c</pre>', 'nWb')
246
\ || 0 < searchpair('\c<pre>', '', '\c</pre>', 'nW')
247
" we're in a line with </pre> or inside <pre> ... </pre>
254
" [-- special handling for <javascript>: use cindent --]
255
let js = '<script.*type\s*=\s*.*java'
257
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
258
" by Tye Zdrojewski <zdro@yahoo.com>, 05 Jun 2006
259
" ZDR: This needs to be an AND (we are 'after the start of the pair' AND
260
" we are 'before the end of the pair'). Otherwise, indentation
261
" before the start of the script block will be affected; the end of
262
" the pair will still match if we are before the beginning of the
265
if 0 < searchpair(js, '', '</script>', 'nWb')
266
\ && 0 < searchpair(js, '', '</script>', 'nW')
267
" we're inside javascript
268
if getline(lnum) !~ js && getline(a:lnum) != '</script>'
272
return cindent(a:lnum)
276
if getline(lnum) =~ '\c</pre>'
277
" line before the current line a:lnum contains
278
" a closing </pre>. --> search for line before
279
" starting <pre> to restore the indent.
280
let preline = prevnonblank(search('\c<pre>', 'bW') - 1)
285
return indent(preline)
289
let ind = <SID>HtmlIndentSum(lnum, -1)
290
let ind = ind + <SID>HtmlIndentSum(a:lnum, 0)
296
return indent(lnum) + (&sw * ind)
299
let &cpo = s:cpo_save
302
" [-- EOF <runtime>/indent/html.vim --]
305
cat > /tmp/"$(_basename "${0}")"/vimrc << EOF
306
set runtimepath=/tmp/$(_basename "${0}"),\$VIMRUNTIME
309
set encoding=utf-8 "utf is able to represent any character
310
set softtabstop=${INDENTATION}
311
set shiftwidth=${INDENTATION}
312
set expandtab "tabs mutate into spaces, if you wanna insert "real"
313
filetype plugin indent on "enable filetype-specific plugins
319
for _format_var_testcase; do
320
printf "%s\\n" "${_format_var_testcase}"
321
tidy -f /tmp/"$(_basename "${0}")"/testcase -xml -i "${_format_var_testcase}" > /dev/null
322
[ "${?}" -eq "2" ] && { cat /tmp/"$(_basename "${0}")"/testcase; exit 1; }
323
vim -es -u /tmp/"$(_basename "${0}")"/vimrc -c "set ft=html" -c "normal gg=G" -c wq "${_format_var_testcase}"
328
#there is input comming from pipe or file, add to the end of $@
332
[ "${#}" -eq "0" ] && _usage
334
for arg in "${@}"; do #parse options
338
-*) printf "%s\\n" "$(expr "${0}" : '.*/\([^/]*\)'): unrecognized option \`${arg}'" >&2; _usage ;;
342
[ -f "$(command -v "vim")" ] || { printf "%b\\n" "$(_basename "${0}"): You need to install 'vim' & 'tidy' to run this program\n$ sudo apt-get install vim tidy" >&2; exit 1; }
343
[ -f "$(command -v "tidy")" ] || { printf "%b\\n" "$(_basename "${0}"): You need to install 'tidy' to run this program\n$ sudo apt-get install tidy" >&2; exit 1; }