Skip to content

Commit

Permalink
Merge pull request #22 from mwouts/v0.4.1
Browse files Browse the repository at this point in the history
V0.4.1
  • Loading branch information
mwouts authored Jul 19, 2018
2 parents 31c4203 + f50685f commit 67d3745
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 10 deletions.
8 changes: 8 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ Release History
dev
+++

0.4.1 (2018-07-20)
+++++++++++++++++++

**BugFixes**

- Indented python code will not start a new cell #20
- Fixed parsing of Rmd cell metadata #21

0.4.0 (2018-07-18)
+++++++++++++++++++

Expand Down
5 changes: 4 additions & 1 deletion nbrmd/cell_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import ast
import json
import re

_boolean_options_dictionary = [('hide_input', 'echo', True),
('hide_output', 'include', True)]
Expand Down Expand Up @@ -168,12 +169,14 @@ def parse_rmd_options(line):


def rmd_options_to_metadata(options):
options = options.split(' ', 1)
options = re.split('\s|,', options, 1)
if len(options) == 1:
language = options[0]
chunk_options = []
else:
language, others = options
language = language.rstrip(' ,')
others = others.lstrip(' ,')
chunk_options = parse_rmd_options(others)

language = 'R' if language == 'r' else language
Expand Down
35 changes: 27 additions & 8 deletions nbrmd/cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,26 @@ def parse_code_options(line, ext):
line).group(2))


def next_code_is_indented(lines):
for next in lines:
if next.startswith('#'):
continue
if _blank.match(next):
continue
return next.startswith(' ')

return False


def no_code_before_next_blank_line(lines):
for next in lines:
if next.startswith('#'):
continue
return _blank.match(next)

return True


def code_to_cell(self, lines, parse_opt):
# Parse options
if parse_opt:
Expand Down Expand Up @@ -156,22 +176,21 @@ def code_to_cell(self, lines, parse_opt):
if prev_blank:
if _blank.match(line):
# Two blank lines => end of cell
# (py: unless next code is indented)
# Two blank lines at the end == empty code cell

if self.ext == '.py':
if next_code_is_indented(lines[pos:]):
continue

return new_code_cell(
source='\n'.join(lines[parse_opt:(pos - 1)]),
metadata=metadata), min(pos + 1, len(lines) - 1)

# are all the lines from here to next blank
# escaped with the prefix?
if self.prefix == '#':
found_code = False
for next in lines[pos:]:
if next.startswith('#'):
continue
found_code = not _blank.match(next)
break

if not found_code:
if no_code_before_next_blank_line(lines[pos:]):
return new_code_cell(
source='\n'.join(lines[parse_opt:(pos - 1)]),
metadata=metadata), pos
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name='nbrmd',
version='0.4.0',
version='0.4.1',
author='Marc Wouts',
author_email='marc.wouts@gmail.com',
description='Jupyter from/to R markdown notebooks',
Expand Down
31 changes: 31 additions & 0 deletions tests/test_read_simple_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,37 @@ def h(y):
compare(py, py2)


def test_read_non_pep8_file(py="""# ---
# title: Non-pep8 file
# ---
# This file is non-pep8 as the function below has
# two consecutive blank lines in its body
def f(x):
return x+1
"""):
nb = nbrmd.reads(py, ext='.py')

assert len(nb.cells) == 3
assert nb.cells[0].cell_type == 'raw'
assert nb.cells[0].source == '---\ntitle: Non-pep8 file\n---'
assert nb.cells[1].cell_type == 'markdown'
assert nb.cells[1].source == 'This file is non-pep8 as ' \
'the function below has\n' \
'two consecutive blank lines ' \
'in its body'
assert nb.cells[2].cell_type == 'code'
compare(nb.cells[2].source,
'def f(x):\n\n\n'
' return x+1')

py2 = nbrmd.writes(nb, ext='.py')
compare(py, py2)


def test_no_space_after_code(py=u"""# -*- coding: utf-8 -*-
# Markdown cell
Expand Down
57 changes: 57 additions & 0 deletions tests/test_read_simple_rmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import nbrmd
import re
from testfixtures import compare


def test_read_mostly_py_rmd_file(rmd="""# ---
# title: Simple file
# ---
```{python, echo=TRUE}
x = np.arange(0, 2*math.pi, eps)
```
```{python, echo=TRUE}
x = np.arange(0,1,eps)
y = np.abs(x)-.5
```
```{r}
ls()
```
```{r, results='asis'}
cat(stringi::stri_rand_lipsum(3), sep='\n\n')
```
"""):
nb = nbrmd.reads(rmd, ext='.Rmd')
assert nb.metadata == {'main_language': 'python'}
assert nb.cells == [{'cell_type': 'markdown',
'source': '# ---\n# title: Simple file\n# ---',
'metadata': {}},
{'cell_type': 'code',
'metadata': {'hide_input': False},
'execution_count': None,
'source': 'x = np.arange(0, 2*math.pi, eps)',
'outputs': []},
{'cell_type': 'code',
'metadata': {'hide_input': False},
'execution_count': None,
'source': 'x = np.arange(0,1,eps)\ny = np.abs(x)-.5',
'outputs': []},
{'cell_type': 'code',
'metadata': {},
'execution_count': None,
'source': '%%R\nls()',
'outputs': []},
{'cell_type': 'code',
'metadata': {'results': "'asis'"},
'execution_count': None,
'source': "%%R\ncat(stringi::"
"stri_rand_lipsum(3), sep='\n\n')",
'outputs': []}]

rmd2 = nbrmd.writes(nb, ext='.Rmd')
rmd2 = re.sub(r'```{r ', '```{r, ', rmd2)
rmd2 = re.sub(r'```{python ', '```{python, ', rmd2)
compare(rmd, rmd2)

0 comments on commit 67d3745

Please sign in to comment.