Module:Utils

-- module Utils --  copied/stolen from where-noted --  tested/checked in testcases/here

local p = {}

-- note: getArgs can have options to choose-parent, backtranslate, etc... local getargs = require("Dev:Arguments").getArgs

--local noglobals = require("Dev:No globals") local inspect = require("Dev:Inspect") local tabletools = require("Dev:TableTools") local utility = require("Dev:Utility")

-- local functions local function pc( fcn, args ) -- protected-calls always return true/false first.. if args == nil then return 'missing args for pcall. \n' end local ok, result = pcall( fcn, args ) if ok then return result else return 'ERROR...msg=' .. result .. '\n' end end

-- see:http://www.luafaq.org/#T1.15 local function dump(o) if type(o) == 'table' then local s = '{ ' for k,v in pairs(o) do               if type(k) ~= 'number' then k = '"'..k..'"' end s = s .. '['..k..'] = ' .. dump(v) .. ','       end return s .. '} '   else return tostring(o) end end -- want to modify print-chars by using opts-table w/named-records... local function dump2(o,opts) local defOpts defOpts = { bTab = '{ ', eTab = ' }', bKey = '[', eKey = ']', bStr = '"', eStr ='"', toVal = '==>', nPair = '; ', saySize = false, } if type(opts)=='table' then tabletools.merge(defOpts,opts) end --   if type(opts)=='table' then tabletools.merge(opts,defOpts) end if type(o) == 'table' then local s = '' if defOpts.saySize then s = 'Table has size (rows) = '..tabletools.size(o) end s = s.. defOpts.bTab for k,v in pairs(o) do               if type(k) ~= 'number' then k = defOpts.bStr..k..defOpts.eStr end s = s ..defOpts.bKey..k..defOpts.eKey s = s .. defOpts.toVal .. dump2(v,opts) ..defOpts.nPair end return s .. defOpts.eTab else return tostring(o) end end

local function _hjfdump(o) --    { ["newTemplateParserValue"] = function,["getParent"] = function,["preprocess"] = function,["getTitle"] = function,["args"] = { [1] = 'hello, world',} ,["newParserValue"] = function,["argumentPairs"] = function,["getArgument"] = function,["expandTemplate"] = function,} if type(o) == 'table' then local s = '{ ' for k,v in pairs(o) do               if type(k) ~= 'number' then k = '"'..k..'"' end s = s .. '['..k..'] = ' .. _hjfdump(v) .. ','       end return s .. '} '   else return tostring(o) end end local function _hjfprint(o) local res = '' local clone = {} res = res .. 'hjfprint says type=' .. type(o) .. '\n' res = res .. 'hjfprint says data=' .. tostring(o) .. '\n' if type(o) == 'table' then --       res = res .. dump(o) local dumpclone = {} res = res .. 'hjfprint says cloning...\n' clone = mw.clone(o) res = res .. 'hjfprint says dumping-clone-args...\n' dumpclone = dump(clone) --       res = res .. tostring(getargs(dumpclone)) .. '\n' res = res .. dumpclone .. '\n' end return res end

local function _prtTable(t) local tbl = inspect(t) local tbl2 = utility.table_print(t) local nam, num --   local msg = ';PrtTable' local msg = 'PrtTable' --[==[ if (type(nT)=='string') and (#nT>0) then nam,num = string.gsub(nT,"%W",'.') if num > 0 then msg = msg .. ':Bad-name='.. nam .. ':'       else msg = msg .. '(' .. nam .. ') is ' end else msg = msg .. ':'   end --]==]   msg = msg .. ':'   msg = msg .. ' inspected\n' .. tbl .. '\n' msg = msg .. ' utility\n' .. tbl2 .. '\n' return msg end

function p.hjfprint(o) return _hjfprint(p.getargs(o)) end function p.prtTable(t) return _prtTable(p.getargs(t)) end

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- frame -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- function p.getargs(frame) -- fucking ARGUMENTS !!! local a   local o, options o = { translate = {}, --backtranslate = nil, wrappers = {}, -- option-wrappers inside options possibilities... frameOnly = false, parentOnly = false, parentFirst = false, valueFunc = function return false end, trim = false, removeBlanks = false, readOnly = false, noOverwrite = false, }   -- options = o    -- a = getargs(frame,options) a = getargs(frame) local msg = '' -- a is a TABLE, which has the arguments inside it...   -- the args-table-elements are only strings or numbers afaict... msg = msg..'a==>'..dump(a) return msg end

return p