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