Skip to content

Instantly share code, notes, and snippets.

@stringertheory
Created March 8, 2016 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stringertheory/8c1e00f306676f089590 to your computer and use it in GitHub Desktop.
Save stringertheory/8c1e00f306676f089590 to your computer and use it in GitHub Desktop.
playing with proselint in python
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