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