跳转到内容

草稿:Module:Collapsible list

维基百科,自由的百科全书

local p = {} local yesno = require('module:yesno')

local function getListItem( data )

   if not type( data ) == 'string' then
       return 
   end
   local emptyList = mw.html.create('li')

:cssText('line-height: inherit') :cssText('margin: 0') :wikitext(data)

   return tostring(emptyList)

end

-- Returns an array containing the keys of all positional arguments -- that contain data (i.e. non-whitespace values). local function getArgNums( args )

   local nums = {}
   for k, v in pairs( args ) do
       if type( k ) == 'number' and
           k >= 1 and
           math.floor( k ) == k and
           type( v ) == 'string' and
           mw.ustring.match( v, '%S' ) then
               table.insert( nums, k )
       end
   end
   table.sort( nums )
   return nums

end

-- TODO: use Module:List. Since the update for this comment is routine, -- Modification: uses module:yesno and mw.html.create local function buildList( args )

   local argNums = getArgNums( args )
   if #argNums == 0 then
       return 
   end
   
   -- ordered list option added
   local isOrdered = yesno(args.ordered, false)
   local isBullets = yesno(args.bullets, false)
   -- hack:collapsible button jumps with title
   local textAlignmentCentered = 'text%-align%s*:%s*center'
   local centeredTitle =
       (args.title_style and args.title_style:lower():match(textAlignmentCentered))
       or (args.titlestyle and args.titlestyle:lower():match(textAlignmentCentered))
   local centeredTitleSpacing = centeredTitle and 'margin: 0 4em' or nil
   local hlist_templatestyles = 
   if args.hlist then
       hlist_templatestyles = mw.getCurrentFrame():extensionTag{
           name = 'templatestyles',
           args = { src = 'Hlist/styles.css' }
       }
   end
   -- collapsible frame
   local root = mw.html.create('div')
       :addClass('collapsible-list')
       :addClass('mw-collapsible')
       :addClass(not args.expand and 'mw-collapsed')
       :cssText('text-align: left;')
       :cssText(args.frame_style or args.framestyle)
   -- title
   local titleWrapper = root:tag('div')
       :css('line-height', '1.6em')
       :css('font-weight', 'bold')
       :cssText(args.title_style or args.titlestyle)
   titleWrapper:tag('div')
       :cssText(centeredTitleSpacing)
       :wikitext(args.title or 'List')
   -- list(ul / ol)
   local list = root:tag(isOrdered and 'ol' or 'ul')
       :addClass('mw-collapsible-content')
       :addClass(args.hlist and 'hlist')
       :css('margin-top', 0)
       :css('margin-bottom', 0)
       :css('line-height', 'inherit')
       :cssText(args.list_style or args.liststyle)
   if not isOrdered and not isBullets then
       list
           :css('list-style', 'none')
           :css('margin-left', 0)
   end
   -- list items
   for _, num in ipairs( argNums ) do
       list:tag('li')
           :css('line-height', 'inherit')
           :css('margin', 0)
           :wikitext(args[num])
   end
   return hlist_templatestyles .. tostring(root)

end

function p.main( frame )

   local origArgs
   if frame == mw.getCurrentFrame() then
       origArgs = frame:getParent().args
       for k, v in pairs( frame.args ) do
           origArgs = frame.args
           break
       end
   else
       origArgs = frame
   end
   
   local args = {}
   for k, v in pairs( origArgs ) do
       if type( k ) == 'number' or v ~=  then
           args[ k ] = v
       end
   end
   return buildList( args )

end

return p