From 119bfd39817b20ba13275fafe02d16fad322b349 Mon Sep 17 00:00:00 2001 From: Finn Palmer Date: Fri, 2 Oct 2020 16:32:51 +0100 Subject: [PATCH 1/3] Add support for a nospell directive, simply remove the lines before they are spell-checked. --- scspell/__init__.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/scspell/__init__.py b/scspell/__init__.py index 7aae848..5df5894 100644 --- a/scspell/__init__.py +++ b/scspell/__init__.py @@ -95,6 +95,9 @@ # File-id specifiers take this form FILE_ID_REGEX = re.compile(r'scspell-id:[ \t]*([a-zA-Z0-9_\-]+)') +# No spell-checking directive (ignores line) +NO_SPELL = "# nospell" + class MatchDescriptor(object): @@ -580,6 +583,10 @@ def spell_check_file(filename, dicts, ignores, report_only, c_escapes): else: token_regex = TOKEN_REGEX + # Remove lines with the '# nospell' directive + source_text = "".join( + [l for l in source_text.splitlines(keepends=True) if NO_SPELL not in l]) + # Search for tokens to spell-check data = source_text pos = 0 From f90c3e69d64fc632da6273fdf0ed878ac3caf65b Mon Sep 17 00:00:00 2001 From: Finn Palmer Date: Mon, 26 Oct 2020 09:55:48 +0000 Subject: [PATCH 2/3] Add --disable-nospell option and add documentation to README.txt --- README.rst | 8 ++++++++ scspell/__init__.py | 21 +++++++++++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/README.rst b/README.rst index f6418bd..28fbd15 100644 --- a/README.rst +++ b/README.rst @@ -147,6 +147,14 @@ Spell-checking Options see the tokens "``lpha``", "``eta``", "``amma``", and "``elta``". +--disable-nospell\ + By default, **scspell** will ignore any lines in a file which contain the + string ``# nospell``. This inline directive allows users to select lines to + ommit from spell checking at their discretion. + + The ``--disable-nospell`` will disable this functionality, and ``# nospell`` + directives will be ignored by **scspell**. + Creating File IDs ----------------- diff --git a/scspell/__init__.py b/scspell/__init__.py index 5df5894..e4d59da 100644 --- a/scspell/__init__.py +++ b/scspell/__init__.py @@ -545,7 +545,8 @@ def spell_check_token( False) -def spell_check_file(filename, dicts, ignores, report_only, c_escapes): +def spell_check_file(filename, dicts, ignores, report_only, c_escapes, + disable_nospell): """Spell check a single file. :param filename: name of the file to check @@ -583,9 +584,12 @@ def spell_check_file(filename, dicts, ignores, report_only, c_escapes): else: token_regex = TOKEN_REGEX - # Remove lines with the '# nospell' directive - source_text = "".join( - [l for l in source_text.splitlines(keepends=True) if NO_SPELL not in l]) + if not disable_nospell: + # Remove lines with the '# nospell' directive + source_text = "".join( + [l for l in source_text.splitlines(keepends=True) + if NO_SPELL not in l] + ) # Search for tokens to spell-check data = source_text @@ -718,7 +722,7 @@ def find_dict_file(override_dictionary): def spell_check(source_filenames, override_dictionary=None, base_dicts=[], relative_to=None, report_only=False, c_escapes=True, - test_input=False, + disable_nospell=False, test_input=False, additional_extensions=None): """Run the interactive spell checker on the set of source_filenames. @@ -739,7 +743,8 @@ def spell_check(source_filenames, override_dictionary=None, dicts.register_extension(*extension) ignores = set() for f in source_filenames: - if not spell_check_file(f, dicts, ignores, report_only, c_escapes): + if not spell_check_file(f, dicts, ignores, report_only, c_escapes, + disable_nospell): okay = False return okay @@ -852,6 +857,10 @@ def main(): '--no-c-escapes', dest='c_escapes', action='store_false', default=True, help='treat \\label as label, for e.g. LaTeX') + spell_group.add_argument( + '--disable-nospell', action='store_true', + help='Disable the effect of "# nospell". This will spell check lines ' + 'with "# nospell" in.') dict_group.add_argument( '--override-dictionary', dest='override_filename', From 85f8c3374a1932261900133dd41ec7d4fa03c8de Mon Sep 17 00:00:00 2001 From: Finn Palmer Date: Thu, 29 Oct 2020 16:15:41 +0000 Subject: [PATCH 3/3] Pass `keepends` to `str.splitlines` as a non-keyword argument to maintain compatibility with Python 2. --- scspell/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/scspell/__init__.py b/scspell/__init__.py index e4d59da..b742e37 100644 --- a/scspell/__init__.py +++ b/scspell/__init__.py @@ -587,8 +587,7 @@ def spell_check_file(filename, dicts, ignores, report_only, c_escapes, if not disable_nospell: # Remove lines with the '# nospell' directive source_text = "".join( - [l for l in source_text.splitlines(keepends=True) - if NO_SPELL not in l] + [l for l in source_text.splitlines(True) if NO_SPELL not in l] ) # Search for tokens to spell-check