#! /bin/sh #description: format file according to https://wiki.ubuntu.com/Testing/TestCaseFormat #usage: test-case-format files INDENTATION="4" _usage() { printf "%s\\n" "Usage: ${PROGNAME} testcase ..." printf "%s\\n" "https://wiki.ubuntu.com/Testing/TestCaseFormat" } _die() { [ -z "${1}" ] || printf "%s\\n" "${*}" >&2 _usage >&2; exit 1 } _basename() { [ -z "${1}" ] && return 1 || _basename__name="${1}" [ -z "${2}" ] || _basename__suffix="${2}" case "${_basename__name}" in /*|*/*) _basename__name="$(expr "${_basename__name}" : '.*/\([^/]*\)')" ;; esac if [ -n "${_basename__suffix}" ] && [ "${#_basename__name}" -gt "${#2}" ]; then if [ X"$(printf "%s" "${_basename__name}" | cut -c"$((${#_basename__name} - ${#_basename__suffix} + 1))"-"${#_basename__name}")" \ = X"$(printf "%s" "${_basename__suffix}")" ]; then _basename__name="$(printf "%s" "${_basename__name}" | cut -c1-"$((${#_basename__name} - ${#_basename__suffix}))")" fi fi printf "%s\\n" "${_basename__name}" } _tmp_files() { mkdir -p /tmp/"${PROGNAME}"/indent #this file can also be copied to ~/.vim/indent/html.vim so #vim indent html files the testcase way
: no indenting --] if getline(a:lnum) =~ '\c' \ || 0 < searchpair('\c
', '', '\c', 'nWb') \ || 0 < searchpair('\c
', '', '\c', 'nW') " we're in a line with or inside
...if restore_ic == 0 setlocal noic endif return -1 endif " [-- special handling for
to restore the indent. let preline = prevnonblank(search('\c', 'bW') - 1) if preline > 0 if restore_ic == 0 setlocal noic endif return indent(preline) endif endif let ind =HtmlIndentSum(lnum, -1) let ind = ind + HtmlIndentSum(a:lnum, 0) if restore_ic == 0 setlocal noic endif return indent(lnum) + (&sw * ind) endfun let &cpo = s:cpo_save unlet s:cpo_save " [-- EOF /indent/html.vim --] EOF cat > /tmp/"${PROGNAME}"/vimrc << EOF set runtimepath=/tmp/${PROGNAME},\$VIMRUNTIME syntax on set ffs=unix,dos set encoding=utf-8 "utf is able to represent any character set softtabstop=${INDENTATION} set shiftwidth=${INDENTATION} set expandtab "tabs mutate into spaces, if you wanna insert "real" filetype plugin indent on "enable filetype-specific plugins EOF } _format() { for _format__testcase; do printf "%s\\n" "${_format__testcase}" tidy -f /tmp/"${PROGNAME}"/testcase -xml \ -i "${_format__testcase}" > /dev/null [ "${?}" -eq "2" ] && { cat /tmp/"${PROGNAME}"/testcase; exit 1; } vim -es -u /tmp/"${PROGNAME}"/vimrc \ -c "set ft=html" -c "normal gg=G" -c wq "${_format__testcase}" done } PROGNAME="$(_basename "${0}")" if [ ! -t 0 ]; then #there is input comming from pipe or file, add to the end of $@ set -- "${@}" $(cat) fi [ "${#}" -eq "0" ] && _die for arg in "${@}"; do #parse options case "${arg}" in -h|--help) _usage && exit ;; -*) _die "$(expr "${0}" : '.*/\([^/]*\)'): unrecognized option '${arg}'" ;; esac done [ -f "$(command -v "vim")" ] || { printf "%b\\n" \ "${PROGNAME}: install 'vim' & 'tidy' to run this program\n$ sudo apt-get install vim tidy" >&2 exit 1; } [ -f "$(command -v "tidy")" ] || { printf "%b\\n" \ "${PROGNAME}: install 'tidy' to run this program\n$ sudo apt-get install tidy" >&2 exit 1; } _tmp_files _format "${@}"