Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚧 Ignore escaped characters #1422

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
"""
VERSION = '1.17.0.dev0'

SUB_DICT = {'\\n': ' ', r"\'": "'"}

# Users might want to link this file into /usr/local/bin, so we resolve the
# symbolic link path to the real path if necessary.
default_dictionary = os.path.join(os.path.dirname(os.path.realpath(__file__)),
Expand Down Expand Up @@ -369,6 +371,38 @@ def fix_case(word, fixword):
return fixword


def multiple_replace(find_dict, text):
"""Multiple find and replace based on a dictionary

Parameters
----------
find_dict : dict
Dictionary containing values to find and replace. For example
``{'\\n': ' ', r"\'": "'"}``

text : str
Text to perform substitution on.

Returns
-------
sub_text : str
Text with substitutions.

Examples
--------
>>> line = r'this was a cat meow meow\nWhere don\'t'
>>> find_dict = {'\\n': ' ', r"\'": "'"}
>>> multiple_replace(find_dict, text)
this was a cat meow meow Where don't

"""
# Create a regular expression from all of the dictionary keys
regex = re.compile("|".join(map(re.escape, find_dict.keys())))

# For each match, look up the corresponding value in the dictionary
return regex.sub(lambda match: find_dict[match.group(0)], text)


def ask_for_word_fix(line, wrongword, misspelling, interactivity):
if interactivity <= 0:
return misspelling.fix, fix_case(wrongword, misspelling.data)
Expand Down Expand Up @@ -492,6 +526,9 @@ def parse_file(filename, colors, summary, misspellings, exclude_lines,
fixed_words = set()
asked_for = set()

# escape valid characters
line = multiple_replace(SUB_DICT, line)

for word in word_regex.findall(line):
lword = word.lower()
if lword in misspellings:
Expand Down
11 changes: 11 additions & 0 deletions codespell_lib/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ def test_basic(tmpdir, capsys):
assert cs.main(d) == 0


def test_escaped(tmpdir, capsys):
"""Test escaping characters"""
assert cs.main('_does_not_exist_') == 0
with open(op.join(str(tmpdir), 'tmp'), 'w') as f:
pass
d = str(tmpdir)
with open(op.join(d, 'escaped_char.txt'), 'w') as f:
f.write(r"\n\nWe can")
assert cs.main(d) == 0


def test_interactivity(tmpdir, capsys):
"""Test interaction"""
# Windows can't read a currently-opened file, so here we use
Expand Down