Content deleted Content added
m Trappist the monk moved page Module:Sandbox/trappist the monk/tag2 to Module:Extract short description without leaving a redirect: get it out of the sandbox |
use require('strict') instead of require('Module:No globals') |
||
(11 intermediate revisions by 2 users not shown) | |||
Line 1:
require('
--[[--------------------------< E X T R A C T _ F R O M _
no direct template access
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 all without
returns two values:
on success, returns the short description text
on failure, returns error message and nil
]]
local function extract_from_template (frame, article_title,
local content = mw.title.new (article_title):getContent(); -- read the unparsed article source
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
end
local template_name_pattern;
local start;
if 'string' == type (template_names_tbl) then -- when single template name passed in as a string
local start = string.find (content, '{{%s*' .. template_name_pattern); -- find the start of {{template name ...;▼
template_names_tbl = {template_names_tbl}; -- convert to a table
▲ if not start then
end
▲ return '<span style="font-size:100%;" class="error">error: no template: ' .. template_name .. ' in: ' .. article_title .. '</span>';
local templateName
for _, template_name in ipairs (template_names_tbl) do -- loop through the name in the table
▲
▲
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
return '<span style="font-size:100%;" class="error">error: no template: ' .. template_names_tbl[1] .. ' in: ' .. article_title .. '</span>';
end
local text = string.match (content, '%b{}', start); -- start points to first { of the
if not text then
return '<span style="font-size:100%;" class="error">error: failed to extract template: ' ..
end
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: ' ..
end
return
-- preprocess the template then apply syntax highlighting
-- this will display the preprocessed template; not usable here
-- for much other than debugging because syntaxhighlight returns a stripmarker
-- return template_name .. frame:callParserFunction ('#tag:syntaxhighlight', frame:preprocess (
end
Line 61 ⟶ 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 116 ⟶ 145:
if template_name then
return text;
else
return extract_from_article (article_title);
Line 129 ⟶ 159:
extract_short_description = extract_short_description,
extract_from_template = extract_from_template,
extract_from_article = extract_from_article,
}
|