Skip to content

Commit c681ee6

Browse files
committed
Revamp parsing to work with htmlhint's new json output format introduced in 0.9.8. Use direct JSON parsing rather than regular expressions.
1 parent adb596d commit c681ee6

File tree

4 files changed

+62
-34
lines changed

4 files changed

+62
-34
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ Before using this plugin, you must ensure that `htmlhint` is installed on your s
1313

1414
1. Install [Node.js](http://nodejs.org) (and [npm](https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager) on Linux).
1515

16-
1. Install `htmlhint` by typing the following in a terminal:
16+
1. Install the latest `htmlhint` by typing the following in a terminal:
1717
```
18-
npm install -g htmlhint@0.9.7
18+
npm install -g htmlhint@latest
1919
```
2020

2121
1. If you are using `nvm` and `zsh`, ensure that the line to load `nvm` is in `.zshenv` and not `.zshrc`.
2222

23-
**Note:** This plugin requires `htmlhint` 0.9.0 - 0.9.7. Later versions are currently not supported.
23+
**Note:** This plugin requires `htmlhint` 0.9.8 or greater. Upgrade your existing installation by running step 2 above.
2424

2525
### Linter configuration
2626
In order for `htmlhint` to be executed by SublimeLinter, you must ensure that its path is available to SublimeLinter. Before going any further, please read and follow the steps in [“Finding a linter executable”](http://sublimelinter.readthedocs.org/en/latest/troubleshooting.html#finding-a-linter-executable) through “Validating your PATH” in the documentation.

linter.py

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
"""This module exports the Htmlhint plugin class."""
1212

13-
import re
13+
import sublime
1414
from SublimeLinter.lint import Linter, persist
1515

1616

@@ -19,37 +19,54 @@ class Htmlhint(Linter):
1919
"""Provides an interface to htmlhint."""
2020

2121
syntax = 'html'
22-
cmd = 'htmlhint'
22+
cmd = ('htmlhint', '--format', 'json')
2323
version_args = '--version'
2424
version_re = r'(?P<version>\d+\.\d+\.\d+)'
25-
version_requirement = '>= 0.9.0, <= 0.9.7'
26-
regex = r'^\s*line (?P<line>\d+), col (?P<col>\d+): (?P<message>.+)'
25+
version_requirement = '>= 0.9.8'
26+
# empty regex so plugin initializes properly
27+
regex = r''
2728
tempfile_suffix = '-'
2829
config_file = ('--config', '.htmlhintrc', '~')
2930

30-
# htmlhint uses color codes to distinguish errors and warnings
31-
# colors get stripped by sublimelinter
32-
# match warnings instead
33-
warn_regex = (
34-
r'^(Doctype must be html5.'
35-
r'|The script tag can not be used in head'
36-
r'|The value of href \[ .*?\] must be'
37-
r'|The value of .*? can not use ad keyword.'
38-
r'|Alt of img tag must be set value.'
39-
r'|Mixed spaces and tabs in front of line'
40-
r'|Style tag can not be use'
41-
r'|The empty tag : \[ \w+ \] must closed by self.)'
42-
)
43-
warn_re = re.compile(warn_regex)
44-
45-
def split_match(self, match):
46-
match, line, col, error, warning, message, near = super().split_match(match)
47-
if match:
48-
# check if message is a warning
49-
warn = self.warn_re.match(message)
50-
if warn:
51-
return match, line, col, False, True, message, near
52-
53-
persist.debug('match -- msg:"{}", match:{}, line:{}, col:{}, near:{}, warn: {}'.format(message, match, line, col, near, warn))
54-
55-
return match, line, col, error, warning, message, near
31+
def find_errors(self, output):
32+
"""
33+
Override find_errors, parsing output json into json_object.
34+
35+
Calls parse_message for each error found.
36+
37+
"""
38+
39+
output_json = sublime.decode_value(output)
40+
41+
# persist.debug('output_json:"{}", file: "{}"'.format(output_json, self.filename))
42+
43+
for file in output_json:
44+
for message in file['messages']:
45+
yield self.parse_message(message)
46+
47+
def parse_message(self, message):
48+
"""
49+
Parse message object into standard elements of an error and return them.
50+
51+
"""
52+
53+
error_message = message['message']
54+
line = message['line'] - 1
55+
col = message['col']
56+
57+
# set error and warning flags based on message type
58+
error = None
59+
warning = None
60+
if message['type'] == 'error':
61+
error = True
62+
warning = False
63+
elif message['type'] == 'warning':
64+
error = False
65+
warning = True
66+
elif message['type'] == 'info':
67+
# ignore info messages by setting message to None
68+
message = None
69+
70+
persist.debug('message -- msg:"{}", line:{}, col:{}, error: {}, warning: {}, message_obj:{}'.format(error_message, line, col, error, warning, message))
71+
72+
return message, line, col, error, warning, error_message, None

messages.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"install": "messages/install.txt",
3-
"1.0.1": "messages/1.0.1.txt"
3+
"1.0.1": "messages/1.0.1.txt",
4+
"1.1.0": "messages/1.1.0.txt"
45
}

messages/1.1.0.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
SublimeLinter-contrib-htmlhint 1.1.0
2+
------------------------------------
3+
This plugin has been rewritten to handle the latest output formats of htmlhint version 0.9.8 and later. It now parses the JSON output of htmlhint.
4+
5+
*** NOTE: htmlhint version 0.9.8 and greater is now required. ***
6+
7+
Upgrade your existing htmlhint to the latest by running:
8+
npm install -g htmlhint@latest
9+
10+
Please refer to https://github.com/mmaday/SublimeLinter-contrib-htmlhint for complete installation instructions.

0 commit comments

Comments
 (0)