Show More
@@ -1,265 +1,264 | |||||
1 | """Module that allows custom Sphinx parameters to be set on the notebook and |
|
1 | """Module that allows custom Sphinx parameters to be set on the notebook and | |
2 | on the 'other' object passed into Jinja. Called prior to Jinja conversion |
|
2 | on the 'other' object passed into Jinja. Called prior to Jinja conversion | |
3 | process. |
|
3 | process. | |
4 | """ |
|
4 | """ | |
5 | #----------------------------------------------------------------------------- |
|
5 | #----------------------------------------------------------------------------- | |
6 | # Copyright (c) 2013, the IPython Development Team. |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
7 | # |
|
7 | # | |
8 | # Distributed under the terms of the Modified BSD License. |
|
8 | # Distributed under the terms of the Modified BSD License. | |
9 | # |
|
9 | # | |
10 | # The full license is in the file COPYING.txt, distributed with this software. |
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 |
|
16 | |||
17 | from __future__ import print_function, absolute_import |
|
17 | from __future__ import print_function, absolute_import | |
18 |
|
18 | |||
19 | # Stdlib imports |
|
19 | # Stdlib imports | |
20 | import os.path |
|
20 | import os.path | |
21 | import sys |
|
|||
22 |
|
21 | |||
23 | # Used to set the default date to today's date |
|
22 | # Used to set the default date to today's date | |
24 | from datetime import date |
|
23 | from datetime import date | |
25 |
|
24 | |||
26 | # Third-party imports |
|
25 | # Third-party imports | |
27 | # Needed for Pygments latex definitions. |
|
26 | # Needed for Pygments latex definitions. | |
28 | from pygments.formatters import LatexFormatter |
|
27 | from pygments.formatters import LatexFormatter | |
29 |
|
28 | |||
30 | # Our own imports |
|
29 | # Our own imports | |
31 | # Configurable traitlets |
|
30 | # Configurable traitlets | |
32 | from IPython.utils.traitlets import Unicode, Bool |
|
31 | from IPython.utils.traitlets import Unicode, Bool | |
33 | from IPython.utils import text |
|
32 | from IPython.utils import text | |
34 |
|
33 | |||
35 | # Needed to override preprocessor |
|
34 | # Needed to override preprocessor | |
36 | from .base import (Preprocessor) |
|
35 | from .base import (Preprocessor) | |
37 |
|
36 | |||
38 | from IPython.nbconvert.utils import console |
|
37 | from IPython.nbconvert.utils import console | |
39 |
|
38 | |||
40 | #----------------------------------------------------------------------------- |
|
39 | #----------------------------------------------------------------------------- | |
41 | # Classes and functions |
|
40 | # Classes and functions | |
42 | #----------------------------------------------------------------------------- |
|
41 | #----------------------------------------------------------------------------- | |
43 |
|
42 | |||
44 | class SphinxPreprocessor(Preprocessor): |
|
43 | class SphinxPreprocessor(Preprocessor): | |
45 | """ |
|
44 | """ | |
46 | Sphinx utility preprocessor. |
|
45 | Sphinx utility preprocessor. | |
47 |
|
46 | |||
48 | This preprocessor is used to set variables needed by the latex to build |
|
47 | This preprocessor is used to set variables needed by the latex to build | |
49 | Sphinx stylized templates. |
|
48 | Sphinx stylized templates. | |
50 | """ |
|
49 | """ | |
51 |
|
50 | |||
52 | interactive = Bool(False, config=True, help=""" |
|
51 | interactive = Bool(False, config=True, help=""" | |
53 | Allows you to define whether or not the Sphinx exporter will prompt |
|
52 | Allows you to define whether or not the Sphinx exporter will prompt | |
54 | you for input during the conversion process. If this is set to false, |
|
53 | you for input during the conversion process. If this is set to false, | |
55 | the author, version, release, date, and chapter_style traits should |
|
54 | the author, version, release, date, and chapter_style traits should | |
56 | be set. |
|
55 | be set. | |
57 | """) |
|
56 | """) | |
58 |
|
57 | |||
59 | author = Unicode("Unknown Author", config=True, help="Author name") |
|
58 | author = Unicode("Unknown Author", config=True, help="Author name") | |
60 |
|
59 | |||
61 | version = Unicode("", config=True, help=""" |
|
60 | version = Unicode("", config=True, help=""" | |
62 | Version number |
|
61 | Version number | |
63 | You can leave this blank if you do not want to render a version number. |
|
62 | You can leave this blank if you do not want to render a version number. | |
64 | Example: "1.0.0" |
|
63 | Example: "1.0.0" | |
65 | """) |
|
64 | """) | |
66 |
|
65 | |||
67 | release = Unicode("", config=True, help=""" |
|
66 | release = Unicode("", config=True, help=""" | |
68 | Release name |
|
67 | Release name | |
69 | You can leave this blank if you do not want to render a release name. |
|
68 | You can leave this blank if you do not want to render a release name. | |
70 | Example: "Rough Draft" |
|
69 | Example: "Rough Draft" | |
71 | """) |
|
70 | """) | |
72 |
|
71 | |||
73 | publish_date = Unicode("", config=True, help=""" |
|
72 | publish_date = Unicode("", config=True, help=""" | |
74 | Publish date |
|
73 | Publish date | |
75 | This is the date to render on the document as the publish date. |
|
74 | This is the date to render on the document as the publish date. | |
76 | Leave this blank to default to todays date. |
|
75 | Leave this blank to default to todays date. | |
77 | Example: "June 12, 1990" |
|
76 | Example: "June 12, 1990" | |
78 | """) |
|
77 | """) | |
79 |
|
78 | |||
80 | chapter_style = Unicode("Bjarne", config=True, help=""" |
|
79 | chapter_style = Unicode("Bjarne", config=True, help=""" | |
81 | Sphinx chapter style |
|
80 | Sphinx chapter style | |
82 | This is the style to use for the chapter headers in the document. |
|
81 | This is the style to use for the chapter headers in the document. | |
83 | You may choose one of the following: |
|
82 | You may choose one of the following: | |
84 | "Bjarne" (default) |
|
83 | "Bjarne" (default) | |
85 | "Lenny" |
|
84 | "Lenny" | |
86 | "Glenn" |
|
85 | "Glenn" | |
87 | "Conny" |
|
86 | "Conny" | |
88 | "Rejne" |
|
87 | "Rejne" | |
89 | "Sonny" (used for international documents) |
|
88 | "Sonny" (used for international documents) | |
90 | """) |
|
89 | """) | |
91 |
|
90 | |||
92 | output_style = Unicode("notebook", config=True, help=""" |
|
91 | output_style = Unicode("notebook", config=True, help=""" | |
93 | Nbconvert Ipython |
|
92 | Nbconvert Ipython | |
94 | notebook input/output formatting style. |
|
93 | notebook input/output formatting style. | |
95 | You may choose one of the following: |
|
94 | You may choose one of the following: | |
96 | "simple (recommended for long code segments)" |
|
95 | "simple (recommended for long code segments)" | |
97 | "notebook" (default) |
|
96 | "notebook" (default) | |
98 | """) |
|
97 | """) | |
99 |
|
98 | |||
100 | center_output = Bool(False, config=True, help=""" |
|
99 | center_output = Bool(False, config=True, help=""" | |
101 | Optional attempt to center all output. If this is false, no additional |
|
100 | Optional attempt to center all output. If this is false, no additional | |
102 | formatting is applied. |
|
101 | formatting is applied. | |
103 | """) |
|
102 | """) | |
104 |
|
103 | |||
105 | use_headers = Bool(True, config=True, help=""" |
|
104 | use_headers = Bool(True, config=True, help=""" | |
106 | Whether not a header should be added to the document. |
|
105 | Whether not a header should be added to the document. | |
107 | """) |
|
106 | """) | |
108 |
|
107 | |||
109 | #Allow the user to override the title of the notebook (useful for |
|
108 | #Allow the user to override the title of the notebook (useful for | |
110 | #fancy document titles that the file system doesn't support.) |
|
109 | #fancy document titles that the file system doesn't support.) | |
111 | overridetitle = Unicode("", config=True, help="") |
|
110 | overridetitle = Unicode("", config=True, help="") | |
112 |
|
111 | |||
113 |
|
112 | |||
114 | def preprocess(self, nb, resources): |
|
113 | def preprocess(self, nb, resources): | |
115 | """ |
|
114 | """ | |
116 | Sphinx preprocessing to apply on each notebook. |
|
115 | Sphinx preprocessing to apply on each notebook. | |
117 |
|
116 | |||
118 | Parameters |
|
117 | Parameters | |
119 | ---------- |
|
118 | ---------- | |
120 | nb : NotebookNode |
|
119 | nb : NotebookNode | |
121 | Notebook being converted |
|
120 | Notebook being converted | |
122 | resources : dictionary |
|
121 | resources : dictionary | |
123 | Additional resources used in the conversion process. Allows |
|
122 | Additional resources used in the conversion process. Allows | |
124 | preprocessors to pass variables into the Jinja engine. |
|
123 | preprocessors to pass variables into the Jinja engine. | |
125 | """ |
|
124 | """ | |
126 | # import sphinx here, so that sphinx is not a dependency when it's not used |
|
125 | # import sphinx here, so that sphinx is not a dependency when it's not used | |
127 | import sphinx |
|
126 | import sphinx | |
128 |
|
127 | |||
129 | # TODO: Add versatile method of additional notebook metadata. Include |
|
128 | # TODO: Add versatile method of additional notebook metadata. Include | |
130 | # handling of multiple files. For now use a temporay namespace, |
|
129 | # handling of multiple files. For now use a temporay namespace, | |
131 | # '_draft' to signify that this needs to change. |
|
130 | # '_draft' to signify that this needs to change. | |
132 | if not isinstance(resources["sphinx"], dict): |
|
131 | if not isinstance(resources["sphinx"], dict): | |
133 | resources["sphinx"] = {} |
|
132 | resources["sphinx"] = {} | |
134 |
|
133 | |||
135 | if self.interactive: |
|
134 | if self.interactive: | |
136 |
|
135 | |||
137 | # Prompt the user for additional meta data that doesn't exist currently |
|
136 | # Prompt the user for additional meta data that doesn't exist currently | |
138 | # but would be usefull for Sphinx. |
|
137 | # but would be usefull for Sphinx. | |
139 | resources["sphinx"]["author"] = self._prompt_author() |
|
138 | resources["sphinx"]["author"] = self._prompt_author() | |
140 | resources["sphinx"]["version"] = self._prompt_version() |
|
139 | resources["sphinx"]["version"] = self._prompt_version() | |
141 | resources["sphinx"]["release"] = self._prompt_release() |
|
140 | resources["sphinx"]["release"] = self._prompt_release() | |
142 | resources["sphinx"]["date"] = self._prompt_date() |
|
141 | resources["sphinx"]["date"] = self._prompt_date() | |
143 |
|
142 | |||
144 | # Prompt the user for the document style. |
|
143 | # Prompt the user for the document style. | |
145 | resources["sphinx"]["chapterstyle"] = self._prompt_chapter_title_style() |
|
144 | resources["sphinx"]["chapterstyle"] = self._prompt_chapter_title_style() | |
146 | resources["sphinx"]["outputstyle"] = self._prompt_output_style() |
|
145 | resources["sphinx"]["outputstyle"] = self._prompt_output_style() | |
147 |
|
146 | |||
148 | # Small options |
|
147 | # Small options | |
149 | resources["sphinx"]["centeroutput"] = console.prompt_boolean("Do you want to center the output? (false)", False) |
|
148 | resources["sphinx"]["centeroutput"] = console.prompt_boolean("Do you want to center the output? (false)", False) | |
150 | resources["sphinx"]["header"] = console.prompt_boolean("Should a Sphinx document header be used? (true)", True) |
|
149 | resources["sphinx"]["header"] = console.prompt_boolean("Should a Sphinx document header be used? (true)", True) | |
151 | else: |
|
150 | else: | |
152 |
|
151 | |||
153 | # Try to use the traitlets. |
|
152 | # Try to use the traitlets. | |
154 | resources["sphinx"]["author"] = self.author |
|
153 | resources["sphinx"]["author"] = self.author | |
155 | resources["sphinx"]["version"] = self.version |
|
154 | resources["sphinx"]["version"] = self.version | |
156 | resources["sphinx"]["release"] = self.release |
|
155 | resources["sphinx"]["release"] = self.release | |
157 |
|
156 | |||
158 | # Use todays date if none is provided. |
|
157 | # Use todays date if none is provided. | |
159 | if self.publish_date: |
|
158 | if self.publish_date: | |
160 | resources["sphinx"]["date"] = self.publish_date |
|
159 | resources["sphinx"]["date"] = self.publish_date | |
161 | elif len(resources['metadata']['modified_date'].strip()) == 0: |
|
160 | elif len(resources['metadata']['modified_date'].strip()) == 0: | |
162 | resources["sphinx"]["date"] = date.today().strftime(text.date_format) |
|
161 | resources["sphinx"]["date"] = date.today().strftime(text.date_format) | |
163 | else: |
|
162 | else: | |
164 | resources["sphinx"]["date"] = resources['metadata']['modified_date'] |
|
163 | resources["sphinx"]["date"] = resources['metadata']['modified_date'] | |
165 |
|
164 | |||
166 | # Sphinx traitlets. |
|
165 | # Sphinx traitlets. | |
167 | resources["sphinx"]["chapterstyle"] = self.chapter_style |
|
166 | resources["sphinx"]["chapterstyle"] = self.chapter_style | |
168 | resources["sphinx"]["outputstyle"] = self.output_style |
|
167 | resources["sphinx"]["outputstyle"] = self.output_style | |
169 | resources["sphinx"]["centeroutput"] = self.center_output |
|
168 | resources["sphinx"]["centeroutput"] = self.center_output | |
170 | resources["sphinx"]["header"] = self.use_headers |
|
169 | resources["sphinx"]["header"] = self.use_headers | |
171 |
|
170 | |||
172 | # Find and pass in the path to the Sphinx dependencies. |
|
171 | # Find and pass in the path to the Sphinx dependencies. | |
173 | resources["sphinx"]["texinputs"] = os.path.realpath(os.path.join(sphinx.package_dir, "texinputs")) |
|
172 | resources["sphinx"]["texinputs"] = os.path.realpath(os.path.join(sphinx.package_dir, "texinputs")) | |
174 |
|
173 | |||
175 | # Generate Pygments definitions for Latex |
|
174 | # Generate Pygments definitions for Latex | |
176 | resources["sphinx"]["pygment_definitions"] = self._generate_pygments_latex_def() |
|
175 | resources["sphinx"]["pygment_definitions"] = self._generate_pygments_latex_def() | |
177 |
|
176 | |||
178 | if not (self.overridetitle == None or len(self.overridetitle.strip()) == 0): |
|
177 | if not (self.overridetitle == None or len(self.overridetitle.strip()) == 0): | |
179 | resources['metadata']['name'] = self.overridetitle |
|
178 | resources['metadata']['name'] = self.overridetitle | |
180 |
|
179 | |||
181 | # End |
|
180 | # End | |
182 | return nb, resources |
|
181 | return nb, resources | |
183 |
|
182 | |||
184 |
|
183 | |||
185 | def _generate_pygments_latex_def(self): |
|
184 | def _generate_pygments_latex_def(self): | |
186 | """ |
|
185 | """ | |
187 | Generate the pygments latex definitions that allows pygments |
|
186 | Generate the pygments latex definitions that allows pygments | |
188 | to work in latex. |
|
187 | to work in latex. | |
189 | """ |
|
188 | """ | |
190 |
|
189 | |||
191 | return LatexFormatter().get_style_defs() |
|
190 | return LatexFormatter().get_style_defs() | |
192 |
|
191 | |||
193 |
|
192 | |||
194 | def _prompt_author(self): |
|
193 | def _prompt_author(self): | |
195 | """ |
|
194 | """ | |
196 | Prompt the user to input an Author name |
|
195 | Prompt the user to input an Author name | |
197 | """ |
|
196 | """ | |
198 | return console.input("Author name: ") |
|
197 | return console.input("Author name: ") | |
199 |
|
198 | |||
200 |
|
199 | |||
201 | def _prompt_version(self): |
|
200 | def _prompt_version(self): | |
202 | """ |
|
201 | """ | |
203 | prompt the user to enter a version number |
|
202 | prompt the user to enter a version number | |
204 | """ |
|
203 | """ | |
205 | return console.input("Version (ie ""1.0.0""): ") |
|
204 | return console.input("Version (ie ""1.0.0""): ") | |
206 |
|
205 | |||
207 |
|
206 | |||
208 | def _prompt_release(self): |
|
207 | def _prompt_release(self): | |
209 | """ |
|
208 | """ | |
210 | Prompt the user to input a release name |
|
209 | Prompt the user to input a release name | |
211 | """ |
|
210 | """ | |
212 |
|
211 | |||
213 | return console.input("Release Name (ie ""Rough draft""): ") |
|
212 | return console.input("Release Name (ie ""Rough draft""): ") | |
214 |
|
213 | |||
215 |
|
214 | |||
216 | def _prompt_date(self, resources): |
|
215 | def _prompt_date(self, resources): | |
217 | """ |
|
216 | """ | |
218 | Prompt the user to enter a date |
|
217 | Prompt the user to enter a date | |
219 | """ |
|
218 | """ | |
220 |
|
219 | |||
221 | if resources['metadata']['modified_date']: |
|
220 | if resources['metadata']['modified_date']: | |
222 | default_date = resources['metadata']['modified_date'] |
|
221 | default_date = resources['metadata']['modified_date'] | |
223 | else: |
|
222 | else: | |
224 | default_date = date.today().strftime(text.date_format) |
|
223 | default_date = date.today().strftime(text.date_format) | |
225 |
|
224 | |||
226 | user_date = console.input("Date (deafults to \"" + default_date + "\"): ") |
|
225 | user_date = console.input("Date (deafults to \"" + default_date + "\"): ") | |
227 | if len(user_date.strip()) == 0: |
|
226 | if len(user_date.strip()) == 0: | |
228 | user_date = default_date |
|
227 | user_date = default_date | |
229 | return user_date |
|
228 | return user_date | |
230 |
|
229 | |||
231 |
|
230 | |||
232 | def _prompt_output_style(self): |
|
231 | def _prompt_output_style(self): | |
233 | """ |
|
232 | """ | |
234 | Prompts the user to pick an IPython output style. |
|
233 | Prompts the user to pick an IPython output style. | |
235 | """ |
|
234 | """ | |
236 |
|
235 | |||
237 | # Dictionary of available output styles |
|
236 | # Dictionary of available output styles | |
238 | styles = {1: "simple", |
|
237 | styles = {1: "simple", | |
239 | 2: "notebook"} |
|
238 | 2: "notebook"} | |
240 |
|
239 | |||
241 | #Append comments to the menu when displaying it to the user. |
|
240 | #Append comments to the menu when displaying it to the user. | |
242 | comments = {1: "(recommended for long code segments)", |
|
241 | comments = {1: "(recommended for long code segments)", | |
243 | 2: "(default)"} |
|
242 | 2: "(default)"} | |
244 |
|
243 | |||
245 | return console.prompt_dictionary(styles, default_style=2, menu_comments=comments) |
|
244 | return console.prompt_dictionary(styles, default_style=2, menu_comments=comments) | |
246 |
|
245 | |||
247 |
|
246 | |||
248 | def _prompt_chapter_title_style(self): |
|
247 | def _prompt_chapter_title_style(self): | |
249 | """ |
|
248 | """ | |
250 | Prompts the user to pick a Sphinx chapter style |
|
249 | Prompts the user to pick a Sphinx chapter style | |
251 | """ |
|
250 | """ | |
252 |
|
251 | |||
253 | # Dictionary of available Sphinx styles |
|
252 | # Dictionary of available Sphinx styles | |
254 | styles = {1: "Bjarne", |
|
253 | styles = {1: "Bjarne", | |
255 | 2: "Lenny", |
|
254 | 2: "Lenny", | |
256 | 3: "Glenn", |
|
255 | 3: "Glenn", | |
257 | 4: "Conny", |
|
256 | 4: "Conny", | |
258 | 5: "Rejne", |
|
257 | 5: "Rejne", | |
259 | 6: "Sonny"} |
|
258 | 6: "Sonny"} | |
260 |
|
259 | |||
261 | #Append comments to the menu when displaying it to the user. |
|
260 | #Append comments to the menu when displaying it to the user. | |
262 | comments = {1: "(default)", |
|
261 | comments = {1: "(default)", | |
263 | 6: "(for international documents)"} |
|
262 | 6: "(for international documents)"} | |
264 |
|
263 | |||
265 | return console.prompt_dictionary(styles, menu_comments=comments) |
|
264 | return console.prompt_dictionary(styles, menu_comments=comments) |
General Comments 0
You need to be logged in to leave comments.
Login now