Changeset 677
- Timestamp:
- 04/21/06 00:21:42 (3 years ago)
- Files:
-
- latexformulamacro/0.9 (copied) (copied from latexformulamacro/0.8)
- latexformulamacro/0.9/formula.py (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
latexformulamacro/0.9/formula.py
r130 r677 1 1 """ 2 2 Convert a latex formula into an image. 3 by Valient Gough <vgough@pobox.com> 3 by Valient Gough <vgough@pobox.com>, David Douard <david.douard@gmail.com> 4 4 5 5 Changes: 6 2006-01-16 (David Douard): 7 * make this macro work with Trac 0.9 8 * make the generated images be saved in $PROJECT/htdocs/formulas 9 * make default image format be 'png' 10 * replaced every Tab by spaces 11 * make tmp dir creation recursive 6 12 2005-10-03: 7 * make image format selectable via 'image_format' configuration option8 (defaults to 'jpg')9 * allow paths to executables to be specified in configuration by10 setting 'latex_path', 'dvips_path', 'convert_path' to point to11 executable. Based on code by Reed Cartwright.13 * make image format selectable via 'image_format' configuration option 14 (defaults to 'jpg') 15 * allow paths to executables to be specified in configuration by 16 setting 'latex_path', 'dvips_path', 'convert_path' to point to 17 executable. Based on code by Reed Cartwright. 12 18 2005-10-01: 13 19 * add #display and #fleqn options to add html formatting around image 14 20 (Christian Marquardt). 15 21 2005-09-21: 16 * add #center and #indent options to add html formatting around image.22 * add #center and #indent options to add html formatting around image. 17 23 2005-08-02: 18 * remove hard-coded paths, read from configuration. Fixes #2624 * remove hard-coded paths, read from configuration. Fixes #26 19 25 2005-07-27: 20 * figured out how to get rid of the annoying internal error after latex21 was run. Redirected latex output to /dev/null..22 * found out that {{{#!figure ... }}} runs wiki macro, and doesn't have23 the problem of not being able to use paranthesis. So this is the24 default usage now. Can still use [[formula(...)]] for simple formula.25 * add "nomode" command, which can be used to turn off automatic26 enclosure of commands in display-math mode ("$$ ... $$")26 * figured out how to get rid of the annoying internal error after latex 27 was run. Redirected latex output to /dev/null.. 28 * found out that {{{#!figure ... }}} runs wiki macro, and doesn't have 29 the problem of not being able to use paranthesis. So this is the 30 default usage now. Can still use [[formula(...)]] for simple formula. 31 * add "nomode" command, which can be used to turn off automatic 32 enclosure of commands in display-math mode ("$$ ... $$") 27 33 2005-07-26: first release 28 34 … … 30 36 1. Copy into wiki-macros directory. 31 37 2. Edit conf/trac.ini and add a [latex] group with three values: 32 [latex] 33 # temp_dir points to directory where temporary files are created 34 temp_dir = /var/tmp/trac 35 # image_path is directory where final images are written 36 image_path = /var/www/html/formula 37 # display_path is URL where formula images can be accessed 38 display_path = http://foo.net/formula 38 [latex] 39 # temp_dir points to directory where temporary files are created 40 temp_dir = /var/tmp/trac 39 41 # Set to 1 for fleqn style equations (default is centered) 40 42 fleqn = 0 … … 58 60 Optional keywords (must be specified before the latex code): 59 61 #density=100 60 Density defaults to 100. Larger values produces larger images.62 Density defaults to 100. Larger values produces larger images. 61 63 #nomode 62 Disable the default display mode setting. Use this if you want to63 include things outside of tex's display mode.64 Disable the default display mode setting. Use this if you want to 65 include things outside of tex's display mode. 64 66 #display 65 67 Create a displayed equation (either centered or fleqn style, 66 68 depending on the fleqn variable in the config file. 67 69 #center 68 Center the equation on the page.70 Center the equation on the page. 69 71 #fleqn 70 72 fleqn style equation; indentation is controlled by fleqn_witdh in 71 73 conf/trac.ini. 72 74 #indent [=class name] 73 places image link in a paragraph <p>...</p>74 If class name is specified, then it is used to specify a CSS class for75 the paragraph.75 places image link in a paragraph <p>...</p> 76 If class name is specified, then it is used to specify a CSS class for 77 the paragraph. 76 78 77 79 Notes: … … 108 110 def render(hdf, env, texData, density, fleqnMode, mathMode): 109 111 # gets paths from configuration 110 tmpdir = env.get_config('latex', 'temp_dir') 111 imagePath = env.get_config('latex', 'image_path') 112 displayPath = env.get_config('latex', 'display_path') 113 fleqnIndent = env.get_config('latex', 'fleqn_indent') 114 latexPath = env.get_config('latex', 'latex_path') 115 dvipsPath = env.get_config('latex', 'dvips_path') 116 convertPath = env.get_config('latex', 'convert_path') 117 texMag = env.get_config('latex', 'text_mag') 118 imageFormat = env.get_config('latex', 'image_format') 119 120 if not tmpdir or not imagePath or not displayPath: 121 return "<b>Error: missing configuration settings in 'latex' macro</b><br>" 112 tmpdir = env.config.get('latex', 'temp_dir') 113 114 fleqnIndent = env.config.get('latex', 'fleqn_indent') 115 latexPath = env.config.get('latex', 'latex_path') 116 dvipsPath = env.config.get('latex', 'dvips_path') 117 convertPath = env.config.get('latex', 'convert_path') 118 texMag = env.config.get('latex', 'text_mag') 119 imageFormat = env.config.get('latex', 'image_format') 120 121 imagePath = os.path.normpath(os.path.join(env.get_htdocs_dir(), "formulas")) 122 if not os.path.exists(imagePath): 123 try: 124 os.mkdir(imagePath) 125 except: 126 return "<b>Error: unable to create image directory</b><br>" 127 128 if not tmpdir: 129 return "<b>Error: missing configuration 'tmpdir' setting in 'latex' macro</b><br>" 122 130 123 131 # set defaults … … 125 133 fleqnIndent = '5%' 126 134 if not latexPath: 127 latexPath = 'latex'135 latexPath = 'latex' 128 136 if not dvipsPath: 129 dvipsPath = 'dvips'137 dvipsPath = 'dvips' 130 138 if not convertPath: 131 convertPath = 'convert'139 convertPath = 'convert' 132 140 if not texMag: 133 texMag = 1000 # I'm told this is latex's default value141 texMag = 1000 # I'm told this is latex's default value 134 142 if not imageFormat: 135 imageFormat = 'jpg'136 137 path = tmpdir # + hdf.getValue("project.name.encoded", "default")143 imageFormat = 'png' 144 145 path = tmpdir 138 146 # create temporary directory if necessary 147 def mkd(path): 148 if not os.path.exists(path): 149 d, t, = os.path.split(path) 150 if not os.path.exists(d): 151 mkd(d) 152 153 os.mkdir(path) 154 139 155 try: 140 if not os.path.exists(path):141 mkdir(path)156 if not os.path.exists(path): 157 mkd(path) 142 158 except: 143 return "Unable to create temporary directory " + path159 return "Unable to create temporary directory " + path 144 160 145 161 # generate final image name. Use a hash of the parameters which affect … … 154 170 log = "<br>" 155 171 if not os.path.exists(imageFile): 156 # latex writes out lots of stuff to the current directory, so we have157 # to run it from there.158 cwd = os.getcwd()159 os.chdir(path)160 161 texFile = name + ".tex"162 makeTexFile(texFile, texData, mathMode, texMag)163 164 # the output from latex on stdout seems to cause problems, so sent it165 # to /dev/null166 cmd = "%s %s > /dev/null" % (latexPath, texFile)167 log += execprog( cmd )168 os.chdir(cwd)169 170 # use dvips to convert to eps171 dviFile = "%s/%s.dvi" % (path, name)172 epsFile = "%s/%s.eps" % (path, name)173 cmd = "%s -q -D 600 -E -n 1 -p 1 -o %s %s" % (dvipsPath, epsFile, dviFile)174 log += execprog( cmd )175 176 # and finally, ImageMagick to convert from eps to [imageFormat] type177 cmd = "%s -antialias -density %ix%i %s %s" % (convertPath, density, density, epsFile, imageFile)178 log += execprog( cmd )172 # latex writes out lots of stuff to the current directory, so we have 173 # to run it from there. 174 cwd = os.getcwd() 175 os.chdir(path) 176 177 texFile = name + ".tex" 178 makeTexFile(texFile, texData, mathMode, texMag) 179 180 # the output from latex on stdout seems to cause problems, so sent it 181 # to /dev/null 182 cmd = "%s %s > /dev/null" % (latexPath, texFile) 183 log += execprog( cmd ) 184 os.chdir(cwd) 185 186 # use dvips to convert to eps 187 dviFile = "%s/%s.dvi" % (path, name) 188 epsFile = "%s/%s.eps" % (path, name) 189 cmd = "%s -q -D 600 -E -n 1 -p 1 -o %s %s" % (dvipsPath, epsFile, dviFile) 190 log += execprog( cmd ) 191 192 # and finally, ImageMagick to convert from eps to [imageFormat] type 193 cmd = "%s -antialias -density %ix%i %s %s" % (convertPath, density, density, epsFile, imageFile) 194 log += execprog( cmd ) 179 195 180 196 if fleqnMode: 181 197 margin = " margin-left: %s" % fleqnIndent 182 198 else: 183 margin = ""199 margin = "" 184 200 185 html = "<img src='%s /%s.%s' border='0' style='vertical-align: middle;%s' alt='formula' />" % (displayPath, name, imageFormat, margin)201 html = "<img src='%s' border='0' style='vertical-align: middle;%s' alt='formula' />" % (env.href.chrome('site','formulas/%s.%s'%(name, imageFormat)), margin) 186 202 return html 187 203 … … 207 223 tex += "\\begin{document}\n" 208 224 if mathMode: 209 tex += "$$\n"225 tex += "$$\n" 210 226 tex += "%s\n" % texData 211 227 if mathMode: 212 tex += "$$\n"228 tex += "$$\n" 213 229 tex += "\\pagebreak\n" 214 230 tex += "\\end{document}\n" 215 231 216 232 FILE = open(texFile, "w") 217 233 FILE.write( tex ) … … 221 237 def execute(hdf, text, env): 222 238 # TODO: unescape all html escape codes 239 223 240 text = text.replace("&", "&") 224 241 225 242 # defaults 226 243 density = 100 227 244 mathMode = 1 # default to using display-math mode for LaTeX processing 228 245 displayMode = 0 # default to generating inline formula 229 fleqnMode = env. get_config('latex', 'fleqn')246 fleqnMode = env.config.get('latex', 'fleqn') 230 247 centerImage = 0 231 248 indentImage = 0 … … 237 254 errors = "" 238 255 for line in text.split("\n"): 239 m = command.match(line)240 if m:241 if m.group(1) == "density":242 density = int(m.group(2))243 elif m.group(1) == "nomode":244 mathMode = 0245 elif m.group(1) == "center":246 centerImage = 1256 m = command.match(line) 257 if m: 258 if m.group(1) == "density": 259 density = int(m.group(2)) 260 elif m.group(1) == "nomode": 261 mathMode = 0 262 elif m.group(1) == "center": 263 centerImage = 1 247 264 fleqnMode = 0 248 elif m.group(1) == "indent": 249 indentImage = 1 250 indentClass = m.group(2) 251 elif m.group(1) == "display": 252 displayMode = 1 253 elif m.group(1) == "fleqn": 265 elif m.group(1) == "indent": 266 indentImage = 1 267 indentClass = m.group(2) 268 elif m.group(1) == "display": 254 269 displayMode = 1 255 fleqnMode = 1 256 else: 257 errors = '<br>Unknown <i>formula</i> command "%s"<br>' % m.group(1) 258 else: 259 formula += line + "\n" 270 elif m.group(1) == "fleqn": 271 displayMode = 1 272 fleqnMode = 1 273 else: 274 errors = '<br>Unknown <i>formula</i> command "%s"<br>' % m.group(1) 275 else: 276 formula += line + "\n" 260 277 261 278 # Set display and fleqn defaults 262 279 if displayMode: 263 if fleqnMode:264 centerImage = 0265 else:266 centerImage = 1280 if fleqnMode: 281 centerImage = 0 282 else: 283 centerImage = 1 267 284 268 285 # Render formula
