Miki Tebeka avatar Miki Tebeka committed 22ebd22

Dynamic js and content-type

Comments (0)

Files changed (3)

             Some random text here ...
         </p>
     </body>
-    <script src="jquery.js"></script>
-    <script>
-
-        function get_options(cls) {
-            var options = {};
-            var parts = cls.split(" ");
-            for (var i in parts) {
-                var part = parts[i];
-                var subparts = part.split('=');
-                if (subparts.length == 1) {
-                    if (subparts[0] == 'linenos') {
-                        options['linenos'] = true;
-                    }
-                }
-                else if (subparts.length == 2) {
-                    options[subparts[0]] = subparts[1];
-                }
-            }
-
-            return options;
-        }
-
-        function highlight() {
-            var obj = $('pre.highlight');
-            var options = get_options(obj.attr('class'));
-            options['code'] = obj.text();
-            obj.load("http://localhost:8080/highlight", options);
-        }
-
-        function on_ready() {
-            highlight();
-        }
-
-        $(document).ready(on_ready);
-    </script>
+    <script src="http://localhost:8080/js"></script>
 </html>
+function get_options(cls) {
+    var options = {};
+    var parts = cls.split(" ");
+    for (var i in parts) {
+        var part = parts[i];
+        var subparts = part.split('=');
+        if (subparts.length == 1) {
+            if (subparts[0] == 'linenos') {
+                options['linenos'] = true;
+            }
+        }
+        else if (subparts.length == 2) {
+            options[subparts[0]] = subparts[1];
+        }
+    }
+
+    return options;
+}
+
+function highlight() {
+    var obj = $('pre.highlight');
+    var options = get_options(obj.attr('class'));
+    options['code'] = obj.text();
+    obj.load("http://localhost:8080/highlight", options);
+}
+
+$(document).ready(highlight);
 #!/usr/bin/env python
 
-from cherrypy import expose, quickstart
+from cherrypy import expose, quickstart, tools
 from pygments import highlight
 from pygments.lexers import get_lexer_by_name, guess_lexer
 from pygments.formatters import HtmlFormatter
         return highlight(code, lexer, formatter)
 
     @expose
+    @tools.response_headers(headers=[("Content-Type", "text/css")])
     def css(self, **kw):
         return HtmlFormatter().get_style_defs()
 
     @expose
+    @tools.response_headers(headers=[("Content-Type", "application/json")])
+    def js(self, **kw):
+        with open("jquery.js") as fo:
+            jquery = fo.read()
+        with open("highlight.js") as fo:
+            script = fo.read()
+
+        return "%s\n%s" % (jquery, script)
+
+    @expose
     def demo(self, **kw):
         with open("demo.html") as fo:
             return fo.read()
 
 if __name__ == "__main__":
-    from os.path import dirname, join, abspath
-
-    root = dirname(abspath(__file__))
-    config = {
-        "/jquery.js" : {
-            "tools.staticfile.on": 1,
-            "tools.staticfile.filename": join(root, "jquery.js"),
-        }
-    }
-
-    quickstart(Highlighter(), config=config)
+    quickstart(Highlighter())
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.