Module:Excerpt/sandbox: Difference between revisions

Content deleted Content added
Rename most variables to camelCase for inner consistency and with general Lua and MediaWiki style and better readability on long variable names. Also un-abbreviated a few variable names, again for better readability and hopefully easier onboarding of new developers.
Rename a few more variables. Getting ready to split abstract functions from template-specific functions
Line 141:
local argname, position, image = mw.ustring.match(text, "|%s*([^=|]-[Ii][Mm][Aa][Gg][Ee][^=|]-)%s*=%s*()(.*)", captureFrom)
if image then -- ImageCaption=, image_size=, image_upright=, etc. do not introduce an image
local lcArgnamelcArgName = mw.ustring.lower(argname)
if mw.ustring.find(lcArgnamelcArgName, "caption")
or mw.ustring.find(lcArgnamelcArgName, "size")
or mw.ustring.find(lcArgnamelcArgName, "upright") then
image = nil
end
Line 219:
 
-- altText is terminated by }} or |, but first skip any matched [[...]] and {{...}}
local lookfromlookFrom = math.max( -- find position after whichever comes last: start of string, end of last ]] or end of last }}
mw.ustring.match(altText, ".*{%b{}}()") or 1, -- if multiple {{...}}, .* consumes all but one, leaving the last for %b
mw.ustring.match(altText, ".*%[%b[]%]()") or 1)
 
local length = mw.ustring.len(altText)
local aftertextafterText = math.min( -- find position after whichever comes first: end of string, }} or |
mw.ustring.match(altText, "()}}", lookfromlookFrom) or length+1,
mw.ustring.match(altText, "()|", lookfromlookFrom) or length+1)
altText = mw.ustring.sub(altText, 1, aftertextafterText-1) -- chop off |... or }}... which is not part of [[...]] or {{...}}
 
altText = mw.text.trim(altText)
Line 690:
 
-- Shared template invocation code for lead and random functions
local function invoke(frame, functemplate)
-- args = { 1,2,... = page names, paragraphs = list e.g. "1,3-5", files = list, more = text}
local args = {} -- args[k] = frame.args[k] or frame:getParent().args[k] for all k in either (numeric or not)
Line 697:
errors = args["errors"] -- set the module level boolean used in local function err
 
local articlecountarticleCount = #args -- must be 1 except with selected=Foo and Foo=Somepage
if articlecountarticleCount < 1 and not (functemplate == "selected" and args[functemplate] and args[args[functemplate]]) then
return err("No articles provided")
end
 
local pageNames = {}
if functemplate == "lead" then
pageNames = { args[1] }
elseif functemplate == "linked" or functemplate == "listitem" then
-- Read named page and find its wikilinks
local page = args[1]
Line 720:
-- replace annotated links with real links
text = mw.ustring.gsub(text, "{{%s*[Aa]nnotated[ _]link%s*|%s*(.-)%s*}}", "[[%1]]")
if functemplate == "linked" then
for p in mw.ustring.gmatch(text, "%[%[%s*([^%]|\n]*)") do table.insert(pageNames, p) end
else -- listitem: first wikilink on a line beginning *, :#, etc. except in "See also" or later section
Line 726:
for p in mw.ustring.gmatch(text, "\n:*[%*#][^\n]-%[%[%s*([^%]|\n]*)") do table.insert(pageNames, p) end
end
elseif functemplate == "random" then
-- accept any number of page names. If more than one, we'll pick one randomly
for i, p in pairs(args) do
if p and type(i) == 'number' then table.insert(pageNames, p) end
end
elseif functemplate == "selected" then
local articlekeyarticleKey = args[functemplate]
if tonumber(articlekeyarticleKey) then -- normalise article number into the range 1..#args
articlekeyarticleKey = articlekeyarticleKey % articlecountarticleCount
if articlekeyarticleKey == 0 then articlekeyarticleKey = articlecountarticleCount end
end
pageNames = { args[articlekeyarticleKey] }
end
 
Line 805:
options.paraflags = numberFlags(args.paragraphs or "") -- parse paragraphs, e.g. "1,3-5" → {"1","3-5"}
options.fileflags = numberFlags(args.files or "1") -- parse file numbers
options.nobold = 1
options.keepTables = is(args.tables) and args.tables or 1
options.keepRefs = is(args.references) and args.references or 1