Skip to content

Commit

Permalink
Adding validate-html hook based on v.Nu through svenkreiss/html5valid…
Browse files Browse the repository at this point in the history
…ator
  • Loading branch information
Lucas-C committed Jul 13, 2016
1 parent 0f5055d commit 2916a99
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/pre_commit_hooks.egg-info
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# A list of common hooks I use
- repo: git://github.com/Lucas-C/pre-commit-hooks
sha: b600798f8dacfcc29862e3d58367b89d3090c6a7
sha: 0f5055d31f39ea3ac1f5335a0cca320200cd0c51
hooks:
- id: forbid-crlf
- id: remove-crlf
Expand All @@ -16,7 +16,7 @@
- --html-files-dir
- .
- repo: git://github.com/pre-commit/pre-commit-hooks
sha: e306ff3b7d0d9a6fc7d128ef9ca2e0b6e6e76e8f
sha: 3fa02652357ff0dbb42b5bc78c673b7bc105fcf3
hooks:
- id: trailing-whitespace
files: ''
Expand All @@ -25,7 +25,7 @@
- id: check-json
- id: check-yaml
- repo: git://github.com/pre-commit/mirrors-pylint
sha: 4972270a188e28f100cd0db472e05f59d9621e54
sha: 4de6c8dfadef1a271a814561ce05b8bc1c446d22
hooks:
- id: pylint
args:
Expand Down
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[MESSAGES CONTROL]
disable = bad-continuation, duplicate-code, import-error, missing-docstring
disable = bad-continuation, duplicate-code, import-error, missing-docstring, multiple-imports

[FORMAT]
max-line-length = 110
1 change: 1 addition & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pre-commit
9 changes: 7 additions & 2 deletions hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@
args: [ --whitespaces-count, '4' ]
files: ''
exclude: (Makefile|debian/rules)(\.in)?$
- id: validate-html
name: HTML validator using v.Nu through svenkreiss/html5validator
entry: validate_html
language: python
files: .*\.(html|hbs)$
- id: forbid-html-img-without-alt-text
name: HTML img tag alt text checker
description: "List every <img> tag without a alt= or data-ng-attr-alt= attribute"
entry: forbid_html_img_without_alt_text
language: python
files: .*\.html$
files: .*\.(html|hbs)$
- id: forbid-non-std-html-attributes
name: Non-standard HTML attributes checker
description: "Detect any non-standard attribute that is not prefixed with data-"
Expand All @@ -39,7 +44,7 @@
args:
- --extra-known-attributes
- content,http-equiv,placeholder,role,style
files: .*\.html$
files: .*\.(html|hbs)$
- id: detect-missing-css-classes
name: Missing CSS classes checker
name: Detect unused or missing CSS classes definitions, and abort if any of the later ones is found
Expand Down
2 changes: 0 additions & 2 deletions pre_commit_hooks/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import string

# Taken from: http://code.activestate.com/recipes/173220-test-if-a-file-or-string-is-text-or-binary/

KNOWN_BINARY_FILE_EXT = ['.pdf']
Expand Down
44 changes: 44 additions & 0 deletions pre_commit_hooks/validate_html.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from __future__ import print_function
import argparse, sys
from html5validator.validator import Validator

def main(argv=None):
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*', help='filenames to check')
parser.add_argument('--error_only', dest='error_only',
action='store_false', default=True)
parser.add_argument('--ignore', nargs='*', default=None,
type=lambda s: (s.decode('utf-8')
if isinstance(s, bytes) else s),
help='ignore messages containing the given strings')
parser.add_argument('--ignore-re', nargs='*', default=None,
type=lambda s: (s.decode('utf-8')
if isinstance(s, bytes) else s),
dest='ignore_re',
help='regular expression of messages to ignore')
parser.add_argument('-l', action='store_const', const=2048,
dest='stack_size',
help=('run on larger files: sets Java '
'stack size to 2048k')
)
parser.add_argument('-ll', action='store_const', const=8192,
dest='stack_size',
help=('run on larger files: sets Java '
'stack size to 8192k')
)
parser.add_argument('-lll', action='store_const', const=32768,
dest='stack_size',
help=('run on larger files: sets Java '
'stack size to 32768k')
)
args = parser.parse_args(argv)

validator = Validator(directory=None, match=None, ignore=args.ignore, ignore_re=args.ignore_re)
sys.exit(validator.validate(
args.filenames,
errors_only=args.error_only,
stack_size=args.stack_size,
))

if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@

packages=find_packages('.'),
install_requires=[
'html5validator',
'lxml',
'tinycss2'
'tinycss2',
],
entry_points={
'console_scripts': [
'detect_missing_css_classes = pre_commit_hooks.detect_missing_css_classes:main',
'forbid_crlf = pre_commit_hooks.forbid_crlf:main',
'validate_html = pre_commit_hooks.validate_html:main',
'forbid_html_img_without_alt_text = pre_commit_hooks.forbid_html_img_without_alt_text:main',
'forbid_non_std_html_attributes = pre_commit_hooks.forbid_non_std_html_attributes:main',
'forbid_tabs = pre_commit_hooks.forbid_tabs:main',
Expand Down

0 comments on commit 2916a99

Please sign in to comment.