Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Keyword: Difference between revisions

From EmissaryFGC
dustloop>TarkusLee
No edit summary
m 25 revisions imported: Credit to the Dustloop wiki (dustloop.com).
 
(19 intermediate revisions by one other user not shown)
Line 4: Line 4:
local cache = {}
local cache = {}


local splitString = require( 'Module:SplitStringToTable' ).splitStringIntoTable


--- Return the Moves data and cache the data for reuse on the same page
--- Return the Moves data and cache the data for reuse on the same page
Line 17: Line 18:
     local tables = 'Glossary'
     local tables = 'Glossary'
     local fields = 'term,definition,gameSpecifier'
     local fields = 'term,definition,gameSpecifier'
    local args = {}
   
     if gameSpecifier then
     if gameSpecifier then
     local args = {
     -- mw.log(gameSpecifier)
     where  = string.format('Glossary.gameSpecifier="%s"', gameSpecifier),
    args = {
        orderBy = 'Glossary._rowID',
     where  = string.format('Glossary.gameSpecifier HOLDS "%s"', gameSpecifier),
        limit = "300",
    orderBy = 'Glossary._rowID',
    }
    limit   = '300',
}
     else
     else
     local args = {
     args = {
        orderBy = 'Glossary._rowID',
    orderBy = 'Glossary._rowID',
        limit = "300",
    limit   = '300',
     }
     }
     end
     end
     local results = cargo.getQueryResults( tables, fields, args, false )
     local results = cargo.getQueryResults( tables, fields, args, false )


     local items = {}
     local items = {}
     for _, result in pairs( results ) do
     for _, result in pairs( results ) do
         items[result.input] = result
         items[ string.lower(result.term) ] = result
     end
     end


     -- Save to cache
     -- Save to cache
     cache = items
     cache = items
    -- mw.logObject(cache)


     return cache
     return cache
Line 47: Line 49:
     local data = getGlossaryData( gameSpecifier )
     local data = getGlossaryData( gameSpecifier )


local terms = {}
local match = data[ string.lower(term) ]
for _, key in pairs( inputs ) do
if not match then
local match = data[ key ]
        error( string.format( 'Could not find matching term: %s', term ) )
if not match then
        error( string.format( 'Could not find matching term: %s', term ) )
end
terms[ key ] = data[ term ]
end
end


     return terms
     return match
end
end


local function buildTooltip(term)
local function buildTooltip(term, label)
-- mw.logObject(term)
local tooltipHTML = mw.html.create('span'):addClass('tooltip')
local tooltipHTML = mw.html.create('span'):addClass('tooltip')
:wikitext(term['term'])
tooltipHTML:wikitext(label)
:tag('span'):addClass('tooltiptext')
:tag('span'):addClass('tooltiptext')
:wikitext(term['definition '])
:wikitext(term['definition'])
:done()
:done()
:done()
:done()
Line 81: Line 81:
function p._main( args )
function p._main( args )
      
      
     local term = args['term']
     local term = args['term'] or args[1]
    local gameSpecifier = args['gameSpecifier']
   
     if not term then
     if not term then
        error( 'No term specified for the template' )
        error( 'No term specified for the template' )
     end
     end
   
    local gameSpecifier = args['gameSpecifier']
    local label = args['label'] or args[1] or args['term']


local term = getTerm( chara, gameSpecifier )
local term = getTerm( term, gameSpecifier )
local html = buildTooltip(term)
local html = buildTooltip(term, label)
 
     return tostring(html)
     return tostring(html)
end
end


return p
return p

Latest revision as of 21:52, 7 October 2024

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

local p = {}
local mArguments
local cargo
local cache = {}

local splitString = require( 'Module:SplitStringToTable' ).splitStringIntoTable

--- Return the Moves data and cache the data for reuse on the same page
---
--- @return table
local function getGlossaryData( gameSpecifier )
    -- Return cached data
    if #cache > 0 then return cache end

    -- Lazy initalize Module:Cargo
    cargo = require( 'Module:Cargo' )

    local tables = 'Glossary'
    local fields = 'term,definition,gameSpecifier'
    local args = {}
    
    if gameSpecifier then
    	-- mw.log(gameSpecifier)
    	args = {
    		where   = string.format('Glossary.gameSpecifier HOLDS "%s"', gameSpecifier),
    		orderBy = 'Glossary._rowID',
    		limit   = '300',
		}
    else
    	args = {
    		orderBy = 'Glossary._rowID',
    		limit   = '300',
    	}
    end
    local results = cargo.getQueryResults( tables, fields, args, false )

    local items = {}
    for _, result in pairs( results ) do
        items[ string.lower(result.term) ] = result
    end

    -- Save to cache
    cache = items

    return cache
end

local function getTerm( term, gameSpecifier )
    local data = getGlossaryData( gameSpecifier )

	local match = data[ string.lower(term) ]
	if not match then
        error( string.format( 'Could not find matching term: %s', term ) )
	end

    return match
end

local function buildTooltip(term, label)
	-- mw.logObject(term)
	
	local tooltipHTML = mw.html.create('span'):addClass('tooltip')
		tooltipHTML:wikitext(label)
		:tag('span'):addClass('tooltiptext')
			:wikitext(term['definition'])
		:done()
	:done()
	
	return tooltipHTML
end

function p.main(frame)
	mArguments = require( 'Module:Arguments' )
	local args = mArguments.getArgs(frame)
	return p._main(args)
end

--- Return the wikitext needed for the template
---
--- @return string
function p._main( args )
    
    local term = args['term'] or args[1]
    if not term then
        error( 'No term specified for the template' )
    end
    
    local gameSpecifier = args['gameSpecifier']
    local label = args['label'] or args[1] or args['term']

	local term = getTerm( term, gameSpecifier )
	local html = buildTooltip(term, label)
    return tostring(html)
end

return p
MediaWiki Appliance - Powered by TurnKey Linux