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
___________________________________________________________________________________

Reply via email to