Module:Extract short description: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
Line 11:
frame: frame object required to preprocess template_name
article_title: the name of the article to inspect - correct spelling and captialization is required
templatetemplate_names_tbl: thetable nameof template names without namespace ofto the template tobe inspectinspected - correct spelling and captialization is required
 
returns two values:
on success, returns the short description text; errorand message elsetrue
on failure, returns error message and nil
 
]]
 
--local function extract_from_template (frame, article_title, template_nametemplate_names)
local function extract_from_template (frame, article_title, template_names_tbl)
local content = mw.title.new (article_title):getContent(); -- read the unparsed article source
if not content then
return '<span style="font-size:100%;" class="error">error: no article: ' .. article_title .. '</span>';
end
 
local template_name_pattern;
local start
localfor _, template_name in ipairs (template_names_tbl) do
template_name_pattern = template_name:gsub ('^%a', string.lower):gsub ('^%a', '%[%1%1%]'):gsub ('%[%a', string.upper); -- make lua pattern for initial letter upper or lower case: A -> [Aa]
local start = string.content:find (content, '{{%s*' .. template_name_pattern); -- find the start of {{template name ...;
if not start then
break; -- found a matching template
end
end
 
if not start then -- no templates found: return name of first template in template_names_tbl in error message
local start = string.find (content, '{{%s*' .. template_name_pattern); -- find the start of {{template name ...;
return '<span style="font-size:100%;" class="error">error: no template: ' .. template_nametemplate_names_tbl[1] .. ' in: ' .. article_title .. '</span>';
if not start then
return '<span style="font-size:100%;" class="error">error: no template: ' .. template_name .. ' in: ' .. article_title .. '</span>';
end
 
Line 43 ⟶ 54:
end
 
return text and mw.text.trim (text), true; -- trim whitespace and done
-- preprocess the template then apply syntax highlighting
-- this will display the preprocessed template; not usable here
Line 130 ⟶ 141:
return extract_from_article (article_title);
end
end
 
 
 
-----test-----
-- temporary test function to be deleted
local function test (frame)
local tbl = {'Infobox television episode/sandbox', 'short description'};
local text, good = extract_from_template (frame, 'User:Trappist the monk/sandbox', tbl);
if good then
return text;
else
return 'extract_from_template() returned this: ' .. text
end
end
 
Line 139 ⟶ 164:
extract_short_description = extract_short_description,
extract_from_template = extract_from_template,
extract_from_article = extract_from_article,
test = test, -- to be deleted
}