Module:Lorem ipsum: Difference between revisions
From Attu Project
More actions
Content deleted Content added
imported>Ahecht add joinprefixsuffix |
Consolidate redundant code |
||
| (One intermediate revision by one other user not shown) | |||
| Line 1: | Line 1: | ||
local yn = require("Module:Yesno") |
local yn = require("Module:Yesno") |
||
local p = {} |
|||
return { |
|||
main = function(frame) |
|||
function p._main(args) |
|||
local args = require("Module:Arguments").getArgs(frame) |
|||
local paragraphs = {} |
|||
local data = args["data"] and mw.loadData(args["data"]) or mw.loadData("Module:Lorem_ipsum/data") |
|||
local outTbl = setmetatable({}, {__call = function (t, v) t[#t+1] = v end}) |
|||
local paragraphs = {} |
|||
local link = yn(args["link"]) or yn(args[4]) or false |
|||
for k,v in ipairs(data) do |
|||
local join = yn(args["join"]) or false |
|||
table.insert(paragraphs, v) |
|||
local joinprefixsuffix = yn(args["joinprefixsuffix"]) or false |
|||
local cat = yn(args["cat"]) or true |
|||
local count = tonumber(args[1]) or 1 |
|||
local i = tonumber(args["start"]) or 1 |
|||
local data = mw.loadData(args["data"] or "Module:Lorem_ipsum/data") |
|||
local maxLen = tonumber(args["max len"]) |
|||
for k,v in ipairs(data) do |
|||
table.insert(paragraphs, v) |
|||
end |
|||
if join and (not joinprefixsuffix) then |
|||
outTbl(args["prefix"] or args[2] or "") |
|||
end |
|||
while i > 0 and i <= count do |
|||
if (not join) or joinprefixsuffix then |
|||
outTbl(args["prefix"] or args[2] or "\n") |
|||
end |
end |
||
mw.log(count, i, data) |
|||
local out = '' |
|||
outTbl(mw.ustring.format(paragraphs[math.mod(i - 1, #paragraphs) + 1], (link and "link" or ""))) |
|||
local link = yn(args["link"]) or yn(args[4]) or false |
|||
if not join then |
|||
outTbl((args["suffix"] or args[3] or "") .. "\n") |
|||
local joinprefixsuffix = yn(args["joinprefixsuffix"]) or false |
|||
else |
|||
local cat = yn(args["cat"]) or true |
|||
if joinprefixsuffix then |
|||
local count = tonumber(args[1] or 1) |
|||
outTbl(args["suffix"] or args[3] or "") |
|||
local i = 1 |
|||
if join and (not joinprefixsuffix) then |
|||
out = out .. (args["prefix"] or args[2] or "") |
|||
end |
|||
while i <= count do |
|||
if (not join) or joinprefixsuffix then |
|||
out = out .. (args["prefix"] or args[2] or "\n") |
|||
end |
end |
||
outTbl(i == count and "" or " ") |
|||
out = out .. mw.ustring.format(paragraphs[math.mod(i - 1, #paragraphs) + 1], (link and "link" or "")) |
|||
if not join then |
|||
out = out .. (args["suffix"] or args[3] or "") .. "\n" |
|||
else |
|||
if joinprefixsuffix then |
|||
out = out .. (args["suffix"] or args[3] or "") |
|||
end |
|||
out = out .. (i == count and "" or " ") |
|||
end |
|||
link = false |
|||
i = i + 1 |
|||
end |
end |
||
link = false |
|||
if join and (not joinprefixsuffix) then |
|||
i = i + 1 |
|||
out = out .. (args["suffix"] or args[3] or "") |
|||
end |
|||
if join and (not joinprefixsuffix) then |
|||
outTbl(args["suffix"] or args[3] or "") |
|||
out = out .. "[[Category:Wikipedia articles containing placeholders]]" |
|||
end |
|||
if cat and mw.title.getCurrentTitle().namespace == 0 and not mw.isSubsting() then |
|||
return frame:preprocess(out) |
|||
outTbl("[[Category:Wikipedia articles containing placeholders]]") |
|||
end, |
|||
oneParagraph = function(frame) |
|||
local args = require("Module:Arguments").getArgs(frame) |
|||
local data = args["data"] and mw.loadData(args["data"]) or mw.loadData("Module:Lorem_ipsum/data") |
|||
local paragraphs = {} |
|||
for k,v in ipairs(data) do |
|||
table.insert(paragraphs, v) |
|||
end |
|||
local i = tonumber(args[1] or 1) |
|||
local link = yn(args["link"]) |
|||
local paragraph = paragraphs[math.mod(i - 1, #paragraphs) + 1] |
|||
local out = "" |
|||
if cat and mw.title.getCurrentTitle().namespace == 0 and not mw.isSubsting() then |
|||
out = out .. "[[Category:Wikipedia articles containing placeholders]]" |
|||
end |
|||
out = out .. mw.ustring.format(paragraph, (link and "link" or "")) |
|||
out = frame:preprocess(out) |
|||
local maxLen = tonumber(args["max len"] or mw.ustring.len(out)) |
|||
return mw.ustring.sub(out, 1, maxLen) |
|||
end |
end |
||
local outStr = mw.getCurrentFrame():preprocess(table.concat(outTbl)) |
|||
} |
|||
return maxLen and mw.ustring.sub(outStr, 1, maxLen) or outStr |
|||
end |
|||
function p.main(frame) |
|||
local args = require("Module:Arguments").getArgs(frame) |
|||
return p._main(args, false) |
|||
end |
|||
function p.oneParagraph(frame) |
|||
local args = require("Module:Arguments").getArgs(frame) |
|||
args["join"] = true |
|||
args["start"] = tonumber(args[1]) or 1 |
|||
return p._main(args) |
|||
end |
|||
return p |
|||
Latest revision as of 14:15, 22 April 2026
Documentation for this module may be created at Module:Lorem ipsum/doc
local yn = require("Module:Yesno")
local p = {}
function p._main(args)
local paragraphs = {}
local outTbl = setmetatable({}, {__call = function (t, v) t[#t+1] = v end})
local link = yn(args["link"]) or yn(args[4]) or false
local join = yn(args["join"]) or false
local joinprefixsuffix = yn(args["joinprefixsuffix"]) or false
local cat = yn(args["cat"]) or true
local count = tonumber(args[1]) or 1
local i = tonumber(args["start"]) or 1
local data = mw.loadData(args["data"] or "Module:Lorem_ipsum/data")
local maxLen = tonumber(args["max len"])
for k,v in ipairs(data) do
table.insert(paragraphs, v)
end
if join and (not joinprefixsuffix) then
outTbl(args["prefix"] or args[2] or "")
end
while i > 0 and i <= count do
if (not join) or joinprefixsuffix then
outTbl(args["prefix"] or args[2] or "\n")
end
mw.log(count, i, data)
outTbl(mw.ustring.format(paragraphs[math.mod(i - 1, #paragraphs) + 1], (link and "link" or "")))
if not join then
outTbl((args["suffix"] or args[3] or "") .. "\n")
else
if joinprefixsuffix then
outTbl(args["suffix"] or args[3] or "")
end
outTbl(i == count and "" or " ")
end
link = false
i = i + 1
end
if join and (not joinprefixsuffix) then
outTbl(args["suffix"] or args[3] or "")
end
if cat and mw.title.getCurrentTitle().namespace == 0 and not mw.isSubsting() then
outTbl("[[Category:Wikipedia articles containing placeholders]]")
end
local outStr = mw.getCurrentFrame():preprocess(table.concat(outTbl))
return maxLen and mw.ustring.sub(outStr, 1, maxLen) or outStr
end
function p.main(frame)
local args = require("Module:Arguments").getArgs(frame)
return p._main(args, false)
end
function p.oneParagraph(frame)
local args = require("Module:Arguments").getArgs(frame)
args["join"] = true
args["start"] = tonumber(args[1]) or 1
return p._main(args)
end
return p