def validate(filename): """ Use W3C validator service: https://bitbucket.org/nmb10/py_w3c/ . :param filename: the filename to validate """ try: from html.parser import HTMLParser except ImportError: # fallback for Python 2: from HTMLParser import HTMLParser from py_w3c.validators.html.validator import HTMLValidator h = HTMLParser() # for unescaping WC3 messages vld = HTMLValidator() LOG.info("Validating: {0}".format(filename)) # call w3c webservice vld.validate_file(filename) # display errors and warning for err in vld.errors: line = err.get('line') or err['lastLine'] col = err.get('col') or '{}-{}'.format(err['firstColumn'], err['lastColumn']) LOG.error(u'line: {0}; col: {1}; message: {2}'.format( line, col, h.unescape(err['message']))) for err in vld.warnings: line = err.get('line') or err['lastLine'] col = err.get('col') or '{}-{}'.format(err['firstColumn'], err['lastColumn']) LOG.warning(u'line: {0}; col: {1}; message: {2}'.format( line, col, h.unescape(err['message'])))
def validate(filename): """ Use W3C validator service: https://bitbucket.org/nmb10/py_w3c/ . :param filename: the filename to validate """ import HTMLParser from py_w3c.validators.html.validator import HTMLValidator h = HTMLParser.HTMLParser() # for unescaping WC3 messages vld = HTMLValidator() LOG.info("Validating: {0}".format(filename)) # call w3c webservice vld.validate_file(filename) # display errors and warning for err in vld.errors: LOG.error(u'line: {0}; col: {1}; message: {2}'. format(err['line'], err['col'], h.unescape(err['message'])) ) for err in vld.warnings: LOG.warning(u'line: {0}; col: {1}; message: {2}'. format(err['line'], err['col'], h.unescape(err['message'])) )
def validate(filename): """ Use W3C validator service: https://bitbucket.org/nmb10/py_w3c/ . :param filename: the filename to validate """ # Python3 html parser is in different spot from html.parser import HTMLParser from py_w3c.validators.html.validator import HTMLValidator h = HTMLParser() # for unescaping WC3 messages vld = HTMLValidator() LOG.info("Validating: {0}".format(filename)) # call w3c webservice vld.validate_file(filename) # display errors and warning for err in vld.errors: pprint.pprint(err) if "lastLine" in err.keys(): LOG.error("line: {0}; col: {1}; message: {2}".format( err['lastLine'], err['lastColumn'], h.unescape(err['message']))) else: LOG.error("message: {0}".format(h.unescape(err['message']))) for err in vld.warnings: if "lastLine" in err.keys(): LOG.error("line: {0}; col: {1}; message: {2}".format( err['lastLine'], err['lastColumn'], h.unescape(err['message']))) else: LOG.error("message: {0}".format(h.unescape(err['message'])))
def parse(self, response): with open("page.html", "a") as f: f.write(response.text) vld = HTMLValidator() vld.validate_file("page.html") with open("validatePage.json", "a") as f: f.write(f"{json.dumps(vld.errors, indent=4)}")
def run(self): html_validator = HTMLValidator() for file in self.files: html_validator.validate_file(os.path.join(self.project_path, file)) print(os.path.join(self.project_path, file)) if len(html_validator.errors): self.show_error_report(html_validator.errors) raise Exception( "Failed to validate HTML file: {}".format(file))
import nose import os import re import copy from py_w3c.validators.html.validator import HTMLValidator files = [] results = {} for root, dirs, file in os.walk('.'): for name in file: if re.search('.*\.html?$',name): validator = HTMLValidator(charset="utf8") filepath = os.path.join(root,name) validator.validate_file(filepath) results[filepath] = copy.copy(validator) def validation(errortype): if errortype not in ('errors','warnings'): raise NotImplementedError(errortype + ' is not implemented at Validator Object') count = 0 for path,result in results.items(): obj = None if errortype == 'errors': obj = result.errors elif errortype == 'warnings': obj = result.warnings
txt.write(text.get(0.0, Tkinter.END)) txt.close() def key(event): if event.char == '\x1b': root.destroy() else: val = text.index(Tkinter.INSERT).split('.') lineNum.config(text=str(val[0])) colNum.config(text=str(val[1])) vld = HTMLValidator(doctype="HTML 4.01 Strict") vld.validate_file( '/home/user/workspace/Problem Statement Two/StatementTwo/files/25 errors - a1 (1).html' ) errors = vld.errors locationList = [] for error in range(len(errors)): locationList.append(errors[error]['line']) print locationList txt = open( '/home/user/workspace/Problem Statement Two/StatementTwo/files/25 errors - a1 (1).html', 'r') lineNum = 1 errorIndex = 0 for line in txt: ''' #print line