Created
May 8, 2020 18:01
-
-
Save joeydi/fa55606dd0d3566591b86c952097e95e to your computer and use it in GitHub Desktop.
Validator.nu JSON to HTML
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/local/bin/python3 | |
import sys | |
import argparse | |
import html | |
import requests | |
from pprint import pprint | |
ICON_INFO = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24"><path fill="currentColor" d="M13 7.5a1 1 0 11-2 0 1 1 0 012 0zm-3 3.75a.75.75 0 01.75-.75h1.5a.75.75 0 01.75.75v4.25h.75a.75.75 0 010 1.5h-3a.75.75 0 010-1.5h.75V12h-.75a.75.75 0 01-.75-.75z"></path><path fill="currentColor" fill-rule="evenodd" d="M12 1C5.925 1 1 5.925 1 12s4.925 11 11 11 11-4.925 11-11S18.075 1 12 1zM2.5 12a9.5 9.5 0 1119 0 9.5 9.5 0 01-19 0z"></path></svg>' | |
ICON_ERROR = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24"><path fill="currentColor" d="M12 7a.75.75 0 01.75.75v4.5a.75.75 0 01-1.5 0v-4.5A.75.75 0 0112 7zm1 9a1 1 0 11-2 0 1 1 0 012 0z"></path><path fill="currentColor" fill-rule="evenodd" d="M12 1C5.925 1 1 5.925 1 12s4.925 11 11 11 11-4.925 11-11S18.075 1 12 1zM2.5 12a9.5 9.5 0 1119 0 9.5 9.5 0 01-19 0z"></path></svg>' | |
parser = argparse.ArgumentParser( | |
description="Validate URL with Validator.nu and format output as HTML fragments" | |
) | |
parser.add_argument("doc", type=str, help="URL of document to validate") | |
args = parser.parse_args() | |
r = requests.get('https://html5.validator.nu/', params={"doc": args.doc, "out": "json"}) | |
messages = r.json().get('messages', []) | |
if len(messages): | |
print('- %d HTML validation messages:' % len(messages)) | |
for message in messages: | |
template = """ | |
<div class="alert %s" role="alert" style="overflow: hidden;"> | |
<div class="d-flex"> | |
<div class="mr-3" style="color: %s;"> | |
%s | |
</div> | |
<div> | |
<strong class="d-block mb-1">%s</strong> | |
<code>[%d:%d] %s</code> | |
</div> | |
</div> | |
</div> | |
""" | |
extract_start = message['hiliteStart'] | |
extract_end = extract_start + message['hiliteLength'] | |
alert_icon = ICON_INFO if message['type'] == 'info' else ICON_ERROR | |
alert_color = '#856404' if message['type'] == 'info' else '#721c24' | |
alert_class = 'alert-warning' if message['type'] == 'info' else 'alert-danger' | |
output = template % ( | |
alert_class, | |
alert_color, | |
alert_icon, | |
message['message'], | |
message.get('firstLine') or message['lastLine'], | |
message['firstColumn'], | |
html.escape(message['extract'][extract_start:extract_end]), | |
) | |
print(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment