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