Module:Sandbox/Fred Gandt/motd

local p = {}

local request = 'markup'

local data = mw.loadData( 'Module:Sandbox/Fred Gandt/motd/data' )

local function lastMotto local last = '' for index, value in pairs( data[ 'markup' ] ) do		last = value end return { last } end

local function indexesOf( haystack, needle ) needle = string.lower( needle ) local results = {} for index, value in pairs( haystack ) do		if string.lower( value ) == needle then results[ #results + 1 ] = index end end return results end

local function dupeless( t ) local t3 = {} local t4 = {} if #t ~= nil then for index, value in pairs( t ) do			t3[ value ] = true end end if #t3 ~= nil then for key, value in pairs( t3 ) do			t4[ #t4 + 1 ] = key end return t4	end return {} end

local function from( given, arg ) -- if given == 'text' then perform deep searches for similar text? local results = {} local indexes = indexesOf( data[ given ], arg ) for index, value in pairs( indexes ) do		results[ #results + 1 ] = data[ request ][ value ] end return results end

local function fromPartialDate( date_name_1, date_arg_1, date_name_2, date_arg_2 ) local results = {} local date_name_1_indexes = indexesOf( data[ date_name_1 ], date_arg_1 ) local date_name_2_indexes = indexesOf( data[ date_name_2 ], date_arg_2 ) for index_1, value_1 in pairs( date_name_1_indexes ) do		for index_2, value_2 in pairs( date_name_2_indexes ) do			if value_1 == value_2 then results[ #results + 1 ] = data[ request ][ value_2 ] end end end return dupeless( results ) end

local function fromDate( day, month, year ) local day_indexes = indexesOf( data[ 'day' ], day ) local month_indexes = indexesOf( data[ 'month' ], month ) local year_indexes = indexesOf( data[ 'year' ], year ) for d_index, d_value in pairs( day_indexes ) do		for m_index, m_value in pairs( month_indexes ) do			for y_index, y_value in pairs( year_indexes ) do				if d_value == m_value and m_value == y_value then return { data[ request ][ d_value ] } end end end end return {} end

local function _get( args ) request = args.request or request local results = {} if request ~= 'date' then if args.day and args.month and args.year then results = fromDate( args.day, args.month, args.year ) elseif args.day and args.month then results = fromPartialDate( 'day', args.day, 'month', args.month ) elseif args.day and args.year then results = fromPartialDate( 'day', args.day, 'year', args.year ) elseif args.month and args.year then results = fromPartialDate( 'month', args.month, 'year', args.year ) elseif args.day then results = from( 'day', args.day ) elseif args.month then results = from( 'month', args.month ) elseif args.year then results = from( 'year', args.year ) elseif args.text then results = from( 'text', args.text ) else results = lastMotto end elseif args.text then local index = indexesOf( data[ 'text' ], args.text )[ 1 ] results = { data[ 'month' ][ index ] .. ' ' .. data[ 'day' ][ index ] .. ', ' .. data[ 'year' ][ index ] } end if args.list or args.definition then local define = '' local types = { [ 'bullet' ] = { '*', '\n*' }, [ 'number' ] = { '#', '\n#' }, [ 'indent' ] = { ':', '\n:' } }		local metatypes = {} setmetatable( types, metatypes ) metatypes.__index = function( t, key ) return { '', key } end if args.definition then args.list = 'indent' define = ';' .. args.definition .. '\n' end return define .. types[ args.list ][ 1 ] .. table.concat( results, types[ args.list ][ 2 ] ) end return table.concat( results, ' ' ) end

function p.get( frame ) return _get( frame.args ) end

return p

--[[

local f = {} f.args = {}

then

f.args.request = 'day'

or

f.args.request = 'month'

or

f.args.request = 'year'

or

f.args.request = 'text'

or

f.args.request = 'markup'

or

f.args.request = 'date'

with

f.args.text = 'string to find'

and

f.args.day = '03'

or

f.args.month = '01'

or

f.args.year = '2015'

or

f.args.day = '03' f.args.month = '01' f.args.year = '2015'

or

f.args.text = 'foo foo foo'

and

f.args.list = 'bullet'

or

f.args.list = 'number'

or

f.args.list = 'indent'

with optional

f.args.definition = 'This is a list'

then

=p.get( f )

like

local f = {} f.args = {} f.args.request = 'markup' f.args.year = '2015' f.args.list = 'indent' f.args.definition = 'List of mottos' =p.get( f )

]]