Show More
@@ -0,0 +1,182 b'' | |||||
|
1 | """ | |||
|
2 | Module that allows custom Sphinx parameters to be set on the notebook and | |||
|
3 | on the 'other' object passed into Jinja. | |||
|
4 | """ | |||
|
5 | ||||
|
6 | # Used to find Sphinx package location | |||
|
7 | import sphinx | |||
|
8 | import os.path | |||
|
9 | ||||
|
10 | # Used to determine python version | |||
|
11 | import sys | |||
|
12 | ||||
|
13 | # Used to set the default date to today's date | |||
|
14 | from datetime import date | |||
|
15 | ||||
|
16 | # Configurable traitlets | |||
|
17 | from IPython.utils.traitlets import Unicode, Bool | |||
|
18 | ||||
|
19 | # Needed to override transformer | |||
|
20 | from converters.transformers import (ConfigurableTransformers) | |||
|
21 | ||||
|
22 | class SphinxTransformer(ConfigurableTransformers): | |||
|
23 | """ | |||
|
24 | Sphinx utility transformer. | |||
|
25 | ||||
|
26 | This transformer is used to set variables needed by the latex to build | |||
|
27 | Sphinx stylized templates. | |||
|
28 | """ | |||
|
29 | ||||
|
30 | interactive = Bool(True, config=True, help=""" | |||
|
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, | |||
|
33 | the author, version, release, date, and chapterstyle traits should | |||
|
34 | be set. | |||
|
35 | """) | |||
|
36 | ||||
|
37 | author = Unicode("Unknown Author", config=True, help="Author name") | |||
|
38 | ||||
|
39 | version = Unicode("", config=True, help="""Version number | |||
|
40 | You can leave this blank if you do not want to render a version number. | |||
|
41 | Example: "1.0.0" | |||
|
42 | """) | |||
|
43 | ||||
|
44 | release = Unicode("", config=True, help="""Release name | |||
|
45 | You can leave this blank if you do not want to render a release name. | |||
|
46 | Example: "Rough Draft" | |||
|
47 | """) | |||
|
48 | ||||
|
49 | publishdate = Unicode("", config=True, help="""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. | |||
|
52 | Example: "June 12, 1990" | |||
|
53 | """) | |||
|
54 | ||||
|
55 | chapterstyle = Unicode("Bjarne", config=True, help="""Sphinx chapter style | |||
|
56 | This is the style to use for the chapter headers in the document. | |||
|
57 | You may choose one of the following: | |||
|
58 | "Bjarne" (default) | |||
|
59 | "Lenny" | |||
|
60 | "Glenn" | |||
|
61 | "Conny" | |||
|
62 | "Rejne" | |||
|
63 | "Sonny" (used for international documents) | |||
|
64 | """) | |||
|
65 | ||||
|
66 | ||||
|
67 | def __call__(self, nb, other): | |||
|
68 | """ | |||
|
69 | Sphinx transformation to apply on each notebook. | |||
|
70 | ||||
|
71 | Since we are not interested in any additional manipulation on a cell | |||
|
72 | by cell basis, we do not call the base implementation. | |||
|
73 | """ | |||
|
74 | ||||
|
75 | if self.interactive: | |||
|
76 | ||||
|
77 | # Prompt the user for additional meta data that doesn't exist currently | |||
|
78 | # but would be usefull for Sphinx. | |||
|
79 | nb.metadata["author"] = self._prompt_author() | |||
|
80 | nb.metadata["version"] = self._prompt_version() | |||
|
81 | nb.metadata["release"] = self._prompt_release() | |||
|
82 | nb.metadata["date"] = self._prompt_date() | |||
|
83 | ||||
|
84 | # Prompt the user for the document style. | |||
|
85 | other["sphinx_chapterstyle"] = self._prompt_chapter_title_style() | |||
|
86 | else: | |||
|
87 | ||||
|
88 | # Try to use the traitlets. | |||
|
89 | nb.metadata["author"] = self.author | |||
|
90 | nb.metadata["version"] = self.version | |||
|
91 | nb.metadata["release"] = self.release | |||
|
92 | ||||
|
93 | if len(self.publishdate.strip()) == 0: | |||
|
94 | nb.metadata["date"] = date.today().strftime("%B %-d, %Y") | |||
|
95 | else: | |||
|
96 | nb.metadata["date"] = self.publishdate | |||
|
97 | ||||
|
98 | other["sphinx_chapterstyle"] = self.chapterstyle | |||
|
99 | ||||
|
100 | # Find and pass in the path to the Sphinx dependencies. | |||
|
101 | other["sphinx_texinputs"] = os.path.abspath(sphinx.__file__ + "/../texinputs") | |||
|
102 | ||||
|
103 | # End | |||
|
104 | return nb, other | |||
|
105 | ||||
|
106 | def _prompt_author(self): | |||
|
107 | return self._input("Author name: ") | |||
|
108 | ||||
|
109 | def _prompt_version(self): | |||
|
110 | return self._input("Version (ie ""1.0.0""): ") | |||
|
111 | ||||
|
112 | def _prompt_release(self): | |||
|
113 | return self._input("Release Name (ie ""Rough draft""): ") | |||
|
114 | ||||
|
115 | def _prompt_date(self): | |||
|
116 | default_date = date.today().strftime("%B %-d, %Y") | |||
|
117 | user_date = self._input("Date (deafults to \"" + default_date + "\"): ") | |||
|
118 | if len(user_date.strip()) == 0: | |||
|
119 | user_date = default_date | |||
|
120 | return user_date | |||
|
121 | ||||
|
122 | def _prompt_chapter_title_style(self): | |||
|
123 | ||||
|
124 | # Dictionary of available Sphinx styles | |||
|
125 | styles = {1: "Bjarne", | |||
|
126 | 2: "Lenny", | |||
|
127 | 3: "Glenn", | |||
|
128 | 4: "Conny", | |||
|
129 | 5: "Rejne", | |||
|
130 | 6: "Sonny"} | |||
|
131 | default_style = 1 | |||
|
132 | ||||
|
133 | # Build the menu that will be displayed to the user with | |||
|
134 | # all of the options available. | |||
|
135 | style_prompt = "" | |||
|
136 | for key, value in styles.iteritems(): | |||
|
137 | style_prompt += "%d %s" % (key, value) | |||
|
138 | if key == default_style: | |||
|
139 | style_prompt += " (default)" | |||
|
140 | elif value == "Sonny": | |||
|
141 | style_prompt += " (for international documents)" | |||
|
142 | style_prompt += "\n" | |||
|
143 | ||||
|
144 | # Continue to ask the user for a style until an appropriate | |||
|
145 | # one is specified. | |||
|
146 | response = -1 | |||
|
147 | while (0 > response or response > 6): | |||
|
148 | try: | |||
|
149 | text_response = self._input(style_prompt) | |||
|
150 | ||||
|
151 | # Use default option if no input. | |||
|
152 | if len(text_response.strip()) == 0: | |||
|
153 | response = 1 | |||
|
154 | else: | |||
|
155 | response = int(text_response) | |||
|
156 | except: | |||
|
157 | print("Error: Value must be a number between 1 and 6, leave blank for default\n") | |||
|
158 | return styles[response] | |||
|
159 | ||||
|
160 | def _input(self, prompt_text): | |||
|
161 | """ | |||
|
162 | Prompt the user for input. | |||
|
163 | ||||
|
164 | The input command will change depending on the version of python | |||
|
165 | installed. To maintain support for 2 and earlier, we must use | |||
|
166 | raw_input in that case. Else use input. | |||
|
167 | """ | |||
|
168 | ||||
|
169 | # Try to get the python version. This command is only available in | |||
|
170 | # python 2 and later, so it's important that we catch the exception | |||
|
171 | # if the command isn't found. | |||
|
172 | try: | |||
|
173 | majorversion = sys.version_info[0] | |||
|
174 | except: | |||
|
175 | majorversion = 1 | |||
|
176 | ||||
|
177 | # Use the correct function to prompt the user for input depending on | |||
|
178 | # what python version the code is running in. | |||
|
179 | if majorversion >= 3: | |||
|
180 | return input(prompt_text) | |||
|
181 | else: | |||
|
182 | return raw_input(prompt_text) |
@@ -29,6 +29,7 b' from IPython.config.loader import ConfigFileNotFound' | |||||
29 | from IPython.utils.traitlets import Unicode, Bool |
|
29 | from IPython.utils.traitlets import Unicode, Bool | |
30 |
|
30 | |||
31 | from converters.transformers import (ExtractFigureTransformer) |
|
31 | from converters.transformers import (ExtractFigureTransformer) | |
|
32 | from converters.sphinx_transformer import (SphinxTransformer) | |||
32 |
|
33 | |||
33 | from converters.config import GlobalConfigurable |
|
34 | from converters.config import GlobalConfigurable | |
34 |
|
35 | |||
@@ -105,7 +106,13 b' class NbconvertApp(Application):' | |||||
105 | """Convert a notebook in one step""" |
|
106 | """Convert a notebook in one step""" | |
106 | ipynb_file = (self.extra_args or [None])[2] |
|
107 | ipynb_file = (self.extra_args or [None])[2] | |
107 |
|
108 | |||
108 | C = ConverterTemplate(config=self.config) |
|
109 | # If you are writting a custom transformer, append it to the dictionary | |
|
110 | # below. | |||
|
111 | userpreprocessors = {} | |||
|
112 | userpreprocessors["SphinxTransformer"] = SphinxTransformer(config=self.config) # Sphinx templates | |||
|
113 | ||||
|
114 | # Create the converter | |||
|
115 | C = ConverterTemplate(config=self.config, preprocessors=userpreprocessors) | |||
109 |
|
116 | |||
110 | output, resources = C.from_filename(ipynb_file) |
|
117 | output, resources = C.from_filename(ipynb_file) | |
111 | if self.stdout : |
|
118 | if self.stdout : |
@@ -1,8 +1,14 b'' | |||||
1 | c = get_config() |
|
1 | c = get_config() | |
2 |
|
2 | |||
3 | #Inherit |
|
3 | # Inherit | |
4 | load_subconfig('latex_base.nbcv') |
|
4 | load_subconfig('latex_base.nbcv') | |
5 |
|
5 | |||
6 | #Overrides |
|
6 | # Overrides | |
7 | c.ConverterTemplate.template_file='latex_sphinx_base' |
|
7 | c.ConverterTemplate.template_file='latex_sphinx_base' | |
8 | c.NbconvertApp.write = True |
|
8 | c.NbconvertApp.write = True | |
|
9 | ||||
|
10 | #TODO: assign from c.ConverterTemplate.pre_transformer_order | |||
|
11 | Transformers = ['haspyout_transformer'] | |||
|
12 | Transformers.append('SphinxTransformer') # Add Sphinx transformer | |||
|
13 | c.ConverterTemplate.pre_transformer_order = Transformers | |||
|
14 |
@@ -11,13 +11,14 b'' | |||||
11 | ((*- extends 'latex_base.tplx' -*)) |
|
11 | ((*- extends 'latex_base.tplx' -*)) | |
12 |
|
12 | |||
13 | ((* block header *)) |
|
13 | ((* block header *)) | |
|
14 | ||||
14 | % Header, overrides base |
|
15 | % Header, overrides base | |
15 |
|
16 | |||
16 | % Make sure that the sphinx doc style knows who it inherits from. |
|
17 | % Make sure that the sphinx doc style knows who it inherits from. | |
17 | \def\sphinxdocclass{(((parentdocumentclass)))} |
|
18 | \def\sphinxdocclass{(((parentdocumentclass)))} | |
18 |
|
19 | |||
19 | % Declare the document class |
|
20 | % Declare the document class | |
20 | \documentclass[letterpaper,10pt,english]{sphinx(((documentclass)))} |
|
21 | \documentclass[letterpaper,10pt,english]{((( resources.sphinx_texinputs )))/sphinx(((documentclass)))} | |
21 |
|
22 | |||
22 | % Imports |
|
23 | % Imports | |
23 | \usepackage[utf8]{inputenc} |
|
24 | \usepackage[utf8]{inputenc} | |
@@ -26,18 +27,18 b'' | |||||
26 | \usepackage{babel} |
|
27 | \usepackage{babel} | |
27 | \usepackage{times} |
|
28 | \usepackage{times} | |
28 | \usepackage{import} |
|
29 | \usepackage{import} | |
29 | \usepackage[Bjarne]{fncychap} |
|
30 | \usepackage[((( resources.sphinx_chapterstyle )))]{((( resources.sphinx_texinputs )))/fncychap} | |
30 | \usepackage{longtable} |
|
31 | \usepackage{longtable} | |
31 | \usepackage{sphinx} |
|
32 | \usepackage{((( resources.sphinx_texinputs )))/sphinx} | |
32 | \usepackage{multirow} |
|
33 | \usepackage{multirow} | |
33 |
|
34 | |||
34 | % Document level variables |
|
35 | % Document level variables | |
35 | \title{((( nb.metadata.name )))} |
|
36 | \title{((( nb.metadata.name )))} | |
36 | \date{\today} |
|
37 | \date{((( nb.metadata.date )))} | |
37 | \release{} |
|
38 | \release{((( nb.metadata.version )))} | |
38 | \author{Unknown Author} |
|
39 | \author{((( nb.metadata.author )))} | |
39 | \newcommand{\sphinxlogo}{} |
|
40 | \newcommand{\sphinxlogo}{} | |
40 |
\renewcommand{\releasename}{ |
|
41 | \renewcommand{\releasename}{((( nb.metadata.release )))} | |
41 | \makeindex |
|
42 | \makeindex | |
42 |
|
43 | |||
43 |
|
44 |
General Comments 0
You need to be logged in to leave comments.
Login now