BartleBlog live!

I was thi­nkin­g: how can I im­ple­ment pa­ge pre­views in Bar­tle­Blo­g?

The ob­vious way is to ren­der the pa­ge and open the lo­cal fi­le. Ho­we­ve­r, the pa­ge may li­nk or in­clu­de pie­ces that are not up­dated yet in the sta­tic ver­sio­n, so that can gi­ve con­fu­sing re­sul­ts.

Then it hit me… ge­ne­ra­te the pa­ge on the fly and ser­ve it. And do the sa­me for eve­r­y­thing el­se the bro­w­ser asks fo­r.

So, after sear­ching for 15 mi­nu­tes for the sim­plest py­thon “web fra­mewo­rk” that let me use the co­de al­ready in Bar­tle­blo­g, and de­ci­ding for Co­lu­brid

No­w, this is cu­te: bar­tle­blog as a dy­na­mic web app in 34 li­nes.

from colubrid import RegexApplication, HttpResponse, execute

from BartleBlog.backend.blog import Blog
import BartleBlog.backend.dbclasses as db
import os, codecs

class webBlog(Blog):
    def __init__(self):
        Blog.__init__(self)
        self.basepath='http://localhost:8080/'
        self.dest_dir=os.path.expanduser("~/.bartleblog/preview")
        if not os.path.isdir(self.dest_dir):
            os.mkdir(self.dest_dir)


class MyApplication(RegexApplication):
    blog=webBlog()
    urls = [
        (r'^(.*?)$', 'page'),
        (r'^(.*?)/(.*?)$', 'page'),
        (r'^(.*?)/(.*?)/(.*?)$', 'page'),
        (r'^(.*?)/(.*?)/(.*?)/(.*?)$', 'page')
    ]

    def page(self, *args):
        path=''.join(args)
        page=db.pageByPath(path)
        self.blog.renderPage(page)
        return HttpResponse(codecs.open(os.path.join(self.blog.dest_dir, path)).read())

app = MyApplication
app = StaticExports(app, {
    '/static': './static'
})


if __name__ == '__main__':
    execute(app)

Comentarios

Comments powered by Disqus
Contents © 2000-2013 Roberto Alsina
Share