Modul:PageUtil: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
te>PerfektesChaos (+) |
te>PerfektesChaos (+) |
||
Zeile 1: | Zeile 1: | ||
− | --[=[ 2014-12- | + | --[=[ 2014-12-26 |
PageUtil | PageUtil | ||
* merge() | * merge() | ||
Zeile 8: | Zeile 8: | ||
-- table for export | -- table for export | ||
local PageUtil = {} | local PageUtil = {} | ||
+ | local MaxPages = 200 | ||
Zeile 59: | Zeile 60: | ||
-- frame -- object | -- frame -- object | ||
-- Returns content, or false | -- Returns content, or false | ||
+ | -- Uses: | ||
+ | -- mw.title.new() .exists | ||
local r | local r | ||
local seek = "^(#lstx?):%s*%[%[([^%[|%]\n]+)%]%]%s*(%S.*)%s*$" | local seek = "^(#lstx?):%s*%[%[([^%[|%]\n]+)%]%]%s*(%S.*)%s*$" | ||
Zeile 64: | Zeile 67: | ||
if source then | if source then | ||
local page = mw.title.new( source ) | local page = mw.title.new( source ) | ||
+ | source = page.prefixedText | ||
if page.exists then | if page.exists then | ||
section = mw.text.trim( section ) | section = mw.text.trim( section ) | ||
Zeile 73: | Zeile 77: | ||
else | else | ||
r = string.format( "<div class=\"error\">%s</div>", | r = string.format( "<div class=\"error\">%s</div>", | ||
− | + | source ) | |
end | end | ||
end | end | ||
Zeile 88: | Zeile 92: | ||
-- assembly -- table, with page infos | -- assembly -- table, with page infos | ||
-- Returns string with content, or nil | -- Returns string with content, or nil | ||
+ | -- Uses: | ||
+ | -- mw.title.new() .exists | ||
local page = mw.title.new( access ) | local page = mw.title.new( access ) | ||
local r | local r | ||
Zeile 125: | Zeile 131: | ||
end -- for k, v | end -- for k, v | ||
if max > 0 then | if max > 0 then | ||
+ | local n = 0 | ||
local pages = { { mw.title.getCurrentTitle().prefixedText, | local pages = { { mw.title.getCurrentTitle().prefixedText, | ||
"" } } | "" } } | ||
Zeile 137: | Zeile 144: | ||
if swallow then | if swallow then | ||
s = fraction( swallow, frame ) | s = fraction( swallow, frame ) | ||
+ | n = n + 1 | ||
else | else | ||
swallow = s:match( "^%s*%[%[([^%[|%]\n]+)%]%]%s*$" ) | swallow = s:match( "^%s*%[%[([^%[|%]\n]+)%]%]%s*$" ) | ||
if swallow then | if swallow then | ||
s = full( swallow, frame, i, pages ) | s = full( swallow, frame, i, pages ) | ||
+ | n = n + 1 | ||
end | end | ||
end | end | ||
if s then | if s then | ||
− | r = r .. s | + | r = r .. mw.text.trim( s ) |
+ | end | ||
+ | if n > MaxPages then | ||
+ | s = string.format( "'''Too many pages (max. %d)'''", | ||
+ | MaxPages ) | ||
+ | r = string.format( "%s\n\n%s", | ||
+ | r, | ||
+ | fault( s, frame ) ) | ||
+ | break -- for i | ||
end | end | ||
end | end |
Version vom 26. Dezember 2014, 11:24 Uhr
--[=[ 2014-12-26 PageUtil
- merge()
]=]
-- table for export local PageUtil = {} local MaxPages = 200
local function fault( alert, frame )
-- Format message with class="error"
-- alert -- string, with message
-- frame -- object
-- Returns message with markup
local r = alert
if frame then
r = string.format( "%s * %s", frame:getTitle(), r )
end
r = string.format( "ERROR * %s", r )
return r
end -- fault()
local function flat( adjust, assembly )
-- Replace links to pages by inner links -- adjust -- string, with text -- assembly -- table, with page infos -- Returns adjusted string local r = adjust local seek, shift, source, subst for k, v in pairs( assembly ) do source = v[ 1 ] shift = v[ 2 ] source = ":?" .. source:gsub( " ", "[_ ]+" ) :gsub( "[%.%(%)%*%?%+%-]", "%1" ) .. "%s*" seek = "%[%[%s*" .. source .. "(#[^%]]*%]%])" subst = "[[%1" r = r:gsub( seek, subst ) seek = "%[%[%s*" .. source .. "(%|[^%]]*%]%])" subst = "[[#" .. shift .. "%1" r = r:gsub( seek, subst ) seek = "%[%[%s*(" .. source .. "%]%])" subst = "[[#" .. shift .. "|%1" r = r:gsub( seek, subst ) end -- for k, v return r
end -- flat()
local function fraction( access, frame )
-- Retrieve text from section -- access -- string, with request -- frame -- object -- Returns content, or false -- Uses: -- mw.title.new() .exists local r local seek = "^(#lstx?):%s*%[%[([^%[|%]\n]+)%]%]%s*(%S.*)%s*$" local scope, source, section = access:match( seek ) if source then local page = mw.title.new( source ) source = page.prefixedText if page.exists then section = mw.text.trim( section ) if section ~= "" then r = frame:callParserFunction{ name = scope, args = { source, section } } end else
r = string.format( "
%s
",
source ) end end return r
end -- fraction()
local function full( access, frame, alias, assembly )
-- Retrieve text from page -- access -- string, with page name -- frame -- object -- alias -- number, unique -- assembly -- table, with page infos -- Returns string with content, or nil -- Uses: -- mw.title.new() .exists local page = mw.title.new( access ) local r if page.exists then local source = page.prefixedText local segment = string.format( "PageUtilMerge-%d", alias ) local seed if page.namespace == 0 then seed = ":" .. source else seed = source end r = frame:expandTemplate( { title = seed } ) r = string.format( "\n%s", segment, r ) table.insert( assembly, { source, segment } ) else
r = string.format( "
%s
",
page.prefixedText ) end return r
end -- full()
PageUtil.merge = function ( args, frame )
-- Retrieve text -- args -- table, with request -- frame -- object, if available -- Returns string, with content local max = 0 local r = "" for k, v in pairs( args ) do if type( k ) == "number" and k > max then max = k end end -- for k, v if max > 0 then local n = 0 local pages = { { mw.title.getCurrentTitle().prefixedText, "" } } local mode, s, section, swallow if not frame then frame = mw.getCurrentFrame() end for i = 1, max do s = args[ i ] if s then swallow = s:match( "^%s*(#lstx?:[^\n]*%S)%s*$" ) if swallow then s = fraction( swallow, frame ) n = n + 1 else swallow = s:match( "^%s*%[%[([^%[|%]\n]+)%]%]%s*$" ) if swallow then s = full( swallow, frame, i, pages ) n = n + 1 end end if s then r = r .. mw.text.trim( s ) end if n > MaxPages then s = string.format( "Too many pages (max. %d)", MaxPages ) r = string.format( "%s\n\n%s", r, fault( s, frame ) ) break -- for i end end end -- for i r = flat( r, pages ) end return r
end -- .merge()
-- Export local p = { }
p.merge = function ( frame )
local lucky, r = pcall( PageUtil.merge, frame.args, frame ) if not lucky then r = fault( r, frame ) end return r
end
function p.PageUtil()
return PageUtil
end
return p