Created
March 8, 2016 15:36
-
-
Save stringertheory/8c1e00f306676f089590 to your computer and use it in GitHub Desktop.
playing with proselint in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import codecs | |
from proselint.command_line import lint | |
import termcolor | |
WINDOW = 40 | |
FORE = 'white' | |
BACK = 'on_blue' | |
# need to read the text with codecs to match what proseline does | |
filename = sys.argv[1] | |
with codecs.open(filename, "r", encoding='utf-8') as infile: | |
text = infile.readlines() | |
for error in lint(filename): | |
check, message, line, column, start, end, extent, severity, suggest = error | |
error_start = column | |
error_end = column + extent | |
snippet_start = max(error_start - WINDOW, 0) | |
snippet_end = min(error_end + WINDOW, len(text[line])) | |
highlighted = ''.join([ | |
text[line][snippet_start:error_start], | |
termcolor.colored(text[line][error_start:error_end], FORE, BACK), | |
text[line][error_end:snippet_end].strip(), | |
]) | |
print u'MESSAGE: {}\n{}\n'.format(message, highlighted) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment