https://mywikibiz.com/index.php?title=Module:Extract_short_description&feed=atom&action=historyModule:Extract short description - Revision history2024-03-28T10:02:05ZRevision history for this page on the wikiMediaWiki 1.35.3https://mywikibiz.com/index.php?title=Module:Extract_short_description&diff=471630&oldid=prevZoran: Pywikibot 6.4.02021-07-15T21:35:11Z<p>Pywikibot 6.4.0</p>
<p><b>New page</b></p><div>require('Module:No globals');<br />
<br />
--[[--------------------------< E X T R A C T _ F R O M _ T E M P L A T E >------------------------------------<br />
<br />
no direct template access<br />
<br />
extracts short description text from a named template in the named article when that template is found in article<br />
wiki source<br />
<br />
requires three arguments:<br />
frame: frame object required to preprocess template_name<br />
article_title: the name of the article to inspect - correct spelling and captialization is required<br />
template_names_tbl: a single template name (a string) or a table of one or more template names all without<br />
namespace to be inspected - correct spelling and captialization is required<br />
<br />
returns two values:<br />
on success, returns the short description text and true<br />
on failure, returns error message and nil<br />
<br />
]]<br />
<br />
local function extract_from_template (frame, article_title, template_names_tbl)<br />
local content = mw.title.new (article_title):getContent(); -- read the unparsed article source<br />
if not content then<br />
return '<span style="font-size:100%;" class="error">error: no article: ' .. article_title .. '</span>';<br />
end<br />
<br />
local template_name_pattern;<br />
local start;<br />
<br />
if 'string' == type (template_names_tbl) then -- when single template name passed in as a string<br />
template_names_tbl = {template_names_tbl}; -- convert to a table<br />
end<br />
<br />
local templateName<br />
for _, template_name in ipairs (template_names_tbl) do -- loop through the name in the table<br />
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]<br />
start = content:find ('{{%s*' .. template_name_pattern); -- find the start of {{template name ...;<br />
if start then<br />
templateName = template_name<br />
break; -- found a matching template<br />
end<br />
end<br />
<br />
if not start then -- no templates found: return name of first template in template_names_tbl in error message<br />
return '<span style="font-size:100%;" class="error">error: no template: ' .. template_names_tbl[1] .. ' in: ' .. article_title .. '</span>';<br />
end<br />
<br />
local text = string.match (content, '%b{}', start); -- start points to first { of the templateName<br />
if not text then<br />
return '<span style="font-size:100%;" class="error">error: failed to extract template: ' .. templateName .. '</span>';<br />
end<br />
<br />
text = text:gsub ('<ref[^>]->[^<]-</ref>', ''); -- delete references before preprocessing; they do not belong in shortdesc text<br />
text = text:gsub ('<ref[^>]-/ *>', ''); -- also delete self-closed named references<br />
text = text:gsub ('{{%s*sfn[^}]-}}', ''); -- delete sfn template which make references using {{#tag:}} parser functions<br />
text = text:gsub ('{{#tag:ref[^}]-}}', ''); -- and delete these too<br />
<br />
text = frame:preprocess (text):match ('<div[^>]-class="shortdescription.->(.-)</div>'); -- preprocess and extract shortdescription text<br />
if not text then<br />
return '<span style="font-size:100%;" class="error">error: no short description text in: ' .. templateName .. ' in '.. article_title .. '</span>';<br />
end<br />
<br />
return mw.text.trim (text), true; -- trim whitespace and done<br />
-- preprocess the template then apply syntax highlighting<br />
-- this will display the preprocessed template; not usable here<br />
-- for much other than debugging because syntaxhighlight returns a stripmarker<br />
-- return template_name .. frame:callParserFunction ('#tag:syntaxhighlight', frame:preprocess (text));<br />
end<br />
<br />
<br />
--[[--------------------------< E X T R A C T _ F R O M _ A R T I C L E >--------------------------------------<br />
<br />
no direct template access<br />
<br />
extracts short description text from {{short description}} template when that template is found in article wiki<br />
source; searches for both the long name (short description) and the short-name redirect (SHD); if both are present<br />
long name controls; if multiples of the same name are present, the first-found controls.<br />
<br />
requires one argument: article_title is the name of the article to be inspected<br />
<br />
on success, returns the short description text; error message else<br />
<br />
]]<br />
<br />
local function extract_from_article (article_title)<br />
local content = mw.title.new (article_title):getContent(); -- read the unparsed article source<br />
if not content then<br />
return '<span style="font-size:100%;" class="error">error: no article: ' .. article_title .. '</span>';<br />
end<br />
<br />
local text, start;<br />
<br />
start = string.find (content, '{{%s*[Ss]hort description') or -- find the start of {{Short description}} template<br />
string.find (content, '{{%s*SHD'); -- not full name, try the {{SHD}} redirect<br />
<br />
if not start then<br />
return '<span style="font-size:100%;" class="error">error: no short description in: ' .. article_title .. '</span>';<br />
end<br />
<br />
text = content:match ('%b{}', start); -- get the short description template; start points to first { of the template<br />
if not text then<br />
return '<span style="font-size:100%;" class="error">error: failed to extract short description template from ' .. article_title .. '</span>';<br />
end<br />
<br />
text = text:match ('^[^|}]+|%s*(.+)%s*}}$'); -- strip '{{template name|' and '}}'; trim leading and trailing whitespace<br />
<br />
return text and text or '<span style="font-size:100%;" class="error">error: no short description text in: ' .. article_title .. '</span>';<br />
end<br />
<br />
<br />
--[[--------------------------< E X T R A C T _ S H O R T _ D E S C R I P T I O N >----------------------------<br />
<br />
template entry point:<br />
{{#invoke:extract short description|extract_short_description}}<br />
<br />
search for and return text that is used by the {{short description}} template. {{Short description}}, also {{SHD}}<br />
may be located in article wikisource or embedded in a template (commonly an infobox template). When neither of<br />
|template= and {{{2|}}} are set, this code will look in the article wiki source; when set, this code look inside the<br />
named template.<br />
<br />
This template entry takes two parameters:<br />
{{{1}}} or |article=: required; name of wiki article from which to extract the short description<br />
{{{2}}} or |template=; optional; name of template that holds the {{short description}} template<br />
<br />
on success, returns the short description text; error message else<br />
<br />
]]<br />
<br />
local function extract_short_description (frame)<br />
local getArgs = require('Module:Arguments').getArgs;<br />
local args = getArgs(frame);<br />
<br />
if args[1] and args.article then -- both assigned, fail with an error message<br />
return '<span style="font-size:100%;" class="error">error: conflicting |{{{1}}} and |article= parameters</span>';<br />
end<br />
<br />
local article_title = args[1] or args.article; -- the required parameter<br />
<br />
if not article_title then -- not supplied, fail with an error message<br />
return '<span style="font-size:100%;" class="error">error: article title required</span>';<br />
end<br />
<br />
local template_name = args[2] or args.template; -- optional<br />
<br />
if template_name then<br />
local text, _ = extract_from_template (frame, article_title, template_name); -- ignore second return value<br />
return text;<br />
else<br />
return extract_from_article (article_title);<br />
end<br />
end<br />
<br />
<br />
--[[--------------------------< E X P O R T E D F U N C T I O N S >------------------------------------------<br />
]]<br />
<br />
return {<br />
extract_short_description = extract_short_description,<br />
extract_from_template = extract_from_template,<br />
extract_from_article = extract_from_article,<br />
}</div>Zoran