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