Aller au contenu

Module:Morse

Une page de Wikipédia, l'encyclopédie libre.

 Documentation[voir] [modifier] [historique] [purger]

Utilisation[modifier le code]

Fonctions exportables :

  • morse(frame) – affiche le code morse correspondant au premier argument non nommé.

Modules externes et autres éléments dont ce module a besoin pour fonctionner :

  • mw.frame – args[1] = texte en majuscules A..Z 0..9 (le reste donne //)

Exemples[modifier le code]

  • {{#invoke:Morse|morse|SOS}} donne /.../---/.../
  • {{#invoke:Morse|morse|WIKIPEDIA}} donne /.--/../-.-/../.--././-../../.-/
  • {{#invoke:Morse|morse|HELLO WORLD}} donne /...././.-../.-../---//.--/---/.-./.-../-../
  • {{#invoke:Morse|morse|ANNEE 2015}} donne /.-/-./-./././/..---/-----/.----/...../
-- Traduit une chaîne de caractères en morse
--
local c = {}

c.alphas =	{
		".-", "-...", "-.-.", "-..", ".",	-- A-E 
		"..-.", "--.", "....", "..", ".---",	-- F-J
		"-.-", ".-..", "--", "-.", "---",	-- K-O
		".--.", "--.-", ".-.", "...", "-",	-- P-T
		"..-", "...-", ".--", "-..-", "-.--",	-- U-Y
		"--.."					-- Z
		};
c.numbers =	{
		"-----",					-- 0
		".----", "..---", "...--", "....-", ".....",	-- 1-5
		"-....", "--...", "---..", "----."		-- 6-9
		};

function c.morse(frame)
	local s='/'
	for i=1,#frame.args[1],1
		do
			local x = string.byte(frame.args[1],i)	-- ASCII code of ith character
			if ((x >= 65) and (x <= 90))     then s = s .. c.alphas[x-64]  .. "/"	-- A..Z
			elseif ((x >= 48) and (x <= 57)) then s = s .. c.numbers[x-47] .. "/"	-- 0..9
			else	s = s.."/" end							-- space & catch-all
		end
	return s
end

--[[ -----------------------
-- TEST -- Décommenter pour tester en 'standalone'; imprime quelques exemples de traductions
-- 
c.frame = {}
c.frame.args = {}
function pr_morse(txt)
	c.frame.args[1] = txt
	print(c.morse(c.frame))
end
pr_morse("SOS")
pr_morse("WIKIPEDIA")
pr_morse("HELLO WORLD")
pr_morse("A1 S2 O9")
----------------------  --]]
return c