From 8d8d528d2ad52599f11250cfc567fea4f37f2a8b Mon Sep 17 00:00:00 2001 From: Context Git Mirror Bot Date: Tue, 12 Jan 2016 17:15:07 +0100 Subject: 2016-01-12 16:26:00 --- tex/context/modules/mkiv/m-translate.mkiv | 127 ++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 tex/context/modules/mkiv/m-translate.mkiv (limited to 'tex/context/modules/mkiv/m-translate.mkiv') diff --git a/tex/context/modules/mkiv/m-translate.mkiv b/tex/context/modules/mkiv/m-translate.mkiv new file mode 100644 index 000000000..2e6cbe950 --- /dev/null +++ b/tex/context/modules/mkiv/m-translate.mkiv @@ -0,0 +1,127 @@ +%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={PRAGMA ADE \& \CONTEXT\ Development Team}] +%C +%C This module is part of the \CONTEXT\ macro||package and is +%C therefore copyrighted by \PRAGMA. See mreadme.pdf for +%C details. + +%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 + local translators = { } + + moduledata.translators = 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 + + -- local function prepare() + + function translators.register(from,to) + if not list then + list = { [from] = to } + else + list[from] = to + end + compiled = nil + end + + function translators.translate(s) + if list then + if not compiled then + local tree = lpeg.utfchartabletopattern(list) + compiled = lpeg.Cs((tree/list + lpeg.patterns.utf8character)^0 * lpeg.P(-1)) -- the P(1) is needed in order to accept non utf + end + return compiled:match(s) + else + return s + end + end + + local textlineactions = resolvers.openers.helpers.textlineactions + + utilities.sequencers.appendaction(textlineactions,"after","moduledata.translators.translate") + + function translators.enable() + utilities.sequencers.enableaction(textlineactions,"moduledata.translators.translate") + end + + function translators.disable() + utilities.sequencers.disableaction(textlineactions,"moduledata.translators.translate") + end + + function translators.reset(s) + translators.enable() + list, compiled = nil, nil + end + + translators.disable() +\stopluacode + +\unprotect + +\unexpanded\def\translateinput + {\dodoubleargument\module_translate_input} + +\def\module_translate_input[#1][#2]% + {\ctxlua{moduledata.translators.register(\!!bs#1\!!es,\!!bs#2\!!es)}} + +\unexpanded\def\resetinputtranslation + {\ctxlua{moduledata.translators.reset()}} + +\unexpanded\def\enableinputtranslation + {\ctxlua{moduledata.translators.enable()}} + +\unexpanded\def\disableinputtranslation + {\ctxlua{moduledata.translators.disable()}} + +\unexpanded\def\readtranslatedfile#1% + {\enableinputtranslation + \readfile{#1}\donothing\donothing + \disableinputtranslation} + +\protect + +\continueifinputfile{m-translate.mkiv} + +\starttext + + \translateinput[Moica][Mojca] + % \translateinput[Idris][Idris (aka ادريس)] + \translateinput[Idris][Idris (aka )] + + \enableinputtranslation + + Well, it's not that hard to satisfy Idris' and Moicas \TEX\ needs. + + \readtranslatedfile{tufte} + +\stoptext -- cgit v1.2.3