Modul:Interlinear

Version vom 17. September 2024, 19:25 Uhr von Niko Roether (Diskussion | Beiträge) (fix: Use dd for annotations)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)

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 <= mw.ustring.len(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 = mw.ustring.len(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 + mw.ustring.len(word_match) + 1
			else
				local ws_match = mw.ustring.match(text, "^%s*", index)
				index = index + mw.ustring.len(ws_match)
			end
		end
	end
	return words
end

local function create_small_abbr_wikitext(text, title)
	local small = mw.html.create("small")
	local abbr = mw.html.create("abbr")
	abbr:wikitext(text)
	abbr:attr("title", title)
	small:node(abbr)
	return tostring(small)
end

local 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

local function create_interlinear(words, annotations)
	local container = mw.html.create("dl")
	container:addClass("interlinear")
	for i = 1, #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(words[i]) end
		local annotation = mw.html.create("dd")
		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

local 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 = mw.html.create("div")
		translationNode:addClass("interlinear__translation")
		translationNode:wikitext(translation)
		wrapper:node(translationNode)
	end
	return wrapper
end

function M.render(frame)
	local parentFrame = frame:getParent() or {}
	local args = parentFrame.args or {}
	local words = parse_words(args["text"] or args[1] or "")
	local annotations = parse_words(args["annotations"] or args[2] or "")
	local translation = args["translation"] or args[3]
	local elem = create_interlinear_wrapper(words, annotations, translation)
	return tostring(elem)
end

return M