KKeine Bearbeitungszusammenfassung
fix: Use dd for annotations
 
(3 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 6: Zeile 6:
local words = {}
local words = {}
local index = 1
local index = 1
while index <= #text do
while index <= mw.ustring.len(text) do
if mw.ustring.match(text, "^{", index) then
if mw.ustring.match(text, "^{", index) then
index = index + 1
index = index + 1
end_delim = mw.ustring.find(text, "}", index)
end_delim = mw.ustring.find(text, "}", index)
if end_delim == nil then end_delim = #text + 1 end
if end_delim == nil then end_delim = mw.ustring.len(text) + 1 end
table.insert(words, mw.ustring.sub(text, index, end_delim - 1))
table.insert(words, mw.ustring.sub(text, index, end_delim - 1))
index = end_delim + 1
index = end_delim + 1
Zeile 17: Zeile 17:
if word_match ~= nil then
if word_match ~= nil then
table.insert(words, word_match)
table.insert(words, word_match)
index = index + #word_match + 1
index = index + mw.ustring.len(word_match) + 1
else
else
local ws_match = mw.ustring.match(text, "^%s*", index)
local ws_match = mw.ustring.match(text, "^%s*", index)
index = index + #ws_match
index = index + mw.ustring.len(ws_match)
end
end
end
end
Zeile 27: Zeile 27:
end
end


function create_small_abbr_wikitext(text, title)
local function create_small_abbr_wikitext(text, title)
local small = mw.html.create("small")
local small = mw.html.create("small")
local abbr = mw.html.create("abbr")
local abbr = mw.html.create("abbr")
Zeile 36: Zeile 36:
end
end


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


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


function create_interlinear_wrapper(words, annotations, translation)
local function create_interlinear_wrapper(words, annotations, translation)
local wrapper = mw.html.create("div")
local wrapper = mw.html.create("div")
wrapper:addClass("interlinear__wrapper")
wrapper:addClass("interlinear__wrapper")
Zeile 71: Zeile 71:
local translationNode = mw.html.create("div")
local translationNode = mw.html.create("div")
translationNode:addClass("interlinear__translation")
translationNode:addClass("interlinear__translation")
translationNode:wikitext(translation)
wrapper:node(translationNode)
wrapper:node(translationNode)
end
end

Aktuelle Version vom 17. September 2024, 19:25 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 <= 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