Changeset 677

Show
Ignore:
Timestamp:
04/21/06 00:21:42 (3 years ago)
Author:
vgough
Message:

LatexFormulaMacro:

add 0.9 patch from David Douard

2006-01-16 (David Douard):

  • make this macro work with Trac 0.9
  • make the generated images be saved in $PROJECT/htdocs/formulas
  • make default image format be 'png'
  • replaced every Tab by spaces
  • make tmp dir creation recursive
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • latexformulamacro/0.9/formula.py

    r130 r677  
    11""" 
    22Convert a latex formula into an image. 
    3 by Valient Gough <vgough@pobox.com> 
     3by Valient Gough <vgough@pobox.com>, David Douard <david.douard@gmail.com> 
    44 
    55Changes: 
     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  
    612    2005-10-03: 
    7        * make image format selectable via 'image_format' configuration option 
    8          (defaults to 'jpg') 
    9        * allow paths to executables to be specified in configuration by 
    10          setting 'latex_path', 'dvips_path', 'convert_path' to point to 
    11          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. 
    1218    2005-10-01: 
    1319        * add #display and #fleqn options to add html formatting around image 
    1420          (Christian Marquardt). 
    1521    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. 
    1723    2005-08-02: 
    18        * remove hard-coded paths, read from configuration. Fixes #26 
     24        * remove hard-coded paths, read from configuration. Fixes #26 
    1925    2005-07-27: 
    20        * figured out how to get rid of the annoying internal error after latex 
    21        was run.  Redirected latex output to /dev/null.. 
    22        * found out that {{{#!figure ... }}} runs wiki macro, and doesn't have 
    23        the problem of not being able to use paranthesis.  So this is the 
    24        default usage now.  Can still use [[formula(...)]] for simple formula. 
    25        * add "nomode" command, which can be used to turn off automatic 
    26          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 ("$$ ... $$") 
    2733    2005-07-26: first release 
    2834 
     
    3036    1. Copy into wiki-macros directory. 
    3137    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 
    3941        # Set to 1 for fleqn style equations (default is centered) 
    4042        fleqn = 0 
     
    5860Optional keywords (must be specified before the latex code): 
    5961    #density=100 
    60        Density defaults to 100.  Larger values produces larger images. 
     62        Density defaults to 100.  Larger values produces larger images. 
    6163    #nomode 
    62        Disable the default display mode setting.  Use this if you want to 
    63        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. 
    6466    #display 
    6567        Create a displayed equation (either centered or fleqn style, 
    6668        depending on the fleqn variable in the config file. 
    6769    #center 
    68        Center the equation on the page. 
     70        Center the equation on the page. 
    6971    #fleqn 
    7072        fleqn style equation; indentation is controlled by fleqn_witdh in 
    7173        conf/trac.ini. 
    7274    #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 for 
    75        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. 
    7678 
    7779Notes: 
     
    108110def render(hdf, env, texData, density, fleqnMode, mathMode): 
    109111    # 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>" 
    122130 
    123131    # set defaults 
     
    125133        fleqnIndent = '5%' 
    126134    if not latexPath: 
    127        latexPath = 'latex' 
     135        latexPath = 'latex' 
    128136    if not dvipsPath: 
    129        dvipsPath = 'dvips' 
     137        dvipsPath = 'dvips' 
    130138    if not convertPath: 
    131        convertPath = 'convert' 
     139        convertPath = 'convert' 
    132140    if not texMag: 
    133        texMag = 1000 # I'm told this is latex's default value 
     141        texMag = 1000 # I'm told this is latex's default value 
    134142    if not imageFormat: 
    135        imageFormat = 'jpg' 
    136  
    137     path = tmpdir # + hdf.getValue("project.name.encoded", "default") 
     143        imageFormat = 'png' 
     144 
     145    path = tmpdir 
    138146    # 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                  
    139155    try: 
    140        if not os.path.exists(path): 
    141            mkdir(path) 
     156        if not os.path.exists(path): 
     157            mkd(path) 
    142158    except: 
    143        return "Unable to create temporary directory " + path 
     159        return "Unable to create temporary directory " + path 
    144160     
    145161    # generate final image name.  Use a hash of the parameters which affect 
     
    154170    log = "<br>" 
    155171    if not os.path.exists(imageFile): 
    156        # latex writes out lots of stuff to the current directory, so we have 
    157        # 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 it 
    165        # to /dev/null 
    166        cmd = "%s %s > /dev/null" % (latexPath, texFile) 
    167        log += execprog( cmd ) 
    168        os.chdir(cwd) 
    169  
    170        # use dvips to convert to eps 
    171        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] type 
    177        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 ) 
    179195 
    180196    if fleqnMode: 
    181197        margin = " margin-left: %s" % fleqnIndent 
    182198    else: 
    183        margin = "" 
     199        margin = "" 
    184200         
    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) 
    186202    return html 
    187203 
     
    207223    tex += "\\begin{document}\n" 
    208224    if mathMode: 
    209        tex += "$$\n" 
     225        tex += "$$\n" 
    210226    tex += "%s\n" % texData 
    211227    if mathMode: 
    212        tex += "$$\n" 
     228        tex += "$$\n" 
    213229    tex += "\\pagebreak\n" 
    214230    tex += "\\end{document}\n" 
    215          
     231         
    216232    FILE = open(texFile, "w") 
    217233    FILE.write( tex ) 
     
    221237def execute(hdf, text, env): 
    222238    # TODO: unescape all html escape codes 
     239 
    223240    text = text.replace("&amp;", "&") 
    224      
     241         
    225242    # defaults 
    226243    density = 100 
    227244    mathMode = 1    # default to using display-math mode for LaTeX processing 
    228245    displayMode = 0 # default to generating inline formula 
    229     fleqnMode   = env.get_config('latex', 'fleqn') 
     246    fleqnMode   = env.config.get('latex', 'fleqn') 
    230247    centerImage = 0 
    231248    indentImage = 0 
     
    237254    errors = "" 
    238255    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 = 0 
    245            elif m.group(1) == "center": 
    246                centerImage = 1 
     256        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 
    247264                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": 
    254269                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" 
    260277 
    261278    # Set display and fleqn defaults 
    262279    if displayMode: 
    263         if fleqnMode: 
    264             centerImage = 0 
    265         else: 
    266             centerImage = 1 
     280       if fleqnMode: 
     281           centerImage = 0 
     282       else: 
     283           centerImage = 1 
    267284 
    268285    # Render formula