Skip to content

Instantly share code, notes, and snippets.

@btbytes
Created April 21, 2009 18:30
Show Gist options
  • Save btbytes/99305 to your computer and use it in GitHub Desktop.
Save btbytes/99305 to your computer and use it in GitHub Desktop.
A Yaki plugin to render mathematical equations using l2p http://redsymbol.net/software/l2p/
'''
Equations.py
A Yaki plugin to render mathematical equations using
l2p http://redsymbol.net/software/l2p/
Pradeep Gowda <[email protected]>
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment