Modul:AL

Z Wikislovníku
Skočit na navigaci Skočit na vyhledávání

Dokumentaci tohoto modulu lze vytvořit na stránce Nápověda:Modul:AL

p = {}

function RemoveSelflink (link)
	if link == "[[" .. pagename .. "]]" then
		return pagename
	else
		return link
	end
end

local Replacements = mw.loadData ('Modul:AL/Replacements')

function replacements (text)
	for word in pairs (Replacements) do
		for repl in pairs (Replacements[word]) do
			if "[[" .. Replacements[word][repl] .. "]]" == text then
				text = text:gsub( "[[]", "")
				text = text:gsub( "[]]", "")
				return "[[" .. word .. "|" .. text .. "]]"
			end
		end
	end
	return text
end

function link (link)

	-- slova odpovídající následujícím výrazům nebudou mít odkaz
	local noLinks = { "^[(]*[0-9]+[.-,;)]*$", "^[0-9]+[/][0-9]+$" }
	
	for l in pairs (noLinks) do
		if link:find (noLinks[l]) then
			return replacements (link)
		end
	end
	-- slova začínající na ! nebudou prolinkována, ale ! se nevloží
	
	if link:find ("^[!]") then
		return link:gsub ("^[!]", "")
	end
	
	-- podtržítko (_) se změní na mezeru
	
	local out = link:gsub("_", " ")
	
	-- defaultní linkování slova
	out = out:gsub("^(.*)$","[[%1]]")
	
	-- závorky vždy na konec, resp. začátek slova
	out = out:gsub("(.*)[(](.*)", "(%1%2")
	out = out:gsub("(.*)[)](.*)", "%1%2)")
	
	-- interpunkce ignorovaná na konci slova
	
	local ignores = { ";", ",", "!", "?", "/" }
	for i in pairs (ignores) do
		out = out:gsub("[" .. ignores[i] .. "][]][]]", "]]" .. ignores[i])
		out = out:gsub("[" .. ignores[i] .. "][)]", ")" .. ignores[i])
	end

	



	-- výrazy s lomítkem (/)
	if out:find("[^/][/][^/]") then
		out = out:gsub("[]][]]","")
		out = out:gsub("/","]]")
	end
	-- dvě lomítka vloží 1 lomítko
	out = out:gsub("[/][/]","/")
	


	return  replacements (out)

end



function p.print (frame)
	local out = ""
	local arg = {}
	n = 1
	-- aby šlo pracovat s počtem argumentů
	for a in pairs (frame:getParent().args) do
		table.insert (arg, frame:getParent().args[n])
		n = n + 1
	end
	local page = mw.title.getCurrentTitle()
	pagename = page.text
	
	local text = ""
	n = 1
	for a in pairs (arg) do
		text = text .. arg[n]
		if n ~= #arg then
			text = text .. "|" 
		end
		n = n + 1
	end
	
	local words = {}
	for word in text:gmatch ("[^ ]+") do
		table.insert (words, word)
	end
	n = 1
	for word in pairs (words) do
		out = out .. link (words[word])
		if word ~= #words then
			out = out .. " "
		end
	end
	
	return  out
end

return p