Show More
@@ -6,23 +6,17 b' from docutils import io, nodes, statemachine, utils' | |||||
6 | from docutils.error_reporting import ErrorString |
|
6 | from docutils.error_reporting import ErrorString | |
7 | from docutils.parsers.rst import Directive, convert_directive_function |
|
7 | from docutils.parsers.rst import Directive, convert_directive_function | |
8 | from docutils.parsers.rst import directives, roles, states |
|
8 | from docutils.parsers.rst import directives, roles, states | |
9 | from docutils.parsers.rst.directives.body import CodeBlock, NumberLines |
|
|||
10 | from docutils.parsers.rst.roles import set_classes |
|
9 | from docutils.parsers.rst.roles import set_classes | |
11 | from docutils.transforms import misc |
|
10 | from docutils.transforms import misc | |
12 |
|
11 | |||
13 | from nbconvert import ConverterHTML |
|
12 | from nbconvert import ConverterHTML | |
14 |
|
13 | |||
15 | class Notebook(Directive): |
|
|||
16 |
|
14 | |||
|
15 | class Notebook(Directive): | |||
17 | """ |
|
16 | """ | |
18 | Pass through content unchanged |
|
17 | Use nbconvert to insert a notebook into the environment. | |
19 |
|
18 | This is based on the Raw directive in docutils | ||
20 | Content is included in output based on type argument |
|
|||
21 |
|
||||
22 | Content may be included inline (content section of directive) or |
|
|||
23 | imported from a file or url. |
|
|||
24 | """ |
|
19 | """ | |
25 |
|
||||
26 | required_arguments = 1 |
|
20 | required_arguments = 1 | |
27 | optional_arguments = 0 |
|
21 | optional_arguments = 0 | |
28 | final_argument_whitespace = True |
|
22 | final_argument_whitespace = True | |
@@ -38,7 +32,7 b' class Notebook(Directive):' | |||||
38 | attributes = {'format': 'html'} |
|
32 | attributes = {'format': 'html'} | |
39 | encoding = self.options.get( |
|
33 | encoding = self.options.get( | |
40 | 'encoding', self.state.document.settings.input_encoding) |
|
34 | 'encoding', self.state.document.settings.input_encoding) | |
41 | e_handler=self.state.document.settings.input_encoding_error_handler |
|
35 | e_handler = self.state.document.settings.input_encoding_error_handler | |
42 |
|
36 | |||
43 | # get path to notebook |
|
37 | # get path to notebook | |
44 | source_dir = os.path.dirname( |
|
38 | source_dir = os.path.dirname( | |
@@ -50,9 +44,8 b' class Notebook(Directive):' | |||||
50 | # convert notebook to html |
|
44 | # convert notebook to html | |
51 | converter = ConverterHTML(path) |
|
45 | converter = ConverterHTML(path) | |
52 | htmlfname = converter.render() |
|
46 | htmlfname = converter.render() | |
53 |
|
||||
54 | htmlpath = utils.relative_path(None, htmlfname) |
|
47 | htmlpath = utils.relative_path(None, htmlfname) | |
55 |
|
48 | |||
56 | try: |
|
49 | try: | |
57 | raw_file = io.FileInput(source_path=htmlpath, |
|
50 | raw_file = io.FileInput(source_path=htmlpath, | |
58 | encoding=encoding, |
|
51 | encoding=encoding, | |
@@ -70,22 +63,24 b' class Notebook(Directive):' | |||||
70 | % (self.name, ErrorString(error))) |
|
63 | % (self.name, ErrorString(error))) | |
71 | attributes['source'] = htmlpath |
|
64 | attributes['source'] = htmlpath | |
72 |
|
65 | |||
73 |
|
66 | nb_node = notebook('', text, **attributes) | ||
|
67 | (nb_node.source, | |||
|
68 | nb_node.line) = self.state_machine.get_source_and_line(self.lineno) | |||
|
69 | return [nb_node] | |||
74 |
|
70 | |||
75 | raw_node = nodes.raw('', text, **attributes) |
|
|||
76 | (raw_node.source, |
|
|||
77 | raw_node.line) = self.state_machine.get_source_and_line(self.lineno) |
|
|||
78 | return [raw_node] |
|
|||
79 |
|
71 | |||
80 |
class notebook(nodes. |
|
72 | class notebook(nodes.raw): | |
81 | pass |
|
73 | pass | |
82 |
|
74 | |||
|
75 | ||||
83 | def visit_notebook_node(self, node): |
|
76 | def visit_notebook_node(self, node): | |
84 | self.visit_raw(node) |
|
77 | self.visit_raw(node) | |
85 |
|
78 | |||
|
79 | ||||
86 | def depart_notebook_node(self, node): |
|
80 | def depart_notebook_node(self, node): | |
87 | self.depart_raw(node) |
|
81 | self.depart_raw(node) | |
88 |
|
82 | |||
|
83 | ||||
89 | def setup(app): |
|
84 | def setup(app): | |
90 | app.add_node(notebook, |
|
85 | app.add_node(notebook, | |
91 | html=(visit_notebook_node, depart_notebook_node)) |
|
86 | html=(visit_notebook_node, depart_notebook_node)) |
General Comments 0
You need to be logged in to leave comments.
Login now