On 10-12-2010 11:17, Jonas Stein wrote:
\define[]\short{\dontleavehmode \blackrule[width=0.4em]\ }
\define[]\long {\dontleavehmode \blackrule[width=1.4em]\ }
\define[1] \morse {MAGIC}
\morse{.-..}
up to you to complete the tables ... after that I'll clean up the code a
bit
-
Hans Hagen | PRAGMA ADE
Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com
| www.pragma-pod.nl
-
\startluacode
moduledata.morse = moduledata.morse or { }
local morsecodes = {
["A"] = "·â",
["B"] = "â···",
["C"] = "â·â·",
["D"] = "â··",
["E"] = "·",
["F"] = "··â·",
["G"] = "ââ·",
["H"] = "····",
["I"] = "··",
["J"] = "·âââ",
["K"] = "â·â",
["L"] = "·â··",
["M"] = "ââ",
["N"] = "â·",
["O"] = "âââ",
["P"] = "·ââ·",
["Q"] = "ââ·â",
["R"] = "·â·",
["S"] = "···",
["T"] = "â",
["U"] = "··â",
["V"] = "···â",
["W"] = "·ââ",
["X"] = "â··â",
["Y"] = "â·ââ",
["Z"] = "ââ··",
["0"] = "âââââ",
["1"] = "·ââââ",
["2"] = "··âââ",
["3"] = "···ââ",
["4"] = "····â",
["5"] = "·····",
["6"] = "â····",
["7"] = "ââ···",
["8"] = "âââ··",
["9"] = "ââââ·",
["."] = "·â·â·â",
[","] = "ââ··ââ",
["?"] = "··ââ··",
["-"] = "â····â",
["/"] = "â··â· ",
[":"] = "âââ···",
["'"] = "·ââââ·",
["-"] = "â····â",
[")"] = "â·ââ·â",
[";"] = "â·â·â ",
["("] = "â·ââ·",
["="] = "â···â",
["@"] = "·ââ·â·",
}
local simplecodes = {
["."] = "·",
["-"] = "â",
}
function moduledata.morse.tomorse(str,translate)
if translate then
str = utf.gsub(string.upper(str),".",morsecodes)
else
str = utf.gsub(str,".",simplecodes)
end
context.ignorespaces()
for s in string.utfcharacters(str) do
if s == "·" then
context.MorseShort()
elseif s == "â" then
context.MorseLong()
elseif s == "\n" or s == " " then
context.MorseSpace()
else
context.MorseUnknown(s)
end
end
context.removeunwantedspaces()
end
function moduledata.morse.filetomorse(name,translate)
moduledata.morse.tomorse(resolvers.loadtexfile(name),translate)
end
\stopluacode
\unprotect
\def\MorseShort{\dontleavehmode\blackrule[\c!width=0.4em]\space}
\def\MorseLong {\dontleavehmode\blackrule[\c!width=1.4em]\space}
\def\MorseSpace{\emspace}
\def\MorseUnknown#1{\emspace}
\def\MorseCode #1{\ctxlua{moduledata.morse.tomorse("#1")}}
\def\Morse #1{\ctxlua{moduledata.morse.tomorse("#1",true)}}
\def\MorseFile #1{\ctxlua{moduledata.morse.filetomorse("#1",true)}}
\unprotect
\starttext
\MorseCode{.-..}
\Morse{CONTEXT}
\MorseFile{tufte}
\stoptext
___
If your question is of interest to others as well, please add an entry to the
Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://tex.aanhet.net
archive : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___