Show More
@@ -1,205 +1,206 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 chapterstyle traits should |
|
37 | the author, version, release, date, and chapterstyle 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 | publishdate = Unicode("", config=True, help="""Publish date |
|
53 | publishdate = 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 | chapterstyle = Unicode("Bjarne", config=True, help="""Sphinx chapter style |
|
59 | chapterstyle = 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 |
|
70 | |||
71 | def __call__(self, nb, other): |
|
71 | def __call__(self, nb, other): | |
72 | """ |
|
72 | """ | |
73 | Entry |
|
73 | Entry | |
74 | Since we are not interested in any additional manipulation on a cell |
|
74 | Since we are not interested in any additional manipulation on a cell | |
75 | by cell basis, we do not call the base implementation. |
|
75 | by cell basis, we do not call the base implementation. | |
76 | """ |
|
76 | """ | |
77 | if self.enabled: |
|
77 | if self.enabled: | |
78 | return self.transform(nb, other) |
|
78 | return self.transform(nb, other) | |
79 | else: |
|
79 | else: | |
80 | return nb,other |
|
80 | return nb,other | |
81 |
|
81 | |||
82 | def transform(self, nb, other): |
|
82 | def transform(self, nb, other): | |
83 | """ |
|
83 | """ | |
84 | Sphinx transformation to apply on each notebook. |
|
84 | Sphinx transformation to apply on each notebook. | |
85 | """ |
|
85 | """ | |
86 |
|
86 | |||
87 | # TODO: Add versatile method of additional notebook metadata. Include |
|
87 | # TODO: Add versatile method of additional notebook metadata. Include | |
88 | # handling of multiple files. For now use a temporay namespace, |
|
88 | # handling of multiple files. For now use a temporay namespace, | |
89 | # '_draft' to signify that this needs to change. |
|
89 | # '_draft' to signify that this needs to change. | |
90 | nb.metadata._draft = {} |
|
90 | if not "_draft" in nb.metadata: | |
|
91 | nb.metadata._draft = {} | |||
91 |
|
92 | |||
92 | if self.interactive: |
|
93 | if self.interactive: | |
93 |
|
94 | |||
94 | # Prompt the user for additional meta data that doesn't exist currently |
|
95 | # Prompt the user for additional meta data that doesn't exist currently | |
95 | # but would be usefull for Sphinx. |
|
96 | # but would be usefull for Sphinx. | |
96 | nb.metadata._draft["author"] = self._prompt_author() |
|
97 | nb.metadata._draft["author"] = self._prompt_author() | |
97 | nb.metadata._draft["version"] = self._prompt_version() |
|
98 | nb.metadata._draft["version"] = self._prompt_version() | |
98 | nb.metadata._draft["release"] = self._prompt_release() |
|
99 | nb.metadata._draft["release"] = self._prompt_release() | |
99 | nb.metadata._draft["date"] = self._prompt_date() |
|
100 | nb.metadata._draft["date"] = self._prompt_date() | |
100 |
|
101 | |||
101 | # Prompt the user for the document style. |
|
102 | # Prompt the user for the document style. | |
102 | other["sphinx_chapterstyle"] = self._prompt_chapter_title_style() |
|
103 | other["sphinx_chapterstyle"] = self._prompt_chapter_title_style() | |
103 | else: |
|
104 | else: | |
104 |
|
105 | |||
105 | # Try to use the traitlets. |
|
106 | # Try to use the traitlets. | |
106 | nb.metadata._draft["author"] = self.author |
|
107 | nb.metadata._draft["author"] = self.author | |
107 | nb.metadata._draft["version"] = self.version |
|
108 | nb.metadata._draft["version"] = self.version | |
108 | nb.metadata._draft["release"] = self.release |
|
109 | nb.metadata._draft["release"] = self.release | |
109 |
|
110 | |||
110 | if len(self.publishdate.strip()) == 0: |
|
111 | if len(self.publishdate.strip()) == 0: | |
111 | nb.metadata._draft["date"] = date.today().strftime("%B %-d, %Y") |
|
112 | nb.metadata._draft["date"] = date.today().strftime("%B %-d, %Y") | |
112 | else: |
|
113 | else: | |
113 | nb.metadata._draft["date"] = self.publishdate |
|
114 | nb.metadata._draft["date"] = self.publishdate | |
114 |
|
115 | |||
115 | other["sphinx_chapterstyle"] = self.chapterstyle |
|
116 | other["sphinx_chapterstyle"] = self.chapterstyle | |
116 |
|
117 | |||
117 | # Find and pass in the path to the Sphinx dependencies. |
|
118 | # Find and pass in the path to the Sphinx dependencies. | |
118 | other["sphinx_texinputs"] = os.path.abspath(sphinx.__file__ + "/../texinputs") |
|
119 | other["sphinx_texinputs"] = os.path.abspath(sphinx.__file__ + "/../texinputs") | |
119 |
|
120 | |||
120 | # Generate Pygments definitions for Latex |
|
121 | # Generate Pygments definitions for Latex | |
121 | other["pygment_definitions"] = self._generate_pygments_latex_def() |
|
122 | other["pygment_definitions"] = self._generate_pygments_latex_def() | |
122 |
|
123 | |||
123 | # End |
|
124 | # End | |
124 | return nb, other |
|
125 | return nb, other | |
125 |
|
126 | |||
126 | def _generate_pygments_latex_def(self): |
|
127 | def _generate_pygments_latex_def(self): | |
127 | return LatexFormatter().get_style_defs() |
|
128 | return LatexFormatter().get_style_defs() | |
128 |
|
129 | |||
129 | def _prompt_author(self): |
|
130 | def _prompt_author(self): | |
130 | return self._input("Author name: ") |
|
131 | return self._input("Author name: ") | |
131 |
|
132 | |||
132 | def _prompt_version(self): |
|
133 | def _prompt_version(self): | |
133 | return self._input("Version (ie ""1.0.0""): ") |
|
134 | return self._input("Version (ie ""1.0.0""): ") | |
134 |
|
135 | |||
135 | def _prompt_release(self): |
|
136 | def _prompt_release(self): | |
136 | return self._input("Release Name (ie ""Rough draft""): ") |
|
137 | return self._input("Release Name (ie ""Rough draft""): ") | |
137 |
|
138 | |||
138 | def _prompt_date(self): |
|
139 | def _prompt_date(self): | |
139 | default_date = date.today().strftime("%B %-d, %Y") |
|
140 | default_date = date.today().strftime("%B %-d, %Y") | |
140 | user_date = self._input("Date (deafults to \"" + default_date + "\"): ") |
|
141 | user_date = self._input("Date (deafults to \"" + default_date + "\"): ") | |
141 | if len(user_date.strip()) == 0: |
|
142 | if len(user_date.strip()) == 0: | |
142 | user_date = default_date |
|
143 | user_date = default_date | |
143 | return user_date |
|
144 | return user_date | |
144 |
|
145 | |||
145 | def _prompt_chapter_title_style(self): |
|
146 | def _prompt_chapter_title_style(self): | |
146 |
|
147 | |||
147 | # Dictionary of available Sphinx styles |
|
148 | # Dictionary of available Sphinx styles | |
148 | styles = {1: "Bjarne", |
|
149 | styles = {1: "Bjarne", | |
149 | 2: "Lenny", |
|
150 | 2: "Lenny", | |
150 | 3: "Glenn", |
|
151 | 3: "Glenn", | |
151 | 4: "Conny", |
|
152 | 4: "Conny", | |
152 | 5: "Rejne", |
|
153 | 5: "Rejne", | |
153 | 6: "Sonny"} |
|
154 | 6: "Sonny"} | |
154 | default_style = 1 |
|
155 | default_style = 1 | |
155 |
|
156 | |||
156 | # Build the menu that will be displayed to the user with |
|
157 | # Build the menu that will be displayed to the user with | |
157 | # all of the options available. |
|
158 | # all of the options available. | |
158 | style_prompt = "" |
|
159 | style_prompt = "" | |
159 | for key, value in styles.iteritems(): |
|
160 | for key, value in styles.iteritems(): | |
160 | style_prompt += "%d %s" % (key, value) |
|
161 | style_prompt += "%d %s" % (key, value) | |
161 | if key == default_style: |
|
162 | if key == default_style: | |
162 | style_prompt += " (default)" |
|
163 | style_prompt += " (default)" | |
163 | elif value == "Sonny": |
|
164 | elif value == "Sonny": | |
164 | style_prompt += " (for international documents)" |
|
165 | style_prompt += " (for international documents)" | |
165 | style_prompt += "\n" |
|
166 | style_prompt += "\n" | |
166 |
|
167 | |||
167 | # Continue to ask the user for a style until an appropriate |
|
168 | # Continue to ask the user for a style until an appropriate | |
168 | # one is specified. |
|
169 | # one is specified. | |
169 | response = -1 |
|
170 | response = -1 | |
170 | while (0 > response or response > 6): |
|
171 | while (0 > response or response > 6): | |
171 | try: |
|
172 | try: | |
172 | text_response = self._input(style_prompt) |
|
173 | text_response = self._input(style_prompt) | |
173 |
|
174 | |||
174 | # Use default option if no input. |
|
175 | # Use default option if no input. | |
175 | if len(text_response.strip()) == 0: |
|
176 | if len(text_response.strip()) == 0: | |
176 | response = 1 |
|
177 | response = 1 | |
177 | else: |
|
178 | else: | |
178 | response = int(text_response) |
|
179 | response = int(text_response) | |
179 | except: |
|
180 | except: | |
180 | print("Error: Value must be a number between 1 and 6, leave blank for default\n") |
|
181 | print("Error: Value must be a number between 1 and 6, leave blank for default\n") | |
181 | return styles[response] |
|
182 | return styles[response] | |
182 |
|
183 | |||
183 | def _input(self, prompt_text): |
|
184 | def _input(self, prompt_text): | |
184 | """ |
|
185 | """ | |
185 | Prompt the user for input. |
|
186 | Prompt the user for input. | |
186 |
|
187 | |||
187 | The input command will change depending on the version of python |
|
188 | The input command will change depending on the version of python | |
188 | installed. To maintain support for 2 and earlier, we must use |
|
189 | installed. To maintain support for 2 and earlier, we must use | |
189 | raw_input in that case. Else use input. |
|
190 | raw_input in that case. Else use input. | |
190 | """ |
|
191 | """ | |
191 |
|
192 | |||
192 | # Try to get the python version. This command is only available in |
|
193 | # Try to get the python version. This command is only available in | |
193 | # python 2 and later, so it's important that we catch the exception |
|
194 | # python 2 and later, so it's important that we catch the exception | |
194 | # if the command isn't found. |
|
195 | # if the command isn't found. | |
195 | try: |
|
196 | try: | |
196 | majorversion = sys.version_info[0] |
|
197 | majorversion = sys.version_info[0] | |
197 | except: |
|
198 | except: | |
198 | majorversion = 1 |
|
199 | majorversion = 1 | |
199 |
|
200 | |||
200 | # Use the correct function to prompt the user for input depending on |
|
201 | # Use the correct function to prompt the user for input depending on | |
201 | # what python version the code is running in. |
|
202 | # what python version the code is running in. | |
202 | if majorversion >= 3: |
|
203 | if majorversion >= 3: | |
203 | return input(prompt_text) |
|
204 | return input(prompt_text) | |
204 | else: |
|
205 | else: | |
205 | return raw_input(prompt_text) |
|
206 | return raw_input(prompt_text) |
General Comments 0
You need to be logged in to leave comments.
Login now