Sublime Forum

Auto checking HTML5 via w3c

#1

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!!!

0 Likes

#2

The on_post_save method of an EventListener class also takes a second argument that is used to identify the view. So it should be on_post_save(self, view) instead on_post_save(self). That’s the meaning of the error message you’re getting.

1 Like

#3

Still have same bug when I change to on_post_save(self, view) !!!

0 Likes

#4

Sorry. The new bug is: HTML5() missing 1 required positional argument: ‘edit’
https://gyazo.com/18461766ad8f34a7f2f5b76ced58ba39

0 Likes

#5

Yes, because your are calling self.HTML5() in on_post_save but then have a method def HTML5(self, edit) that’s expecting an edit parameter as well (which you haven’t passed), hence the error.

1 Like