Content deleted Content added
Sophivorus (talk | contribs) Undid revision 955466401 by Sophivorus (talk) |
Sophivorus (talk | contribs) 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
--
-- For 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
-- May not work properly on other wikis
local function enwiki(frame, template)
local args = parseArgs(frame)
errors = args["errors"] -- set the module level boolean used in local function err
Line 693 ⟶ 702:
local pageNames = {}
if template == "excerpt" then
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 text = ""
if
local separator = ""
for _, p in pairs(pageNames) do
local t = main({ p },
if t ~= "" then
text = text .. separator .. t
separator =
if separator == "" then separator = "{{clear}}{{hr}}" end
end
end
else
text = main(pageNames,
end
Line 757 ⟶ 820:
end
-- Main invocation function for templates
local function
local args = parseArgs(frame)
local pageNames = { args[1] }
local text = main(pageNames, args)
return frame:preprocess(text)
end
-- Main entry point for templates
function p.lead(frame) return lead(frame) end
-- Entry points for
function p.
function p.
function p.
function p.
function p.
-- Entry points for other Lua modules
|