Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix codefolding preprocessor configuration #1058

Merged
merged 4 commits into from
Aug 23, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,33 @@
"""

from nbconvert.preprocessors import Preprocessor
from traitlets import Bool, Unicode


class CodeFoldingPreprocessor(Preprocessor):
"""
:mod:`nbconvert` Preprocessor for the code_folding nbextension.

Folds codecells as displayed in the notebook.
Folds codecells as displayed in the notebook. The hidden code below the fold gets removed.

The preprocessor is installed by default. To enable codefolding with
NbConvert, you need to set the configuration parameter
`NbConvertApp.codefolding=True`.
`CodeFoldingPreprocessor.remove_folded_code=True`.
This can be done either in the `jupyter_nbconvert_config.py` file::

c.NbConvertApp.codefolding = True
c.CodeFoldingPreprocessor.remove_folded_code=True = True

or using a command line parameter when calling NbConvert::

$ jupyter nbconvert --to html --NbConvertApp.codefolding=True mynotebook.ipynb
$ jupyter nbconvert --to html --CodeFoldingPreprocessor.remove_folded_code=True mynotebook.ipynb

The folding mark can be configured using
c.CodeFoldingPreprocessor.fold_mark = '<->'

""" # noqa: E501

fold_mark = u'↔'
remove_folded_code = Bool(False, help="Remove code that was folded").tag(config=True)
fold_mark = Unicode(u'↔', help="Symbol for folded code").tag(config=True)

def fold_cell(self, cell, folded):
"""
Expand Down Expand Up @@ -54,6 +59,13 @@ def fold_cell(self, cell, folded):
fcell += l
return fcell

def preprocess(self, nb, resources):
"""Skip preprocessor if not enabled"""
if self.remove_folded_code:
return super(CodeFoldingPreprocessor, self).preprocess(nb, resources)
else:
return nb, resources

def preprocess_cell(self, cell, resources, index):
"""
Read cell metadata and remove lines marked as `folded`.
Expand All @@ -69,10 +81,11 @@ def preprocess_cell(self, cell, resources, index):
index : int
Index of the cell being processed (see base.py)
"""
dofolding = self.config.NbConvertApp.get('codefolding', False) is True
if hasattr(cell, 'source') and cell.cell_type == 'code' and dofolding:
if hasattr(cell['metadata'], 'code_folding'):
folded = cell['metadata']['code_folding']
if len(folded) > 0:
cell.source = self.fold_cell(cell.source, folded)
if self.remove_folded_code:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this check necessary, given the check in the preprocess method above?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True.

if hasattr(cell, 'source') and cell.cell_type == 'code':
if hasattr(cell['metadata'], 'code_folding'):
folded = cell['metadata']['code_folding']
if len(folded) > 0:
self.log.debug('Removing folded code in cell')
cell.source = self.fold_cell(cell.source, folded)
return cell, resources
2 changes: 1 addition & 1 deletion tests/test_preprocessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def test_preprocessor_codefolding():
" 'GR4CX32ZT'"]),
metadata={"code_folding": [1]}),
])
customconfig = Config(NbConvertApp={'codefolding': True})
customconfig = Config(CodeFoldingPreprocessor={'remove_folded_code': True})
body, resources = export_through_preprocessor(
notebook_node, CodeFoldingPreprocessor, RSTExporter, 'rst',
customconfig)
Expand Down