##// END OF EJS Templates
Update reveal.js CDN to 2.5.0.
damianavila -
Show More
@@ -1,112 +1,112
1 """PostProcessor for serving reveal.js HTML slideshows."""
1 """PostProcessor for serving reveal.js HTML slideshows."""
2 from __future__ import print_function
2 from __future__ import print_function
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 #Copyright (c) 2013, the IPython Development Team.
4 #Copyright (c) 2013, the IPython Development Team.
5 #
5 #
6 #Distributed under the terms of the Modified BSD License.
6 #Distributed under the terms of the Modified BSD License.
7 #
7 #
8 #The full license is in the file COPYING.txt, distributed with this software.
8 #The full license is in the file COPYING.txt, distributed with this software.
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Imports
12 # Imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 import os
15 import os
16 import webbrowser
16 import webbrowser
17
17
18 from tornado import web, ioloop, httpserver
18 from tornado import web, ioloop, httpserver
19 from tornado.httpclient import AsyncHTTPClient
19 from tornado.httpclient import AsyncHTTPClient
20
20
21 from IPython.utils.traitlets import Bool, Unicode, Int
21 from IPython.utils.traitlets import Bool, Unicode, Int
22
22
23 from .base import PostProcessorBase
23 from .base import PostProcessorBase
24
24
25 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
26 # Classes
26 # Classes
27 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
28
28
29 class ProxyHandler(web.RequestHandler):
29 class ProxyHandler(web.RequestHandler):
30 """handler the proxies requests from a local prefix to a CDN"""
30 """handler the proxies requests from a local prefix to a CDN"""
31 @web.asynchronous
31 @web.asynchronous
32 def get(self, prefix, url):
32 def get(self, prefix, url):
33 """proxy a request to a CDN"""
33 """proxy a request to a CDN"""
34 proxy_url = "/".join([self.settings['cdn'], url])
34 proxy_url = "/".join([self.settings['cdn'], url])
35 client = self.settings['client']
35 client = self.settings['client']
36 client.fetch(proxy_url, callback=self.finish_get)
36 client.fetch(proxy_url, callback=self.finish_get)
37
37
38 def finish_get(self, response):
38 def finish_get(self, response):
39 """finish the request"""
39 """finish the request"""
40 # copy potentially relevant headers
40 # copy potentially relevant headers
41 for header in ["Content-Type", "Cache-Control", "Date", "Last-Modified", "Expires"]:
41 for header in ["Content-Type", "Cache-Control", "Date", "Last-Modified", "Expires"]:
42 if header in response.headers:
42 if header in response.headers:
43 self.set_header(header, response.headers[header])
43 self.set_header(header, response.headers[header])
44 self.finish(response.body)
44 self.finish(response.body)
45
45
46 class ServePostProcessor(PostProcessorBase):
46 class ServePostProcessor(PostProcessorBase):
47 """Post processor designed to serve files
47 """Post processor designed to serve files
48
48
49 Proxies reveal.js requests to a CDN if no local reveal.js is present
49 Proxies reveal.js requests to a CDN if no local reveal.js is present
50 """
50 """
51
51
52
52
53 open_in_browser = Bool(True, config=True,
53 open_in_browser = Bool(True, config=True,
54 help="""Should the browser be opened automatically?"""
54 help="""Should the browser be opened automatically?"""
55 )
55 )
56 reveal_cdn = Unicode("https://cdn.jsdelivr.net/reveal.js/2.4.0", config=True,
56 reveal_cdn = Unicode("https://cdn.jsdelivr.net/reveal.js/2.5.0", config=True,
57 help="""URL for reveal.js CDN."""
57 help="""URL for reveal.js CDN."""
58 )
58 )
59 reveal_prefix = Unicode("reveal.js", config=True, help="URL prefix for reveal.js")
59 reveal_prefix = Unicode("reveal.js", config=True, help="URL prefix for reveal.js")
60 ip = Unicode("127.0.0.1", config=True, help="The IP address to listen on.")
60 ip = Unicode("127.0.0.1", config=True, help="The IP address to listen on.")
61 port = Int(8000, config=True, help="port for the server to listen on.")
61 port = Int(8000, config=True, help="port for the server to listen on.")
62
62
63 def postprocess(self, input):
63 def postprocess(self, input):
64 """Serve the build directory with a webserver."""
64 """Serve the build directory with a webserver."""
65 dirname, filename = os.path.split(input)
65 dirname, filename = os.path.split(input)
66 handlers = [
66 handlers = [
67 (r"/(.+)", web.StaticFileHandler, {'path' : dirname}),
67 (r"/(.+)", web.StaticFileHandler, {'path' : dirname}),
68 (r"/", web.RedirectHandler, {"url": "/%s" % filename})
68 (r"/", web.RedirectHandler, {"url": "/%s" % filename})
69 ]
69 ]
70
70
71 if ('://' in self.reveal_prefix or self.reveal_prefix.startswith("//")):
71 if ('://' in self.reveal_prefix or self.reveal_prefix.startswith("//")):
72 # reveal specifically from CDN, nothing to do
72 # reveal specifically from CDN, nothing to do
73 pass
73 pass
74 elif os.path.isdir(os.path.join(dirname, self.reveal_prefix)):
74 elif os.path.isdir(os.path.join(dirname, self.reveal_prefix)):
75 # reveal prefix exists
75 # reveal prefix exists
76 self.log.info("Serving local %s", self.reveal_prefix)
76 self.log.info("Serving local %s", self.reveal_prefix)
77 else:
77 else:
78 self.log.info("Redirecting %s requests to %s", self.reveal_prefix, self.reveal_cdn)
78 self.log.info("Redirecting %s requests to %s", self.reveal_prefix, self.reveal_cdn)
79 handlers.insert(0, (r"/(%s)/(.*)" % self.reveal_prefix, ProxyHandler))
79 handlers.insert(0, (r"/(%s)/(.*)" % self.reveal_prefix, ProxyHandler))
80
80
81 app = web.Application(handlers,
81 app = web.Application(handlers,
82 cdn=self.reveal_cdn,
82 cdn=self.reveal_cdn,
83 client=AsyncHTTPClient(),
83 client=AsyncHTTPClient(),
84 )
84 )
85 # hook up tornado logging to our logger
85 # hook up tornado logging to our logger
86 try:
86 try:
87 from tornado import log
87 from tornado import log
88 log.app_log = self.log
88 log.app_log = self.log
89 except ImportError:
89 except ImportError:
90 # old tornado (<= 3), ignore
90 # old tornado (<= 3), ignore
91 pass
91 pass
92
92
93 http_server = httpserver.HTTPServer(app)
93 http_server = httpserver.HTTPServer(app)
94 http_server.listen(self.port, address=self.ip)
94 http_server.listen(self.port, address=self.ip)
95 url = "http://%s:%i/%s" % (self.ip, self.port, filename)
95 url = "http://%s:%i/%s" % (self.ip, self.port, filename)
96 print("Serving your slides at %s" % url)
96 print("Serving your slides at %s" % url)
97 print("Use Control-C to stop this server")
97 print("Use Control-C to stop this server")
98 if self.open_in_browser:
98 if self.open_in_browser:
99 webbrowser.open(url, new=2)
99 webbrowser.open(url, new=2)
100 try:
100 try:
101 ioloop.IOLoop.instance().start()
101 ioloop.IOLoop.instance().start()
102 except KeyboardInterrupt:
102 except KeyboardInterrupt:
103 print("\nInterrupted")
103 print("\nInterrupted")
104
104
105 def main(path):
105 def main(path):
106 """allow running this module to serve the slides"""
106 """allow running this module to serve the slides"""
107 server = ServePostProcessor()
107 server = ServePostProcessor()
108 server(path)
108 server(path)
109
109
110 if __name__ == '__main__':
110 if __name__ == '__main__':
111 import sys
111 import sys
112 main(sys.argv[1])
112 main(sys.argv[1])
General Comments 0
You need to be logged in to leave comments. Login now