@@ -39,6 +39,20 @@ class LintWork:
3939 fixed : bool
4040
4141
42+ CHECKS = {}
43+
44+
45+ def check (name : str ):
46+ """Decorator to register a lint check function."""
47+
48+ def decorator (func ):
49+ CHECKS [name ] = func
50+ return func
51+
52+ return decorator
53+
54+
55+ @check ("self" )
4256def find_self_links (work : LintWork ) -> Iterable [LintIssue ]:
4357 """Find :mod: references that link to modules declared in the same section."""
4458 for section in work .doctree .findall (nodes .section ):
@@ -73,6 +87,7 @@ def find_self_links(work: LintWork) -> Iterable[LintIssue]:
7387 )
7488
7589
90+ @check ("paradup" )
7691def find_duplicate_refs_in_paragraph (work : LintWork ) -> Iterable [LintIssue ]:
7792 """Find references that appear more than once in the same paragraph."""
7893 for para in work .doctree .findall (nodes .paragraph ):
@@ -99,7 +114,7 @@ class LintResult:
99114 fixed : bool
100115
101116
102- def lint_content (content : str , fix : bool ) -> LintResult :
117+ def lint_content (content : str , fix : bool , checks : set [ str ] ) -> LintResult :
103118 doctree = parse_rst_file (content )
104119 work = LintWork (
105120 content_lines = content .splitlines (keepends = True ),
@@ -109,8 +124,8 @@ def lint_content(content: str, fix: bool) -> LintResult:
109124 )
110125
111126 issues = []
112- issues . extend ( find_self_links ( work ))
113- # issues.extend(find_duplicate_refs_in_paragraph (work))
127+ for check_name in checks :
128+ issues .extend (CHECKS [ check_name ] (work ))
114129
115130 result = LintResult (
116131 content = "" .join (work .content_lines ),
@@ -121,35 +136,49 @@ def lint_content(content: str, fix: bool) -> LintResult:
121136 return result
122137
123138
124- def lint_file (filepath : str , fix : bool ) -> list [LintIssue ]:
139+ def lint_file (filepath : str , fix : bool , checks : set [ str ] ) -> list [LintIssue ]:
125140 """Lint a single RST file.
126141
127142 Returns a list of LintIssue objects.
128143 """
129144 path = Path (filepath )
130145 content = path .read_text ()
131- result = lint_content (content , fix )
146+ result = lint_content (content , fix , checks )
132147 if fix and result .fixed :
133148 path .write_text (result .content )
134149 return result .issues
135150
136151
137152def main (argv : list [str ]) -> int :
138153 parser = argparse .ArgumentParser ()
154+ parser .add_argument (
155+ "--check" ,
156+ help = f"comma-separated checks to run ({ ', ' .join (CHECKS )} , all)" ,
157+ default = "all" ,
158+ )
139159 parser .add_argument ("--fix" , help = "Fix the issues in place" , action = "store_true" )
140160 parser .add_argument ("files" , nargs = "+" , help = "RST files to lint" )
141161 args = parser .parse_args (argv )
142162
143- exit_code = 0
163+ checks = set (args .check .split ("," ))
164+ unknown = checks - set (CHECKS .keys ()) - {"all" }
165+ if unknown :
166+ print (f"Unknown checks: { ', ' .join (unknown )} " , file = sys .stderr )
167+ return 2
168+ if "all" in checks :
169+ checks = set (CHECKS .keys ())
170+
171+ issues = 0
144172 for filepath in args .files :
145173 # This runs Sphinx on each file separately, which seems slow, but is
146174 # faster than running it once on all the files.
147- for issue in lint_file (filepath , args .fix ):
175+ for issue in lint_file (filepath , args .fix , checks ):
148176 fixed_suffix = " (fixed)" if issue .fixed else ""
149177 print (f"{ filepath } :{ issue .line } : { issue .message } { fixed_suffix } " )
150- exit_code = 1
178+ issues + = 1
151179
152- return exit_code
180+ print (f"Checked { len (args .files )} file(s), found { issues } issue(s)." )
181+ return issues > 0
153182
154183
155184if __name__ == "__main__" :
0 commit comments