Module:Extract short description: Difference between revisions

Content deleted Content added
No edit summary
use require('strict') instead of require('Module:No globals')
 
(4 intermediate revisions by 2 users not shown)
Line 1:
require('Module:No globalsstrict');
 
--[[--------------------------< E X T R A C T _ F R O M _ T E M P L A T E >------------------------------------
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
template_names_tbl: a single template name (a string) or a table of one or more template names withoutall without
namespace to be inspected - correct spelling and captialization is required
 
returns two values:
Line 32 ⟶ 33:
end
local templateName
for _, template_name in ipairs (template_names_tbl) do -- loop through the name in the table
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]
start = content:find ('{{%s*' .. template_name_pattern); -- find the start of {{template name ...;
if start then
templateName = template_name
break; -- found a matching template
end
Line 44 ⟶ 47:
end
 
local text = string.match (content, '%b{}', start); -- start points to first { of the template_nametemplateName
if not text then
return '<span style="font-size:100%;" class="error">error: failed to extract template: ' .. template_nametemplateName .. '</span>';
end
 
text = text:gsub ('<ref[^>]->[^<]-</ref>', ''); -- delete references before preprocessing; they do not belong in shortdesc 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
 
Line 140 ⟶ 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);
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 168 ⟶ 160:
extract_from_template = extract_from_template,
extract_from_article = extract_from_article,
test = test, -- to be deleted
}