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