A Python script and a pre-commit hook that calls it for validating all the HTML files in a directory (recursively) on commit.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this goes in your local Git repo at .git/hooks/pre-commit | |
#!/bin/sh | |
errors=0 | |
for i in $(find . -type f -name "*.html") | |
do | |
printf "$i:\n" | |
validate $i | |
exit_status=$? | |
if [ "${exit_status}" -ne 0 ]; | |
then | |
errors=1 | |
fi | |
printf "\n" | |
done | |
if [ "${errors}" -ne 0 ]; | |
then | |
echo "there were errors, commit aborted" | |
exit 1 | |
fi | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
This goes somewhere on your PATH, I've got it at /usr/local/bin | |
Make sure to `chmod +x <this script>` to make it executable! | |
Validate an HTML file using the W3C HTML validation API | |
https://github.com/validator/validator/wiki/Service-%C2%BB-Input-%C2%BB-POST-body | |
""" | |
import os | |
import pathlib | |
from pprint import pprint | |
import sys | |
import requests | |
W3C_VALIDATE_URL_POST = "https://validator.w3.org/nu/?out=json" | |
def validate_html(args): | |
filename = 'index.html' | |
if args: | |
filename = args[0] | |
with open(filename) as fout: | |
html = fout.read() | |
headers = {'content-type': 'text/html', 'charset': 'utf-8'} | |
response = requests.post(W3C_VALIDATE_URL_POST, headers=headers, data=html.encode()) | |
response_json = response.json() | |
if not response_json['messages']: | |
print('valid!') | |
else: | |
pprint(response_json['messages']) | |
sys.exit(1) | |
if __name__ == "__main__": | |
if len(sys.argv) > 2: | |
raise Exception('please only supply an HTML filename or nothing') | |
validate_html(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment