Module:Extract short description: Difference between revisions

Content deleted Content added
No edit summary
use require('strict') instead of require('Module:No globals')
 
(15 intermediate revisions by 2 users not shown)
Line 1:
require('Module:No globalsstrict');
 
--[[--------------------------< E X T R A C T _ F R O M _ A R T IE CM P L A T E >--------------------------------------
 
no direct template access
Line 9:
 
requires three arguments:
frame: frame object required forto expandTemplatepreprocess template_name
article_title: the name of the article to inspect - correct spelling and captialization is required
templatetemplate_names_tbl: thea name of thesingle template toname inspect(a -string) nameor modifieda totable makeof upperone or lowermore template firstnames characterall agnosticwithout
namespace to be inspected - 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_tbl)
local content = mw.title.new (article_title):getContent(); -- read the unparsed article source
if not startcontent then
local 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]
return '<span style="font-size:100%;" class="error">error: no template: ' .. template_name .. ' inarticle: ' .. article_title .. '</span>';
local start = string.find (content, '{{%s*' .. template_name_pattern); -- find the start of {{template name ...;
if not start then
return '<span style="font-size:100%;" class="error">error: no template: ' .. template_name .. ' in: ' .. article_title .. '</span>';
end
 
local template_name_pattern;
local template_text = string.match (content, '%b{}', start); -- start points to first { of the infobox template_name
local start;
if not template_text then
return '<span style="font-size:100%;" class="error">error: failed to extract template: ' .. template_name .. '</span>';
if 'string' == type (template_names_tbl) then -- when single template name passed in as a string
template_names_tbl = {template_names_tbl}; -- convert to a table
end
local templateName
local template_content = template_text:gsub ('{{%s' .. template_name, ''):gsub ('}}$', ''); -- remove leading {{, template name, and trailing }}
for _, template_name in ipairs (template_names_tbl) do -- loop through the name in the table
local 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 start then
templateName = template_name
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 text = frame:expandTemplate ({title=template_name, args = {template_content}}); -- expand the template to mixture of html and wiki markup
return '<span style="font-size:100%;" class="error">error: no template: ' .. template_names_tbl[1] .. ' in: ' .. article_title .. '</span>';
end
 
local template_texttext = string.match (content, '%b{}', start); -- start points to first { of the infobox template_nametemplateName
if not template_texttext then
return '<span style="font-size:100%;" class="error">error: failed to extract template: ' .. template_nametemplateName .. '</span>';
end
 
text = text and text:matchgsub ('<divref[^>]-class="shortdescription.->([^<]+)-</ref>', ''); -- extractdelete references before preprocessing; they do not belong in shortdescriptionshortdesc text
text = text:gsub ('<ref[^>]-/ *>', ''); -- also delete self-closed named references
text = text:gsub ('{{%s*sfn[^}]-}}', ''); -- delete sfn template which make references using {{#tag:}} parser functions
text = text:gsub ('{{#tag:ref[^}]-}}', ''); -- and delete these too
 
text = frame:preprocess (text):match ('<div[^>]-class="shortdescription.->(.-)</div>'); -- preprocess and extract shortdescription text
if not text then
return '<span style="font-size:100%;" class="error">error: no short description text in: ' .. template_nametemplateName .. ' in '.. article_title .. '</span>';
end
 
return text and mw.text.trim (text), true; -- trim whitespace and done
-- preprocess the template then apply syntax highlighting
-- this will display the expandedpreprocessed template; not usable here because
-- for much other than debugging because syntaxhighlight returns a stripmarker
-- return template_name .. frame:callParserFunction ('#tag:syntaxhighlight', frame:expandTemplatepreprocess ({title=template_name, args = {template_content}}text));
end
 
Line 57 ⟶ 78:
long name controls; if multiples of the same name are present, the first-found controls.
 
requires one argument: article_title is the name toof the article to inspectbe inspected
 
on success, returns the short description text; error message else
Line 65 ⟶ 86:
local function extract_from_article (article_title)
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 text, start;
Line 75 ⟶ 100:
 
text = content:match ('%b{}', start); -- get the short description template; start points to first { of the template
if not text then
text = text:match ('^[^|}]+|%s*(.+)%s*}}$'); -- strip '{{template name| and }}; trim leading and trailing whitespace
return '<span style="font-size:100%;" class="error">error: failed to extract short description template from ' .. article_title .. '</span>';
end
 
text = text:match ('^[^|}]+|%s*(.+)%s*}}$'); -- strip '{{template name|' and '}}'; trim leading and trailing whitespace
return text and text or '<span style="font-size:100%;" class="error">error: no short description text in: ' .. article_title .. '</span>';
Line 91 ⟶ 120:
named template.
 
This template entry taketakes two parameterparameters:
{{{1}}} or |article=: required; name of wiki article from which to extract the short description
{{{2}}} or |template=; optional; name of template that holds the {{short description}} template
Line 102 ⟶ 131:
local getArgs = require('Module:Arguments').getArgs;
local args = getArgs(frame);
if args[1] and args.article then -- both assigned, fail with an error message
return '<span style="font-size:100%;" class="error">error: conflicting |{{{1}}} and |article= parameters</span>';
end
local article_title = args[1] or args.article; -- the required parameter
Line 112 ⟶ 145:
if template_name then
returnlocal text, _ = extract_from_template (frame, article_title, template_name); -- ignore second return value
return text;
else
return extract_from_article (article_title);
Line 125 ⟶ 159:
extract_short_description = extract_short_description,
extract_from_template = extract_from_template,
extract_from_article = extract_from_article,
}