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