Module:descendants tree: Difference between revisions

No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
local export = {}
local export = {}
local alternative_forms_module = "Module:alternative forms"
local string_utilities_module = "Module:string utilities"
local remove_comments = require(string_utilities_module).remove_comments


local function preview_error(what, entry_name, language_name, reason)
local function preview_error(what, entry_name, language_name, reason)
Line 7: Line 13:


local function get_content_after_senseid(content, entry_name, lang, id)
local function get_content_after_senseid(content, entry_name, lang, id)
local m_templateparser = require("Module:templateparser")
local code = lang:getFullCode()
local code = lang:getCode()
local t_start, t_end
local t_start = nil
for template in require("Module:template parser").find_templates(content) do
local t_end = nil
local name = template:get_name()
for name, args, _, index in m_templateparser.findTemplates(content) do
if name == "senseid" then
if name == "senseid" and args[1] == code and args[2] == id then
local args = template:get_arguments()
t_start = index
if args[1] == code and args[2] == id then
t_start = template.index
end
elseif name == "etymid" then
elseif name == "etymid" then
local args = template:get_arguments()
if args[1] == code and args[2] == id then
if args[1] == code and args[2] == id then
t_start = index
t_start = template.index
elseif t_start ~= nil and t_end == nil then
elseif t_start ~= nil and t_end == nil then
t_end = index
t_end = template.index
end
end
elseif name == "head" and args[1] == code then
elseif name == "head" then
if args["id"] == id then
local args = template:get_arguments()
t_start = index
local id_arg = args.id
elseif args["id"] ~= nil and t_start ~= nil and t_end == nil then
if id_arg == id then
t_end = index
t_start = template.index
elseif id_arg ~= nil and t_start ~= nil and t_end == nil then
t_end = template.index
end
end
end
end
Line 46: Line 57:
end
end


function export.getAlternativeForms(lang, sc, term, id)
function export.get_alternative_forms(lang, entry_name, id, default_separator)
local entry_name = require("Module:links").getLinkPage(term, lang, sc)
local page = mw.title.new(entry_name)
local page = mw.title.new(entry_name)
local content = page:getContent()
local content = page:getContent()


local function alt_form_error(reason)
local function alt_form_error(reason)
preview_error("alternative forms", entry_name, lang:getCanonicalName(), reason)
preview_error("alternative forms", entry_name, lang:getFullName(), reason)
end
end
Line 62: Line 72:
local _, index = string.find(content,
local _, index = string.find(content,
"==[ \t]*" .. require("Module:string").pattern_escape(lang:getCanonicalName()) .. "[ \t]*==")
"==[ \t]*" .. require(string_utilities_module).pattern_escape(lang:getFullName()) .. "[ \t]*==")
if not index then
if not index then
Line 77: Line 87:
local _, next_lang = string.find(content, "\n==[^=\n]+==", index, false)
local _, next_lang = string.find(content, "\n==[^=\n]+==", index, false)
local _, index = string.find(content, "\n(====?=?)[ \t]*Alternative forms[ \t]*%1", index, false)
local _, index = string.find(content, "\n(====?=?)[ \t]*Alternative forms[ \t]*%1", index, false)
if not index then
_, index = string.find(content, "\n(====?=?)[ \t]*Alternative reconstructions[ \t]*%1", index, false)
end


if not index then
if not index then
Line 84: Line 97:
end
end


local langCodeRegex = require("Module:string").pattern_escape(lang:getCode())
local langCodeRegex = require(string_utilities_module).pattern_escape(lang:getFullCode())
index = string.find(content, "{{alte?r?|" .. langCodeRegex .. "|[^|}]+", index)
index = string.find(content, "{{alte?r?|" .. langCodeRegex .. "|[^|}]+", index)
if (not index) or (next_lang and next_lang < index) then
if (not index) or (next_lang and next_lang < index) then
Line 100: Line 113:
local altforms = require("Module:alternative forms")
local altforms = require("Module:alternative forms")


for name, args, _, index in require("Module:templateparser").findTemplates(alternative_forms_section) do
for template in require("Module:template parser").find_templates(alternative_forms_section) do
if (name == "alt" or name == "alter") and args[1] == lang:getCode() then
if template:get_name() == "alter" then
saw_alter = true
local args = template:get_arguments()
local formatted_altforms = altforms.display_alternative_forms(args, entry_name, false, "allow self link")
if args[1] == lang:getFullCode() then
table.insert(terms_list, formatted_altforms)
saw_alter = true
local formatted_altforms = altforms.display_alternative_forms(args, entry_name, "allow self link", default_separator)
table.insert(terms_list, formatted_altforms)
end
end
end
end
end
Line 114: Line 130:
end
end


