btbytes (owner)

Revisions

  • c6c68b Tue Apr 21 11:30:26 -0700 2009
gist: 99305 Download_button fork
public
Description:
A Yaki plugin to render mathematical equations using l2p http://redsymbol.net/software/l2p/
Public Clone URL: git://gist.github.com/99305.git
Embed All Files: show embed
equations.py #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'''
Equations.py
A Yaki plugin to render mathematical equations using
l2p http://redsymbol.net/software/l2p/
 
Pradeep Gowda <pradeep@btbytes.com>
2009-04-21
'''
import yaki.Engine, yaki.Store
import urlparse, re
from BeautifulSoup import *
from pygments import highlight
from pygments.lexers import *
from pygments.formatters import *
import md5
import os
import subprocess
 
class EquationsWikiPlugin(yaki.Engine.WikiPlugin):
    def __init__(self, registry, webapp):
        self.webapp = webapp
        registry.register('markup',self, 'math','equation')
                 
    def create_image(self,fname, text):
         cmd = 'l2p -i \'%s\' -o %s' %(text, fname)
         proc = subprocess.Popen(cmd,
                           shell=True,
                           stdout=subprocess.PIPE,
                           )
         stdout_value = proc.communicate()[0]
         if len(stdout_value) > 0:
             return False
         else:
             return True
            
        
    def get_image_url(self, text):
        g = md5.new(text)
        c = self.webapp.getContext()
        fpath = os.path.join(c.media, 'eqns')
        name = '%s.png' % (g.hexdigest(), )
        fname = os.path.join(c.siteinfo['docroot'] ,'eqns', name)
        url = c.siteinfo['siteurl'] + fpath + '/' + name
        if not os.path.exists(fname):
            if not self.create_image(fname, text):
                url = 'http://pradeepgowda.com/media/img/calendar.png'
        return url
 
    def run(self, serial, tag, tagname, pagename, soup, request, response):
        imgurl = self.get_image_url(tag.contents[0])
        result = '<img src="%s" alt="%s" valign="center" />' % (imgurl, tag.contents[0])
        tag.replaceWith(result)
        return False