Module:Extract short description: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
Line 1:
require('Module:No globals');
 
--[[--------------------------< E X T R A C T _ F R O M _ A R T I C L E >--------------------------------------
local function tag (frame)
 
local content = mw.title.new (frame.args[1]):getContent()
no direct template access
local ibox_start = string.find (content, '{{%s*' .. 'Infobox television episode/sandbox') -- find the start of {{Infobox ...; ibox_name is a pattern that accepts various legit spacing and capitalization ...
 
if not ibox_start then
extracts short description text from a named template in the named article when that template is found in article
return 'Infobox television episode/sandbox not found'
wiki source
 
requires three arguments:
frame: frame object required for expandTemplate
article_title: the name of the article to inspect - correct spelling and captialization is required
template: the name of the template to inspect - name modified to make upper or lower first character agnostic
 
on success, returns the short description text; error message else
 
]]
 
local function extract_from_template (frame, article_title, template_name)
local content = mw.title.new (article_title):getContent();
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.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 ibox_texttemplate_text = string.match (content, '%b{}', ibox_startstart); -- ibox_startstart points to first { of the infobox templatetemplate_name
if not ibox_texttemplate_text then
return '<span style="font-size:100%;" class="error">error: failed to extract Infoboxtemplate: television' episode.. template_name .. '</sandboxspan>';
end
local template_content = template_text:gsub ('{{%s' .. template_name, ''):gsub ('}}$', ''); -- remove leading {{, template name, and trailing }}
local template_name = 'Infobox television episode/sandbox'
local template_content = ibox_text:gsub ('{{%s' .. template_name, ''):gsub ('}}$', '')
 
local text = frame:expandTemplate ({title=template_name, args = {template_content}}); -- expand the infobox template to mixture of html and wiki markup
 
text = text and text:match ('<div[^>]-class="shortdescription.->([^<]+)'); -- extract shortdescription text
 
if not text then
return text and mw.text.trim (text) or ''; -- trim whitespace and done
return '<span style="font-size:100%;" class="error">error: no short description text in: ' .. template_name .. ' in '.. article_title .. '</span>';
end
 
return text and mw.text.trim (text); -- trim whitespace and done
-- preprocess the template then apply syntax highlighting
-- this will display the expanded template; not usable here because
-- syntaxhighlight returns a stripmarker
-- return frame:callParserFunction ('#tag:syntaxhighlight', frame:expandTemplate ({title=template_name, args = {template_content}}));
end
 
 
return {tag = tag}
--[[--------------------------< E X T R A C T _ F R O M _ A R T I C L E >--------------------------------------
 
no direct template access
 
extracts short description text from {{short description}} template when that template is found in article wiki
source; searches for both the long name (short description) and the short-name redirect (SHD); if both are present
long name controls; if multiples of the same name are present, the first-found controls.
 
requires one argument: article_title is the name to the article to inspect
 
on success, returns the short description text; error message else
 
]]
 
local function extract_from_article (article_title)
local content = mw.title.new (article_title):getContent(); -- read the unparsed article source
local text, start;
start = string.find (content, '{{%s*[Ss]hort description') or -- find the start of {{Short description}} template
string.find (content, '{{%s*SHD'); -- not full name, try the {{SHD}} redirect
 
if not start then
return '<span style="font-size:100%;" class="error">error: no short description in: ' .. article_title .. '</span>';
end
 
text = content:match ('%b{}', start); -- get the short description template; start points to first { of the template
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>';
end
 
 
--[[--------------------------< E X T R A C T _ S H O R T _ D E S C R I P T I O N >----------------------------
 
template entry point:
{{#invoke:extract short description|extract_short_description}}
 
search for and return text that is used by the {{short description}} template. {{Short description}}, also {{SHD}}
may be located in article wikisource or embedded in a template (commonly an infobox template). When neither of
|template= and {{{2|}}} are set, this code will look in the article wiki source; when set, this code look inside the
named template.
 
This template entry take two parameter:
{{{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
 
on success, returns the short description text; error message else
 
]]
 
local function extract_short_description (frame)
local getArgs = require('Module:Arguments').getArgs;
local args = getArgs(frame);
local article_title = args[1] or args.article; -- the required parameter
if not article_title then -- not supplied, fail with an error message
return '<span style="font-size:100%;" class="error">error: article title required</span>';
end
local template_name = args[2] or args.template; -- optional
if template_name then
return extract_from_template (frame, article_title, template_name);
else
return extract_from_article (article_title);
end
end
 
 
--[[--------------------------< E X P O R T E D F U N C T I O N S >------------------------------------------
]]
 
return {
extract_short_description = extract_short_description,
extract_from_template = extract_from_template,
extract_from_article = extract_from_article
}