Module:descendants tree: Difference between revisions
Created page with "local export = {} local function preview_error(what, entry_name, language_name, reason) mw.log("Could not retrieve " .. what .. " for " .. language_name .. " in the entry [[" .. entry_name .. "]]: " .. reason .. ".") end local function get_content_after_senseid(content, entry_name, lang, id) local m_templateparser = require("Module:templateparser") local code = lang:getNonEtymologicalCode() local t_start = nil local t_end = nil for name, args, _, index in m_te..." |
No edit summary |
||
| (2 intermediate revisions 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 code = lang:getFullCode() | |||
local code = lang: | local t_start, t_end | ||
local t_start | for template in require("Module:template parser").find_templates(content) do | ||
local name = template:get_name() | |||
for | if name == "senseid" then | ||
if name == "senseid" | local args = template:get_arguments() | ||
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" | elseif name == "head" then | ||
if | local args = template:get_arguments() | ||
t_start = index | local id_arg = args.id | ||
elseif | 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. | function export.get_alternative_forms(lang, entry_name, id, default_separator) | ||
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: | 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( | "==[ \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( | 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 | for template in require("Module:template parser").find_templates(alternative_forms_section) do | ||
if ( | if template:get_name() == "alter" then | ||
local args = template:get_arguments() | |||
if args[1] == lang:getFullCode() then | |||
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 | ||
return table.concat(terms_list, default_separator or ", ") | |||
end | end | ||
function export. | function export.get_descendants(lang, entry_name, id, noerror) | ||
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: | 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("{{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: | "%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( | .. 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) | ||
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: | entry_name .. "#" .. lang:getFullName() .. "]])</small>" .. | ||
"[[Category:" .. lang: | "[[Category:" .. lang:getFullName() .. " descendants to be fixed in desctree]]" | ||
else | else | ||
error("No Descendants section was found in the entry [[" | 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 | ||
return desctree_no_descendants("with lang in error") | |||
end | end | ||