-
Notifications
You must be signed in to change notification settings - Fork 131
/
nbsphinx.py
851 lines (690 loc) · 27.4 KB
/
nbsphinx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
# Copyright (c) 2015-2016 Matthias Geier
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""Jupyter Notebook Tools for Sphinx.
http://nbsphinx.rtfd.org/
"""
__version__ = '0.2.5'
import copy
import docutils
from docutils.parsers import rst
import jinja2
import nbconvert
import nbformat
import os
import sphinx
try:
from urllib.parse import unquote # Python 3.x
except ImportError:
from urllib2 import unquote # Python 2.x
_ipynbversion = 4
RST_TEMPLATE = """
{% extends 'rst.tpl' %}
{% macro insert_empty_lines(text) %}
{%- set before, after = text | get_empty_lines %}
{%- if before %}
:empty-lines-before: {{ before }}
{%- endif %}
{%- if after %}
:empty-lines-after: {{ after }}
{%- endif %}
{%- endmacro %}
{% block any_cell %}
{%- if cell.metadata.nbsphinx != 'hidden' %}
{{ super() }}
{% endif %}
{%- endblock any_cell %}
{% block input -%}
.. nbinput:: {% if nb.metadata.language_info -%}
{{ nb.metadata.language_info.pygments_lexer }}
{%- endif -%}
{{ insert_empty_lines(cell.source) }}
{%- if cell.execution_count %}
:execution-count: {{ cell.execution_count }}
{%- endif %}
{%- if not cell.outputs %}
:no-output:
{%- endif %}
{%- if cell.source.strip() %}
{{ cell.source.strip('\n') | indent }}
{%- endif %}
{% endblock input %}
{% block nboutput %}
{%- if output.output_type == 'stream' %}
{%- set datatype = 'ansi' %}
{%- set outputdata = output.text[:-1] %}{# trailing \n is stripped #}
{%- elif output.output_type == 'error' %}
{%- set datatype = 'ansi' %}
{%- set outputdata = '\n'.join(output.traceback) %}
{%- else %}
{%- set datatype = (output.data | filter_data_type)[0] %}
{%- set outputdata = output.data[datatype] %}
{%- endif -%}
.. nboutput::
{%- if datatype == 'text/plain' %}{# nothing #}
{%- else %} rst
{%- endif %}
{%- if output.output_type == 'execute_result' and cell.execution_count %}
:execution-count: {{ cell.execution_count }}
{%- endif %}
{%- if output != cell.outputs[-1] %}
:more-to-come:
{%- endif %}
{%- if output.name == 'stderr' %}
:class: stderr
{%- endif %}
{%- if datatype == 'text/plain' -%}
{{ insert_empty_lines(outputdata) }}
{{ outputdata.strip(\n) | indent }}
{%- elif datatype in ['image/svg+xml', 'image/png', 'image/jpeg', 'application/pdf'] %}
.. image:: {{ output.metadata.filenames[datatype].rsplit('.', 1)[0] + '.*' | posix_path }}
{%- elif datatype in ['text/markdown'] %}
{{ output.data['text/markdown'] | markdown2rst | indent }}
{%- elif datatype in ['text/latex'] %}
.. math::
{{ output.data['text/latex'] | strip_dollars | indent | indent }}
{%- elif datatype == 'text/html' %}
.. raw:: html
{{ output.data['text/html'] | indent | indent }}
{%- elif datatype == 'ansi' %}
.. rst-class:: highlight
.. raw:: html
<pre>
{{ outputdata | ansi2html | indent | indent }}
</pre>
.. raw:: latex
% This comment is needed to force a line break for adjacent ANSI cells
\\begin{OriginalVerbatim}[commandchars=\\\\\\{\\}]
{{ outputdata | ansi2latex | indent | indent }}
\\end{OriginalVerbatim}
{% else %}
WARNING! Data type not implemented: {{ datatype }}
{%- endif %}
{% endblock nboutput %}
{% block execute_result %}{{ self.nboutput() }}{% endblock execute_result %}
{% block display_data %}{{ self.nboutput() }}{% endblock display_data %}
{% block stream %}{{ self.nboutput() }}{% endblock stream %}
{% block error %}{{ self.nboutput() }}{% endblock error %}
{% block markdowncell %}
{%- if 'nbsphinx-toctree' in cell.metadata %}
{{ cell | extract_toctree }}
{%- else %}
{{ super() }}
{% endif %}
{% endblock markdowncell %}
{% block rawcell %}
{%- set raw_mimetype = cell.metadata.get('raw_mimetype', '').lower() %}
{%- if raw_mimetype == '' %}
.. raw:: html
{{ cell.source | indent }}
.. raw:: latex
{{ cell.source | indent }}
{%- elif raw_mimetype == 'text/html' %}
.. raw:: html
{{ cell.source | indent }}
{%- elif raw_mimetype == 'text/latex' %}
.. raw:: latex
{{ cell.source | indent }}
{%- elif raw_mimetype == 'text/markdown' %}
{{ cell.source | markdown2rst }}
{%- elif raw_mimetype == 'text/restructuredtext' %}
{{ cell.source }}
{% endif %}
{% endblock rawcell %}
"""
LATEX_PREAMBLE = r"""
% Notebook prompt colors
\definecolor{nbsphinxin}{rgb}{0.0, 0.0, 0.5}
\definecolor{nbsphinxout}{rgb}{0.545, 0.0, 0.0}
% ANSI colors for traceback highlighting
\definecolor{red}{rgb}{.6,0,0}
\definecolor{green}{rgb}{0,.65,0}
\definecolor{brown}{rgb}{0.6,0.6,0}
\definecolor{blue}{rgb}{0,.145,.698}
\definecolor{purple}{rgb}{.698,.145,.698}
\definecolor{cyan}{rgb}{0,.698,.698}
\definecolor{lightgray}{gray}{0.5}
\definecolor{darkgray}{gray}{0.25}
\definecolor{lightred}{rgb}{1.0,0.39,0.28}
\definecolor{lightgreen}{rgb}{0.48,0.99,0.0}
\definecolor{lightblue}{rgb}{0.53,0.81,0.92}
\definecolor{lightpurple}{rgb}{0.87,0.63,0.87}
\definecolor{lightcyan}{rgb}{0.5,1.0,0.83}
"""
CSS_STRING = """
/* CSS for nbsphinx extension */
/* remove conflicting styling from Sphinx themes */
div.nbinput,
div.nbinput > div,
div.nbinput div[class^=highlight],
div.nbinput div[class^=highlight] pre,
div.nboutput,
div.nboutput > div,
div.nboutput div[class^=highlight],
div.nboutput div[class^=highlight] pre {
background: none;
border: none;
padding: 0 0;
margin: 0;
box-shadow: none;
}
/* input/output containers */
div.nbinput,
div.nboutput {
display: -webkit-flex;
display: flex;
align-items: flex-start;
margin: 0;
}
/* input container */
div.nbinput {
padding-top: 5px;
}
/* last container */
div.nblast {
padding-bottom: 5px;
}
/* input prompt */
div.nbinput > :first-child pre {
color: navy;
}
/* output prompt */
div.nboutput > :first-child pre {
color: darkred;
}
/* all prompts */
div.nbinput > :first-child[class^=highlight],
div.nboutput > :first-child[class^=highlight],
div.nboutput > :first-child {
min-width: 11ex;
padding-top: 0.4em;
padding-right: 0.4em;
text-align: right;
flex: 0;
}
/* input/output area */
div.nbinput > :nth-child(2)[class^=highlight],
div.nboutput > :nth-child(2),
div.nboutput > :nth-child(2)[class^=highlight] {
padding: 0.4em;
-webkit-flex: 1;
flex: 1;
}
/* input area */
div.nbinput > :nth-child(2)[class^=highlight] {
border: 1px solid #cfcfcf;
border-radius: 2px;
background: #f7f7f7;
}
/* override MathJax center alignment in output cells */
div.nboutput div[class*=MathJax] {
text-align: left !important;
}
/* override sphinx.ext.pngmath center alignment in output cells */
div.nboutput div.math p {
text-align: left;
}
/* standard error */
div.nboutput > :nth-child(2).stderr {
background: #fdd;
}
/* ANSI colors */
.ansiblack { color: black; }
.ansired { color: darkred; }
.ansigreen { color: darkgreen; }
.ansiyellow { color: #c4a000; }
.ansiblue { color: darkblue; }
.ansipurple { color: darkviolet; }
.ansicyan { color: steelblue; }
/* See https://github.com/jupyter/nbconvert/issues/174 */
.ansigray { color: gray; } /* nbconvert CSS */
.ansigrey { color: gray; } /* nbconvert HTML output */
.ansibgblack { background-color: black; }
.ansibgred { background-color: red; }
.ansibggreen { background-color: green; }
.ansibgyellow { background-color: yellow; }
.ansibgblue { background-color: blue; }
.ansibgpurple { background-color: magenta; }
.ansibgcyan { background-color: cyan; }
.ansibggray { background-color: gray; }
.ansibold { font-weight: bold; }
"""
class Exporter(nbconvert.RSTExporter):
"""Convert Jupyter notebooks to reStructuredText.
Uses nbconvert to convert Jupyter notebooks to a reStructuredText
string with custom reST directives for input and output cells.
Notebooks without output cells are automatically executed before
conversion.
"""
def __init__(self, allow_errors=False):
"""Initialize the Exporter."""
self._allow_errors = allow_errors
loader = jinja2.DictLoader({'nbsphinx-rst.tpl': RST_TEMPLATE})
super(Exporter, self).__init__(
template_file='nbsphinx-rst', extra_loaders=[loader],
filters={
'get_empty_lines': _get_empty_lines,
'extract_toctree': _extract_toctree,
})
def from_notebook_node(self, nb, resources=None, **kw):
nb = copy.deepcopy(nb)
if resources is None:
resources = {}
else:
resources = copy.deepcopy(resources)
nbsphinx_metadata = nb.metadata.get('nbsphinx', {})
# Execute notebook only if there are code cells and no outputs:
if (any(c.source for c in nb.cells if c.cell_type == 'code') and
not any(c.outputs for c in nb.cells if 'outputs' in c)):
allow_errors = (self._allow_errors or
nbsphinx_metadata.get('allow_errors', False))
pp = nbconvert.preprocessors.ExecutePreprocessor(
allow_errors=allow_errors)
nb, resources = pp.preprocess(nb, resources)
# Call into RSTExporter
rststr, resources = super(Exporter, self).from_notebook_node(
nb, resources, **kw)
orphan = nbsphinx_metadata.get('orphan', False)
if orphan is True:
rststr = ':orphan:\n\n' + rststr
elif orphan is not False:
raise NotebookError('invalid orphan option: {!r}'.format(orphan))
return rststr, resources
class NotebookParser(rst.Parser):
"""Sphinx source parser for Jupyter notebooks.
Uses nbsphinx.Exporter to convert notebook content to a
reStructuredText string, which is then parsed by Sphinx's built-in
reST parser.
"""
def get_transforms(self):
"""List of transforms for documents parsed by this parser."""
return rst.Parser.get_transforms(self) + [ProcessLocalLinks,
CreateSectionLabels]
def parse(self, inputstring, document):
"""Parse `inputstring`, write results to `document`."""
nb = nbformat.reads(inputstring, as_version=_ipynbversion)
env = document.settings.env
srcdir = os.path.dirname(env.doc2path(env.docname))
auxdir = os.path.join(env.doctreedir, 'nbsphinx')
sphinx.util.ensuredir(auxdir)
resources = {}
# Working directory for ExecutePreprocessor
resources['metadata'] = {'path': srcdir}
# Sphinx doesn't accept absolute paths in images etc.
resources['output_files_dir'] = os.path.relpath(auxdir, srcdir)
resources['unique_key'] = env.docname.replace('/', '_')
exporter = Exporter(allow_errors=env.config.nbsphinx_allow_errors)
try:
rststring, resources = exporter.from_notebook_node(nb, resources)
except NotebookError as e:
env.warn(env.docname, str(e))
return # document is unchanged (i.e. empty)
# Create additional output files (figures etc.),
# see nbconvert.writers.FilesWriter.write()
for filename, data in resources.get('outputs', {}).items():
dest = os.path.normpath(os.path.join(srcdir, filename))
with open(dest, 'wb') as f:
f.write(data)
rst.Parser.parse(self, rststring, document)
class NotebookError(Exception):
"""Error during notebook parsing."""
class CodeNode(docutils.nodes.Element):
"""A custom node that contains a literal_block node."""
@classmethod
def create(cls, text, language='none'):
"""Create a new CodeNode containing a literal_block node.
Apparently, this cannot be done in CodeNode.__init__(), see:
https://groups.google.com/forum/#!topic/sphinx-dev/0chv7BsYuW0
"""
node = docutils.nodes.literal_block(text, text, language=language)
return cls(text, node)
# See http://docutils.sourceforge.net/docs/howto/rst-directives.html
class NbInput(rst.Directive):
"""A notebook input cell with prompt and code area."""
required_arguments = 0
optional_arguments = 1 # lexer name
final_argument_whitespace = False
option_spec = {
'execution-count': rst.directives.positive_int,
'empty-lines-before': rst.directives.nonnegative_int,
'empty-lines-after': rst.directives.nonnegative_int,
'no-output': rst.directives.flag,
}
has_content = True
def run(self):
"""This is called by the reST parser."""
execution_count = self.options.get('execution-count')
classes = ['nbinput']
if 'no-output' in self.options:
classes.append('nblast')
container = docutils.nodes.container(classes=classes)
# Input prompt
text = 'In [{}]:'.format(execution_count if execution_count else ' ')
container += CodeNode.create(text)
latex_prompt = text + ' '
# Input code area
text = '\n'.join(self.content.data)
node = CodeNode.create(
text, language=self.arguments[0] if self.arguments else 'none')
_set_emtpy_lines(node, self.options)
node.attributes['latex_prompt'] = latex_prompt
container += node
return [container]
class NbOutput(rst.Directive):
"""A notebook output cell with optional prompt."""
required_arguments = 0
optional_arguments = 1 # 'rst' or nothing (which means literal text)
final_argument_whitespace = False
option_spec = {
'execution-count': rst.directives.positive_int,
'more-to-come': rst.directives.flag,
'empty-lines-before': rst.directives.nonnegative_int,
'empty-lines-after': rst.directives.nonnegative_int,
'class': rst.directives.unchanged,
}
has_content = True
def run(self):
"""This is called by the reST parser."""
outputtype = self.arguments[0] if self.arguments else ''
execution_count = self.options.get('execution-count')
classes = ['nboutput']
if 'more-to-come' not in self.options:
classes.append('nblast')
container = docutils.nodes.container(classes=classes)
# Optional output prompt
if execution_count:
text = 'Out[{}]:'.format(execution_count)
container += CodeNode.create(text)
latex_prompt = text + ' '
else:
container += rst.nodes.container() # empty container for HTML
latex_prompt = ''
# Output area
if outputtype == 'rst':
classes = [self.options.get('class', '')]
output_area = docutils.nodes.container(classes=classes)
self.state.nested_parse(self.content, self.content_offset,
output_area)
container += output_area
else:
text = '\n'.join(self.content.data)
node = CodeNode.create(text)
_set_emtpy_lines(node, self.options)
node.attributes['latex_prompt'] = latex_prompt
container += node
return [container]
def _extract_toctree(cell):
"""Extract links from Markdown cell and create toctree."""
lines = ['.. toctree::']
options = cell.metadata['nbsphinx-toctree']
try:
for option, value in options.items():
if value is True:
lines.append(':{}:'.format(option))
elif value is False:
pass
else:
lines.append(':{}: {}'.format(option, value))
except AttributeError:
raise NotebookError(
'invalid nbsphinx-toctree option: {!r}'.format(options))
text = nbconvert.filters.markdown2rst(cell.source)
settings = docutils.frontend.OptionParser(
components=(rst.Parser,)).get_default_values()
toctree_node = docutils.utils.new_document('extract_toctree', settings)
parser = rst.Parser()
parser.parse(text, toctree_node)
if 'caption' not in options:
for sec in toctree_node.traverse(docutils.nodes.section):
assert sec.children
assert isinstance(sec.children[0], docutils.nodes.title)
title = sec.children[0].astext()
lines.append(':caption: ' + title)
break
lines.append('') # empty line
for ref in toctree_node.traverse(docutils.nodes.reference):
lines.append(ref.astext().replace('\n', '') +
' <' + unquote(ref.get('refuri')) + '>')
return '\n '.join(lines)
def _get_empty_lines(text):
"""Get number of empty lines before and after code."""
before = len(text) - len(text.lstrip('\n'))
after = len(text) - len(text.strip('\n')) - before
return before, after
def _set_emtpy_lines(node, options):
"""Set "empty lines" attributes on a CodeNode.
See http://stackoverflow.com/q/34050044/500098.
"""
for attr in 'empty-lines-before', 'empty-lines-after':
value = options.get(attr, 0)
if value:
node.attributes[attr] = value
class ProcessLocalLinks(docutils.transforms.Transform):
"""Process links to local files.
Marks local files to be copied to the HTML output directory and
turns links to local notebooks into ``:doc:``/``:ref:`` links.
Links to subsections are possible with ``...#Subsection-Title``.
These links use the labels created by CreateSectionLabels.
Links to subsections use ``:ref:``, links to whole notebooks use
``:doc:``. Latter can be useful if you have an ``index.rst`` but
also want a distinct ``index.ipynb`` for use with Jupyter.
In this case you can use such a link in a notebook::
[Back to main page](index.ipynb)
In Jupyter, this will create a "normal" link to ``index.ipynb``, but
in the files generated by Sphinx, this will become a link to the
main page created from ``index.rst``.
"""
default_priority = 400 # Should probably be adjusted?
def apply(self):
env = self.document.settings.env
for node in self.document.traverse(docutils.nodes.reference):
uri = node.get('refuri', '')
if not uri:
continue # No URI (e.g. named reference)
elif '://' in uri:
continue # Not a local link
elif uri.startswith('#') or uri.startswith('mailto:'):
continue # Nothing to be done
for suffix in env.config.source_suffix:
if uri.lower().endswith(suffix.lower()):
target = uri[:-len(suffix)]
break
else:
target = ''
if target:
target_ext = ''
reftype = 'doc'
refdomain = None
elif '.ipynb#' in uri.lower():
idx = uri.lower().find('.ipynb#')
target = uri[:idx]
target_ext = uri[idx:]
reftype = 'ref'
refdomain = 'std'
else:
file = os.path.normpath(
os.path.join(os.path.dirname(env.docname), uri))
if not os.path.isfile(os.path.join(env.srcdir, file)):
env.app.warn('file not found: {!r}'.format(file),
env.doc2path(env.docname))
continue # Link is ignored
elif file.startswith('..'):
env.app.warn(
'link outside of source directory: {!r}'.format(file),
env.doc2path(env.docname))
continue # Link is ignored
if not hasattr(env, 'nbsphinx_files'):
env.nbsphinx_files = {}
env.nbsphinx_files.setdefault(env.docname, []).append(file)
continue # We're done here
target_docname = os.path.normpath(
os.path.join(os.path.dirname(env.docname), target))
if target_docname in env.found_docs:
if target_ext:
target = target_docname + target_ext
target = target.lower()
linktext = node.astext()
xref = sphinx.addnodes.pending_xref(
reftype=reftype, reftarget=target, refdomain=refdomain,
refwarn=True, refexplicit=True, refdoc=env.docname)
xref += docutils.nodes.Text(linktext, linktext)
node.replace_self(xref)
class CreateSectionLabels(docutils.transforms.Transform):
"""Make labels for each notebook and each section thereof.
These labels are referenced in ProcessLocalLinks.
Note: Sphinx lower-cases the HTML section IDs, Jupyter doesn't.
"""
default_priority = 250 # Before references.PropagateTargets (260)
def apply(self):
env = self.document.settings.env
i_still_have_to_create_the_notebook_label = True
for section in self.document.traverse(docutils.nodes.section):
assert section.children
assert isinstance(section.children[0], docutils.nodes.title)
title = section.children[0].astext()
link_id = title.replace(' ', '-')
section['ids'] = [link_id]
label = env.docname + '.ipynb#' + link_id
label = label.lower()
env.domaindata['std']['labels'][label] = (
env.docname, link_id, title)
env.domaindata['std']['anonlabels'][label] = (
env.docname, link_id)
# Create a label for the whole notebook using the first section:
if i_still_have_to_create_the_notebook_label:
label = env.docname.lower() + '.ipynb'
env.domaindata['std']['labels'][label] = (
env.docname, '', title)
env.domaindata['std']['anonlabels'][label] = (
env.docname, '')
i_still_have_to_create_the_notebook_label = False
def builder_inited(app):
"""Add color definitions to LaTeX preamble."""
latex_elements = app.builder.config.latex_elements
latex_elements['preamble'] = '\n'.join([
LATEX_PREAMBLE,
latex_elements.get('preamble', ''),
])
def html_page_context(app, pagename, templatename, context, doctree):
"""Add CSS string to HTML page."""
body = context.get('body', '')
if body:
style = '\n<style>' + CSS_STRING + '</style>\n'
context['body'] = style + body
def html_collect_pages(app):
"""This event handler is abused to copy local files around."""
files = set()
for file_list in getattr(app.env, 'nbsphinx_files', {}).values():
files.update(file_list)
for file in app.status_iterator(files, 'copying linked files... ',
sphinx.util.console.brown, len(files)):
target = os.path.join(app.builder.outdir, file)
sphinx.util.ensuredir(os.path.dirname(target))
try:
sphinx.util.copyfile(os.path.join(app.env.srcdir, file), target)
except OSError as err:
app.warn('cannot copy local file {!r}: {}'.format(file, err))
return [] # No new HTML pages are created
def env_purge_doc(app, env, docname):
"""Remove list of local files for a given document."""
try:
del env.nbsphinx_files[docname]
except (AttributeError, KeyError):
pass
def depart_code_html(self, node):
"""Add empty lines before and after the code."""
text = self.body[-1]
text = text.replace('<pre>',
'<pre>\n' + '\n' * node.get('empty-lines-before', 0))
text = text.replace('</pre>',
'\n' * node.get('empty-lines-after', 0) + '</pre>')
self.body[-1] = text
def visit_code_latex(self, node):
"""Avoid creating a separate prompt node.
The prompt will be pre-pended in the main code node.
"""
if 'latex_prompt' not in node.attributes:
raise docutils.nodes.SkipNode()
def depart_code_latex(self, node):
"""Some changes to code blocks.
* Remove the frame (by changing Verbatim -> OriginalVerbatim)
* Add empty lines before and after the code
* Add prompt to the first line, emtpy space to the following lines
"""
lines = self.body[-1].split('\n')
out = []
assert lines[0] == ''
out.append(lines[0])
assert lines[1].startswith(r'\begin{Verbatim}')
out.append(lines[1].replace('Verbatim', 'OriginalVerbatim'))
code_lines = (
[''] * node.get('empty-lines-before', 0) +
lines[2:-2] +
[''] * node.get('empty-lines-after', 0)
)
prompt = node.get('latex_prompt')
color = 'nbsphinxin' if prompt.startswith('In') else 'nbsphinxout'
prefix = r'\textcolor{' + color + '}{' + prompt + '}' if prompt else ''
for line in code_lines[:1]:
out.append(prefix + line)
prefix = ' ' * len(prompt)
for line in code_lines[1:]:
out.append(prefix + line)
assert lines[-2].startswith(r'\end{Verbatim}')
out.append(lines[-2].replace('Verbatim', 'OriginalVerbatim'))
assert lines[-1] == ''
out.append(lines[-1])
self.body[-1] = '\n'.join(out)
def do_nothing(self, node):
pass
def _add_notebook_parser(app):
"""Ugly hack to modify source_suffix and source_parsers.
Once https://github.com/sphinx-doc/sphinx/pull/2209 is merged (and
some additional time has passed), this should be replaced by ::
app.add_source_parser('.ipynb', NotebookParser)
See also https://github.com/sphinx-doc/sphinx/issues/2162.
"""
source_suffix = app.config._raw_config.get('source_suffix', ['.rst'])
if isinstance(source_suffix, sphinx.config.string_types):
source_suffix = [source_suffix]
if '.ipynb' not in source_suffix:
source_suffix.append('.ipynb')
app.config._raw_config['source_suffix'] = source_suffix
source_parsers = app.config._raw_config.get('source_parsers', {})
if '.ipynb' not in source_parsers and 'ipynb' not in source_parsers:
source_parsers['.ipynb'] = NotebookParser
app.config._raw_config['source_parsers'] = source_parsers
def setup(app):
"""Initialize Sphinx extension."""
_add_notebook_parser(app)
app.add_config_value('nbsphinx_allow_errors', False, rebuild='')
app.add_directive('nbinput', NbInput)
app.add_directive('nboutput', NbOutput)
app.add_node(CodeNode,
html=(do_nothing, depart_code_html),
latex=(visit_code_latex, depart_code_latex))
app.connect('builder-inited', builder_inited)
app.connect('html-page-context', html_page_context)
app.connect('html-collect-pages', html_collect_pages)
app.connect('env-purge-doc', env_purge_doc)
# Make docutils' "code" directive (generated by markdown2rst/pandoc)
# behave like Sphinx's "code-block",
# see https://github.com/sphinx-doc/sphinx/issues/2155:
rst.directives.register_directive('code', sphinx.directives.code.CodeBlock)
return {'version': __version__, 'parallel_read_safe': True}