Commits

Anonymous committed 2e04fbd

Switch from StandaloneHTMLBuilder to SerializeHTMLBuilder

  • Participants
  • Parent commits 727a640

Comments (0)

Files changed (2)

File sphinx/builders/webapp/webapp.py

 import os
 import sys
 import shutil
+import cPickle
 from os import path
 
-from sphinx.builders.html import StandaloneHTMLBuilder
+from sphinx.builders.html import SerializingHTMLBuilder
 from sphinx.errors import SphinxError
 from sphinx.writers.html import HTMLTranslator
 
 DOC_PATH = 0
 DOC_TITLE = 1
 
-class WebAppBuilder(StandaloneHTMLBuilder):
+class WebAppBuilder(SerializingHTMLBuilder):
+    name = 'cPickled'
+    out_suffix = '.pkl'
+    implementation = cPickle
+    globalcontext_filename = 'globalcontext.pickle'
+    searchindex_filename = 'searchindex.pickle'
+
     def init(self):
         # _build/webapp/public - ordinary HTML files produced by Sphinx
         self.public_dir = path.join(self.outdir, 'public')
         self.outdir = self.public_dir
         self.app.add_javascript('jquery.form.js')
         self.app.add_javascript('comments.js')
-        StandaloneHTMLBuilder.init(self)
+        SerializingHTMLBuilder.init(self)
 
     def write_doc(self, docname, doctree):
         href = docname + self.out_suffix
             doc_contents = doctree.astext()
             title = doc_contents[:20]
             self.xapian_fill_db(href, title, doctree.astext())
-        StandaloneHTMLBuilder.write_doc(self, docname, doctree)
+        SerializingHTMLBuilder.write_doc(self, docname, doctree)
 
     def prepare_writing(self, docnames):
-        StandaloneHTMLBuilder.prepare_writing(self, docnames)
+        SerializingHTMLBuilder.prepare_writing(self, docnames)
 
     def finish(self):
-        StandaloneHTMLBuilder.finish(self)
+        SerializingHTMLBuilder.finish(self)
         self.outdir = self.html_dir
         self.handle_page('xapiansearch', {}, 'web/xapiansearch.html')
 
             afile = path.join(templates_dir, f)
             shutil.copyfile(afile, path.join(self.outdir, f))
 
+        src_dir = path.join(path.dirname(__file__), '../../themes/basic/static')
+        dst_dir = path.join(self.outdir, 'public/_static')
+        shutil.copytree(src_dir, dst_dir)
+
 
     ###
     ### Xapian search engine

File sphinx/web/middleware/appserver.py

 
 import locale
 from os import path
-from jinja2 import Environment, FileSystemLoader
+from cPickle import loads
+
+from jinja2 import Environment, FileSystemLoader, Template
 from webob import Request, Response
 
+from sphinx.application import TemplateBridge
+from sphinx.theming import Theme
+
+# it's here only temporarily, until I get all the theming problems resolved
+tmp_template = """
+<html>
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+<link rel="stylesheet" href="_static/basic.css" type="text/css" />
+<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
+<script type="text/javascript">
+    var DOCUMENTATION_OPTIONS = {
+       URL_ROOT:    '',
+       VERSION:     '0.5.2',
+       COLLAPSE_MODINDEX: false,
+       FILE_SUFFIX: '.html',
+       HAS_SOURCE:  true
+    };
+</script>
+<script type="text/javascript" src="_static/jquery.js"></script>
+<script type="text/javascript" src="_static/jquery.form.js"></script>
+<script type="text/javascript" src="_static/comments.js"></script>
+<script type="text/javascript" src="_static/doctools.js"></script>
+</head>
+<body>
+{{ body }}
+</body>
+</html>
+"""
+
 class AppServer(object):
-    def __init__(self, www_dir):
+    def __init__(self, www_dir, theme):
         self.env = None
         self.www_dir = www_dir
         self.template_vals = {}
         else:
             rel_file = req.path[1:]
 
+        # HTML files are being kept as '.pkl' files
+        if rel_file.endswith('.html'):
+            rel_file = rel_file.strip('.html') + '.pkl'
+
         # XXX: '../../../etc/passwd' bug? ;-)
         file_path = path.join(self.www_dir, rel_file)
 
+
         if req.path == '/add_comment':
             resp.status = 200
             resp.body = 'dzieks'
 
+
         if path.isfile(file_path):
             try:
-                file_contents = ''.join(open(file_path).read())
+                if req.path.endswith('.html'):
+                    file_contents = loads(open(file_path).read())['body']
+                else:
+                    file_contents = ''.join(open(file_path).read())
             except:
                 file_contents = '<html><body>doHTMLFile(): 404!</body></html>'
 
             resp.status = 200
-            resp.body = file_contents
+            resp.body = file_contents.encode('utf8')
 
             # Regular HTML file
-            if file_path.endswith('.html'):
+            if req.path.endswith('.html'):
                 resp = self.doHTMLFile(environ, file_contents)
             # CSS file
-            elif file_path.endswith('.css'):
+            elif req.path.endswith('.css'):
                 resp.content_type = 'text/css'
             # java script file
-            elif file_path.endswith('.js'):
+            elif req.path.endswith('.js'):
                 resp.content_type = 'text/javascript'
             # other kind of files
             else:
         r = Response(content_type='text/html', charset='utf8')
         resp = req.get_response(r)
 
-        html_code = unicode(file_contents,
-                            locale.getpreferredencoding(), 'replace')
+        rendered = Template(tmp_template).render({'body': file_contents})
+#        html_code = unicode(file_contents,
+#                            locale.getpreferredencoding(), 'replace')
 
-        self.render_openid(environ)
-        rendered = self.env.from_string(html_code).render(self.template_vals)
+
+#        self.render_openid(environ)
+#        rendered = self.env.from_string(html_code).render(self.template_vals)
         resp.body = rendered.encode('utf8')
         return resp