Modul:Pieskovisko: Rozdiel medzi revíziami

Smazaný obsah Přidaný obsah
ARychlik (diskusia | príspevky)
test
ARychlik (diskusia | príspevky)
Bez shrnutí editace
Značka: vrátenie
 
Riadok 1:
-- Prebrané z enwiki
-- This module implements {{collapsible list}}.
--The purpose of this module is to take a list of linked page, and use it to determine the next and previous page in the list as well as the total number of pages.
 
local p = {}
function anonymize(name)
return mw.ustring.gsub(name,"^"..mw.site.siteName,"Project") or name
end
 
local function gettitlestyletrackingout( ts name)
return mw.ustring.gsub(name,"^Project",mw.site.siteName) or name
if not ts then return '' end
ts = mw.ustring.gsub(mw.ustring.lower(ts), '%s', '')
local tsvals = mw.text.split(ts, ';')
table.sort(tsvals)
local skey = table.concat(tsvals,';')
skey = mw.ustring.gsub(skey, '^;', '')
skey = mw.text.encode(mw.text.encode(skey),'%c%[%]=')
if (mw.ustring.match(';' .. ts, ';background:') or mw.ustring.match(';' .. ts, ';background%-color:'))
and mw.ustring.match(';' .. ts, ';text%-align:') then
return '[[Category:Pages using collapsible list with both background and text-align in titlestyle|' .. skey .. ' ]]'
end
return '[[Category:Pages using collapsible list without both background and text-align in titlestyle|' .. skey .. ' ]]'
end
 
local function getListItemkeyize( data pagename)
-- there was a complaint about "_" breaking things. Do all lookups with _ in place of any space.
if not type( data ) == 'string' then
-- also spaces in the index file (non-module) were causing trouble
return ''
pagename = mw.text.trim(pagename)
end
returnpagename = mw.ustring.formatgsub(pagename, '<li style="line-height: inherit; margin: 0">%s</li>', data "_")
pagename = mw.uri.decode(pagename)
pagename = anonymize(pagename)
return pagename
end
 
function p.main(frame,displacement,varstoreturn)
-- Returns an array containing the keys of all positional arguments
local parent=frame.getParent(frame)
-- that contain data (i.e. non-whitespace values).
local currentpage,indexmodule,defaultpage,noerr
local function getArgNums( args )
---- args in the #invoke itself trump args in the parent frame
local nums = {}
currentpage = frame.args.page and mw.text.trim(frame.args.page)
for k, v in pairs( args ) do
defaultpage = frame.args.defaultpage and mw.text.trim(frame.args.defaultpage)
if type( k ) == 'number' and
indexmodule = frame.args.index and mw.text.trim(frame.args.index)
k >= 1 and
displacement = displacement or frame.args.displacement -- can be passed from the other function names at the end
math.floor( k ) == k and
noerr=frame.args.noerr -- used as boolean
type( v ) == 'string' and
anonymizereturn = frame.args.anonymize -- used as boolean
mw.ustring.match( v, '%S' ) then
---- args in the parent frame come next
table.insert( nums, k )
if parent then
currentpage=currentpage or (parent.args.page and mw.text.trim(parent.args.page))
indexmodule=indexmodule or (parent.args.index and mw.text.trim(parent.args.index)) -- index is a module return{'page1','page2', ...}
defaultpage=defaultpage or (parent.args.defaultpage and mw.text.trim(parent.args.defaultpage))
noerr=noerr or parent.args.noerr
anonymizereturn = anonymizereturn or parent.args.anonymize
end
---- default values if parameters aren't provided
defaultpage=defaultpage or "" -- don't know where to send people by default
if not(indexmodule) then
return "[[Module:TrainingPages]] error:no index parameter specified"
end
if not(currentpage) then
table.sort( nums )
local pp=mw.title.getCurrentTitle()
return nums
if not pp then
end
if noerr then
 
return "","",""
-- Formats a list of classes, styles or other attributes.
else return "[[Module:TrainingPages]] error:failed to access getCurrentTitle" -- this shouldn't happen anyway, I don't think....
local function formatAttributes( attrType, ... )
local attributes = { ... } end
end
local nums = getArgNums( attributes )
currentpage=pp.fullText
local t = {}
for i, num in ipairs( nums ) do
table.insert( t, attributes[ num ] )
end
currentpage=anonymize(currentpage) --- convert "Wikipedia:, "Meta:" etc. into "Project:
if #t == 0 then
local index={}
return '' -- Return the blank string so concatenation will work.
if mw.ustring.sub(indexmodule,1,6)=="Module" then
---- get a table of the pages in order from indexmodule
index=mw.loadData(indexmodule)
else pp=mw.title.new(indexmodule)
if not pp then
if noerr then
return "","",""
else return "[[Module:TrainingPages]] error (''index'' parameter): failed to access mw.title.new("..tostring(indexmodule)..") to load the index file",false,false,true
end
end
local textindex=pp.getContent(pp)
if not textindex then
if noerr then
return "","",""
else return "[[Module:TrainingPages]] error (''index'' parameter):failed to access mw.title.new("..indexmodule.."):getContent() to load the index data",false,false,true
end
end
prowl=mw.ustring.gmatch(textindex,"%[%[(.-)[%]|]") -- first half of any wikilink
index={}
repeat
link=prowl()
if not(link) then break end
link = mw.text.trim(link)
if link~="" then table.insert(index,link) end
until false
end
displacement=displacement or 0 -- assume a null parameter is just display the same
return mw.ustring.format( ' %s="%s"', attrType, table.concat( t, ' ' ) )
---- set up the reverse lookup in lookup.
end
---- it would be faster to set this up in the indexmodule
 
