2 #-------------------------------------------------------------------------------
4 # | ____| _| |_ / __ \ /\ | \/ |
5 # | |__ _ __ ___ ___ / \| | | | / \ | \ / |
6 # | __| '__/ _ \/ _ ( (| |) ) | | |/ /\ \ | |\/| |
7 # | | | | | __/ __/\_ _/| |__| / ____ \| | | |
8 # |_| |_| \___|\___| |_| \____/_/ \_\_| |_|
10 # FreeFOAM: The Cross-Platform CFD Toolkit
12 # Copyright (C) 2008-2012 Michael Wild <themiwi@users.sf.net>
13 # Gerber van der Graaf <gerber_graaf@users.sf.net>
14 #-------------------------------------------------------------------------------
16 # This file is part of FreeFOAM.
18 # FreeFOAM is free software: you can redistribute it and/or modify it
19 # under the terms of the GNU General Public License as published by the
20 # Free Software Foundation, either version 3 of the License, or (at your
21 # option) any later version.
23 # FreeFOAM is distributed in the hope that it will be useful, but WITHOUT
24 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
25 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
28 # You should have received a copy of the GNU General Public License
29 # along with FreeFOAM. If not, see <http://www.gnu.org/licenses/>.
30 #-------------------------------------------------------------------------------
33 # foamPNGMathFilter.py
35 #------------------------------------------------------------------------------
38 foamPNGMathFilter.py [-D dpi] [-m <mathdir>] [-o <outfile>] [<infile>]
40 Filters the AsciiDoc source file read from standard input (or `<infile>` if
41 specified) and replaces all inline and block maths by PNG images generated
42 using `latex` and `dvipng`. The filtered source is written to standard output
43 or (`<outfile>` if specified). The created images are either written to the
44 `math` directory in the current working directory, or if an output file has
45 been specified, in the directory containing the output file. The `-m` option
46 overrides this choice. The file is named `math_<md5sum>.png`.
50 -D Specify the resolution of the output images. Refer to dvipng(1)
51 -m Overrides the default output directory for the generated graphics
52 -o <outfile> Specifies that the output should be written to `<outfile>`
53 instead of the standard output
57 # Suppress warning: "the md5 module is deprecated; use hashlib instead"
59 warnings
.simplefilter('ignore',DeprecationWarning)
69 sys
.path
.insert(0, '@FOAM_PYTHON_DIR@')
70 from FreeFOAM
.compat
import *
81 echo('ERROR: -D option requires argument', file=sys
.stderr
)
83 dpiopt
= ' '.join(args
[0:2])
88 echo('ERROR: -m option requires argument', file=sys
.stderr
)
95 echo('ERROR: -o option requires argument', file=sys
.stderr
)
103 echo('ERROR: Unknown option "%s"'%args
[0], file=sys
.stderr
)
107 infile
= open(args
[0], 'rt')
111 echo('ERROR: Only one non-option argument allowed', file=sys
.stderr
)
115 outdir
= os
.path
.dirname(outname
)
116 if not os
.path
.isdir(outdir
):
118 outfile
= open(outname
, 'wt')
124 mathdir
= os
.path
.join(outdir
, 'math')
126 absmathdir
= os
.path
.isabs(mathdir
) and mathdir
or os
.path
.join(pwd
, mathdir
)
127 #relmathdir = os.path.relpath(mathdir, outdir)
130 \documentclass[12pt]{article}
131 \usepackage[utf8x]{inputenc}
135 \usepackage{amsfonts}
137 \input{@FOAM_DATA_DIR@/asciidoc/dblatex/foam-macros.sty}
151 'inlinemacro': re
.compile(
152 r
'(?su)[\\]?(?P<name>math):(?P<subslist>\S*?)\[(?P<mathtext>.*?)'
154 'blockmacro': re
.compile(
155 r
'(?su)[\\]?(?P<name>math)::(?P<subslist>\S*?)\[(?P<mathtext>.*?)'
158 r
'(?sum)^\[(?P<name>math)\]\n(?:\+{4,})\n(?P<mathtext>.*?)\n'
162 lines
= ''.join(infile
.readlines())
165 for name
, regex
in regexes
.iteritems():
166 for m
in regex
.finditer(lines
):
167 mathtext
= m
.group('mathtext')
168 h
= md5
.new(name
+dpiopt
+mathtext
).hexdigest()
172 'mathtext' : mathtext
,
176 'outname' : 'math_%s.png'%h
179 matches
.sort(key
=lambda x
: x
['begin'], reverse
=True)
185 #imname = os.path.join(relmathdir, m['outname'])
186 imname
= os
.path
.join(absmathdir
, m
['outname'])
187 mathtext
= m
['mathtext']
188 if m
['name'] in 'inlinemacro blockmacro'.split():
190 cleanmathtext
= mathtext
.replace(r
'\]', ']')
191 if m
['name'] == 'inlinemacro':
192 cleanmathtext
= '$%s$'%cleanmathtext
195 replacement
= 'math%s%s[%s]'%(sep
,imname
,mathtext
)
196 elif m
['name'] == 'block':
197 cleanmathtext
= mathtext
198 replacement
= '[math,%s]\n++++++\n%s\n++++++'%(imname
,mathtext
)
200 lines
= lines
[:m
['begin']]+replacement
+lines
[m
['end']:]
201 if not m
['hash'] in hashes
and not os
.path
.isfile(
202 os
.path
.join(absmathdir
,m
['outname'])):
203 hashes
[m
['hash']] = {
205 'tmpname' : 'math%d.png'%idx,
206 'outname' : m
['outname'],
207 'mathtext' : cleanmathtext
,
210 latextext
+= cleanmathtext
+ LATEXSEP
215 latextext
= LATEXHEAD
+ latextext
+ LATEXFOOT
217 if not os
.path
.isdir(absmathdir
):
218 os
.makedirs(absmathdir
)
219 tmpdir
= tempfile
.mkdtemp()
221 open('math.tex', 'wt').write(latextext
)
222 subprocess
.check_call([
223 '@LATEX_EXECUTABLE@',
224 '--interaction=nonstopmode', 'math.tex'],
225 stdout
=subprocess
.PIPE
, stderr
=subprocess
.STDOUT
)
226 subprocess
.check_call([
227 '@DVIPNG_EXECUTABLE@',
228 '-T', 'tight', '-z9', dpiopt
, 'math.dvi'],
229 stdout
=subprocess
.PIPE
, stderr
=subprocess
.STDOUT
)
230 for d
in hashes
.itervalues():
231 shutil
.move(d
['tmpname'], os
.path
.join(absmathdir
, d
['outname']))
234 if tmpdir
is not None:
235 shutil
.rmtree(tmpdir
)
239 # ------------------------- vim: set sw=3 sts=3 et: --------------- end-of-file