မေႃႇၵျူး:descendants tree
Documentation for this module may be created at မေႃႇၵျူး:descendants tree/doc
local export = {}
local function language_header_error(entry_name, language_name)
mw.log("No header for " .. language_name .. " was found in the entry [[|"
.. entry_name .. "]].")
--[[ၶိုၵ်ႉတွၼ်း:WhatLinksHere/descendants tree/language header not found]]
require("Module:debug").track("descendants tree/language header not found")
end
function export.getAlternativeForms(lang, term)
local entry_name = require("Module:links").getLinkPage(term, lang)
local page = mw.title.new(entry_name)
local content = page:getContent()
if not content then
return ""
end
local _, index = string.find(content,
"==" .. lang:getCanonicalName() .. "==", nil, true)
if not index then
language_header_error(entry_name, lang:getCanonicalName())
return ""
end
local _, next_lang = string.find(content, "\n==[^=\n]+==", index, false)
local _, index = string.find(content, "\n(====?=?)Alternative forms%1", index, false)
local langCodeRegex = require("Module:string").pattern_escape(lang:getCode())
index = string.find(content, "{{alter|" .. langCodeRegex .. "|[^|}]+", index)
if (not index) or (next_lang and next_lang < index) then
return ""
end
local next_section = string.find(content, "\n(=+)[^=]+%1", index)
local alternative_forms_section = string.sub(content, index, next_section)
local parameters_list
for alternative_form_template in string.gmatch(alternative_forms_section,
"{{alter|[^}]+}}") do
local parameters
if string.find(alternative_form_template, "||") then
parameters = string.match(alternative_form_template,
"{{alter|" .. langCodeRegex .. "|(.+)||")
else
parameters = string.match(alternative_form_template,
"{{alter|" .. langCodeRegex .. "|(.+)}}")
end
if parameters then
parameters_list = mw.text.split(parameters, "|")
end
end
if not parameters_list or #parameters_list == 0 then
return ""
end
local terms_list = {}
local items = {
t = {},
id = {},
alt = {},
tr = {},
ts = {},
g = {}
}
for _, parameter in ipairs(parameters_list) do
local parameterName, value = string.match(parameter, "^([^=]+)=(.+)$")
if parameterName and value then
local item_type, index = string.match(parameterName, "(%D+)(%d)")
if item_type and index and items[item_type] then
items[item_type][tonumber(index)] = value
elseif parameterName == "sc" then
items.sc = value
end
else
table.insert(terms_list, parameter)
end
end
for i, term in ipairs(terms_list) do
terms_list[i] = require("Module:links").full_link {
term = term,
lang = lang,
sc = items.sc,
alt = items.alt[i],
tr = items.tr[i],
ts = items.ts[i],
genders = items.g[i],
gloss = items.t[i]
}
end
return ", " .. table.concat(terms_list, ", ")
end
function export.getDescendants(lang, term, id)
local entry_name = require("Module:links").getLinkPage(term, lang)
local page = mw.title.new(entry_name)
local content = page:getContent()
if not content then
return ""
end
-- Ignore columns and and blank lines
content = string.gsub(content, "{{top%d}}%s", "")
content = string.gsub(content, "{{mid%d}}%s", "")
content = string.gsub(content, "{{bottom}}%s", "")
content = string.gsub(content, "\n?{{(desc%-%l+)|?[^}]*}}",
function (template_name)
if template_name == "desc-top" or template_name == "desc-bottom" then
return ""
end
end)
content = string.gsub(content, "\n%s*\n", "\n")
local _, index = string.find(content,
"\n==" .. lang:getCanonicalName() .. "==", nil, true)
if not index then
_, index = string.find(content, "^=="
.. require("Module:utilities").pattern_escape(lang:getCanonicalName())
.. "==", nil, false)
end
if not index then
language_header_error(entry_name, lang:getCanonicalName())
return ""
end
local _, next_lang = string.find(content, "\n==[^=\n]+==", index, false)
local _, index = string.find(content, "\n(=====?)Descendants%1", index, false)
if not index then
error("No Descendants section was found in the entry [[|" .. entry_name .. "]].")
elseif next_lang and next_lang < index then
error("No Descendants section under the header for "
.. lang:getCanonicalName() .. " was found in the entry [[|" .. entry_name .. "]].")
end
if id then
content = string.gsub(content,
"^.*{{senseid|"
.. require("Module:string").pattern_escape(lang:getCode()) .. "|"
.. id .. "}}(.*)", "%1")
end
local asterisks, item
local count = 0
local items = {}
local frame = mw.getCurrentFrame()
while string.sub(content, index + 1, index + 2) == "\n*" do
_, index, item = string.find(content, "\n(%*[^\n]+)", index)
-- Preprocess, but replace recursive calls to avoid template loop errors
item = string.gsub(item, "{{desctree|", "{{#invoke:descendants tree/templates|show|")
item = frame:preprocess(item)
-- Parse the list item
asterisks, item = string.match(item, "^(%*+:?) *(.-)$")
item = "<li>" .. item
local newcount = string.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