KKeine Bearbeitungszusammenfassung
KKeine Bearbeitungszusammenfassung
Zeile 28: Zeile 28:


function create_small_abbr_wikitext(text, title)
function create_small_abbr_wikitext(text, title)
return "<small>{{abbr|" .. text .. "|" .. title .. "}}</small>"
return "<small>{{Abbr|" .. text .. "|" .. title .. "}}</small>"
end
end



Version vom 17. September 2024, 17:16 Uhr

Die Dokumentation für dieses Modul kann unter Modul:Interlinear/Doku erstellt werden

local M = {}

local abbr = mw.loadData("Module:Interlinear/abbreviations")

local function parse_words(text)
	local words = {}
	local index = 1
	while index <= #text do
		if mw.ustring.match(text, "^{", index) then
			index = index + 1
			end_delim = mw.ustring.find(text, "}", index)
			if end_delim == nil then end_delim = #text + 1 end
			table.insert(words, mw.ustring.sub(text, index, end_delim - 1))
			index = end_delim + 1
		else
			local word_match = mw.ustring.match(text, "^%S+", index)
			if word_match ~= nil then
				table.insert(words, word_match)
				index =	index + #word_match + 1
			else
				local ws_match = mw.ustring.match(text, "^%s*", index)
				index = index + #ws_match
			end
		end
	end
	return words
end

function create_small_abbr_wikitext(text, title)
	return "<small>{{Abbr|" .. text .. "|" .. title .. "}}</small>"
end

function create_word_wikitext(word)
	return "''" .. word .. "''"	
end

function create_annotation_wikitext(word)
	word = mw.ustring.gsub(word, "-", "<small>–</small>")
	for abbr, title in pairs(abbr.abbreviations) do
		word = mw.ustring.gsub(word, abbr, create_small_abbr_wikitext(abbr, title))
	end
	return word
end

function create_interlinear(words, annotations)
	local container = mw.html.create("dl")
	container:addClass("interlinear")
	for i = 0, #words do
		local column = mw.html.create("div")
		column:addClass("interlinear__column")
		local word = mw.html.create("dt")
		word:addClass("interlinear__word")
		if words[i] then word:wikitext(create_word_wikitext(words[i])) end
		local annotation = mw.html.create("dl")
		annotation:addClass("interlinear__annotation")
		if annotations[i] then annotation:wikitext(create_annotation_wikitext(annotations[i])) end
		column:node(word)
		column:node(annotation)
		container:node(column)
	end
	return container
end

function create_interlinear_wrapper(words, annotations, translation)
	local wrapper = mw.html.create("div")
	wrapper:addClass("interlinear__wrapper")
	wrapper:node(create_interlinear(words, annotations))
	
	if translation ~= nil then
		local translationNode = mv.html.create("div")
		translationNode:addClass("interlinear__translation")
		wrapper:node(translationNode)
	end
	return wrapper
end

function M.render(frame)
	local words = parse_words(frame.args["text"] or frame.args[1] or "temporary default")
	local annotations = parse_words(frame.args["annotations"] or frame.args[2] or "a-NOM b-PL")
	local translation = frame.args["translation"] or frame.args[3]
	local elem = create_interlinear_wrapper(words, annotations, translation)
	return tostring(elem)
end

return M