Attachment 'LTX.py'

Download

   1 #DESCRIPTION: convert LaTeX into PNG in MoinMoin wiki
   2 # uses free online service, such as http://ltx4u.net 
   3 # Instructions are for v1.9, runnning wsgi.  Permissions slightly different if running as cgi  
   4 # (1) Drop this file into data/plugin/macro as LTX.py. (should be readable by apache)
   5 # (2) Create a new directory ltxpngs in your public_html directory, writeable by apache
   6 # (3) Create a file ltxlog.html within that directory, writable by apache
   7 # (4) restart wsgi, or restart webserver
   8 # Example usage in MoinMoin wiki: 
   9 # <<LTX(\int_0^\infty e^{-x^2}dx)>>
  10 
  11 def execute(macro, eqn):
  12 	import urllib2,md5,os,urllib,time
  13 ### start user configuration (set as appropriate):
  14 	ltxurl=r'http://d.ltx4u.net/?' #free external site the converts LaTeX to a PNG  
  15 	ltxdir='/var/www/html/ltxpngs/' #path to store the pngs. (Owner is apache for wsgi)
  16 	ltxlog='ltxlog.html' #name of writable logfile withn ltxdir. (Owner is apache for wsgi)
  17 	httpdir='http://kidcode.us/ltxpngs/' #path for webserver to find the pngs
  18 	pngper=0644 #the permissions required for your webserver to serve the PNG
  19 	pretex=r'\Large ' #optional prepend to all the LaTeX commands you send
  20 ### end user configuration
  21 	eqn=urllib.quote(pretex+eqn) # important step to replace blanks by %20
  22 	hx=md5.md5(ltxurl+eqn).hexdigest() # "unique" hex string for PNG file name
  23 	ltxpng=hx+'.png' #the file name
  24 # If the PNG does not exist, go get it from ltxurl: 
  25 	if not os.path.isfile(ltxdir+ltxpng):
  26 		response=urllib2.urlopen(ltxurl+eqn) # connect to PNG service
  27 		data=response.read() # retrieve the PNG
  28 		oup = open(ltxdir+ltxpng,'w') # for writing the PNG on your server
  29 		oup.write(data) # write the PNG file
  30 		oup.close()
  31 		os.chmod(ltxdir+ltxpng,pngper) # change the permissions as needed
  32 		logfile=open(ltxdir+ltxlog,'a') 
  33 		print >>logfile,time.ctime(),ltxurl+eqn,ltxpng # record a log entry 
  34 		logfile.close()
  35 	str='<img src="'+httpdir+ltxpng+'">'  #return this html to your wiki page
  36 	return str or " "

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2012-01-29 04:09:16, 1.9 KB) [[attachment:LTX.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.