Content deleted Content added
Sophivorus (talk | contribs) Change local function names to camelCase except the entry points for backwards compatibility (see talk page technical debt). Also improve a couple comments. |
Sophivorus (talk | contribs) 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. |
||
Line 37:
-- If template is wanted but produces an unwanted reference then return the string with |shortref or |ref removed
local
-- If a wanted template has unwanted nested templates, purge them too
-- Replace {{audio}} by its text parameter: {{Audio|Foo.ogg|Bar}} → Bar
-- Replace {{Nihongo foot}} by its text parameter: {{Nihongo foot|English|英語|eigo}} → English
if
return nil -- not an unwanted template: keep
Line 76:
end
local
if
if mw.ustring.match(
elseif not
return false
else
-- try commons
end
frame = frame or mw.getCurrentFrame()
return (
end
Line 106:
local function parseCaption(caption)
if not caption then return nil end
local
local
while
local
local
local
if
elseif
else -- argument ends before the next wikilink or template
return mw.ustring.sub(caption, 1,
end
end
Line 137:
local hasImages = false
local images = {}
local
while
local argname, position, image = mw.ustring.match(text, "|%s*([^=|]-[Ii][Mm][Aa][Gg][Ee][^=|]-)%s*=%s*()(.*)",
if image then -- ImageCaption=, image_size=, image_upright=, etc. do not introduce an image
local lcArgname = mw.ustring.lower(argname)
Line 151:
hasImages = true
images[position] = image
else
end
end
while
local position, image = mw.ustring.match(text, "|%s*[^=|]-[Pp][Hh][Oo][Tt][Oo][^=|]-%s*=%s*()(.*)",
if image then
hasImages = true
images[position] = image
else
end
end
while
local position, image = mw.ustring.match(text, "|%s*[^=|{}]-%s*=%s*()%[?%[?([^|{}]*%.%a%a%a%a?)%s*%f[|}]",
if image then
hasImages = true
Line 175:
images[position] = image
end
else
end
end
Line 185:
-- find all captions
local captions = {}
while
local position, caption = matchAny(text, "|%s*", d.captionParams, "%s*=%s*()([^\n]+)",
if caption then
-- extend caption to parse "| caption = Foo {{Template\n on\n multiple lines}} Bar\n"
Line 207:
end
end
else
end
end
Line 223:
mw.ustring.match(altText, ".*%[%b[]%]()") or 1)
local
local aftertext = math.min( -- find position after whichever comes first: end of string, }} or |
mw.ustring.match(altText, "()}}", lookfrom) or
mw.ustring.match(altText, "()|", lookfrom) or
altText = mw.ustring.sub(altText, 1, aftertext-1) -- chop off |... or }}... which is not part of [[...]] or {{...}}
Line 333:
}
local function modifyImage(image,
if
for _, filearg in pairs(mw.text.split(
local fa = mw.ustring.gsub(filearg, "=.*", "") -- "upright=0.75" → "upright"
local group = {fa} -- group of "border" is ["border"]...
Line 362:
-- @param filesOnly : If set, only return the files and not the prose
local function parse(text, options, filesOnly)
local
if options.paraflags then
if type(options.paraflags) ~= "table" then options.paraflags = numberFlags(options.paraflags) end
for _, v in pairs(options.paraflags) do
if v then
end
end
if filesOnly then
options.paraflags = {}
end
Line 382:
end
local
if
local
local t = "" -- the stripped down output text
local
local files = 0 -- how many images so far
local paras = 0 -- how many paragraphs so far
Line 401:
f = "[[File:" .. f .. "]]"
f = modifyImage(f, "thumb")
f = modifyImage(f,
if checkImage(f) then
end
repeat -- loop around parsing a template, image or paragraph
local token = mw.ustring.match(text, "^%b{}%s*") or false -- {{Template}} or {| Table |}
if not
local line = mw.ustring.match(text, "[^\n]*")
Line 421:
if token then -- found a template which is not the prefix to a line of text
if
if not filesOnly and not startLine then t = t .. token end
Line 441:
if options.fileflags and options.fileflags[files] then -- if displaying this image
image = modifyImage(image, "thumb")
image = modifyImage(image,
end
end
Line 454:
if options.fileflags and options.fileflags[files] then
local image = token -- copy token for manipulation by adding |right etc. without changing the original
image = modifyImage(image,
end
end
else -- got a paragraph, which ends at a file, image, blank line or end of text
local
local
local
mw.ustring.find(text, "%[%[%s*[Ff]ile%s*:") or
mw.ustring.find(text, "%[%[%s*[Ii]mage%s*:") or
token = mw.ustring.sub(text, 1,
if
token = token .. mw.ustring.match(text, "\n%s*\n",
end
local isHatnote = not(
if not isHatnote then
paras = paras + 1
if
end
end -- of "else got a paragraph"
Line 483:
text = mw.ustring.gsub(t, "\n+$", "") -- remove trailing line feeds, so "{{Transclude text excerpt|Foo}} more" flows on one line
return
end
Line 531:
-- Parse a ==Section== from a page
local function getSection(text, section,
local escapedSection = mw.ustring.gsub(mw.uri.decode(section), "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1") -- %26 → & etc, then ^ → %^ etc.
local level, content = mw.ustring.match(text .. "\n", "\n(==+)%s*" .. escapedSection .. "%s*==.-\n(.*)")
if not content then return nil end -- no such section
local
if
else
end
content = mw.ustring.gsub(content,
return content
end
Line 547:
-- Remove unmatched <tag> or </tag> tags
local function fixTags(text, tag)
local
for i in mw.ustring.gmatch(text, "<%s*" .. tag .. "%f[^%w_].->") do
local
for i in mw.ustring.gmatch(text, "<%s*/" .. tag .. "%f[^%w_].->") do
if
local i = 0
text = mw.ustring.gsub(text, "<%s*" .. tag .. "%f[^%w_].->", function(t)
i = i + 1
if i >
end) -- "end" here terminates the anonymous replacement function(t) passed to gsub
elseif
text = mw.ustring.gsub(text, "<%s*/" .. tag .. "%f[^%w_].->", "",
end
return text
Line 566:
-- Main function returns a string value: text of the lead of a page
local function main(
if not
local
local text
local
local
local
local
local section
-- read the page, or a random one if multiple pages were provided
if
while not text and
local
if
if
-- We have page or [[page]] or [[page|text]], possibly followed by |opt1|opt2...
local pn
pn,
if pn then
else -- we have page or page|opt...
end
if
local pn
pn, section = mw.ustring.match(
text,
if not
return err("No title for page name " ..
else
end
if text and options.nostubs then
Line 607:
end
if not section then
section = mw.ustring.match(
end
if text and section and section ~= "" then text = getSection(text, section) end
end
end
if not text then table.remove(
end
if not text then return err("Cannot read a valid page: first name is " ..
text = cleanupText(text, options)
local
for k, v in pairs(options) do
if
for _, t in pairs(mw.text.split(
local k, v = mw.ustring.match(t, "%s*([^=]-)%s*=(.-)%s*$")
end
if
end
local
-- replace the bold title or synonym near the start of the article by a wikilink to the article
local lang = mw.language.getContentLanguage()
local pos = mw.ustring.find(text, "'''" .. lang:ucfirst(
or mw.ustring.find(text, "'''" .. lang:lcfirst(
if pos then
local len = mw.ustring.len(
text = mw.ustring.sub(text, 1, pos + 2) .. "[[" .. mw.ustring.sub(text, pos + 3, pos + len + 2) .. "]]" .. mw.ustring.sub(text, pos + len + 3, -1) -- link it
else -- look for anything unlinked in bold, assumed to be a synonym of the title (e.g. a person's birth name)
text = mw.ustring.gsub(text, "()'''(.-'*)'''", function(a, b)
if a < 100 and not mw.ustring.find(b, "%[") then ---if early in article and not wikilinked
return "'''[[" ..
else
return nil -- instruct gsub to make no change
Line 652:
-- remove '''bold text''' if requested
if is(
text =
-- Seek and destroy unterminated templates and wikilinks
Line 674:
text = fixTags(text, "div")
if
if
local listtext =
if listtext == "" then listtext = "Other articles" end
text = text .. "{{collapse top|title={{resize|85%|" ..listtext .. "}}|bg=fff}}{{hlist"
for _, p in pairs(
if mw.ustring.match(p, "%S") then text = text .. "|[[" .. mw.text.trim(p) .. "]]" end
end
Line 702:
end
local
if func == "lead" then
elseif func == "linked" or func == "listitem" then
-- Read named page and find its wikilinks
Line 721:
text = mw.ustring.gsub(text, "{{%s*[Aa]nnotated[ _]link%s*|%s*(.-)%s*}}", "[[%1]]")
if func == "linked" then
for p in mw.ustring.gmatch(text, "%[%[%s*([^%]|\n]*)") do table.insert(
else -- listitem: first wikilink on a line beginning *, :#, etc. except in "See also" or later section
text = mw.ustring.gsub(text, "\n== *See also.*", "")
for p in mw.ustring.gmatch(text, "\n:*[%*#][^\n]-%[%[%s*([^%]|\n]*)") do table.insert(
end
elseif func == "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(
end
elseif func == "selected" then
Line 737:
if articlekey == 0 then articlekey = articlecount end
end
end
Line 748:
if options.showall then
local separator = ""
for _, p in pairs(
local t = main({ p }, options)
if t ~= "" then
Line 757:
end
else
text = main(
end
Line 811:
if options.more and options.more == "" then options.more = "Read more..." end -- more= is short for this default text
local
local text = main(
if text == "" and d.brokenCategory and d.brokenCategory ~= "" and mw.title.getCurrentTitle().isContentPage then
Line 847:
function p.parseimage(text, start) return parseImage(text, start) end
function p.cleanupText(text, options) return cleanupText(text, options) end
function p.main(
function p.numberflags(str) return numberFlags(str) end
|