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