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

C escapes #174

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
SORT_ARGS := -f

PHONY := all check check-dictionary sort-dictionary clean
PHONY := all check check-dictionary sort-dictionary test clean

all: check-dictionary codespell.1

Expand All @@ -20,5 +20,8 @@ sort-dictionary:
pypi:
python setup.py sdist register upload

test:
PYTHONPATH=`pwd` PATH=`pwd`/bin:$$PATH nosetests --with-coverage --cover-erase

clean:
rm -rf codespell.1
10 changes: 10 additions & 0 deletions codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
quiet_level = 0
encodings = ['utf-8', 'iso-8859-1']
word_regex = re.compile(r"[\w\-']+")
c_escape_regex = re.compile(r'\\\w')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to use the standard set of escape sequences?

# 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 @@ -281,6 +282,12 @@ def parse_options(args):
action='store_true', default=False,
help='Check file names as well.')

parser.add_option('--c-escapes',
action='store_true', default=False,
help='Treats files as if they contain C-style character '
'escapes. So for example "\\nHello" is parsed as '
'"hello" instead of "nhallo".')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nhallo -> nhello

also \\nHello -> \\nhello or capitalize the other two

And maybe r"\nhello" is clearer than "\\nhello"?


(o, args) = parser.parse_args(list(args))

if not args:
Expand Down Expand Up @@ -473,6 +480,9 @@ def parse_file(filename, colors, summary):
fixed_words = set()
asked_for = set()

if options.c_escapes:
line = c_escape_regex.sub(' ', line)

for word in word_regex.findall(line):
lword = word.lower()
if lword in misspellings:
Expand Down
9 changes: 9 additions & 0 deletions codespell_lib/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,15 @@ def test_check_filename():
assert_equal(cs.main('-f', d), 1)


def test_c_escapes():
"""Test c-esacpes option"""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meta spelling error, it should be c-escapes! 😄

with TemporaryDirectory() as d:
with open(op.join(d, 'test.txt'), 'w') as f:
f.write('\\nabandonned')
assert_equal(cs.main(d), 0)
assert_equal(cs.main('--c-escapes', d), 1)


class TemporaryDirectory(object):
"""Backport for 2.7"""

Expand Down