Skip to content

Commit

Permalink
Merge pull request #473 from amperser/replacements
Browse files Browse the repository at this point in the history
Add replacements to return value of checks
  • Loading branch information
suchow committed Apr 16, 2016
2 parents 91dd002 + 4d8c03a commit 8eb3b95
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 32 deletions.
2 changes: 1 addition & 1 deletion proselint/checks/links/broken.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def check(text):
url = "http://" + url

if is_broken_link(url):
errors.append((m.start(), m.end(), err, msg.format(url)))
errors.append((m.start(), m.end(), err, msg.format(url), None))

return errors

Expand Down
8 changes: 6 additions & 2 deletions proselint/checks/misc/greylist.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ def check(text):
for word in bad_words:
occ = [m for m in re.finditer(word, text.lower())]
for o in occ:
errors.append((o.start(), o.end(), err,
msg.format(word, explanations[word])))
errors.append((
o.start(),
o.end(),
err,
msg.format(word, explanations[word]),
None))

return errors
7 changes: 6 additions & 1 deletion proselint/checks/misc/tense_present.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ def check(text):
for i in illogics:
for m in re.finditer(u"\s{}\s".format(i), text, flags=re.U | re.I):
txt = m.group(0).strip()
errors.append((m.start() + 1, m.end(), err, msg.format(txt)))
errors.append((
m.start() + 1,
m.end(),
err,
msg.format(txt),
None))

return errors
2 changes: 1 addition & 1 deletion proselint/checks/typography/exclamation.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ def check_exclamations_ppm(text):

if ppm > 30 and count > 1:
loc = re.search(regex, text).start() + 1
return [(loc, loc+1, err, msg)]
return [(loc, loc+1, err, msg, ".")]
else:
return []
3 changes: 2 additions & 1 deletion proselint/checks/uncomparables/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,5 @@ def check(text):
comparators, uncomparables) if i not in exceptions]

occ = re.finditer("|".join(all), text.lower())
return [(o.start(), o.end(), err, msg.format(o.group(0))) for o in occ]
return [(o.start(), o.end(), err, msg.format(o.group(0)), None)
for o in occ]
52 changes: 26 additions & 26 deletions proselint/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ def errors_to_json(errors):
"replacements": e[8],
})

return json.dumps(dict(status="success", data={"errors": out}))
return json.dumps(
dict(status="success", data={"errors": out}), sort_keys=True)


def reverse(text):
Expand Down Expand Up @@ -230,11 +231,11 @@ def lint(input_file, debug=False):
result = check(text)

for error in result:
(start, end, check, message) = error
(start, end, check, message, replacements) = error
(line, column) = line_and_column(text, start)
if not is_quoted(start, text):
errors += [(check, message, line, column, start, end,
end - start, "warning", None)]
end - start, "warning", replacements)]

if len(errors) > options["max_errors"]:
break
Expand All @@ -258,25 +259,22 @@ def consistency_check(text, word_pairs, err, msg, offset=0):
msg = " ".join(msg.split())

for w in word_pairs:
match1 = [m for m in re.finditer(w[0], text)]
match2 = [m for m in re.finditer(w[1], text)]

if len(match1) > 0 and len(match2) > 0:

if len(match1) > len(match2):
for m in match2:
errors.append((
m.start() + offset,
m.end() + offset,
err,
msg.format(m.group(0), w[0])))
else:
for m in match1:
errors.append((
m.start() + offset,
m.end() + offset,
err,
msg.format(m.group(0), w[1])))
matches = [
[m for m in re.finditer(w[0], text)],
[m for m in re.finditer(w[1], text)]
]

if len(matches[0]) > 0 and len(matches[1]) > 0:

idx_minority = len(matches[0]) > len(matches[1])

for m in matches[idx_minority]:
errors.append((
m.start() + offset,
m.end() + offset,
err,
msg.format(w[~idx_minority], m.group(0)),
w[~idx_minority]))

return errors

Expand All @@ -301,7 +299,8 @@ def preferred_forms_check(text, list, err, msg, ignore_case=True, offset=0,
m.start() + 1 + offset,
m.end() + offset,
err,
msg.format(p[0], txt)))
msg.format(p[0], txt),
p[0]))

errors = truncate_to_max(errors, max_errors)

Expand Down Expand Up @@ -346,7 +345,8 @@ def existence_check(text, list, err, msg, ignore_case=True,
m.start() + 1 + offset,
m.end() + offset,
err,
msg.format(txt)))
msg.format(txt),
None))

errors = truncate_to_max(errors, max_errors)

Expand All @@ -359,15 +359,15 @@ def truncate_to_max(errors, max_errors):
Give the total number of times that the error was found elsewhere.
"""
if len(errors) > max_errors:
start1, end1, err1, msg1 = errors[0]
start1, end1, err1, msg1, replacements = errors[0]

if len(errors) == (max_errors + 1):
msg1 += " Found once elsewhere."
else:
msg1 += " Found {} times elsewhere.".format(len(errors))

errors = errors[1:max_errors]
errors = [(start1, end1, err1, msg1)] + errors
errors = [(start1, end1, err1, msg1, replacements)] + errors

return errors

Expand Down

0 comments on commit 8eb3b95

Please sign in to comment.