Skip to main content

Ralsina.Me — Roberto Alsina's website

BartleBlog live!

I was think­ing: how can I im­ple­ment page pre­views in Bartle­Blog?

The ob­vi­ous way is to ren­der the page and open the lo­cal file. How­ev­er, the page may link or in­clude pieces that are not up­dat­ed yet in the stat­ic ver­sion, so that can give con­fus­ing re­sult­s.

Then it hit me... gen­er­ate the page on the fly and serve it. And do the same for ev­ery­thing else the brows­er asks for.

So, af­ter search­ing for 15 min­utes for the sim­plest python "web frame­work" that let me use the code al­ready in Bartle­blog, and de­cid­ing for Col­u­brid...

Now, this is cute: bartle­blog as a dy­nam­ic web app in 34 lines.

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)

Contents © 2000-2024 Roberto Alsina