Hello,
I’m trying to customize w3c plugin: When saving file, the plugin will run
Expect Result: https://gyazo.com/b204609c8363047030bdcaac18b72335
Currently I have a problem: https://gyazo.com/2ef041064e96c364959e9737e96f68bb
This is my code:
import sys
import json
import sublime
import sublime_plugin
import requests
PY3 = sys.version_info[0] == 3
if PY3:
from urllib.request import urlopen
from urllib.parse import urlencode
else:
from urllib import urlopen
from urllib import urlencode
class validator(sublime_plugin.EventListener):
markup_validator_url = 'http://validator.w3.org/check'
css_validator_url = 'http://jigsaw.w3.org/css-validator/validator'
def on_post_save(self):
self.HTML5()
def HTML5(self, edit):
# This is no longer responding with valid JSON
# self.validate(edit, 'HTML5', self.markup_validator_url)
# Temporary fix
# TODO: refactor this and DRY it up
region = sublime.Region(0, self.view.size())
file_contents = self.view.substr(region).encode('utf-8').strip()
results = requests.post('https://html5.validator.nu?out=json', data=file_contents, headers={'Content-Type': 'text/html; charset=UTF-8'}).json()
if not results['messages']:
sublime.message_dialog('This document was successfully checked as HTML5')
else:
message_contents = ''
formatted_messages = []
formatted_messages.append(
'Errors found while checking this document as HTML5:\n\n')
for message in results['messages']:
formatted_messages.append(
'Line %s: %s\n\n' % (message['lastLine'], message['message']))
message_contents = message_contents.join(formatted_messages)
output = sublime.active_window().new_file()
output.set_scratch(True)
output.set_name('W3C Validation Errors')
output.insert(edit, 0, message_contents)
Plz help me fix it. Thank you!!!