1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
%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 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
\def\translateinput{\dodoubleargument\dotranslateinput}
\def\dotranslateinput[#1][#2]{\ctxlua{moduledata.translators.register(\!!bs#1\!!es,\!!bs#2\!!es)}}
\def\resetinputtranslation {\ctxlua{moduledata.translators.reset()}}
\def\enableinputtranslation {\ctxlua{moduledata.translators.enable()}}
\def\disableinputtranslation{\ctxlua{moduledata.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
|