File: mimetex.py

package info (click to toggle)
nodebox-web 1.9.4.6-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,900 kB
  • sloc: python: 7,582; ansic: 581; xml: 239; makefile: 2
file content (36 lines) | stat: -rw-r--r-- 1,089 bytes parent folder | download | duplicates (2)
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
### MIMETEX ##########################################################################################
# Code for connecting to mimeTeX server to convert LaTeX math to images.

# Author: Tom De Smedt.
# Copyright (c) 2007 by Tom De Smedt.
# See LICENSE.txt for details.

from nodebox_web.web.url import URLAccumulator
from urllib import quote
from nodebox_web.web.cache import Cache

def clear_cache():
    Cache("mimetex").clear()

class mimeTeX(URLAccumulator):
    
    """ The mimeTeX server returns a GIF-image for a LaTeX math expression.
    http://www.forkosh.com/mimetex.html
    """
    
    def __init__(self, eq, wait=10, asynchronous=False):
        
        url = "http://www.forkosh.dreamhost.com/mimetex.cgi?"+quote(eq)
        URLAccumulator.__init__(self, url, wait, asynchronous, "mimetex", type=".gif", throttle=1)

    def load(self, data):
        
        # Provide the path to the GIF stored in cache.
        self.image = self._cache.hash(self.url)

def gif(eq):
    
    return mimeTeX(eq).image

#eq = "E = hf = \frac{hc}{\lambda} \,\! "
#image(gif(eq), 10, 10)