1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18\startluacode
19 local translators = { }
20
21 moduledata.translators = translators
22
23 local compiled, list = nil, nil
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 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))
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{mtranslate.mkiv}
114
115\starttext
116
117 \translateinput[Moica][Mojca]
118
119 \translateinput[Idris][Idris (aka <something arabic here>)]
120
121 \enableinputtranslation
122
123 Well, its not that hard to satisfy Idris and Moicas \TEX\ needs.
124
125 \readtranslatedfile{tufte}
126
127\stoptext
128 |