Module:Args: Difference between revisions

From Zelda Dungeon Wiki
Jump to navigation Jump to search
Want an adless experience? Log in or Create an account.
(don't try to parse values to numbers because a module might be expecting a string and the input just happens to be a number)
(remove the Arg class)
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
local Arg = {}
local p = {}
Arg.__index = Arg


function Arg.new( value )
-- place the value in the right spot in the arg tree, determined by underscores in the key
   return setmetatable( {
function p.expandInto( args, key, value )
    __value = value
   local first, rest = string.match( key, '^(.-)_(.*)$' )
   }, Arg )
  local myKey = first or key
end
   myKey = tonumber( myKey ) or myKey


-- get the Arg node's value, if it has one
  if first then -- this is an internal node so insert children
function Arg:val()
    args[myKey] = type( args[myKey] ) == 'table' and args[myKey] -- already a table
   return self.__value
      or args[myKey] and { __value = args[myKey] } -- move existing value into a new table
      or {} -- create new table
    p.expandInto( args[myKey], rest, value )
   else -- this is a leaf so set value
    if type( args[myKey] ) == 'table' then args[myKey].__value = value
    else args[myKey] = value
    end
  end
end
end


-- get the values of the Arg node's direct children
-- expand the arg tree for the given flattened args
-- this is used to re-flatten/unwrap a particular piece of an Arg tree
function p.expand( args )
function Arg:values()
   local expandedArgs = {}
   local vals = {}
   for k, v in pairs( args ) do
   for k, v in pairs( self ) do
     p.expandInto( expandedArgs, k, v )
     if type( v ) == 'table' and v.val then
      vals[k] = v:val()
    end
   end
   end


   return vals
   return expandedArgs
end
end


local p = {}
-- coerce the given node into a table, in case it is a standalone value
function p.getTable( val )
  return type( val ) == 'table' and val or { __value = val }
end
 
-- get the value at the given node, in case the node contains a table
function p.getValue( val )
  return type( val ) == 'table' and val.__value or val
end


function insertInto( tbl, key, value )
-- expand the arg tree from the given frame and its parent
  local first, rest = string.match( key, '^(.-)_(.*)$' )
function p.fromFrame( frame )
   local myKey = first or key
   local parsedArgs = {}
  myKey = tonumber( myKey ) or myKey


   -- make sure the node exists
   for k, v in pairs( frame.args ) do
  if not tbl[myKey] then tbl[myKey] = Arg.new() end
    p.expandInto( parsedArgs, k, v )
  end


   if first then -- this is an internal node so insert children
  local parentFrame = frame:getParent()
     insertInto( tbl[myKey], rest, value )
   if parentFrame and parentFrame.args then
  else -- this is a leaf so set value
     for k, v in pairs (parentFrame.args ) do
     tbl[myKey].__value = value
      p.expandInto( parsedArgs, k, v )
     end
   end
   end
  return parsedArgs
end
end


function p.parse( args )
-- parse and expand the arg tree for the given template on the given page
   local parsedArgs = Arg.new()
function p.fromPageContent( content, templateName )
   for k, v in pairs( args ) do
  templateName = templateName or 'Properties'
    insertInto( parsedArgs, k, v )
   local parsedArgs = {}
 
  local templateContents = string.match( content, '{{%s*' .. templateName .. '%s*|%s*(.-)%s*}}' )
 
   if templateContents then
    for prop in mw.text.gsplit( templateContents, '%s*|%s*' ) do
      local k, v = unpack( mw.text.split( prop, '%s*=%s*' ) )
      p.expandInto( parsedArgs, k, v )
    end
   end
   end



Latest revision as of 16:51, June 27, 2020

Documentation for this module may be created at Module:Args/doc

local p = {}

-- place the value in the right spot in the arg tree, determined by underscores in the key
function p.expandInto( args, key, value )
  local first, rest = string.match( key, '^(.-)_(.*)$' )
  local myKey = first or key
  myKey = tonumber( myKey ) or myKey

  if first then -- this is an internal node so insert children
    args[myKey] = type( args[myKey] ) == 'table' and args[myKey] -- already a table
      or args[myKey] and { __value = args[myKey] } -- move existing value into a new table
      or {} -- create new table
    p.expandInto( args[myKey], rest, value )
  else -- this is a leaf so set value
    if type( args[myKey] ) == 'table' then args[myKey].__value = value
    else args[myKey] = value
    end
  end
end

-- expand the arg tree for the given flattened args
function p.expand( args )
  local expandedArgs = {}
  for k, v in pairs( args ) do
    p.expandInto( expandedArgs, k, v )
  end

  return expandedArgs
end

-- coerce the given node into a table, in case it is a standalone value
function p.getTable( val )
  return type( val ) == 'table' and val or { __value = val }
end

-- get the value at the given node, in case the node contains a table
function p.getValue( val )
  return type( val ) == 'table' and val.__value or val
end

-- expand the arg tree from the given frame and its parent
function p.fromFrame( frame )
  local parsedArgs = {}

  for k, v in pairs( frame.args ) do
    p.expandInto( parsedArgs, k, v )
  end

  local parentFrame = frame:getParent()
  if parentFrame and parentFrame.args then
    for k, v in pairs (parentFrame.args ) do
      p.expandInto( parsedArgs, k, v )
    end
  end

  return parsedArgs
end

-- parse and expand the arg tree for the given template on the given page
function p.fromPageContent( content, templateName )
  templateName = templateName or 'Properties'
  local parsedArgs = {}

  local templateContents = string.match( content, '{{%s*' .. templateName .. '%s*|%s*(.-)%s*}}' )

  if templateContents then
    for prop in mw.text.gsplit( templateContents, '%s*|%s*' ) do
      local k, v = unpack( mw.text.split( prop, '%s*=%s*' ) )
      p.expandInto( parsedArgs, k, v )
    end
  end

  return parsedArgs
end

return p