tidy.vim 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. "============================================================================
  2. "File: tidy.vim
  3. "Description: Syntax checking plugin for syntastic.vim
  4. "Maintainer: Martin Grenfell <martin.grenfell at gmail dot com>
  5. "License: This program is free software. It comes without any warranty,
  6. " to the extent permitted by applicable law. You can redistribute
  7. " it and/or modify it under the terms of the Do What The Fuck You
  8. " Want To Public License, Version 2, as published by Sam Hocevar.
  9. " See http://sam.zoy.org/wtfpl/COPYING for more details.
  10. "
  11. "============================================================================
  12. if exists('g:loaded_syntastic_html_tidy_checker')
  13. finish
  14. endif
  15. let g:loaded_syntastic_html_tidy_checker = 1
  16. let s:save_cpo = &cpo
  17. set cpo&vim
  18. " Checker options {{{1
  19. if !exists('g:syntastic_html_tidy_ignore_errors')
  20. let g:syntastic_html_tidy_ignore_errors = []
  21. endif
  22. if !exists('g:syntastic_html_tidy_blocklevel_tags')
  23. let g:syntastic_html_tidy_blocklevel_tags = []
  24. endif
  25. if !exists('g:syntastic_html_tidy_inline_tags')
  26. let g:syntastic_html_tidy_inline_tags = []
  27. endif
  28. if !exists('g:syntastic_html_tidy_empty_tags')
  29. let g:syntastic_html_tidy_empty_tags = []
  30. endif
  31. " }}}1
  32. " Constants {{{1
  33. let s:IGNORE_ERRORS = [
  34. \ "<table> lacks \"summary\" attribute",
  35. \ "not approved by W3C",
  36. \ "<input> proprietary attribute \"placeholder\"",
  37. \ "<meta> proprietary attribute \"charset\"",
  38. \ "<meta> lacks \"content\" attribute",
  39. \ "inserting \"type\" attribute",
  40. \ "proprietary attribute \"data-",
  41. \ "missing <!DOCTYPE> declaration",
  42. \ "inserting implicit <body>",
  43. \ "inserting missing 'title' element",
  44. \ "unescaped & or unknown entity",
  45. \ "<input> attribute \"type\" has invalid value",
  46. \ "proprietary attribute \"role\"",
  47. \ "proprietary attribute \"aria-activedescendant\"",
  48. \ "proprietary attribute \"aria-atomic\"",
  49. \ "proprietary attribute \"aria-autocomplete\"",
  50. \ "proprietary attribute \"aria-busy\"",
  51. \ "proprietary attribute \"aria-checked\"",
  52. \ "proprietary attribute \"aria-controls\"",
  53. \ "proprietary attribute \"aria-describedby\"",
  54. \ "proprietary attribute \"aria-disabled\"",
  55. \ "proprietary attribute \"aria-dropeffect\"",
  56. \ "proprietary attribute \"aria-expanded\"",
  57. \ "proprietary attribute \"aria-flowto\"",
  58. \ "proprietary attribute \"aria-grabbed\"",
  59. \ "proprietary attribute \"aria-haspopup\"",
  60. \ "proprietary attribute \"aria-hidden\"",
  61. \ "proprietary attribute \"aria-invalid\"",
  62. \ "proprietary attribute \"aria-label\"",
  63. \ "proprietary attribute \"aria-labelledby\"",
  64. \ "proprietary attribute \"aria-level\"",
  65. \ "proprietary attribute \"aria-live\"",
  66. \ "proprietary attribute \"aria-multiline\"",
  67. \ "proprietary attribute \"aria-multiselectable\"",
  68. \ "proprietary attribute \"aria-orientation\"",
  69. \ "proprietary attribute \"aria-owns\"",
  70. \ "proprietary attribute \"aria-posinset\"",
  71. \ "proprietary attribute \"aria-pressed\"",
  72. \ "proprietary attribute \"aria-readonly\"",
  73. \ "proprietary attribute \"aria-relevant\"",
  74. \ "proprietary attribute \"aria-relevant\"",
  75. \ "proprietary attribute \"aria-required\"",
  76. \ "proprietary attribute \"aria-selected\"",
  77. \ "proprietary attribute \"aria-setsize\"",
  78. \ "proprietary attribute \"aria-sort\"",
  79. \ "proprietary attribute \"aria-valuemax\"",
  80. \ "proprietary attribute \"aria-valuemin\"",
  81. \ "proprietary attribute \"aria-valuenow\"",
  82. \ "proprietary attribute \"aria-valuetext\""
  83. \ ]
  84. lockvar! s:IGNORE_ERRORS
  85. let s:BLOCKLEVEL_TAGS = [
  86. \ 'main',
  87. \ 'section',
  88. \ 'article',
  89. \ 'aside',
  90. \ 'header',
  91. \ 'footer',
  92. \ 'nav',
  93. \ 'figure',
  94. \ 'figcaption'
  95. \ ]
  96. lockvar! s:BLOCKLEVEL_TAGS
  97. let s:INLINE_TAGS = [
  98. \ 'video',
  99. \ 'audio',
  100. \ 'source',
  101. \ 'embed',
  102. \ 'mark',
  103. \ 'progress',
  104. \ 'meter',
  105. \ 'time',
  106. \ 'ruby',
  107. \ 'rt',
  108. \ 'rp',
  109. \ 'canvas',
  110. \ 'command',
  111. \ 'details',
  112. \ 'datalist'
  113. \ ]
  114. lockvar! s:INLINE_TAGS
  115. let s:EMPTY_TAGS = [
  116. \ 'wbr',
  117. \ 'keygen'
  118. \ ]
  119. lockvar! s:EMPTY_TAGS
  120. " }}}1
  121. function! SyntaxCheckers_html_tidy_GetLocList() dict " {{{1
  122. let makeprg = self.makeprgBuild({ 'args_after': s:Args() })
  123. let errorformat =
  124. \ '%Wline %l column %v - Warning: %m,' .
  125. \ '%Eline %l column %v - Error: %m,' .
  126. \ '%-G%.%#'
  127. let loclist = SyntasticMake({
  128. \ 'makeprg': makeprg,
  129. \ 'errorformat': errorformat,
  130. \ 'defaults': {'bufnr': bufnr('')},
  131. \ 'returns': [0, 1, 2] })
  132. " filter out valid HTML5 from the errors
  133. for e in loclist
  134. if e['valid'] && s:IgnoreError(e['text']) == 1
  135. let e['valid'] = 0
  136. endif
  137. endfor
  138. return loclist
  139. endfunction " }}}1
  140. " Utilities {{{1
  141. " TODO: join this with xhtml.vim for DRY's sake?
  142. function! s:TidyEncOptByFenc() " {{{2
  143. let TIDY_OPTS = {
  144. \ 'utf-8': '-utf8',
  145. \ 'ascii': '-ascii',
  146. \ 'latin1': '-latin1',
  147. \ 'iso-2022-jp': '-iso-2022',
  148. \ 'cp1252': '-win1252',
  149. \ 'macroman': '-mac',
  150. \ 'utf-16le': '-utf16le',
  151. \ 'utf-16': '-utf16',
  152. \ 'big5': '-big5',
  153. \ 'cp932': '-shiftjis',
  154. \ 'sjis': '-shiftjis',
  155. \ 'cp850': '-ibm858',
  156. \ }
  157. return get(TIDY_OPTS, &fileencoding, '-utf8')
  158. endfunction " }}}2
  159. function! s:IgnoreError(text) " {{{2
  160. for item in s:IGNORE_ERRORS + g:syntastic_html_tidy_ignore_errors
  161. if stridx(a:text, item) != -1
  162. return 1
  163. endif
  164. endfor
  165. return 0
  166. endfunction " }}}2
  167. function! s:NewTags(name) " {{{2
  168. return syntastic#util#shescape(join( s:{toupper(a:name)} + g:syntastic_html_tidy_{a:name}, ',' ))
  169. endfunction " }}}2
  170. function! s:Args() " {{{2
  171. let args = s:TidyEncOptByFenc() .
  172. \ ' --new-blocklevel-tags ' . s:NewTags('blocklevel_tags') .
  173. \ ' --new-inline-tags ' . s:NewTags('inline_tags') .
  174. \ ' --new-empty-tags ' . s:NewTags('empty_tags') .
  175. \ ' -e'
  176. return args
  177. endfunction " }}}2
  178. " }}}1
  179. call g:SyntasticRegistry.CreateAndRegisterChecker({
  180. \ 'filetype': 'html',
  181. \ 'name': 'tidy'})
  182. let &cpo = s:save_cpo
  183. unlet s:save_cpo
  184. " vim: set sw=4 sts=4 et fdm=marker: