Module:descendants tree: difference between revisions
Jump to navigation
Jump to search
Content deleted Content added
No edit summary |
make named parameters of T:alter be recognized |
||
Line 32: | Line 32: | ||
for alternative_form_template in mw.ustring.gmatch(alternative_forms_section, "{{alter|[^}]+}}") do |
for alternative_form_template in mw.ustring.gmatch(alternative_forms_section, "{{alter|[^}]+}}") do |
||
local |
local term_parameters |
||
if mw.ustring.find(alternative_form_template, "||") then |
if mw.ustring.find(alternative_form_template, "||") then |
||
term_parameters = mw.ustring.match(alternative_form_template, "{{alter|" .. lang:getCode() .. "|(.+)||") |
|||
else |
else |
||
term_parameters = mw.ustring.match(alternative_form_template, "{{alter|" .. lang:getCode() .. "|(.+)}}") |
|||
end |
end |
||
parameters_list = mw.text.split(term_parameters, "|") |
|||
end |
end |
||
if # |
if #parameters_list == 0 then |
||
return "" |
return "" |
||
end |
|||
local terms_list = {} |
|||
local items = { |
|||
id = {}, |
|||
alt = {}, |
|||
tr = {}, |
|||
g = {} |
|||
} |
|||
for i, parameter in ipairs(parameters_list) do |
|||
local parameterName, value = mw.ustring.match(parameter, "^([^=]+)=(.+)$") |
|||
if parameterName and value then |
|||
local item_type, index = mw.ustring.match(parameterName, "([^%d]+)(%d)") |
|||
if item_type and index and items[item_type] then |
|||
index = tonumber(index) |
|||
items[item_type][index] = value |
|||
elseif parameterName == "sc" then |
|||
items.sc = value |
|||
end |
|||
else |
|||
table.insert(terms_list, parameter) |
|||
end |
|||
end |
end |
||
for i, term in ipairs(terms_list) do |
for i, term in ipairs(terms_list) do |
||
terms_list[i] = require("Module:links").full_link{term = term, |
terms_list[i] = require("Module:links").full_link{ |
||
term = term, |
|||
lang = lang, |
|||
sc = items.sc, |
|||
alt = items.alt[i], |
|||
tr = items.tr[i], |
|||
g = items.g[i] |
|||
} |
|||
end |
end |
||
Revision as of 23:15, 6 June 2017
- The following documentation is located at Module:descendants tree/documentation. [edit]
- Useful links: subpage list • links • transclusions • testcases • sandbox
This module is used by Module:descendants tree/templates to generate the content of {{desctree}}
. It gets the content of an entry and searches for a list of descendants. If there is such a list, it displays the contents of the list, or else it displays an error.
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:string").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==[^=\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 term_parameters
if mw.ustring.find(alternative_form_template, "||") then
term_parameters = mw.ustring.match(alternative_form_template, "{{alter|" .. lang:getCode() .. "|(.+)||")
else
term_parameters = mw.ustring.match(alternative_form_template, "{{alter|" .. lang:getCode() .. "|(.+)}}")
end
parameters_list = mw.text.split(term_parameters, "|")
end
if #parameters_list == 0 then
return ""
end
local terms_list = {}
local items = {
id = {},
alt = {},
tr = {},
g = {}
}
for i, parameter in ipairs(parameters_list) do
local parameterName, value = mw.ustring.match(parameter, "^([^=]+)=(.+)$")
if parameterName and value then
local item_type, index = mw.ustring.match(parameterName, "([^%d]+)(%d)")
if item_type and index and items[item_type] then
index = tonumber(index)
items[item_type][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],
g = items.g[i]
}
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
-- Ignore columns and and blank lines
content = mw.ustring.gsub(content, "{{top%d}}%s", "")
content = mw.ustring.gsub(content, "{{mid%d}}%s", "")
content = mw.ustring.gsub(content, "{{bottom}}%s", "")
content = mw.ustring.gsub(content, "^%n%s", "")
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==[^=\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(%*[^\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