Module:Excerpt/sandbox: Difference between revisions

Content deleted Content added
Undid revision 955466401 by Sophivorus (talk)
Code refactoring. Group code specific to the English Wikipedia under an enwiki function. Create a "lead" function as the generic entry point for templates. Also create a "parseArgs" function to abstract some common code.
Line 314:
end
 
-- ConvertHelper function to convert a comma-separated list of numbers or min-max ranges into a list of booleans,
-- For e.g.example: "1,3-5" to {1=true,2=false,3=true,4=true,5=true}
local function numberFlags(str)
local ranges = mw.text.split(str, ",") -- parse ranges, e.g. "1,3-5" → {"1","3-5"}
Line 326 ⟶ 327:
end
return flags
end
 
-- Helper function that parses the arguments of a template and returns an array of arguments fit for main()
local function parseArgs(frame)
local args = {}
for key, value in pairs(frame:getParent().args) do args[key] = value end
for key, value in pairs(frame.args) do args[key] = value end -- args from a Lua call have priority over parent args from template
args.paraflags = numberFlags(args["paragraphs"] or "") -- parse paragraphs: "1,3-5" to {"1","3-5"}
args.fileflags = numberFlags(args["files"] or "") -- parse file numbers
return args
end
 
Line 679 ⟶ 690:
end
 
-- Shared template invocation codefunction for lead andEnglish randomWikipedia functionstemplates
-- May not work properly on other wikis
local function invoke(frame, template)
local function enwiki(frame, template)
-- args = { 1,2,... = page names, paragraphs = list e.g. "1,3-5", files = list, more = text}
local args = parseArgs(frame)
local args = {} -- args[k] = frame.args[k] or frame:getParent().args[k] for all k in either (numeric or not)
for k, v in pairs(frame:getParent().args) do args[k] = v end
for k, v in pairs(frame.args) do args[k] = v end -- args from a Lua call have priority over parent args from template
errors = args["errors"] -- set the module level boolean used in local function err
 
Line 693 ⟶ 702:
 
local pageNames = {}
 
if template == "lead" then
if template == "excerpt" then
pageNames = { args[1] }
local tag = is(args.tag) and args.tag or 'div'
local article = is(args.article) and args.article or args[1] or '{{{1}}}'
local section = is(args.section) and args.section or args[2]
local output = {}
output[1] = frame:extensionTag{ name = 'templatestyles', args = {src='Excerpt/styles.css'} }
output[2] = '<' .. tag .. ' class="excerpt-block">'
output[3] = is(args.indicator) and ('<' .. tag .. ' class="excerpt-indicator">') or ''
if is(args.nohat) then
output[4] = ''
else
local hatnote = {}
hatnote[1] = is(args.indicator) and d.excerpt.hatnote or d.excerpt.hatnoteSection
hatnote[2] = '[['
hatnote[3] = article .. (is(section) and ('#' .. frame:callParserFunction( 'urlencode', section, 'WIKI' )) or '')
hatnote[4] = '|'
hatnote[5] = article .. (is(section) and (frame:callParserFunction( '#tag:nowiki', ' § ' ) .. section) or '')
hatnote[6] = ']]'
hatnote[7] = "''" .. '<span class="mw-editsection-like plainlinks"><span>[ </span>['
local title = mw.title.new(article) or mw.title.getCurrentTitle()
hatnote[8] = title:fullUrl('action=edit') .. ' ' .. d.excerpt.edit
hatnote[9] = ']<span> ]</span></span>' .. "''"
output[4] = require('Module:Hatnote')._hatnote(table.concat(hatnote), {selfref=true}) or err("hatnote")
end
output[5] = '<' .. tag .. ' class="excerpt">\n'
if article ~= '{{{1}}}' then
local options = args -- turn template arguments into module options
options.paraflags = args.paragraphs
options.fileflags = args.files or 1
options.nobold = 1
options.fragment = args.fragment
options.keepTables = args.tables or 1
options.keepRefs = args.references or 1
options.keepSubsections = args.subsections
local pageNames = { (article .. '#' .. (section or '')) }
local text = main(pageNames, options)
if text == "" and d.brokenCategory and d.brokenCategory ~= "" and mw.title.getCurrentTitle().isContentPage then
output[6] = "[[Category:" .. d.brokenCategory .. "]]"
else
output[6] = frame:preprocess(text) or err("preprocess")
end
else
output[6] = err("noArticle")
end
output[7] = '</' .. tag .. '>'
output[8] = is(args.indicator) and ('</' .. tag .. '>') or ''
output[9] = '</' .. tag .. '>'
output[10] = mw.title.getCurrentTitle().isContentPage and ('[[' .. d.excerpt.category .. ']]') or ''
return table.concat(output)
 
elseif template == "linked" or template == "listitem" then
-- Read named page and find its wikilinks
Line 716 ⟶ 780:
for p in mw.ustring.gmatch(text, "\n:*[%*#][^\n]-%[%[%s*([^%]|\n]*)") do table.insert(pageNames, p) end
end
 
elseif template == "random" then
-- accept any number of page names. If more than one, we'll pick one randomly
Line 721 ⟶ 786:
if p and type(i) == 'number' then table.insert(pageNames, p) end
end
 
elseif template == "selected" then
local articleKey = args[template]
Line 730 ⟶ 796:
end
 
