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