Vorlagenprogrammierung Diskussionen Lua Unterseiten
Modul Deutsch English

Modul: Dokumentation

Diese Seite enthält Code in der Programmiersprache Lua. Einbindungszahl Cirrus

Alle wissenswerten Hinweis sind vorübergehend an passender Stelle im Lua-Quellcode als Kommentar zu finden.

Modul für die Vorlage:Morse, siehe auch Dokumentation dazu.


--[[ 
Module:Morse 2021-09-21 © (CC BY-SA 3.0) by User:Kpfiwa Version: 3.4
--]]
frame = mw.getCurrentFrame() -- wichtig für Regionsaabfrage als Zusatzparameeter in der Vorlage "region={{int:lang}}"
-- For versions in other languages, names for the module and template must be adapted HERE
local PROS = require("Module:Morse/Prosigns")  -- HERE
local DIGR = require("Module:Morse/Digraphs")  -- HERE
local MOGR = require("Module:Morse/Monographs")  -- HERE


local p, version = {}, {}
local size, sicec,color, bgcolor=0,0,'',''
local txt, erg = "", ""
local size = 40
local audio ITU, USA, GERKE, HM, COMPACT = {}, {}, {}, {}, {}
local version = ITU -- Version International = default

local TMP = {
    ["&"] = "x",
    ["#"] = "y",
    [";"] = "z",
    ["0"] = "o",
    ["1"] = "b",
    ["2"] = "c",
    ["3"] = "d",
    ["4"] = "e",
    ["5"] = "f",
    ["6"] = "g",
    ["7"] = "h",
    ["8"] = "i",
    ["9"] = "j"
}

function usout(frame)
    local ind, erg = "  ", ""
    for key, val in pairs(frame.args) do -- Alle Parameter sammeln zuordnen
        if key == "noindent" then
            ind = ""
        elseif val == "word" then
            erg = erg .. "    "
        elseif val == "dot" then
            erg = erg .. "▄ "
        elseif val == "dash" then
            erg = erg .. "▄▄▄ "
        elseif val == "adash" then
            erg = erg .. "▄▄ "
        elseif val == "aspace" then
            erg = erg .. " "
        elseif val == "ldash" then
            erg = erg .. "▄▄▄▄ "
        elseif val == "zerodash" then
            erg = erg .. "▄▄▄▄▄▄▄▄▄▄▄ "
        end
    end
    return '<span style="font-family:monospace;font-size:40%;vertical-align:1.5ex; white-space:pre-wrap;">' ..
        ind .. erg .. '</span>'
end -- function usout()

local function tu(a, z)
    return string.sub(string.upper(a or ''), 1, z or 1)
end

local function colore(c, bg)
    local css, f, tmpc = "", "", ""
    local val = c or ""
    local v = tu(val)
    local red = "#ff0000"
    local gre = "#508f10"
    local blu = "#0000a0"
    local yel = "#ffff00"
    local wht = "#ffffff"
    local orn = "#ff8000"
    local blk = "#000000"
    if v == "#" then -- da hat jemand die richtige Farbe eingegeben?
        tmpc = c
    elseif v == "Y" then
        tmpc = yel -- yellow
    elseif v == "B" then
        tmpc = blu -- blau, blue
    elseif v == "O" then
        tmpc = orn -- orange
    elseif v == "W" then
        tmpc = wht -- weiß, white
    elseif v == "S" or v == "N" or v == "D" then
        tmpc = blk -- schwarz, noir, dark
    elseif v == "R" then
        tmpc = red -- rot
    elseif v == "G" then -- gelb oder grün? (special for Germans)
        if tu(c, 2) == "GR" then
            tmpc = gre -- grün
        else
            tmpc = yel -- gelb
        end --  if tu(c)
    end -- if v == #
    if bg then --
        css = "background-color:" .. tmpc .. ";"
    else
        css = "color:" .. tmpc .. ";link:" .. tmpc .. ";vlink:" .. tmpc .. ";"
    end
    return css
end -- function colore()

