m-translate.mkiv /size: 3322 b    last modification: 2020-07-01 14:35
1%D \module
2%D   [       file=m-translate,
3%D        version=2008.10.09,
4%D          title=\CONTEXT\ Modules,
5%D       subtitle=Translations,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14%D We can make this module more clever (wildcards and such) but since
15%D it's only a demo we stick to the simple case for now. After all, it's
16%D better to fix your source.
17
18\startluacode
19    local translators = { }
20
21    moduledata.translators = translators
22
23    local compiled, list = nil, nil
24
25 -- function translators.register(from,to)
26 --     local l = lpeg.P(from)/to
27 --     if not list then
28 --         list = l
29 --     else
30 --         list = list + l
31 --     end
32 --     compiled = nil
33 -- end
34 --
35 -- function translators.translate(s)
36 --     if list then
37 --         if not compiled then
38 --             compiled = lpeg.Cs((list + lpeg.P(1))^0)
39 --         end
40 --         return compiled:match(s)
41 --     else
42 --         return s
43 --     end
44 -- end
45
46    -- local function prepare()
47
48    function translators.register(from,to)
49        if not list then
50            list = { [from] = to }
51        else
52            list[from] = to
53        end
54        compiled = nil
55    end
56
57    function translators.translate(s)
58        if list then
59            if not compiled then
60                local tree = lpeg.utfchartabletopattern(list)
61                compiled = lpeg.Cs((tree/list + lpeg.patterns.utf8character)^0 * lpeg.P(-1)) -- the P(1) is needed in order to accept non utf
62            end
63            return compiled:match(s)
64        else
65            return s
66        end
67    end
68
69    local textlineactions = resolvers.openers.helpers.textlineactions
70
71    utilities.sequencers.appendaction(textlineactions,"after","moduledata.translators.translate")
72
73    function translators.enable()
74        utilities.sequencers.enableaction(textlineactions,"moduledata.translators.translate")
75    end
76
77    function translators.disable()
78        utilities.sequencers.disableaction(textlineactions,"moduledata.translators.translate")
79    end
80
81    function translators.reset(s)
82        translators.enable()
83        list, compiled = nil, nil
84    end
85
86    translators.disable()
87\stopluacode
88
89\unprotect
90
91\unexpanded\def\translateinput
92  {\dodoubleargument\module_translate_input}
93
94\def\module_translate_input[#1][#2]%
95  {\ctxlua{moduledata.translators.register(\!!bs#1\!!es,\!!bs#2\!!es)}}
96
97\unexpanded\def\resetinputtranslation
98  {\ctxlua{moduledata.translators.reset()}}
99
100\unexpanded\def\enableinputtranslation
101  {\ctxlua{moduledata.translators.enable()}}
102
103\unexpanded\def\disableinputtranslation
104  {\ctxlua{moduledata.translators.disable()}}
105
106\unexpanded\def\readtranslatedfile#1%
107  {\enableinputtranslation
108   \readfile{#1}\donothing\donothing
109   \disableinputtranslation}
110
111\protect
112
113\continueifinputfile{m-translate.mkiv}
114
115\starttext
116
117    \translateinput[Moica][Mojca]
118  % \translateinput[Idris][Idris (aka ادريس)]
119    \translateinput[Idris][Idris (aka <something arabic here>)]
120
121    \enableinputtranslation
122
123    Well, it's not that hard to satisfy Idris' and Moicas \TEX\ needs.
124
125    \readtranslatedfile{tufte}
126
127\stoptext
128