-- FIXME: Why do we return a leading comma like this? Why not have the caller add the comma?
return table.concat(terms_list, default_separator or ", ")
return ", " .. table.concat(terms_list, "; ")
end
end


function export.getDescendants(lang, sc, term, id, noerror)
function export.get_descendants(lang, entry_name, id, noerror)
local entry_name = require("Module:links").getLinkPage(term, lang, sc)
local page = mw.title.new(entry_name)
local page = mw.title.new(entry_name)
local content = page:getContent()
local content = page:getContent()
Line 125: Line 139:


local function desc_error(reason)
local function desc_error(reason)
preview_error("descendants", entry_name, lang:getCanonicalName(), reason)
preview_error("descendants", entry_name, lang:getFullName(), reason)
end
end


Line 135: Line 149:
-- Ignore HTML comments, columns and blank lines.
-- Ignore HTML comments, columns and blank lines.
content = content
content = remove_comments(content)
:gsub("<!%-%-.-%-%->", "")
:gsub("{{top%d}}%s", "")
:gsub("{{top%d}}%s", "")
:gsub("{{mid%d}}%s", "")
:gsub("{{mid%d}}%s", "")
Line 149: Line 162:
local _, index = string.find(content,
local _, index = string.find(content,
"%f[^\n%z]==[ \t]*" .. lang:getCanonicalName() .. "[ \t]*==", nil, true)
"%f[^\n%z]==[ \t]*" .. lang:getFullName() .. "[ \t]*==", nil, true)
if not index then
if not index then
_, index = string.find(content, "%f[^\n%z]==[ \t]*"
_, index = string.find(content, "%f[^\n%z]==[ \t]*"
.. require("Module:utilities").pattern_escape(lang:getCanonicalName())
.. require(string_utilities_module).pattern_escape(lang:getFullName())
.. "[ \t]*==", nil, false)
.. "[ \t]*==", nil, false)
end
end
if not index then
if not index then
desc_error("L2 header for language not found")
desc_error("L2 header for language not found")
-- FIXME, should be an error
return ""
return ""
end
end
Line 167: Line 181:
local _, next_lang = string.find(content, "\n==[^=\n]+==", index, false)
local _, next_lang = string.find(content, "\n==[^=\n]+==", index, false)
local _, index = string.find(content, "\n(====*)[ \t]*Descendants[ \t]*%1", index, false)
local _, index = string.find(content, "\n(====*)[ \t]*Descendants[ \t]*%1", index, false)
if not index then
local function desctree_no_descendants(with_lang_in_error)
if noerror and (namespace == "" or namespace == "Reconstruction") then
if noerror and (namespace == "" or namespace == "Reconstruction") then
return "<small class=\"error previewonly\">(" ..
return "<small class=\"error previewonly\">(" ..
"Please either change this template to {{desc}} " ..
"Please either change this template to {{desc}} " ..
"or insert a ====Descendants==== section in [[" ..
"or insert a ====Descendants==== section in [[" ..
entry_name .. "#" .. lang:getCanonicalName() .. "]])</small>" ..
entry_name .. "#" .. lang:getFullName() .. "]])</small>" ..
"[[Category:" .. lang:getCanonicalName() .. " descendants to be fixed in desctree]]"
"[[Category:" .. lang:getFullName() .. " descendants to be fixed in desctree]]"
else
else
error("No Descendants section was found in the entry [[" .. entry_name .. "]].")
error(("No Descendants section was found in the entry [[%s]]%s"):format(entry_name,
with_lang_in_error and (" under the header for %s"):format(lang:getFullName()) or ""))
end
end
end
if not index then
return desctree_no_descendants()
elseif next_lang and next_lang < index then
elseif next_lang and next_lang < index then
if noerror and (namespace == "" or namespace == "Reconstruction") then
return desctree_no_descendants("with lang in error")
return "<small class=\"error previewonly\">(" ..
"Please either change this template to {{desc}} " ..
"or insert a ====Descendants==== section in [[" ..
entry_name .. "#" .. lang:getCanonicalName() .. "]])</small>" ..
"[[Category:" .. lang:getCanonicalName() .. " descendants to be fixed in desctree]]"
else
error("No Descendants section was found in the entry [[" .. entry_name
.. "]] under the header for " .. lang:getCanonicalName() .. ".")
end
end
end