Module:Cbox: Difference between revisions

From The Satanic Wiki
Jump to navigation Jump to search
Mediawiki>Magiczocker01
m (Small optimizations.)
 
No edit summary
 
(One intermediate revision by the same user not shown)
Line 2: Line 2:
local cbox
local cbox
local styles,css,args = {},{},{}
local styles,css,args = {},{},{}
local lomod = require('Dev:Cbox/layout')
local lomod = require('Module:Cbox/layout')
local chrome = require('Dev:Colors')
local chrome = require('Module:Colors')
local getArgs = require('Dev:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs
local notmeta_checks = {
local notmeta_checks = {
   above = true,
   above = true,

Latest revision as of 05:30, 2 May 2021

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

local p = {}
local cbox
local styles,css,args = {},{},{}
local lomod = require('Module:Cbox/layout')
local chrome = require('Module:Colors')
local getArgs = require('Module:Arguments').getArgs
local notmeta_checks = {
  above = true,
  below = true,
  class = true,
  data = true,
  fontsize = true,
  header = true,
  image = true,
  imagelink = true,
  imagesize = true,
  layout = true,
  name = true,
  quote = true,
  reason = true,
  reasontext = true,
  text = true,
  timestamp = true,
  towhere = true,
}
local main_checks = {
  above = true,
  below = true,
  data = true,
  header = true,
  image = true,
  imagelink = true,
  name = true,
  quote = true,
  reason = true,
  reasontext = true,
  text = true,
  timestamp = true,
  towhere = true,
}
local test_checks = main_checks

local function doublekey(str,v)
    local k,ka = 1,{}
    for n in mw.ustring.gmatch(str,'[^#]+') do
        ka[k] = n
        k=k+1
    end
    if not styles[ka[2] or 'cbox'] then styles[ka[2] or 'cbox'] = {} end
    styles[ka[2] or 'cbox'][ka[1]] = v
    args[str] = nil
end
local function multikey(loc,str,v)
    if type(str)=="table" then
        styles[loc]=styles[loc] or {}
        for i=1,#str do
            styles[loc][str[i]] = v
        end
    end
end
local function adddata(moto)
    local text={args.text}
    if tostring(moto)=="table" then
        text[#text+1] ='entering is:table<br/>'
        for k,v in pairs(moto) do
            text[#text+1] =tostring(k)..':『'..tostring(v)..'』<br/>'
        end
    else
        text[#text+1] ='value:『'..tostring(moto)..'』<br/>'
    end
    args.text = table.concat(text, "")
end
 
local function notmeta(k,v)
    if v=='' or v==nil then
        return false
    elseif mw.ustring.find(k,'#') then
        return true
    elseif notmeta_checks[k] then
        return false
    end
    return true
end
local function autotext(size)
    if size then
    local o = size..'pt'
    local h = tostring(tonumber(size)+2)..'pt'
    styles['cbox'] = styles['cbox'] or {}
    styles['cbox']['font-size'] = o
    styles['header'] = styles['header'] or {}
    styles['header']['font-size'] = h
    end
end
local function colorfix(header)
    if styles[header] then
        local c,bc = styles[header]['color'],styles[header]['background-color']
        if c and not bc then
            styles[header]['background-color'] = chrome.text({['args']={c}})
        elseif bc and not c then
            styles[header].color = chrome.text({['args']={bc}})
        end
    end
end
local function quickcreate(element,class)
    local cell = mw.html.create(element)
    cell:addClass(class)
    if styles[class] then
        cell:css(styles[class])
    end
    if css[class] then
        cell:cssText(css[class])
    end
    return cell
end
 
function p.maketest(frame)
    multikey('cbox',{"border-radius","-moz-border-radius","-webkit-border-radius","-khtml-border-radius","-icab-border-radius","-o-border-radius"},"30px")
    cbox=quickcreate('table','cbox')
    lomod.layout1()
    return tostring(cbox)
end
function p.test(frame)
    adddata(frame)
    local moto = getArgs(frame,{
        valueFunc = function(key,value)
            if not value then
                return nil
            end
            if type(key)=="number" then return value end
            local k = mw.ustring.lower(key)
            if not test_checks[k] then
                value = mw.ustring.lower(value)
            end
            if mw.ustring.find(value,'%S') then
                return value
            end
            return nil
        end
    })
    for k,v in pairs(moto) do
        if type(k)=="string" then
            key = mw.ustring.lower(k)
        else
            key = tostring(k)
        end
        if not args[key] then
            args[key] = v
        end
    end
    adddata(args)
 
    if args.round and tonumber(args.round)>0 then
        multikey('cbox',{"border-radius","-moz-border-radius","-webkit-border-radius","-khtml-border-radius","-icab-border-radius","-o-border-radius"},args.round .. 'px')
    end
    css['cbox'] = args.style
    args.style = ''
    css['header'] = args.headerstyle
    args.headerstyle = ''
    css['description'] = args.textstyle
    args.textstyle = ''
    css['reason'] = args.reasonstyle
    args.reasonstyle = ''
    css['image'] = args.imagestyle
    args.imagestyle = ''
    autotext(args.fontsize)
    args.fontsize = ''
 
    -- Loop over all the args
    colorfix('cbox')
    colorfix('header')
    colorfix('reason')
 
    cbox = quickcreate('table','cbox')
    cbox:addClass(args['class'] or '')
 
    lomod.layout1()
 
    return tostring(cbox)
end
function p.main(frame)
    local moto = getArgs(frame,{
        valueFunc = function(key,value)
            if not value then
                return nil
            end
            if type(key)=='number' then
                return value
            end

            local k = mw.ustring.lower(key)
            if not main_checks[k] then
                value = mw.ustring.lower(value)
            end
            if mw.ustring.find(value,'%S') then
                return value
            end
            return nil
        end
    })
    for k,v in pairs(moto) do
        if type(k)=="string" then
            key = mw.ustring.lower(k)
        else
            key = tostring(k)
        end
        if not args[key] then
            args[key] = v
        end
    end
 
    if args.round and tonumber(args.round)>0 then
        multikey('cbox',{"border-radius","-moz-border-radius","-webkit-border-radius","-khtml-border-radius","-icab-border-radius","-o-border-radius"},args.round .. 'px')
    end
    css['cbox'] = args.style
    args['style'] = ''
    css['header'] = args.headerstyle
    args.headerstyle = ''
    css['description'] = args.textstyle
    args.textstyle = ''
    css['reason'] = args.reasonstyle
    args.reasonstyle = ''
    css['image'] = args.imagestyle
    args.imagestyle = ''
    autotext(args.fontsize)
    args.fontsize = ''
 
    -- Loop over all the args
    for k,v in pairs(args) do
        -- 先に↑を消すことで飛ばすようになる
        if notmeta(k,v) then
            doublekey(k,v) -- Simple copy
        end
    end
    colorfix('cbox')
    colorfix('header')
    colorfix('reason')
 
    cbox = quickcreate('table','cbox')
    cbox:addClass(args['class'] or '')
 
    local layout = tonumber(args.layout) or 0
    local func = lomod["layout" .. layout]
    if func and layout > 1 and layout < 7 then
        cbox = func(cbox,styles,css,args)
    end
 
    return tostring(cbox)
end
return p