summaryrefslogtreecommitdiff
path: root/src/t-transliterator.mkiv
blob: ae08278ece651ddf89e1dd275462dafc2c1d037b (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
%D \module
%D   [      file=t-transliterator,
%D        version=2021-11-21 18:19:24+0100,
%D          title=\CONTEXT\ User Module,
%D       subtitle=The Transliterator,
%D         author=Philipp Gesang,
%D           date=\currentdate,
%D      copyright=Philipp Gesang,
%D        license=2-clause BSD,
%D          email={pgesang at ix dot urz dot uni-heidelberg dot de}]
%D This module is licensed under the conditions of the BSD license with 
%D two clauses, there is a copy it in a file named "COPYING" in the
%D transliterator source tree.

\writestatus{loading}{Transliteration from non-Latin scripts}

\unprotect

\definenamespace [TRL] [
  name=transliterate,
  type=module,
  setup=list,
  parent=TRL,
  style=no,
  version=2021,
  comment=Transliteration from non-Latin scripts.,
]

\ctxlua{environment.loadluafile ("transliterator")}

%D Use the Transliterator by adding \type{\usemodule[transliterator]} somewhere
%D before \type{\starttext}.  Adjust the Transliterator through the
%D \type{\setuptransliterate} command.  As a first argument it accepts a set of
%D key-value options; at present you may configure \type{mode} and
%D \type{hyphenate}.

\def\set_serbian_exceptions{%
  \doifelse{\transliterateparameter{sr_exceptions}}\v!yes
           {\ctxlua{thirddata.translit.sr_except = true}}
           {\ctxlua{thirddata.translit.sr_except = false}}%
}

\def\set_hinting{%
  \doifelse{\transliterateparameter{hinting}}\v!yes
           {\ctxlua{thirddata.translit.hinting = true}}
           {\ctxlua{thirddata.translit.hinting = false}}%
}

\appendtoks \set_serbian_exceptions \to \everysetuptransliterate
\appendtoks \set_hinting            \to \everysetuptransliterate

%D At first we'll set some defaults:

\setuptransliterate[%
  debug=\v!false,
  hinting=\v!yes,
  hyphenate=cz,
  mode=ru_old,
  sr_exceptions=\v!yes,
  deficient_font=\v!no,
]

%D Possible values for \type{mode} are by the time of this writing:
%D \type{ru}, \type{ru_transcript_de}, \type{ru_transcript_en}, \type{ru_old},
%D \type{all}, \type{iso9_ocs}, \type{ocs}, \type{ocs_gla}, \type{ru_cz},
%D \type{ocs_cz}, \type{gr} and \type{gr_n}.
%D As not all fonts, even the expensive ones, support some of the most frequent
%D unicode signs used in ISO~9, there are fallbacks for the transliterations of
%D the weak and hard sign.
%D They work with the modes \type{iso9_ocs}, \type{all} and
%D \type{ru_old} only and can be triggered by setting the
%D variable \type{deficient_font} to the value {\em yes}.
%D This will transliterate {\em ь} and {\em ъ} (both upper and
%D lower case) to the more common, but non-ISO characters {\em ’} and {\em ”}
%D respectively.
%D Possible values for \type{hyphenate} are all valid \CONTEXT\ language code, for an
%D overview see \type{http://wiki.contextgarden.net/Language_Codes}.
%D In praxi you may want to choose either Czech (the default) or Slovak
%D (\type{sk}) for most transliterations from cyrillic scripts.  I've not yet
%D made up my mind concerning Greek transliteration, any suggestions are
%D welcome.

%D The following will help debugging and reviewing tables.  Make sure your
%D typescript can handle the characters, in general it's no use with Latin
%D Modern which unfortunately provides only a restricted set of the unicode
%D range.
%D
%D The user-level command to output a single substitution table is
%D \type{\showOneTranslitTab{#1}}.

\define[1]\showOneTranslitTab{%
  \startluacode
    environment.loadluafile ("trans_tables_iso9")
    environment.loadluafile ("trans_tables_trsc")
    environment.loadluafile ("trans_tables_scntfc")
    environment.loadluafile ("trans_tables_trsc")
    environment.loadluafile ("trans_tables_glag")
    environment.loadluafile ("trans_tables_gr")
    thirddata.translit.gen_rules_en()
    thirddata.translit.gen_rules_de()
    thirddata.translit.show_tab(translit["\luaescapestring{#1}"])
  \stopluacode
}

%D The user-level command to output all defined tables is
%D \type{\showTranslitTabs}.

\define\showTranslitTabs{%
  \ctxlua{thirddata.translit.show_all_tabs()}%
}

\def\translitDebug#1{%
  \doif{\transliterateparameter{debug}}{yes}{%
    {\ss\inmargin{\ctxlua{thirddata.translit.debug_next()}} #1}%
  }%
}


%D The user-level command \type{\transliterate[#1]{#2}} does the job of
%D switching to a given language (for hyphenation) and adjusting the
%D substitution method locally.  It takes an optional list \type{[#1]} of
%D key-value arguments to allow ad-hoc specification of either two that deviate
%D from the defaults set initially by means of \type{\setuptransliterate}.
%D 
%D Internally, \type{\dotransliterate} is called according to the \CONTEXT\ 
%D coding style and in case the user provides \type{hyphenate=} or
%D \type{mode=} those will be used instead of the globals.  Note that this
%D leaves the latter unchanged.  Thus, in order to permanently switch to
%D another transliteration style the user would have to set it by calling
%D \type{\setuptransliterate} again.
%D
% All credits for rewriting the TeX code go to Wolfgang as well.
% http://www.ntg.nl/pipermail/ntg-context/2010/047816.html

\def\dotransliterate[#1]#2{%
  \bgroup\iffirstargument
    \setuptransliterate[#1]%
  \fi
    \language[\transliterateparameter{hyphenate}]%
    \ctxlua{
      thirddata.translit.deficient_font = "\transliterateparameter{deficient_font}"
      thirddata.translit.transliterate("\transliterateparameter{mode}","\luaescapestring{#2}")
    }%
  \egroup%
}

\unexpanded\def\transliterate{\dosingleempty\dotransliterate}

\def\expandabletransliterate#1{%
  %\bgroup
    %\setuptransliterate[#1]%
    %\language[\transliterateparameter{hyphenate}]%
    \ctxlua{
      thirddata.translit.deficient_font = "\transliterateparameter{deficient_font}"
      thirddata.translit.transliterate("\transliterateparameter{mode}","#1")
    }%
  %\egroup%
}

\unexpanded\def\starttransliterate{%
  \bgroup%
  \dosingleempty\dostarttransliterate%
}

\let\stoptransliterate\relax

\def\dostarttransliterate[#1]#2\stoptransliterate{%
  \iffirstargument
    \setuptransliterate[#1]%
  \fi
  \language[\transliterateparameter{hyphenate}]%
  \ctxlua{thirddata.translit.transliterate("\transliterateparameter{mode}","\luaescapestring{#2}")}%
  \egroup%
}

\newconditional\transliterate_useexpanded \setfalse\transliterate_useexpanded

\def\transliterate_conditional[#1]#2{%
  \ifconditional\transliterate_useexpanded
    \transliterate[#1]{#2}%
  \else
    \expandabletransliterate{#2}%
  \fi%
}

\protect \endinput

%   vim:ft=context