---- but we don't want inconsistencies from user input!
local function buildList( args )
local lookup={}
-- Get the list items.
local listItems i= {}0
repeat
local argNums = getArgNums( args )
for i, num in ipairs( argNums ) doi=i+1
local j=index[i]
table.insert( listItems, getListItem( args[ num ] ) )
if j then lookup[keyize(j)]=i else break end -- lookup["page name"] => page number
until false
--- get the page to return
local returnpage,currentpagenumber
if tonumber(currentpage) then
currentpagenumber=tonumber(currentpage)
returnpage=index[currentpagenumber+displacement] or defaultpage
else if (lookup[keyize(currentpage)]) then
currentpagenumber=lookup[keyize(currentpage)]
returnpage=index[currentpagenumber+displacement] or defaultpage
else returnpage=defaultpage
end
end
if #listItems == 0anonymizereturn then
returnpage=anonymize(returnpage)
return ''
else
returnpage=out(returnpage)
end
if returnpage then returnpage = mw.text.trim(returnpage) end
listItems = table.concat( listItems )
if not(varstoreturn) then return tostring(returnpage) else return tostring(returnpage),currentpagenumber,#index end
-- Get class, style and title data.
local div1class = formatAttributes( 'class', 'NavFrame', not args.expand and 'collapsed' )
local div1style = formatAttributes(
'style',
args.frame_style,
args.framestyle,
not ( args.frame_style or args.framestyle ) and 'border: none; padding: 0;'
)
local div2class = formatAttributes( 'class', 'NavHead' )
local div2style = formatAttributes(
'style',
'font-size: 105%;',
args.title_style,
args.titlestyle,
not ( args.title_style or args.titlestyle ) and 'background: transparent; text-align: left;'
)
local title = args.title or 'List'
local ulclass = formatAttributes( 'class', 'NavContent', args.hlist and 'hlist' )
local ulstyle = formatAttributes(
'style',
not args.bullets and 'list-style: none none; margin-left: 0;',
args.list_style,
args.liststyle,
not ( args.list_style or args.liststyle ) and 'text-align: left;',
'font-size: 105%; margin-top: 0; margin-bottom: 0; line-height: inherit;'
)
-- Build the list.
return mw.ustring.format(
'<div%s%s>\n<div%s%s>%s</div>\n<ul%s%s>%s</ul>\n</div>',
div1class, div1style, div2class, div2style, title, ulclass, ulstyle, listItems
) .. gettitlestyletracking(args.title_style or args.titlestyle)
end
-- Return the next page in the index
-- Used like if on a page that is part of the index:
--{{#invoke:TrainingPages| next_page | index=Project:Training/For students/Editing module index }}
-- Used like this to find the next page after a specified page:
--{{#invoke:TrainingPages| next_page | index=Project:Training/For students/Editing module index | currentpage=Project:Training/For students/My sandbox }}
 
function p.mainnext_page( frame )
local returnpage,pagenumber,totalpages,errcode=p.main(frame,1,true)
local origArgs
return returnpage
if frame == mw.getCurrentFrame() then
end
origArgs = frame:getParent().args
p.next = p.next_page
for k, v in pairs( frame.args ) do
origArgs = frame.args
-- Same as above, but returns the previous page
break
function p.last_page(frame)
end
local returnpage,pagenumber,totalpages,errcode=p.main(frame,-1,true)
else
return returnpage
origArgs = frame
end
p.last = p.last_page
local args = {}
function p.page_number(frame)
for k, v in pairs( origArgs ) do
local returnpage,pagenumber,totalpages,errcode=p.main(frame,0,true)
if type( k ) == 'number' or v ~= '' then
if errcode then return returnpage else return pagenumber end
args[ k ] = v
end
p.page = p.page_number
end
 
return buildList( args )
function p.total_pages(frame)
local returnpage,pagenumber,totalpages,errcode=p.main(frame,0,true)
if errcode then return returnpage else return totalpages end
end
p.total = p.total_pages
 
return p