forked from n-riesco/ijavascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoc-build.py
executable file
·317 lines (258 loc) · 8.88 KB
/
doc-build.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""\
Python script to build documentation in folder gh-pages.
"""
# BSD 3-Clause License
#
# Copyright (c) 2015, Nicolas Riesco and others as credited in the AUTHORS file
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors
# may be used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import json
import os
import shutil
import subprocess
import urllib2
import IPython
import IPython.nbconvert as nbconvert
import IPython.nbformat.v3 as nbformat
import jinja2
class Exporter(nbconvert.HTMLExporter):
def __init__(self, template_file="full.tpl", **kw):
if IPython.version_info[0] < 2:
# Template "basic.tpl" is missing in IPython v1,
# use fake "compat/basic.tpl" instead.
super(nbconvert.HTMLExporter, self).__init__(extra_loaders=[
jinja2.FileSystemLoader([
config["in_tpl"],
config["in_tpl_compat"],
]),
], **kw)
else:
super(nbconvert.HTMLExporter, self).__init__(extra_loaders=[
jinja2.FileSystemLoader([
config["in_tpl"],
]),
], **kw)
self.template_file = template_file
def render(title, infilename, outfilename):
if os.path.basename(os.path.dirname(outfilename)) == "doc":
exporter = config["exporter_doc"]
else:
exporter = config["exporter_root"]
if infilename.endswith(".md"):
render_markdown(exporter, title, infilename, outfilename)
else:
render_notebook(exporter, title, infilename, outfilename)
def render_notebook(exporter, title, infilename, outfilename):
resources = {
"title": title,
"navbar": config["navbar"],
}
html, resources = exporter.from_filename(
infilename,
resources=resources
)
with open(outfilename, "w") as outfile:
outfile.write(html)
def render_markdown(exporter, title, infilename, outfilename):
notebook = config["nb_empty"]
resources = {
"title": title,
"navbar": config["navbar"],
"md": subprocess.check_output([
"pandoc",
"--from=markdown_github-hard_line_breaks",
"--to=html",
infilename,
])
}
html, resources = exporter.from_notebook_node(
notebook,
resources=resources
)
with open(outfilename, "w") as outfile:
outfile.write(html)
def render_css(outfilename):
exporter = config["exporter_css"]
notebook = config["nb_empty"]
resources = {}
css, resources = exporter.from_notebook_node(
notebook,
resources=resources
)
with open(outfilename, "w") as outfile:
outfile.write(css)
def make_folders():
if os.path.exists(config["out"]):
shutil.rmtree(config["out"])
for path in [
config["out"],
config["out_doc"],
config["out_jsdoc"],
config["out_js"],
config["out_css"],
]:
os.mkdir(path)
def copy_images():
if os.path.exists(config["out_images"]):
shutil.rmtree(config["out_images"])
shutil.copytree(
config["in_images"],
config["out_images"],
)
def build_css():
nbcss = "nb.css"
render_css(
os.path.join(config["out_css"], nbcss)
)
customcss = "custom.css"
shutil.copyfile(
os.path.join(config["in_tpl"], customcss),
os.path.join(config["out_css"], customcss)
)
def build_root():
render(
"Overview",
os.path.join(config["root"], "README.md"),
os.path.join(config["out"], "index.html")
)
render(
"Contribution guidelines",
os.path.join(config["root"], "CONTRIBUTING.md"),
os.path.join(config["out"], "contributing.html")
)
def build_doc():
navbar = config["navbar"]
docs = [
("Installation", navbar["Installation"]),
("Usage", navbar["Usage"]),
]
for title, filename in navbar["Features"].iteritems():
docs.append(
(title[4:], filename)
)
for author, tutorials in navbar["Tutorials"].iteritems():
for title, filename in tutorials.iteritems():
docs.append(
(author + ": " + title[4:], filename)
)
for title, filename in docs:
render(
title,
os.path.join(config["in_doc"], filename),
os.path.join(config["out_doc"], filename + ".html")
)
def build_jsdoc():
outfolder = config["out_jsdoc"]
if os.path.exists(outfolder):
shutil.rmtree(outfolder)
subprocess.check_call([
"jsdoc",
"-c",
os.path.join(config["in_jsdoc"], "conf.json"),
])
def download_libs():
urls = [
"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css",
]
folder = config["out_css"]
download(urls, folder)
urls = [
"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js",
"https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.2/html5shiv.min.js",
"https://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.min.js",
"https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.10/require.min.js",
"https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js",
]
folder = config["out_js"]
download(urls, folder)
def download(urls, folder):
for url in urls:
filename = url.split("/")[-1].split("?")[0]
outfilename = os.path.join(folder, filename)
tmpfilename = outfilename + ".tmp"
try:
with open(tmpfilename, "wb") as tmpfile:
shutil.copyfileobj(
urllib2.urlopen(url, timeout=10),
tmpfile
)
except urllib2.URLError:
os.remove(tmpfilename)
raise
os.rename(outfilename + ".tmp", outfilename)
def main():
print "Building IJavascript website..."
make_folders()
copy_images()
build_css()
build_root()
build_doc()
print "Generating JSDoc documentation..."
build_jsdoc()
print "Downloading CSS and JS libraries..."
try:
download_libs()
print "Done."
except urllib2.URLError:
print "Download failed."
# Global settings
config = {}
# Folders
config["root"] = os.path.dirname(
os.path.realpath(os.path.dirname(__file__))
)
config["in_images"] = os.path.join(config["root"], "images")
config["in_doc"] = os.path.join(config["root"], "doc")
config["in_tpl"] = os.path.join(config["in_doc"], "nbconvert")
config["in_tpl_compat"] = os.path.join(config["in_tpl"], "compat")
config["in_jsdoc"] = os.path.join(config["root"], "jsdoc")
config["out"] = os.path.join(config["root"], "gh-pages")
config["out_doc"] = os.path.join(config["out"], "doc")
config["out_jsdoc"] = os.path.join(config["out"], "jsdoc")
config["out_images"] = os.path.join(config["out"], "images")
config["out_js"] = os.path.join(config["out"], "js")
config["out_css"] = os.path.join(config["out"], "css")
# Empty notebook
config["nb_empty"] = nbformat.NotebookNode({
"metadata": {
"name": ""
},
"worksheets": [],
"cells": []
})
# List of notebooks
with open(os.path.join(config["in_doc"], "navbar.json")) as infile:
config["navbar"] = json.load(infile)
# Exporters
config["exporter_root"] = Exporter("root.tpl") # For files in the root folder
config["exporter_doc"] = Exporter("doc.tpl") # For files in the doc folder
config["exporter_css"] = Exporter("css.tpl") # To generate nbconvert CSS
if __name__ == "__main__":
main()