Skip to content

Commit f9f45e0

Browse files
authored
Merge pull request #1058 from juhasch/fix/cf_preprocessor
Fix codefolding preprocessor configuration
2 parents 4c4e4c6 + fe90a66 commit f9f45e0

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

src/jupyter_contrib_nbextensions/nbconvert_support/pre_codefolding.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,33 @@
55
"""
66

77
from nbconvert.preprocessors import Preprocessor
8+
from traitlets import Bool, Unicode
89

910

1011
class CodeFoldingPreprocessor(Preprocessor):
1112
"""
1213
:mod:`nbconvert` Preprocessor for the code_folding nbextension.
1314
14-
Folds codecells as displayed in the notebook.
15+
Folds codecells as displayed in the notebook. The hidden code below the fold gets removed.
1516
1617
The preprocessor is installed by default. To enable codefolding with
1718
NbConvert, you need to set the configuration parameter
18-
`NbConvertApp.codefolding=True`.
19+
`CodeFoldingPreprocessor.remove_folded_code=True`.
1920
This can be done either in the `jupyter_nbconvert_config.py` file::
2021
21-
c.NbConvertApp.codefolding = True
22+
c.CodeFoldingPreprocessor.remove_folded_code=True = True
2223
2324
or using a command line parameter when calling NbConvert::
2425
25-
$ jupyter nbconvert --to html --NbConvertApp.codefolding=True mynotebook.ipynb
26+
$ jupyter nbconvert --to html --CodeFoldingPreprocessor.remove_folded_code=True mynotebook.ipynb
27+
28+
The folding mark can be configured using
29+
c.CodeFoldingPreprocessor.fold_mark = '<->'
2630
2731
""" # noqa: E501
2832

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

3136
def fold_cell(self, cell, folded):
3237
"""
@@ -54,10 +59,16 @@ def fold_cell(self, cell, folded):
5459
fcell += l
5560
return fcell
5661

62+
def preprocess(self, nb, resources):
63+
"""Skip preprocessor if not enabled"""
64+
if self.remove_folded_code:
65+
return super(CodeFoldingPreprocessor, self).preprocess(nb, resources)
66+
else:
67+
return nb, resources
68+
5769
def preprocess_cell(self, cell, resources, index):
5870
"""
5971
Read cell metadata and remove lines marked as `folded`.
60-
Requires configuration parameter 'NbConvertApp.codefolding = True'
6172
6273
Parameters
6374
----------
@@ -69,10 +80,10 @@ def preprocess_cell(self, cell, resources, index):
6980
index : int
7081
Index of the cell being processed (see base.py)
7182
"""
72-
dofolding = self.config.NbConvertApp.get('codefolding', False) is True
73-
if hasattr(cell, 'source') and cell.cell_type == 'code' and dofolding:
83+
if hasattr(cell, 'source') and cell.cell_type == 'code':
7484
if hasattr(cell['metadata'], 'code_folding'):
7585
folded = cell['metadata']['code_folding']
7686
if len(folded) > 0:
87+
self.log.debug('Removing folded code in cell')
7788
cell.source = self.fold_cell(cell.source, folded)
7889
return cell, resources

tests/test_preprocessors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def test_preprocessor_codefolding():
5959
" 'GR4CX32ZT'"]),
6060
metadata={"code_folding": [1]}),
6161
])
62-
customconfig = Config(NbConvertApp={'codefolding': True})
62+
customconfig = Config(CodeFoldingPreprocessor={'remove_folded_code': True})
6363
body, resources = export_through_preprocessor(
6464
notebook_node, CodeFoldingPreprocessor, RSTExporter, 'rst',
6565
customconfig)

0 commit comments

Comments
 (0)