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