summaryrefslogtreecommitdiff
path: root/tex/context/base/m-translate.tex
diff options
context:
space:
mode:
authorMarius <mariausol@gmail.com>2010-07-04 15:32:09 +0300
committerMarius <mariausol@gmail.com>2010-07-04 15:32:09 +0300
commit85b7bc695629926641c7cb752fd478adfdf374f3 (patch)
tree80293f5aaa7b95a500a78392c39688d8ee7a32fc /tex/context/base/m-translate.tex
downloadcontext-85b7bc695629926641c7cb752fd478adfdf374f3.tar.gz
stable 2010-05-24 13:10
Diffstat (limited to 'tex/context/base/m-translate.tex')
-rw-r--r--tex/context/base/m-translate.tex88
1 files changed, 88 insertions, 0 deletions
diff --git a/tex/context/base/m-translate.tex b/tex/context/base/m-translate.tex
new file mode 100644
index 000000000..9c550eca7
--- /dev/null
+++ b/tex/context/base/m-translate.tex
@@ -0,0 +1,88 @@
+%D \module
+%D [ file=m-translate,
+%D version=2008.10.09,
+%D title=\CONTEXT\ Modules,
+%D subtitle=Translations,
+%D author=Hans Hagen,
+%D date=\currentdate,
+%D copyright=Hans Hagen]
+%C
+%C This module is part of the \CONTEXT\ macro||package and is
+%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
+%C details.
+
+\doifnotmode{mkiv}{\endinput}
+
+%D We can make this module more clever (wildcards and such) but since
+%D it's only a demo we stick to the simple case for now. After all, it's
+%D better to fix your source.
+
+\startluacode
+ translators = { }
+
+ local compiled, list = nil, nil
+
+ function translators.register(from,to)
+ local l = lpeg.P(from)/to
+ if not list then
+ list = l
+ else
+ list = list + l
+ end
+ compiled = nil
+ end
+
+ function translators.translate(s)
+ if list then
+ if not compiled then
+ compiled = lpeg.Cs((list + lpeg.P(1))^0)
+ end
+ return compiled:match(s)
+ else
+ return s
+ end
+ end
+
+ function translators.reset(s)
+ resolvers.install_text_filter("user",nil)
+ list, compiled = nil, nil
+ end
+ function translators.enable(s)
+ resolvers.install_text_filter("user",translators.translate)
+ end
+ function translators.disable(s)
+ resolvers.install_text_filter("user",nil)
+ end
+\stopluacode
+
+\unprotect
+
+\def\translateinput{\dodoubleargument\dotranslateinput}
+
+\def\dotranslateinput[#1][#2]{\ctxlua{translators.register(\!!bs#1\!!es,\!!bs#2\!!es)}}
+
+\def\resetinputtranslation {\ctxlua{translators.reset()}}
+\def\enableinputtranslation {\ctxlua{translators.enable()}}
+\def\disableinputtranslation{\ctxlua{translators.disable()}}
+
+\def\readtranslatedfile#1%
+ {\enableinputtranslation
+ \readfile{#1}\donothing\donothing
+ \disableinputtranslation}
+
+\protect
+
+\doifnotmode{demo}{\endinput}
+
+\starttext
+
+ \translateinput[Moica][Mojca]
+ \translateinput[Idris][Idris (aka ادريس)]
+
+ \enableinputtranslation
+
+ Well, it's not that hard to satisfy Idris and Moica.
+
+ \readtranslatedfile{tufte}
+
+\stoptext