-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminted-openfoam.sty
165 lines (165 loc) · 7.9 KB
/
minted-openfoam.sty
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
%%
%% This is file `minted-openfoam.sty',
%%
%% Applies some trickery to minted macros to be able to fake highlighting OpenFOAM snippets
%% with foam-highlighter utility (Still needs pygmentize for some decorations) in case a
%% boolean UseFoamHLT is true (default)
%%
%% Original behavior (by Geoffrey M. Poore and Konrad Rudolph) can be turned on
%% simply by setting the UseFoamHLT bool to false
%%
%% Additionally, the file may be distributed under the terms of the new BSD
%% license.
%%
%% The Current Maintainer of this work is Mohammed Elwardi Fadeli.
\makeatletter
\newif\ifusefoamhlt
\usefoamhlttrue
\providecommand{\escapeinsidechar}{?}
\def\escapeinsidechar{?}
\renewcommand{\minted@pygmentize}[2][\minted@outputdir\minted@jobname.pyg]{%
\minted@checkstyle{\minted@get@opt{style}{default}}%
\stepcounter{minted@pygmentizecounter}%
\ifthenelse{\equal{\minted@get@opt{autogobble}{false}}{true}}%
{\def\minted@codefile{\minted@outputdir\minted@jobname.pyg}}%
{\def\minted@codefile{#1}}%
\ifthenelse{\boolean{minted@isinline}}%
{\def\minted@optlistcl@inlines{%
\minted@optlistcl@g@i
\csname minted@optlistcl@lang\minted@lang @i\endcsname}}%
{\let\minted@optlistcl@inlines\@empty}%
\def\minted@cmd{%
\ifminted@kpsewhich
\ifwindows
\detokenize{for /f "usebackq tokens=*"}\space\@percentchar\detokenize{a in (`kpsewhich}\space\minted@codefile\detokenize{`) do}\space
\fi
\fi%
\ifusefoamhlt
%% Trickery to use the foam-highlighter package
%% Download from https://github.com/FoamScience/foam-highlighter
echo "" | \MintedPygmentize\space -l #2
-f latex -P commandprefix=PYG -F tokenmerge
\minted@optlistcl@g \csname minted@optlistcl@lang\minted@lang\endcsname
\minted@optlistcl@inlines
\minted@optlistcl@cmd\space | head -1 > \minted@outputdir\minted@infile\space &&
foam-highlighter\space pygtex\space
\ifminted@kpsewhich
\ifwindows
\@percentchar\detokenize{a}%
\else
\detokenize{`}kpsewhich \minted@codefile\space
\detokenize{||} \minted@codefile\detokenize{`}%
\fi
\else
\minted@codefile
\fi\space%
\detokenize{'}\escapeinsidechar\detokenize{'}\space >> \minted@outputdir\minted@infile\space &&
echo "" | \MintedPygmentize\space -l #2
-f latex -P commandprefix=PYG -F tokenmerge
\minted@optlistcl@g \csname minted@optlistcl@lang\minted@lang\endcsname
\minted@optlistcl@inlines
\minted@optlistcl@cmd\space | tail -1 >> \minted@outputdir\minted@infile\space%
\else
%% Original behavior
\MintedPygmentize\space -l #2
-f latex -P commandprefix=PYG -F tokenmerge
\minted@optlistcl@g \csname minted@optlistcl@lang\minted@lang\endcsname
\minted@optlistcl@inlines
\minted@optlistcl@cmd -o \minted@outputdir\minted@infile\space
\ifminted@kpsewhich
\ifwindows
\@percentchar\detokenize{a}%
\else
\detokenize{`}kpsewhich \minted@codefile\space
\detokenize{||} \minted@codefile\detokenize{`}%
\fi
\else
\minted@codefile
\fi%
\fi}%
% For debugging, uncomment: %%%%
\immediate\typeout{\minted@cmd}%
% %%%%
\ifthenelse{\boolean{minted@cache}}%
{%
\ifminted@frozencache
\else
\ifx\XeTeXinterchartoks\minted@undefined
\ifthenelse{\equal{\minted@get@opt{autogobble}{false}}{true}}%
{\edef\minted@hash{\pdf@filemdfivesum{#1}%
\pdf@mdfivesum{\minted@cmd autogobble(\ifx\FancyVerbStartNum\z@ 0\else\FancyVerbStartNum\fi-\ifx\FancyVerbStopNum\z@ 0\else\FancyVerbStopNum\fi)}}}%
{\edef\minted@hash{\pdf@filemdfivesum{#1}%
\pdf@mdfivesum{\minted@cmd}}}%
\else
\ifx\mdfivesum\minted@undefined
\immediate\openout\minted@code\minted@jobname.mintedcmd\relax
\immediate\write\minted@code{\minted@cmd}%
\ifthenelse{\equal{\minted@get@opt{autogobble}{false}}{true}}%
{\immediate\write\minted@code{autogobble(\ifx\FancyVerbStartNum\z@ 0\else\FancyVerbStartNum\fi-\ifx\FancyVerbStopNum\z@ 0\else\FancyVerbStopNum\fi)}}{}%
\immediate\closeout\minted@code
\edef\minted@argone@esc{#1}%
\StrSubstitute{\minted@argone@esc}{\@backslashchar}{\@backslashchar\@backslashchar}[\minted@argone@esc]%
\StrSubstitute{\minted@argone@esc}{"}{\@backslashchar"}[\minted@argone@esc]%
\edef\minted@tmpfname@esc{\minted@outputdir\minted@jobname}%
\StrSubstitute{\minted@tmpfname@esc}{\@backslashchar}{\@backslashchar\@backslashchar}[\minted@tmpfname@esc]%
\StrSubstitute{\minted@tmpfname@esc}{"}{\@backslashchar"}[\minted@tmpfname@esc]%
%Cheating a little here by using ASCII codes to write `{` and `}`
%in the Python code
\def\minted@hashcmd{%
\detokenize{python -c "import hashlib; import os;
hasher = hashlib.sha1();
f = open(os.path.expanduser(os.path.expandvars(\"}\minted@tmpfname@esc.mintedcmd\detokenize{\")), \"rb\");
hasher.update(f.read());
f.close();
f = open(os.path.expanduser(os.path.expandvars(\"}\minted@argone@esc\detokenize{\")), \"rb\");
hasher.update(f.read());
f.close();
f = open(os.path.expanduser(os.path.expandvars(\"}\minted@tmpfname@esc.mintedmd5\detokenize{\")), \"w\");
macro = \"\\edef\\minted@hash\" + chr(123) + hasher.hexdigest() + chr(125) + \"\";
f.write(\"\\makeatletter\" + macro + \"\\makeatother\\endinput\n\");
f.close();"}}%
\ShellEscape{\minted@hashcmd}%
\minted@input{\minted@outputdir\minted@jobname.mintedmd5}%
\else
\ifthenelse{\equal{\minted@get@opt{autogobble}{false}}{true}}%
{\edef\minted@hash{\mdfivesum file {#1}%
\mdfivesum{\minted@cmd autogobble(\ifx\FancyVerbStartNum\z@ 0\else\FancyVerbStartNum\fi-\ifx\FancyVerbStopNum\z@ 0\else\FancyVerbStopNum\fi)}}}%
{\edef\minted@hash{\mdfivesum file {#1}%
\mdfivesum{\minted@cmd}}}%
\fi
\fi
\edef\minted@infile{\minted@cachedir/\minted@hash.pygtex}%
\IfFileExists{\minted@infile}{}{%
\ifthenelse{\equal{\minted@get@opt{autogobble}{false}}{true}}{%
\minted@autogobble{#1}}{}%
\ShellEscape{\minted@cmd}}%
\fi
\ifthenelse{\boolean{minted@finalizecache}}%
{%
\edef\minted@cachefilename{listing\arabic{minted@pygmentizecounter}.pygtex}%
\edef\minted@actualinfile{\minted@cachedir/\minted@cachefilename}%
\ifwindows
\StrSubstitute{\minted@infile}{/}{\@backslashchar}[\minted@infile@windows]
\StrSubstitute{\minted@actualinfile}{/}{\@backslashchar}[\minted@actualinfile@windows]
\ShellEscape{move /y \minted@outputdir\minted@infile@windows\space\minted@outputdir\minted@actualinfile@windows}%
\else
\ShellEscape{mv -f \minted@outputdir\minted@infile\space\minted@outputdir\minted@actualinfile}%
\fi
\let\minted@infile\minted@actualinfile
\expandafter\minted@addcachefile\expandafter{\minted@cachefilename}%
}%
{\ifthenelse{\boolean{minted@frozencache}}%
{%
\edef\minted@cachefilename{listing\arabic{minted@pygmentizecounter}.pygtex}%
\edef\minted@infile{\minted@cachedir/\minted@cachefilename}%
\expandafter\minted@addcachefile\expandafter{\minted@cachefilename}}%
{\expandafter\minted@addcachefile\expandafter{\minted@hash.pygtex}}%
}%
\minted@inputpyg}%
{%
\ifthenelse{\equal{\minted@get@opt{autogobble}{false}}{true}}{%
\minted@autogobble{#1}}{}%
\ShellEscape{\minted@cmd}%
\minted@inputpyg}%
}
\makeatother