local export = {}
function export.getAlternativeForms(lang, term)
local page = mw.title.new(require("Module:links").getLinkPage(term, lang))
local content = page:getContent()
if not content then
return ""
end
local _, index = mw.ustring.find(content, "\n==" .. lang:getCanonicalName() .. "==", nil, true)
if not index then
_, index = mw.ustring.find(content, "^==" .. require("Module:utilities").pattern_escape(lang:getCanonicalName()) .. "==", nil, false)
end
if not index then
error("Language not found: " .. lang:getCanonicalName() .. ".")
end
_, next_lang = mw.ustring.find(content, "\n==+==", index, false)
_, index = mw.ustring.find(content, "\n(=====?)Alternative forms%1", index, false)
index = mw.ustring.find(content, "{{alter|" .. lang:getCode() .. "|+", index)
if (not index) or (next_lang and next_lang < index) then
return ""
end
local next_section = mw.ustring.find(content, "\n(=+)+%1", index)
local alternative_forms_section = mw.ustring.sub(content, index, next_section)
mw.log(alternative_forms_section)
local terms_list = {}
for alternative_form_template in mw.ustring.gmatch(alternative_forms_section, "{{alter|+}}") do
local terms
if mw.ustring.find(alternative_form_template, "||") then
terms = mw.ustring.match(alternative_form_template, "{{alter|" .. lang:getCode() .. "|(.+)||")
else
terms = mw.ustring.match(alternative_form_template, "{{alter|" .. lang:getCode() .. "|(.+)}}")
end
terms_list = mw.text.split(terms, "|")
end
if #terms_list == 0 then
return ""
end
for i, term in ipairs(terms_list) do
terms_list = require("Module:links").full_link{term = term, lang = lang}
end
return ", " .. table.concat(terms_list, ", ")
end
function export.getDescendants(lang, term)
local page = mw.title.new(require("Module:links").getLinkPage(term, lang))
local content = page:getContent()
if not content then
return ""
end
local _, index = mw.ustring.find(content, "\n==" .. lang:getCanonicalName() .. "==", nil, true)
if not index then
_, index = mw.ustring.find(content, "^==" .. require("Module:utilities").pattern_escape(lang:getCanonicalName()) .. "==", nil, false)
end
if not index then
error("Language not found: " .. lang:getCanonicalName() .. ".")
end
_, next_lang = mw.ustring.find(content, "\n==+==", index, false)
_, index = mw.ustring.find(content, "\n(=====?)Descendants%1", index, false)
if (not index) or (next_lang and next_lang < index) then
error("Descendants not found")
end
local asterisks, item
local count = 0
local items = {}
while mw.ustring.sub(content, index + 1, index + 2) == "\n*" do
_, index, item = mw.ustring.find(content, "\n(%*+)", index)
-- Preprocess, but replace recursive calls to avoid template loop errors
item = mw.ustring.gsub(item, "{{desctree|", "{{#invoke:descendants tree/templates|show|")
item = mw.getCurrentFrame():preprocess(item)
-- Parse the list item
asterisks, item = mw.ustring.match(item, "^(%*+:?) *(.-)$")
item = "<li>" .. item
local newcount = mw.ustring.len(asterisks)
if newcount > count then
while newcount > count do
item = "<ul>" .. item
count = count + 1
end
else
item = "</li>" .. item
end
while newcount < count do
item = "</li></ul>" .. item
count = count - 1
end
table.insert(items, item)
end
while 0 < count do
table.insert(items, "</li></ul>")
count = count - 1
end
return table.concat(items)
end
return export