2 #-------------------------------------------------------------------------------
4 # | ____| _| |_ / __ \ /\ | \/ |
5 # | |__ _ __ ___ ___ / \| | | | / \ | \ / |
6 # | __| '__/ _ \/ _ ( (| |) ) | | |/ /\ \ | |\/| |
7 # | | | | | __/ __/\_ _/| |__| / ____ \| | | |
8 # |_| |_| \___|\___| |_| \____/_/ \_\_| |_|
10 # FreeFOAM: The Cross-Platform CFD Toolkit
12 # Copyright (C) 2008-2010 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 2 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, write to the Free Software Foundation,
30 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #-------------------------------------------------------------------------------
34 # foamPNGMathFilter.py
36 #------------------------------------------------------------------------------
39 foamPNGMathFilter.py [-D dpi] [-m <mathdir>] [-o <outfile>] [<infile>]
41 Filters the AsciiDoc source file read from standard input (or `<infile>` if
42 specified) and replaces all inline and block maths by PNG images generated
43 using `latex` and `dvipng`. The filtered source is written to standard output
44 or (`<outfile>` if specified). The created images are either written to the
45 `math` directory in the current working directory, or if an output file has
46 been specified, in the directory containing the output file. The `-m` option
47 overrides this choice. The file is named `math_<md5sum>.png`.
51 -D Specify the resolution of the output images. Refer to dvipng(1)
52 -m Overrides the default output directory for the generated graphics
53 -o <outfile> Specifies that the output should be written to `<outfile>`
54 instead of the standard output
58 # Suppress warning: "the md5 module is deprecated; use hashlib instead"
60 warnings
.simplefilter('ignore',DeprecationWarning)
70 sys
.path
.insert(0, '@FOAM_PYTHON_DIR@')
71 from FreeFOAM
.compat
import *
82 echo('ERROR: -D option requires argument', file=sys
.stderr
)
84 dpiopt
= ' '.join(args
[0:2])
89 echo('ERROR: -m option requires argument', file=sys
.stderr
)
96 echo('ERROR: -o option requires argument', file=sys
.stderr
)
104 echo('ERROR: Unknown option "%s"'%args
[0], file=sys
.stderr
)
108 infile
= open(args
[0], 'rt')
112 echo('ERROR: Only one non-option argument allowed', file=sys
.stderr
)
116 outdir
= os
.path
.dirname(outname
)
117 if not os
.path
.isdir(outdir
):
119 outfile
= open(outname
, 'wt')
125 mathdir
= os
.path
.join(outdir
, 'math')
127 absmathdir
= os
.path
.isabs(mathdir
) and mathdir
or os
.path
.join(pwd
, mathdir
)
128 #relmathdir = os.path.relpath(mathdir, outdir)
131 \documentclass[12pt]{article}
132 \usepackage[utf8x]{inputenc}
136 \usepackage{amsfonts}
138 \input{@FOAM_DATA_DIR@/asciidoc/dblatex/foam-macros.sty}
152 'inlinemacro': re
.compile(
153 r
'(?su)[\\]?(?P<name>math):(?P<subslist>\S*?)\[(?P<mathtext>.*?)'
155 'blockmacro': re
.compile(
156 r
'(?su)[\\]?(?P<name>math)::(?P<subslist>\S*?)\[(?P<mathtext>.*?)'
159 r
'(?sum)^\[(?P<name>math)\]\n(?:\+{4,})\n(?P<mathtext>.*?)\n'
163 lines
= ''.join(infile
.readlines())
166 for name
, regex
in regexes
.iteritems():
167 for m
in regex
.finditer(lines
):
168 mathtext
= m
.group('mathtext')
169 h
= md5
.new(name
+dpiopt
+mathtext
).hexdigest()
173 'mathtext' : mathtext
,
177 'outname' : 'math_%s.png'%h
180 matches
.sort(key
=lambda x
: x
['begin'], reverse
=True)
186 #imname = os.path.join(relmathdir, m['outname'])
187 imname
= os
.path
.join(absmathdir
, m
['outname'])
188 mathtext
= m
['mathtext']
189 if m
['name'] in 'inlinemacro blockmacro'.split():
191 cleanmathtext
= mathtext
.replace(r
'\]', ']')
192 if m
['name'] == 'inlinemacro':
193 cleanmathtext
= '$%s$'%cleanmathtext
196 replacement
= 'math%s%s[%s]'%(sep
,imname
,mathtext
)
197 elif m
['name'] == 'block':
198 cleanmathtext
= mathtext
199 replacement
= '[math,%s]\n++++++\n%s\n++++++'%(imname
,mathtext
)
201 lines
= lines
[:m
['begin']]+replacement
+lines
[m
['end']:]
202 if not m
['hash'] in hashes
and not os
.path
.isfile(
203 os
.path
.join(absmathdir
,m
['outname'])):
204 hashes
[m
['hash']] = {
206 'tmpname' : 'math%d.png'%idx,
207 'outname' : m
['outname'],
208 'mathtext' : cleanmathtext
,
211 latextext
+= cleanmathtext
+ LATEXSEP
216 latextext
= LATEXHEAD
+ latextext
+ LATEXFOOT
218 if not os
.path
.isdir(absmathdir
):
219 os
.makedirs(absmathdir
)
220 tmpdir
= tempfile
.mkdtemp()
222 open('math.tex', 'wt').write(latextext
)
223 subprocess
.check_call([
224 '@LATEX_EXECUTABLE@',
225 '--interaction=nonstopmode', 'math.tex'],
226 stdout
=subprocess
.PIPE
, stderr
=subprocess
.STDOUT
)
227 subprocess
.check_call([
228 '@DVIPNG_EXECUTABLE@',
229 '-T', 'tight', '-z9', dpiopt
, 'math.dvi'],
230 stdout
=subprocess
.PIPE
, stderr
=subprocess
.STDOUT
)
231 for d
in hashes
.itervalues():
232 os
.rename(d
['tmpname'], os
.path
.join(absmathdir
, d
['outname']))
235 if tmpdir
is not None:
236 shutil
.rmtree(tmpdir
)
240 # ------------------------- vim: set sw=3 sts=3 et: --------------- end-of-file