')
except ValueError:
firstline = 0
# Generate a form body
data = {
'doctype': 'Inline',
'group': '0',
'verbose': '1',
'prefill': '1',
'prefill_doctype': 'html401',
'fragment': contents,
}
# Now submit it to the w3c validator
resp = requests.post(
'https://validator.w3.org/check',
data=data,
headers={
"User-Agent": "localcheck-tester/0.0",
},
timeout=20,
)
if resp.headers['x-w3c-validator-status'] == 'Valid':
print("Page validates!")
sys.exit(0)
elif resp.headers['x-w3c-validator-status'] == 'Invalid':
print("Invalid!")
print("Errors: %s" % resp.headers['x-w3c-validator-errors'])
print("Warnings: %s" % resp.headers['x-w3c-validator-warnings'])
hp = html.parser.HTMLParser()
for m in re.findall('
.*?', resp.text, re.DOTALL):
r = re.search(r'
Line (\d+).*(.*?)', m, re.DOTALL)
if r:
print("Line %s (should be around %s): %s" % (r.group(1), int(r.group(1)) - firstline, hp.unescape(r.group(2))))
r2 = re.search('(.*?)(.*?)(.*?)
', m, re.DOTALL)
if r2:
s = "%s%s%s" % r2.groups()
print("Source: %s" % hp.unescape(s))
print("")
else:
print("Unknown status: %s" % headers['x-w3c-validator-status'])
print(headers)
sys.exit(1)