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