summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex
diff options
context:
space:
mode:
Diffstat (limited to 'Master/texmf-dist/tex')
-rw-r--r--Master/texmf-dist/tex/context/third/filter/t-filter.tex167
1 files changed, 167 insertions, 0 deletions
diff --git a/Master/texmf-dist/tex/context/third/filter/t-filter.tex b/Master/texmf-dist/tex/context/third/filter/t-filter.tex
new file mode 100644
index 00000000000..9ae777aa3b7
--- /dev/null
+++ b/Master/texmf-dist/tex/context/third/filter/t-filter.tex
@@ -0,0 +1,167 @@
+%D \module
+%D [ file=t-filter,
+%D version=2010.09.26,
+%D title=\CONTEXT\ User Module,
+%D subtitle=Filter,
+%D author=Aditya Mahajan,
+%D date=\currentdate,
+%D copyright=Aditya Mahajan,
+%D email=adityam <at> umich <dot> edu,
+%D license=Simplified BSD License]
+
+\writestatus{loading}{ConTeXt User Module / Filter}
+
+\startmodule [filter]
+
+\unprotect
+
+\startinterface all
+ \setinterfaceconstant {filter} {filter}
+ \setinterfaceconstant {filtercommand} {filtercommand}
+ \setinterfaceconstant {output} {output}
+ \setinterfaceconstant {read} {read}
+ \setinterfaceconstant {readcommand} {readcommand}
+\stopinterface
+
+\def\??externalfilter??{externalfilter}
+\def\currentexternalfilter{}
+\def\externalfiltercountername{\??externalfilter??-\currentexternalfilter-counter}
+
+\def\externalfilterparameter#1%
+ {\csname
+ \docheckparentparameter{\??externalfilter??\currentexternalfilter}{#1}%
+ \endcsname}
+
+\def\docheckparentparameter#1#2%
+ {\ifcsname#1#2\endcsname
+ #1#2%
+ \else
+ \expandafter\redocheckparentparameter\csname#1\s!parent\endcsname{#2}%
+ \fi}
+
+\def\redocheckparentparameter#1#2%
+ {\ifx#1\relax
+ \s!empty
+ \else
+ \docheckparentparameter{#1}{#2}%
+ \fi}
+
+\def\setupexternalfilters
+ {\dodoubleargument\dosetupexternalfilters}
+
+\def\dosetupexternalfilters[#1][#2]%
+ {\ifsecondargument
+ \getparameters[\??externalfilter??#1][#2]%
+ \else
+ \getparameters[\??externalfilter??][#1]%
+ \fi}
+
+
+\def\defineexternalfilter
+ {\dodoubleargument\dodefineexternalfilter}
+
+\def\dodefineexternalfilter[#1][#2]%
+ {\edef\currentexternalfilter{#1}%
+ \getparameters[\??externalfilter??#1][\s!parent=\??externalfilter??,#2]%
+ \doif{\externalfilterparameter\c!continue}\v!yes
+ {\expandafter\newcounter\csname\externalfiltercountername\endcsname}%
+ \setvalue{\e!start#1}{\bgroup\obeylines\dodoubleargument\dostartexternalfilter[#1]}%
+ \setvalue{\e!stop#1}{\doprocessexternalfilter}%
+ \setvalue{process#1file}{\dodoubleargument\doprocessexternalfile[#1]}%
+ %TODO: Inline.
+ %\setvalue{#1}##1{\save ##1 to file}
+ }
+
+
+\def\dostartexternalfilter[#1][#2]% filter options
+ {% Initializations
+ \egroup %\bgroup in \start#1
+ \begingroup % to keep assignments local
+ \edef\currentexternalfilter {#1}%
+ \getparameters[\??externalfilter??#1][\c!name=,#2]%
+ % Set the name of temp file for os filter
+ \doifelse{\externalfilterparameter\c!continue}\v!yes
+ {\edef\externalfiltertmpfile{\??externalfilter??-#1-\csname\externalfiltercountername\endcsname}}
+ {\edef\externalfiltertmpfile{\??externalfilter??-#1}}
+ \doifsomething{\externalfilterparameter\c!name}
+ {\edef\externalfiltertmpfile{\externalfilterparameter\c!name}}
+ % The following macros are useful for filter= and filtercommand= options
+ % The basename of the external file
+ \edef\externalfilterbasefile {\jobname-\externalfiltertmpfile}%
+ % The name of the buffer to which data is written
+ \edef\externalfilterinputfile {\externalfilterbasefile.tmp}%
+ % The name of the file to which the output is written
+ \edef\externalfilteroutputfile{\externalfilterparameter\c!output}%
+ % Capture the contents of the buffer
+ \dostartbuffer[\externalfiltertmpfile][\e!start#1][\e!stop#1]}
+
+\def\doprocessexternalfilter%
+ {% By defualt, buffers are in memory in MkIV
+ \doifmode\s!mkiv{\savebuffer[\externalfiltertmpfile]}%
+ % Run external command
+ \doexecuteexternalfilter
+ \doreadprocessedfile
+ \endgroup
+ % Finalization
+ \doif{\externalfilterparameter\c!continue}\v!yes
+ {\expandafter\increment\csname\externalfiltercountername\endcsname}%
+ }
+
+\def\doexecuteexternalfilter
+ {\doifelse{\externalfilterparameter\c!continue}\v!yes
+ {\doifmode{*first}
+ {\executesystemcommand
+ {mtxrun --ifchanged=\externalfilterinputfile\space
+ bin:\externalfilterparameter\c!filtercommand}}}
+ {\executesystemcommand
+ {\externalfilterparameter\c!filtercommand}}}
+
+\def\doreadprocessedfile
+ {\doif{\externalfilterparameter\c!read}\v!yes
+ {\doiffileelse{\externalfilteroutputfile}
+ {\dodoreadprocessedfile}
+ {{\tttf TODO: File \externalfilteroutputfile\ not found! Check your definition}}}}
+
+\def\dodoreadprocessedfile
+ {\begingroup
+ \externalfilterparameter\c!before
+ \processcommacommand[\externalfilterparameter\c!setups]\directsetup
+ \externalfilterparameter\c!readcommand\externalfilteroutputfile
+ \externalfilterparameter\c!after
+ \endgroup}
+
+\def\doprocessexternalfile[#1][#2]%
+ {\edef\currentexternalfilter {#1}%
+ \edef\externalfilterinputfile {#2}%
+ \splitfiletype {#2}%
+ %BEWARE. \edef doesn not work
+ \def\externalfilterbasefile {\splitoffname}
+ \def\externalfilteroutputfile{\externalfilterparameter\c!output}%
+ \doexecuteexternalfilter
+ \doreadprocessedfile}
+
+
+\setupexternalfilters
+ [
+ \c!before=,
+ \c!after=,
+ \c!setups=,
+ \c!continue=\v!no,
+ \c!read=\v!yes,
+ \c!readcommand=\ReadFile,
+ \c!output=\externalfilterbasefile.tex,
+ \c!filter=,
+ \c!filtercommand={\externalfilterparameter\c!filter\space \externalfilterinputfile},
+ ]
+
+% For tracing
+\def\showexternalfilterstate
+ {\starttable[|l|l|]
+ \NC currentexternalfilter \NC \currentexternalfilter \NC \AR
+ \NC externalfilterinputfile \NC \externalfilterinputfile \NC \AR
+ \NC externalfilteroutputfile \NC \externalfilteroutputfile \NC \AR
+ \NC externalfilterbasefile \NC \externalfilterbasefile \NC \AR
+ \stoptable}
+
+\protect
+\stopmodule