local function los(t, tbl) -- für alle Fälle
    local ret = t or ""
    for k, v in pairs(tbl) do --
        if (type(k) == "string") and (type(v) == "string") then
            ret = ret:gsub(k, v)
        end --  if (type(k)
    end -- for k, v in pairs(tbl)
    return ret
end -- function los

function monochange(txt)
    ret = ""
    -- mw.ustring.len(txt)
    for i = 1, mw.ustring.len(txt) do
        local c =  mw.ustring.sub(txt, i, i) 
        ret = ret .. (MOGR[c] or c)
    end
    return ret
end -- function monochange

local function gerk() -- https://dk9vz.com/index.php/faszination-telegrafie/clemens-friedrich-gerke-1801-1888
    MOGR["o"] = "nt" -- 0 zerodash
    MOGR["b"] = "pllpt" -- 1
    MOGR["c"] = "pplppt" -- 2
    MOGR["e"] = "ppplpt" -- 3
    MOGR["f"] = "lllt" -- 5
    MOGR["g"] = "pppppt" -- 6
    MOGR["h"] = "llppt" -- 7
    MOGR["i"] = "lppppt" -- 8
    MOGR["j"] = "lpplt" -- 9
    MOGR["C"] = "lplpt" -- G
    MOGR["F"] = "pplpt" -- G
    MOGR["J"] = "plllt" -- G
    MOGR["O"] = "lllt" -- G
    MOGR["M"] = "plpppt" -- G
    MOGR["P"] = "pppppt" -- G
    MOGR["X"] = "pplpppt" -- G
    MOGR["Y"] = "llpppt" -- G
    MOGR["Z"] = "pllppt" -- G
    return true
end --function gerk()

local function arr()
    --[[ In order to avoid conflicts, these letters were exchanged with others 
        [&]=x, [#]=y, [;]=z, [0]=a, [1]=b, [2]=c, [3]=d, [4]=e, [5]=f, [6]=g, [7]=h, [8]=i, [9]=j --]]
    MOGR["%."] = "ppllppt" -- period .
    MOGR["%,"] = "plplt" -- comma ,
    MOGR["?"] = "lpplpt" -- Question mar k ?
    MOGR["!"] = "lllpt" -- Exclamation mark !
    MOGR["x"] = "pqpppt" -- Ampersand & !!
    MOGR["X"] = "llllt" -- Paragraph break -- ["%="] = "lppplt",   ????
    MOGR["z"] = "pppqppt" -- Semicolon
    MOGR["%:"] = "lplqpqpt" -- Colon
    MOGR["%'"] = "pplpqplppt" -- Apostrophe '
    MOGR["%/"] = "pplqlt" -- Slash /
    MOGR["%-"] = "ppppqplppt" -- Hyphen [-]
    MOGR["%("] = "pppppqlpt" -- Parenthesis (open) [(]
    MOGR["%)"] = "pppppqppqppt" -- Parenthesis (close) [)]
    MOGR["o"] = "nt" -- 0 zerodash
    MOGR["b"] = "pllpt" -- 1
    MOGR["c"] = "pplppt" -- 2
    MOGR["e"] = "ppplpt" -- 3
    MOGR["f"] = "lllt" -- 5
    MOGR["g"] = "pppppt" -- 6
    MOGR["h"] = "llppt" -- 7
    MOGR["i"] = "lppppt" -- 8
    MOGR["j"] = "lpplt" -- 9
    MOGR["C"] = "ppqpt"
    MOGR["F"] = "pplpt"
    MOGR["J"] = "lplpt"
    MOGR["L"] = "mt" -- ldash
    MOGR["O"] = "pqpt"
    MOGR["P"] = "pppppt"
    MOGR["Q"] = "pplpt"
    MOGR["R"] = "pqppt"
    MOGR["X"] = "plppt"
    MOGR["Y"] = "ppqppt"
    MOGR["Z"] = "pppqpt"
    return true
end --function arr()

local function sound(L, txt, sz) 
    L = tu(L,3) -- Sonderfall SOS: File:SOS morse code.ogg
    if L ~= "SOS" then
        L = tu(L)
    end
    local n = ""
    if L:find("[A-Z0-9]+") then
        if L:find("%d+") then
            n = " number"
        end
        txt ="[[File:Loudspeaker.svg|" ..  math.floor(sz / 5 + 0.5) .. 
        "px|link=File:" .. L .. n .. " morse code.ogg]]&#160;[[Media:" .. 
        L .. n .. " morse code.ogg|" .. txt .. "]]"

    end
    return txt
end -- function sound()
function laststep(txt, version, color, bgcolor, size, sizec)
    local i = 1
    -- CSS-Umrahmung
    -- Ein ' ' vor und nach 'span' wäre ggf. sinnvoll , ggf. nur für USA?
    local csss =
        '<span style="font-family:monospace;white-space:pre-wrap;font-size:' ..
        tonumber(size) * (sizec or 1) ..
            "%;padding-bottom:" ..
                math.floor(size / 12 / (sizec or 1) + 0.5) .. "px;vertical-align:1.5px;" .. color .. bgcolor .. '"><sup>'
    local csse = "</sup></span>"
    while i do -- trim trailing spaces
        i, j = string.find(txt, "[ts]+", -1)
        if i then
            txt = txt:sub(1, i - 1)
        end
    end -- while i
    txt = txt:gsub(" ", "r") --  passt zu Wortlücke 7, umbrechbar
    if version == "USA" then
        txt = txt:gsub("L", "&#9604;&#9604;&#9604;&#9604;&#160;") -- = US 'L'
        txt = txt:gsub("l", "&#9604;&#9604;&#160;") --- 2 unit
        txt = txt:gsub("m", "&#9604;&#9604;&#9604;&#9604;&#160;")
        txt = txt:gsub("q", "&#160;")
        txt = txt:gsub("n", "&#9604;&#9604;&#9604;&#9604;&#9604;&#9604;&#9604;&#9604;&#9604;&#9604;&#9604;&#160;")
        txt = txt:gsub("r", "&#160;&#160;&#160;&#32;")
    elseif version == "GERKE" then
        txt = txt:gsub("l", "&#9604;&#9604;&#160;")
        txt = txt:gsub("m", "&#9604;&#9604;&#9604;&#160;")
        txt = txt:gsub("n", "&#9604;&#9604;&#9604;&#9604;&#9604;&#9604;&#9604;&#9604;&#9604;&#9604;&#9604;&#160;") -- = US Null '0'
        txt = txt:gsub("r", "&#160;&#160;&#160;&#32;")
    elseif version == "COMPACT" then
        txt = txt:gsub("l", "&#10073;")
        txt = txt:gsub("r", "&#160;&#32;")
        txt = txt:gsub("p", "•")
        txt = txt:gsub("t", "&#160;")
    elseif version == "SV" then -- Schwedisch
        txt = txt:gsub("l", "<s>&#160;&#160;&#160;</s>&#160;")
        txt = txt:gsub("p", "<s>&#160;</s>&#160;")
        csss = "'''"
        csse = "'''"
    elseif version == "FR" then -- Französich
        txt = txt:gsub("p", ".")
        txt = txt:gsub("l", "-")
        txt = txt:gsub("[tr]", "/")
        csss = "'''"
        csse = "//'''"
    else
        txt = txt:gsub("l", "&#9604;&#9604;&#9604;&#160;")
        txt = txt:gsub("r", "&#160;&#160;&#160;&#32;")
    end
    txt = txt:gsub("p", "&#9604;&#160;")
    txt = txt:gsub("r", "&#160;&#160;&#160;&#32;")
    txt = txt:gsub("t", "&#160;&#160;")
    -- falls am Ende '&#160;', dann tauschen gegen '&#32';
    i, j = string.find(txt, "&#160;", -7)
    if i then
        erg = txt:sub(1, i - 1)
    end
    return csss .. txt .. csse
end -- function laststep


function p.morse(frame) -- START
    local lang = tu(frame.args["lang"]) -- Falls Sonderbehandlung erforderlich (FR, SV)
    if lang == "DE" then
        version = "INT"
    end
    -- version= tu(version,-1)
    local frame = frame:getParent()
    local txt, color, bgcolor = "", "", ""
    for key, val in pairs(frame.args) do
        --- Falls :en:Template:morse, hier gleich verzweigen
        v = string.find("XadashXaspaceXdotXdashXnoindentXldashXwordXzerodashX", "X" .. val .. "X")
        if v or key == "noindent" then
            return usout(frame) -- und finito hier!
        end
        key = tu(key)
        if key == "R" then
            val = tu(val, 2)
            version = val -- FR, SV
        end
        if key == "1" then
            txt = val
            if #txt == 1 then
                version = "HM" -- quasi Kurzverfahren
            end
        elseif key == "2" then
            size = tonumber(val) or 40
        elseif key == "Z" or key == "C" then
            color = colore(val)
        elseif key == "H" or key == "B" then
            bgcolor = colore(val, 1)
        elseif key == "V" then -- Regionsangabe
            val = tu(val)
            if val == "U" then
                version = "USA"
                arr() -- will transfer letters  to USA code
            elseif val == "K" or val == "C" then
                version = "COMPACT"
                sizec = 2
            elseif val == "G" then
                gerk()
                version = "GERKE"
            elseif val == "A" or val == "Q" or val == "H" then
                version = "HM"
            end
        elseif key == "A" then
            audio = txt
        end
    end -- for key, val
    --
    local erg = string.upper(txt) or ""
    erg = los(erg, TMP)
	if version ~= "HM" and  version ~= "FR" and  version ~= "SV" then
		erg = los(erg, PROS)
		erg = los(erg, DIGR)
	end
    erg = monochange(erg)
    erg = laststep(erg, version, color, bgcolor, size, sizec)
    -- Audio-Eimbindung
    if audio then
        erg = sound(audio, erg, size)
    end
    -- UND AB GEHT DIE POST!
    return erg
end -- function p.f(frame)

return p