Modul:Interlinear: Unterschied zwischen den Versionen
Die Seite wurde neu angelegt: „local M = {} local abbr = mw.loadData("Module:Interlinear/abbreviations") local function parse_words(text) local words = {} local index = 0 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 =…“ |
Keine Bearbeitungszusammenfassung |
||
| Zeile 27: | Zeile 27: | ||
end | end | ||
M.parse_words = parse_words | 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[1]) | |||
local annotations = parse_words(frame.args[2]) | |||
local translation = frame.args[3] | |||
local elem = create_interlinear_wrapper(words, annotations, translation) | |||
return tostring(elem) | |||
end | |||
return M | return M | ||
Version vom 17. September 2024, 16:53 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 = 0
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[1])
local annotations = parse_words(frame.args[2])
local translation = frame.args[3]
local elem = create_interlinear_wrapper(words, annotations, translation)
return tostring(elem)
end
return M