summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/m-morse.mkvi
blob: d106acfdefbe8178f0d253ac98c2265c343efd20 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
%D \module
%D   [       file=m-morse,
%D        version=2010.12.10,
%D          title=\CONTEXT\ Extra Modules,
%D       subtitle=Morse,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright=PRAGMA]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

% todo: act upon the node list
% make it a buffer operation
% nice in cld manual

\startluacode

moduledata.morse = moduledata.morse or { }
local morse      = moduledata.morse

local utfcharacters, gsub = string.utfcharacters, string.gsub
local ucchars, shchars = characters.ucchars, characters.shchars

local codes = {

    ["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"] = "————·",

    ["."] = "·—·—·—",
    [","] = "——··——",
    [":"] = "———···",
    [";"] = "—·—·—",

    ["?"] = "··——··",
    ["!"] = "—·—·——",

    ["-"] = "—····—",
    ["/"] = "—··—· ",

    ["("] = "—·——·",
    [")"] = "—·——·—",

    ["="] = "—···—",
    ["@"] = "·——·—·",

    ["'"] = "·————·",
    ['"'] = "·—··—·",

    ["À"] =	"·——·—",
    ["Å"] =	"·——·—",
    ["Ä"] =	"·—·—",
    ["Æ"] =	"·—·—",
    ["Ç"] =	"—·—··",
    ["É"] =	"··—··",
    ["È"] =	"·—··—",
    ["Ñ"] =	"——·——",
    ["Ö"] =	"———·",
    ["Ø"] =	"———·",
    ["Ü"] =	"··——",
    ["ß"] =	"··· ···",

}

morse.codes = codes

setmetatable(codes, { __index = function(t,k)
    if k then
        local u = ucchars[k]
        local v = rawget(t,u) or rawget(t,shchars[u]) or false
        t[k] = v
        return v
    else
        return false
    end
end })

local MorseBetweenWords      = context.MorseBetweenWords
local MorseBetweenCharacters = context.MorseBetweenCharacters
local MorseLong              = context.MorseLong
local MorseShort             = context.MorseShort
local MorseSpace             = context.MorseSpace
local MorseUnknown           = context.MorseUnknown

local function toverbose(str)
    str = gsub(str,"%s*+%s*","+")
    str = gsub(str,"%s+"," ")
    local done = false
    for m in utfcharacters(str) do
        if done then
            MorseBetweenCharacters()
        end
        if m == "·" or m == "." then
            MorseShort()
            done = true
        elseif m == "—" or m == "-" then
            MorseLong()
            done = true
        elseif m == " " then
            if done then
                MorseBetweenCharacters()
            end
            done = false
        elseif m == "+" then
            MorseBetweenWords()
            done = false
        else
            MorseUnknown(m)
        end
    end
end

local function toregular(str)
    local inmorse = false
    for s in utfcharacters(str) do
        local m = codes[s]
        if m then
            if inmorse then
                MorseBetweenWords()
            else
                inmorse = true
            end
            local done = false
            for m in utfcharacters(m) do
                if done then
                    MorseBetweenCharacters()
                else
                    done = true
                end
                if m == "·" then
                    MorseShort()
                elseif m == "—" then
                    MorseLong()
                elseif m == " " then
                    MorseBetweenCharacters()
                end
            end
            inmorse = true
        elseif s == "\n" or s == " " then
            MorseSpace()
            inmorse = false
        else
            if inmorse then
                MorseBetweenWords()
            else
                inmorse = true
            end
            MorseUnknown(s)
        end
    end
end

local function tomorse(str,verbose)
    if verbose then
        toverbose(str)
    else
        toregular(str)
    end
end

morse.tomorse = tomorse

function morse.filetomorse(name,verbose)
    tomorse(resolvers.loadtexfile(name),verbose)
end

function morse.showtable()
    context.starttabulate { "|l|l|" } -- { "|l|l|l|" }
    for k, v in table.sortedpairs(codes) do
        context.NC() context(k)
     -- context.NC() context(v)
        context.NC() tomorse(v,true)
        context.NC() context.NR()
    end
    context.stoptabulate()
end

\stopluacode

\unprotect

% todo: \setupmorse, but probably it's not worth the trouble.

\def\MorseWidth             {0.4em}
\def\MorseHeight            {0.2em}
%def\MorseShort             {\dontleavehmode\blackrule[\c!height=\MorseHeight,\c!width=\dimexpr\MorseWidth]}
%def\MorseLong              {\dontleavehmode\blackrule[\c!height=\MorseHeight,\c!width=3\dimexpr\MorseWidth]}
\def\MorseShort             {\dontleavehmode\vrule\!!width \dimexpr\MorseWidth\!!height\MorseHeight\!!depth\zeropoint\relax}
\def\MorseLong              {\dontleavehmode\vrule\!!width3\dimexpr\MorseWidth\!!height\MorseHeight\!!depth\zeropoint\relax}
\def\MorseBetweenCharacters {\kern\MorseWidth}
\def\MorseBetweenWords      {\hskip3\dimexpr\MorseWidth\relax}
\def\MorseSpace             {\hskip7\dimexpr\MorseWidth\relax}
\def\MorseUnknown      #text{[\detokenize{#text}]}

\def\MorseCode         #text{\ctxlua{moduledata.morse.tomorse(\!!bs#text\!!es,true)}}
\def\MorseString       #text{\ctxlua{moduledata.morse.tomorse(\!!bs#text\!!es)}}
\def\MorseFile         #text{\ctxlua{moduledata.morse.filetomorse("#text")}}
\def\MorseTable             {\ctxlua{moduledata.morse.showtable()}}

\let\Morse                   \MorseString

\protect

\doifnotmode{demo}{\endinput}

\starttext

\MorseTable

\startlines
\MorseCode{—·—· ——— —· — · —··— —+—— —·— ·· ···—}
\MorseCode{—·—· ——— —· — · —··— — + —— —·— ·· ···—}
\Morse{ÀÁÂÃÄÅàáâãäå}
\Morse{ÆÇæç}
\Morse{ÈÉÊËèéêë}
\Morse{ÌÍÎÏìíîï}
\Morse{Ññ}
\Morse{ÒÓÔÕÖòóôõö}
\Morse{Øø}
\Morse{ÙÚÛÜùúû}
\Morse{Ýýÿ}
\Morse{ß}
\Morse{Ţţ}
\stoplines

\Morse{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}

\stoptext