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