Show More
@@ -1,266 +1,266 b'' | |||
|
1 | 1 | """ |
|
2 | 2 | Module that allows custom Sphinx parameters to be set on the notebook and |
|
3 | 3 | on the 'other' object passed into Jinja. |
|
4 | 4 | """ |
|
5 | 5 | from __future__ import absolute_import |
|
6 | 6 | |
|
7 | 7 | # Used to find Sphinx package location |
|
8 | 8 | import sphinx |
|
9 | 9 | import os.path |
|
10 | 10 | |
|
11 | 11 | # Used to determine python version |
|
12 | 12 | import sys |
|
13 | 13 | |
|
14 | 14 | # Used to set the default date to today's date |
|
15 | 15 | from datetime import date |
|
16 | 16 | |
|
17 | 17 | # Configurable traitlets |
|
18 | 18 | from IPython.utils.traitlets import Unicode, Bool |
|
19 | 19 | |
|
20 | 20 | # Needed for Pygments latex definitions. |
|
21 | 21 | from pygments.formatters import LatexFormatter |
|
22 | 22 | |
|
23 | 23 | # Needed to override transformer |
|
24 | 24 | from .transformers import (ActivatableTransformer) |
|
25 | 25 | |
|
26 | 26 | class SphinxTransformer(ActivatableTransformer): |
|
27 | 27 | """ |
|
28 | 28 | Sphinx utility transformer. |
|
29 | 29 | |
|
30 | 30 | This transformer is used to set variables needed by the latex to build |
|
31 | 31 | Sphinx stylized templates. |
|
32 | 32 | """ |
|
33 | 33 | |
|
34 | 34 | interactive = Bool(True, config=True, help=""" |
|
35 | 35 | Allows you to define whether or not the Sphinx exporter will prompt |
|
36 | 36 | you for input during the conversion process. If this is set to false, |
|
37 | 37 | the author, version, release, date, and chapter_style traits should |
|
38 | 38 | be set. |
|
39 | 39 | """) |
|
40 | 40 | |
|
41 | 41 | author = Unicode("Unknown Author", config=True, help="Author name") |
|
42 | 42 | |
|
43 | 43 | version = Unicode("", config=True, help="""Version number |
|
44 | 44 | You can leave this blank if you do not want to render a version number. |
|
45 | 45 | Example: "1.0.0" |
|
46 | 46 | """) |
|
47 | 47 | |
|
48 | 48 | release = Unicode("", config=True, help="""Release name |
|
49 | 49 | You can leave this blank if you do not want to render a release name. |
|
50 | 50 | Example: "Rough Draft" |
|
51 | 51 | """) |
|
52 | 52 | |
|
53 | 53 | publish_date = Unicode("", config=True, help="""Publish date |
|
54 | 54 | This is the date to render on the document as the publish date. |
|
55 | 55 | Leave this blank to default to todays date. |
|
56 | 56 | Example: "June 12, 1990" |
|
57 | 57 | """) |
|
58 | 58 | |
|
59 | 59 | chapter_style = Unicode("Bjarne", config=True, help="""Sphinx chapter style |
|
60 | 60 | This is the style to use for the chapter headers in the document. |
|
61 | 61 | You may choose one of the following: |
|
62 | 62 | "Bjarne" (default) |
|
63 | 63 | "Lenny" |
|
64 | 64 | "Glenn" |
|
65 | 65 | "Conny" |
|
66 | 66 | "Rejne" |
|
67 | 67 | "Sonny" (used for international documents) |
|
68 | 68 | """) |
|
69 | 69 | |
|
70 | 70 | output_style = Unicode("notebook", config=True, help="""Nbconvert Ipython |
|
71 | 71 | notebook input/output formatting style. |
|
72 | 72 | You may choose one of the following: |
|
73 | 73 | "simple (recommended for long code segments)" |
|
74 | 74 | "notebook" (default) |
|
75 | 75 | """) |
|
76 | 76 | |
|
77 | 77 | center_output = Bool(False, config=True, help=""" |
|
78 | 78 | Optional attempt to center all output. If this is false, no additional |
|
79 | 79 | formatting is applied. |
|
80 | 80 | """) |
|
81 | 81 | |
|
82 | 82 | use_headers = Bool(True, config=True, help=""" |
|
83 | 83 | Whether not a header should be added to the document. |
|
84 | 84 | """) |
|
85 | 85 | |
|
86 | 86 | def __call__(self, nb, other): |
|
87 | 87 | """ |
|
88 | 88 | Entry |
|
89 | 89 | Since we are not interested in any additional manipulation on a cell |
|
90 | 90 | by cell basis, we do not call the base implementation. |
|
91 | 91 | """ |
|
92 | 92 | if self.enabled: |
|
93 | 93 | return self.transform(nb, other) |
|
94 | 94 | else: |
|
95 | 95 | return nb,other |
|
96 | 96 | |
|
97 | 97 | def transform(self, nb, other): |
|
98 | 98 | """ |
|
99 | 99 | Sphinx transformation to apply on each notebook. |
|
100 | 100 | """ |
|
101 | 101 | |
|
102 | 102 | # TODO: Add versatile method of additional notebook metadata. Include |
|
103 | 103 | # handling of multiple files. For now use a temporay namespace, |
|
104 | 104 | # '_draft' to signify that this needs to change. |
|
105 | 105 | if not "_draft" in nb.metadata: |
|
106 | 106 | nb.metadata._draft = {} |
|
107 | 107 | |
|
108 | 108 | if not "sphinx" in other: |
|
109 | 109 | other["sphinx"] = {} |
|
110 | 110 | |
|
111 | 111 | if self.interactive: |
|
112 | 112 | |
|
113 | 113 | # Prompt the user for additional meta data that doesn't exist currently |
|
114 | 114 | # but would be usefull for Sphinx. |
|
115 | 115 | nb.metadata._draft["author"] = self._prompt_author() |
|
116 | 116 | nb.metadata._draft["version"] = self._prompt_version() |
|
117 | 117 | nb.metadata._draft["release"] = self._prompt_release() |
|
118 | 118 | nb.metadata._draft["date"] = self._prompt_date() |
|
119 | 119 | |
|
120 | 120 | # Prompt the user for the document style. |
|
121 | 121 | other["sphinx"]["chapterstyle"] = self._prompt_chapter_title_style() |
|
122 | 122 | other["sphinx"]["outputstyle"] = self._prompt_output_style() |
|
123 | 123 | |
|
124 | 124 | # Small options |
|
125 | 125 | other["sphinx"]["centeroutput"] = self._prompt_boolean("Do you want to center the output? (false)", False) |
|
126 | 126 | other["sphinx"]["header"] = self._prompt_boolean("Should a Sphinx document header be used? (true)", True) |
|
127 | 127 | else: |
|
128 | 128 | |
|
129 | 129 | # Try to use the traitlets. |
|
130 | 130 | nb.metadata._draft["author"] = self.author |
|
131 | 131 | nb.metadata._draft["version"] = self.version |
|
132 | 132 | nb.metadata._draft["release"] = self.release |
|
133 | 133 | |
|
134 | 134 | # Use todays date if none is provided. |
|
135 | 135 | if len(self.publish_date.strip()) == 0: |
|
136 | 136 | nb.metadata._draft["date"] = date.today().strftime("%B %-d, %Y") |
|
137 | 137 | else: |
|
138 | 138 | nb.metadata._draft["date"] = self.publish_date |
|
139 | 139 | |
|
140 | 140 | # Sphinx traitlets. |
|
141 | 141 | other["sphinx"]["chapterstyle"] = self.chapter_style |
|
142 | 142 | other["sphinx"]["outputstyle"] = self.output_style |
|
143 | 143 | other["sphinx"]["centeroutput"] = self.center_output |
|
144 | 144 | other["sphinx"]["header"] = self.use_headers |
|
145 | 145 | |
|
146 | 146 | # Find and pass in the path to the Sphinx dependencies. |
|
147 | 147 | other["sphinx"]["texinputs"] = os.path.abspath(sphinx.__file__ + "/../texinputs") |
|
148 | 148 | |
|
149 | 149 | # Generate Pygments definitions for Latex |
|
150 | 150 | other["sphinx"]["pygment_definitions"] = self._generate_pygments_latex_def() |
|
151 | 151 | |
|
152 | 152 | # End |
|
153 | 153 | return nb, other |
|
154 | 154 | |
|
155 | 155 | def _generate_pygments_latex_def(self): |
|
156 | 156 | return LatexFormatter().get_style_defs() |
|
157 | 157 | |
|
158 | 158 | def _prompt_author(self): |
|
159 | 159 | return self._input("Author name: ") |
|
160 | 160 | |
|
161 | 161 | def _prompt_version(self): |
|
162 | 162 | return self._input("Version (ie ""1.0.0""): ") |
|
163 | 163 | |
|
164 | 164 | def _prompt_release(self): |
|
165 | 165 | return self._input("Release Name (ie ""Rough draft""): ") |
|
166 | 166 | |
|
167 | 167 | def _prompt_date(self): |
|
168 | 168 | default_date = date.today().strftime("%B %-d, %Y") |
|
169 | 169 | user_date = self._input("Date (deafults to \"" + default_date + "\"): ") |
|
170 | 170 | if len(user_date.strip()) == 0: |
|
171 | 171 | user_date = default_date |
|
172 | 172 | return user_date |
|
173 | 173 | |
|
174 | 174 | def _prompt_boolean(self, prompt, default=False): |
|
175 | 175 | response = self._input(prompt) |
|
176 | 176 | response = response.strip().lower() |
|
177 | 177 | |
|
178 | 178 | #Catch 1, true, yes as True |
|
179 | if (response == "1" or response[0] == "t" or response[0] == "y"): | |
|
179 | if len(response) > 0 and (response == "1" or response[0] == "t" or response[0] == "y"): | |
|
180 | 180 | return True |
|
181 | 181 | |
|
182 | 182 | #Catch 0, false, no as False |
|
183 | elif (response == "0" or response[0] == "f" or response[0] == "n"): | |
|
183 | elif len(response) > 0 and (response == "0" or response[0] == "f" or response[0] == "n"): | |
|
184 | 184 | return False |
|
185 | 185 | |
|
186 | 186 | else: |
|
187 |
return |
|
|
187 | return default | |
|
188 | 188 | |
|
189 | 189 | def _prompt_output_style(self): |
|
190 | 190 | |
|
191 | 191 | # Dictionary of available output styles |
|
192 | 192 | styles = {1: "simple", |
|
193 | 193 | 2: "notebook"} |
|
194 | 194 | |
|
195 | 195 | #Append comments to the menu when displaying it to the user. |
|
196 | 196 | comments = {1: "(recommended for long code segments)", |
|
197 | 197 | 2: "(default)"} |
|
198 | 198 | |
|
199 | 199 | return self._prompt_dictionary(styles, default_style=2, menu_comments=comments) |
|
200 | 200 | |
|
201 | 201 | def _prompt_chapter_title_style(self): |
|
202 | 202 | |
|
203 | 203 | # Dictionary of available Sphinx styles |
|
204 | 204 | styles = {1: "Bjarne", |
|
205 | 205 | 2: "Lenny", |
|
206 | 206 | 3: "Glenn", |
|
207 | 207 | 4: "Conny", |
|
208 | 208 | 5: "Rejne", |
|
209 | 209 | 6: "Sonny"} |
|
210 | 210 | |
|
211 | 211 | #Append comments to the menu when displaying it to the user. |
|
212 | 212 | comments = {1: "(default)", |
|
213 | 213 | 6: "(for international documents)"} |
|
214 | 214 | |
|
215 | 215 | return self._prompt_dictionary(styles, menu_comments=comments) |
|
216 | 216 | |
|
217 | 217 | def _prompt_dictionary(self, choices, default_style=1, menu_comments={}): |
|
218 | 218 | |
|
219 | 219 | # Build the menu that will be displayed to the user with |
|
220 | 220 | # all of the options available. |
|
221 | 221 | prompt = "" |
|
222 | 222 | for key, value in choices.iteritems(): |
|
223 | 223 | prompt += "%d %s " % (key, value) |
|
224 | 224 | if key in menu_comments: |
|
225 | 225 | prompt += menu_comments[key] |
|
226 | 226 | prompt += "\n" |
|
227 | 227 | |
|
228 | 228 | # Continue to ask the user for a style until an appropriate |
|
229 | 229 | # one is specified. |
|
230 | 230 | response = -1 |
|
231 | 231 | while (not response in choices): |
|
232 | 232 | try: |
|
233 | 233 | text_response = self._input(prompt) |
|
234 | 234 | |
|
235 | 235 | # Use default option if no input. |
|
236 | 236 | if len(text_response.strip()) == 0: |
|
237 | 237 | response = default_style |
|
238 | 238 | else: |
|
239 | 239 | response = int(text_response) |
|
240 | 240 | except: |
|
241 | 241 | print("Error: Value is not an available option. 0 selects the default.\n") |
|
242 | 242 | return choices[response] |
|
243 | 243 | |
|
244 | 244 | def _input(self, prompt_text): |
|
245 | 245 | """ |
|
246 | 246 | Prompt the user for input. |
|
247 | 247 | |
|
248 | 248 | The input command will change depending on the version of python |
|
249 | 249 | installed. To maintain support for 2 and earlier, we must use |
|
250 | 250 | raw_input in that case. Else use input. |
|
251 | 251 | """ |
|
252 | 252 | |
|
253 | 253 | # Try to get the python version. This command is only available in |
|
254 | 254 | # python 2 and later, so it's important that we catch the exception |
|
255 | 255 | # if the command isn't found. |
|
256 | 256 | try: |
|
257 | 257 | majorversion = sys.version_info[0] |
|
258 | 258 | except: |
|
259 | 259 | majorversion = 1 |
|
260 | 260 | |
|
261 | 261 | # Use the correct function to prompt the user for input depending on |
|
262 | 262 | # what python version the code is running in. |
|
263 | 263 | if majorversion >= 3: |
|
264 | 264 | return input(prompt_text) |
|
265 | 265 | else: |
|
266 | 266 | return raw_input(prompt_text) |
General Comments 0
You need to be logged in to leave comments.
Login now