<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://mywikibiz.com/index.php?action=history&amp;feed=atom&amp;title=Module%3AExtract_short_description</id>
	<title>Module:Extract short description - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://mywikibiz.com/index.php?action=history&amp;feed=atom&amp;title=Module%3AExtract_short_description"/>
	<link rel="alternate" type="text/html" href="https://mywikibiz.com/index.php?title=Module:Extract_short_description&amp;action=history"/>
	<updated>2026-06-13T23:25:46Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.35.3</generator>
	<entry>
		<id>https://mywikibiz.com/index.php?title=Module:Extract_short_description&amp;diff=471630&amp;oldid=prev</id>
		<title>Zoran: Pywikibot 6.4.0</title>
		<link rel="alternate" type="text/html" href="https://mywikibiz.com/index.php?title=Module:Extract_short_description&amp;diff=471630&amp;oldid=prev"/>
		<updated>2021-07-15T21:35:11Z</updated>

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