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