if is(args.more) then args.more = "Read more..." end -- more= is short for this default text
local options = args -- pick up miscellaneous options: more, errors, fileargs
options.paraflags = numberFlags(args["paragraphs"] or "") -- parse paragraphs, e.g. "1,3-5" → {"1","3-5"}
options.fileflags = numberFlags(args["files"] or "") -- parse file numbers
if options.more and options.more == "" then options.more = "Read more..." end -- more= is short for this default text
 
local text = ""
if optionsargs.showall then
local separator = ""
for _, p in pairs(pageNames) do
local t = main({ p }, optionsargs)
if t ~= "" then
text = text .. separator .. t
separator = optionsargs.showall
if separator == "" then separator = "{{clear}}{{hr}}" end
end
end
else
text = main(pageNames, optionsargs)
end
 
Line 757 ⟶ 820:
end
 
-- Main invocation function for templates
-- Replicate {{Excerpt}} entirely in Lua for reduced Post-expand include size
local function excerptlead(frame)
local args = parseArgs(frame)
local args = {} -- args[k] = frame.args[k] or frame:getParent().args[k] for all k in either (numeric or not)
local pageNames = { args[1] }
for k, v in pairs(frame:getParent().args) do args[k] = v end
local text = main(pageNames, args)
for k, v in pairs(frame.args) do args[k] = v end -- args from a Lua call have priority over parent args from template
return frame:preprocess(text)
end
 
-- Main entry point for templates
local tag = is(args.tag) and args.tag or 'div'
function p.lead(frame) return lead(frame) end
local article = is(args.article) and args.article or args[1] or '{{{1}}}'
local section = is(args.section) and args.section or args[2]
 
local output = {}
output[1] = frame:extensionTag{ name = 'templatestyles', args = {src='Excerpt/styles.css'} }
output[2] = '<' .. tag .. ' class="excerpt-block">'
output[3] = is(args.indicator) and ('<' .. tag .. ' class="excerpt-indicator">') or ''
if is(args.nohat) then
output[4] = ''
else
local hatnote = {}
hatnote[1] = is(args.indicator) and d.excerpt.hatnote or d.excerpt.hatnoteSection
hatnote[2] = '[['
hatnote[3] = article .. (is(section) and ('#' .. frame:callParserFunction( 'urlencode', section, 'WIKI' )) or '')
hatnote[4] = '|'
hatnote[5] = article .. (is(section) and (frame:callParserFunction( '#tag:nowiki', ' § ' ) .. section) or '')
hatnote[6] = ']]'
hatnote[7] = "''" .. '<span class="mw-editsection-like plainlinks"><span>[ </span>['
local title = mw.title.new(article) or mw.title.getCurrentTitle()
hatnote[8] = title:fullUrl('action=edit') .. ' ' .. d.excerpt.edit
hatnote[9] = ']<span> ]</span></span>' .. "''"
output[4] = require('Module:Hatnote')._hatnote(table.concat(hatnote), {selfref=true}) or err("hatnote")
end
output[5] = '<' .. tag .. ' class="excerpt">\n'
if article ~= '{{{1}}}' then
local options = args -- turn template arguments into module options
options.paraflags = args.paragraphs
options.fileflags = args.files or 1
options.nobold = 1
options.fragment = args.fragment
options.keepTables = args.tables or 1
options.keepRefs = args.references or 1
options.keepSubsections = args.subsections
 
local pageNames = { (article .. '#' .. (section or '')) }
local text = main(pageNames, options)
 
if text == "" and d.brokenCategory and d.brokenCategory ~= "" and mw.title.getCurrentTitle().isContentPage then
output[6] = "[[Category:" .. d.brokenCategory .. "]]"
else
output[6] = frame:preprocess(text) or err("preprocess")
end
else
output[6] = err("noArticle")
end
output[7] = '</' .. tag .. '>'
output[8] = is(args.indicator) and ('</' .. tag .. '>') or ''
output[9] = '</' .. tag .. '>'
output[10] = mw.title.getCurrentTitle().isContentPage and ('[[' .. d.excerpt.category .. ']]') or ''
return table.concat(output)
end
 
-- Entry points for templateEnglish callersWikipedia using #invoke:templates
function p.leadlinked(frame) return invokeenwiki(frame, "leadlinked") end -- {{Transclude leadlinked excerpt}} reads thea firstrandomly and onlyselected article linked from the given page
function p.linkedlistitem(frame) return invokeenwiki(frame, "linkedlistitem") end -- {{Transclude linkedlist item excerpt}} reads a randomly selected article linkedlisted fromon the given page
function p.listitemrandom(frame) return invokeenwiki(frame, "listitemrandom") end -- {{Transclude list itemrandom excerpt}} reads a randomly selectedany article listed(default onfor theinvoke givenwith pageone argument)
function p.randomselected(frame) return invokeenwiki(frame, "randomselected") end -- {{Transclude randomselected excerpt}} reads anythe article (defaultwhose forkey invokeis within onethe argument)selected= parameter
function p.selectedexcerpt(frame) return invokeenwiki(frame, "selectedexcerpt") end -- {{Transclude selected excerptExcerpt}} readstranscludes thepart articleof whosean keyarticle isinto inanother the selected= parameterarticle
function p.excerpt(frame) return excerpt(frame) end -- {{Excerpt}} transcludes part of an article into another article
 
-- Entry points for other Lua modules