Skip to content

Commit

Permalink
base syntax highlighting and script extension on kernel language
Browse files Browse the repository at this point in the history
Bump nbconvert dependency to 5.4 to ensure that 'language_info' is
populated after notebook execution.
  • Loading branch information
jbweston committed Oct 19, 2018
1 parent 603ef72 commit fb90706
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
15 changes: 10 additions & 5 deletions jupyter_sphinx/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ def run(self):
return [Cell('',
docutils.nodes.literal_block(
text='\n'.join(content),
language='ipython'
),
hide_code=('hide-code' in self.options),
hide_output=('hide-output' in self.options),
Expand Down Expand Up @@ -178,7 +177,6 @@ def cell_output_to_nodes(cell, data_priority, dir):
to_add.append(docutils.nodes.literal_block(
text=output['text'],
rawsource=output['text'],
language='ipython',
))
elif (
output_type == 'error'
Expand Down Expand Up @@ -226,7 +224,6 @@ def cell_output_to_nodes(cell, data_priority, dir):
to_add.append(docutils.nodes.literal_block(
text=data,
rawsource=data,
language='ipython',
))

return to_add
Expand Down Expand Up @@ -281,9 +278,11 @@ def write_notebook_output(notebook, output_dir, notebook_name):
nbformat.writes(notebook), resources,
os.path.join(output_dir, notebook_name + '.ipynb')
)
# Write a Python script too.
# Write a script too.
ext = notebook.metadata.language_info.file_extension
file_name = '.'.join((notebook_name, ext))
contents = '\n\n'.join(cell.source for cell in notebook.cells)
with open(os.path.join(output_dir, notebook_name + '.py'), 'w') as f:
with open(os.path.join(output_dir, file_name), 'w') as f:
f.write(contents)


Expand Down Expand Up @@ -351,6 +350,12 @@ def apply(self):
[nbformat.v4.new_code_cell(node.astext()) for node in nodes],
self.config.jupyter_execute_kwargs,
)

for node in nodes:
source = node.children[0]
lexer = notebook.metadata.language_info.pygments_lexer
source.attributes['language'] = lexer

# Modifies 'notebook' in-place, adding metadata specifying the
# filenames of the saved outputs.
write_notebook_output(notebook, output_dir, file_name)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
'Sphinx>=0.6',
'ipywidgets>=6.0.0',
'IPython',
'nbconvert',
'nbconvert>=5.4',
'nbformat',
],
)

0 comments on commit fb90706

Please sign in to comment.