Skip to content

Commit

Permalink
Fix active cells in .md extension
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Oct 15, 2019
1 parent c54aae3 commit 55ad5a9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
13 changes: 7 additions & 6 deletions jupytext/cell_to_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def __init__(self, *args, **kwargs):
self.comment = ''

def html_comment(self, metadata, code='region'):
"""Protect a Markdown or Raw cell with HTML comments"""
if metadata:
region_start = ['<!-- #' + code]
if 'title' in metadata and '{' not in metadata['title']:
Expand Down Expand Up @@ -143,17 +144,17 @@ def code_to_text(self):
comment_magic(source, self.language, self.comment_magics)

options = []
self.language = self.metadata.pop('language', self.language)
if self.cell_type == 'code' and self.language:
options.append(self.language)

filtered_metadata = {key: self.metadata[key] for key in self.metadata
if key not in ['active', 'language']}

if filtered_metadata:
options.append(metadata_to_md_options(filtered_metadata))
if self.metadata:
options.append(metadata_to_md_options(self.metadata))

if self.cell_type == 'raw':
return self.html_comment(filtered_metadata, 'raw')
if self.metadata.get('active') == '':
self.metadata.pop('active')
return self.html_comment(self.metadata, 'raw')

return ['```{}'.format(' '.join(options))] + source + ['```']

Expand Down
32 changes: 28 additions & 4 deletions tests/test_active_cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@
# ---
''',
'.md': '''---
jupyter:
jupytext:
main_language: python
---
''',
'.Rmd': '''---
jupyter:
jupytext:
Expand All @@ -32,6 +38,10 @@
'.Rmd': """```{python active="ipynb,py,R,Rmd"}
# This cell is active in all extensions
```
""",
'.md': """```python active="ipynb,py,R,Rmd"
# This cell is active in all extensions
```
""",
'.R': """# + {"active": "ipynb,py,R,Rmd"}
# This cell is active in all extensions
Expand All @@ -43,7 +53,7 @@
'outputs': []}}


@pytest.mark.parametrize('ext', ['.Rmd', '.py', '.R'])
@pytest.mark.parametrize('ext', ['.Rmd', '.md', '.py', '.R'])
def test_active_all(ext, no_jupytext_version_number):
nb = jupytext.reads(HEADER[ext] + ACTIVE_ALL[ext], ext)
assert len(nb.cells) == 1
Expand All @@ -59,6 +69,11 @@ def test_active_all(ext, no_jupytext_version_number):
# This cell is active only in ipynb
%matplotlib inline
```
""",
'.md': """```python active="ipynb"
# This cell is active only in ipynb
%matplotlib inline
```
""",
'.R': """# + {"active": "ipynb"}
# # This cell is active only in ipynb
Expand All @@ -73,7 +88,7 @@ def test_active_all(ext, no_jupytext_version_number):


@skip_if_dict_is_not_ordered
@pytest.mark.parametrize('ext', ['.Rmd', '.py', '.R'])
@pytest.mark.parametrize('ext', ['.Rmd', '.md', '.py', '.R'])
def test_active_ipynb(ext, no_jupytext_version_number):
nb = jupytext.reads(HEADER[ext] + ACTIVE_IPYNB[ext], ext)
assert len(nb.cells) == 1
Expand All @@ -89,6 +104,11 @@ def test_active_ipynb(ext, no_jupytext_version_number):
# This cell is active only in ipynb and Rmd
# %matplotlib inline
```
""",
'.md': """```python tags=["active-ipynb-Rmd"]
# This cell is active only in ipynb and Rmd
%matplotlib inline
```
""",
'.R': """# + {"tags": ["active-ipynb-Rmd"]}
# # This cell is active only in ipynb and Rmd
Expand All @@ -103,7 +123,7 @@ def test_active_ipynb(ext, no_jupytext_version_number):


@skip_if_dict_is_not_ordered
@pytest.mark.parametrize('ext', ['.Rmd', '.py', '.R'])
@pytest.mark.parametrize('ext', ['.Rmd', '.md', '.py', '.R'])
def test_active_ipynb_rmd_using_tags(ext, no_jupytext_version_number):
nb = jupytext.reads(HEADER[ext] + ACTIVE_IPYNB_RMD_USING_TAG[ext], ext)
assert len(nb.cells) == 1
Expand Down Expand Up @@ -137,6 +157,10 @@ def test_active_ipynb_rspin(no_jupytext_version_number):
'.Rmd': """```{python active="ipynb,py", eval=FALSE}
# This cell is active in py and ipynb extensions
```
""",
'.md': """```python active="ipynb,py"
# This cell is active in py and ipynb extensions
```
""",
'.R': """# + {"active": "ipynb,py"}
# # This cell is active in py and ipynb extensions
Expand All @@ -150,7 +174,7 @@ def test_active_ipynb_rspin(no_jupytext_version_number):


@skip_if_dict_is_not_ordered
@pytest.mark.parametrize('ext', ['.Rmd', '.py', '.R'])
@pytest.mark.parametrize('ext', ['.Rmd', '.md', '.py', '.R'])
def test_active_py_ipynb(ext, no_jupytext_version_number):
nb = jupytext.reads(HEADER[ext] + ACTIVE_PY_IPYNB[ext], ext)
assert len(nb.cells) == 1
Expand Down

0 comments on commit 55ad5a9

Please sign in to comment.