summaryrefslogtreecommitdiff
path: root/tex/context/base/char-tex.lua
diff options
context:
space:
mode:
authorMarius <mariausol@gmail.com>2010-07-04 15:32:09 +0300
committerMarius <mariausol@gmail.com>2010-07-04 15:32:09 +0300
commit85b7bc695629926641c7cb752fd478adfdf374f3 (patch)
tree80293f5aaa7b95a500a78392c39688d8ee7a32fc /tex/context/base/char-tex.lua
downloadcontext-85b7bc695629926641c7cb752fd478adfdf374f3.tar.gz
stable 2010-05-24 13:10
Diffstat (limited to 'tex/context/base/char-tex.lua')
-rw-r--r--tex/context/base/char-tex.lua89
1 files changed, 89 insertions, 0 deletions
diff --git a/tex/context/base/char-tex.lua b/tex/context/base/char-tex.lua
new file mode 100644
index 000000000..ed9a244d7
--- /dev/null
+++ b/tex/context/base/char-tex.lua
@@ -0,0 +1,89 @@
+if not modules then modules = { } end modules ['char-tex'] = {
+ version = 1.001,
+ comment = "companion to char-ini.mkiv",
+ author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
+ copyright = "PRAGMA ADE / ConTeXt Development Team",
+ license = "see context related readme files"
+}
+
+characters = characters or { }
+characters.tex = characters.tex or { }
+
+local find = string.find
+
+local accent_map = {
+ ['~'] = "̃" , -- ̃ Ẽ
+ ['"'] = "̈" , -- ̈ Ë
+ ["`"] = "̀" , -- ̀ È
+ ["'"] = "́" , -- ́ É
+ ["^"] = "̂" , -- ̂ Ê
+ -- ̄ Ē
+ -- ̆ Ĕ
+ -- ̇ Ė
+ -- ̉ Ẻ
+ -- ̌ Ě
+ -- ̏ Ȅ
+ -- ̑ Ȇ
+ -- ̣ Ẹ
+ -- ̧ Ȩ
+ -- ̨ Ę
+ -- ̭ Ḙ
+ -- ̰ Ḛ
+}
+
+local accents = table.concat(table.keys(accent_map))
+
+local function remap_accents(a,c,braced)
+ local m = accent_map[a]
+ if m then
+ return c .. m
+ elseif braced then
+ return "\\" .. a .. "{" .. c .. "}"
+ else
+ return "\\" .. a .. c
+ end
+end
+
+local command_map = {
+ ["i"] = "ı"
+}
+
+local function remap_commands(c)
+ local m = command_map[c]
+ if m then
+ return m
+ else
+ return "\\" .. c
+ end
+end
+
+local P, C, R, S, Cs, Cc = lpeg.P, lpeg.C, lpeg.R, lpeg.S, lpeg.Cs, lpeg.Cc
+local U, lpegmatch = lpeg.patterns.utf8, lpeg.match
+
+local accents = (P('\\') * C(S(accents)) * (P("{") * C(U) * P("}" * Cc(true)) + C(U) * Cc(false))) / remap_accents
+local commands = (P('\\') * C(R("az","AZ")^1)) / remap_commands
+
+local convert_accents = Cs((accents + P(1))^0)
+local convert_commands = Cs((commands + P(1))^0)
+
+local no_l = P("{") / ""
+local no_r = P("}") / ""
+
+local convert_accents_strip = Cs((no_l * accents * no_r + accents + P(1))^0)
+local convert_commands_strip = Cs((no_l * commands * no_r + commands + P(1))^0)
+
+function characters.tex.toutf(str,strip)
+ if find(str,"\\") then -- we can start at teh found position
+ if strip then
+ str = lpegmatch(convert_commands_strip,str)
+ str = lpegmatch(convert_accents_strip,str)
+ else
+ str = lpegmatch(convert_commands,str)
+ str = lpegmatch(convert_accents,str)
+ end
+ end
+ return str
+end
+
+--~ print(characters.tex.toutf([[\"{e}]]),true)
+--~ print(characters.tex.toutf([[{\"{e}}]],true))