Skip to content

Commit

Permalink
Merge pull request #2 from FHaase/feature/jupyter
Browse files Browse the repository at this point in the history
jupyter and ipython code-blocks
  • Loading branch information
kataev authored Oct 29, 2018
2 parents 645dee1 + 7ca2de2 commit 35a56c8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
17 changes: 15 additions & 2 deletions flake8_rst/rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

RST_RE = re.compile(
r'(?P<before>'
r'^(?P<indent> *)\.\. (code-block|sourcecode):: (python|pycon)\n'
r'^(?P<indent> *)\.\. (code-block|sourcecode|ipython):: (python|pycon)\n'
r'((?P=indent) +:.*\n)*'
r'\n*'
r')'
r'(?P<code>(^((?P=indent) +.*)?\n)+)',
re.MULTILINE,
)

INDENT_RE = re.compile('^ +(?=[^ ])', re.MULTILINE)
TRAILING_NL_RE = re.compile(r'\n+\Z', re.MULTILINE)

Expand All @@ -18,9 +19,21 @@
'>>>', '...', # empty lines
)

ANCHOR_RE = re.compile(
r'(?P<before>'
r'(?P<code>('
r'^(?P<indent> *)>>> .*\n'
r'^(((?P=indent)(>>>|...)(.*))?\n)+)'
r'))',
re.MULTILINE,
)

EXPRESSIONS = (RST_RE, ANCHOR_RE)


def find_sourcecode(src):
for match in RST_RE.finditer(src):
matches = (match for expression in EXPRESSIONS for match in expression.finditer(src))
for match in matches:
origin_code = match.group('code')

try:
Expand Down
19 changes: 19 additions & 0 deletions tests/data/example_10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
deserunt mollit anim id est laborum.
>>> # extract 100 LDA topics, using default parameters
>>> lda = LdaModel(corpus=mm, id2word=id2word, num_topics=100, distributed=True)
Allow non code between blocks of code
and also allow wrapping with ...
>>> # extract 100 LDA topics, using default parameters
>>> lda = LdfModel(corpus=cm, id2word=id2word,
... num_topics=100, distributed=lda)
Only check for code, not results:
>>> print('This line is highlighted.')
This line is highlighted.
"""
8 changes: 8 additions & 0 deletions tests/result_py2/result_10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
('test_precisely',
[('F821', 21, 10, "undefined name 'LdaModel'", None),
('F821', 21, 26, "undefined name 'mm'", None),
('F821', 21, 38, "undefined name 'id2word'", None),
('F821', 23, 10, "undefined name 'LdfModel'", None),
('F821', 23, 26, "undefined name 'cm'", None),
('F821', 23, 38, "undefined name 'id2word'", None)],
{'logical lines': 5, 'physical lines': 6, 'tokens': 52})
8 changes: 8 additions & 0 deletions tests/result_py3/result_10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
('test_precisely',
[('F821', 21, 10, "undefined name 'LdaModel'", None),
('F821', 21, 26, "undefined name 'mm'", None),
('F821', 21, 38, "undefined name 'id2word'", None),
('F821', 23, 10, "undefined name 'LdfModel'", None),
('F821', 23, 26, "undefined name 'cm'", None),
('F821', 23, 38, "undefined name 'id2word'", None)],
{'logical lines': 5, 'physical lines': 6, 'tokens': 52})

0 comments on commit 35a56c8

Please sign in to comment.