Module:Pseudo image

MyWikiBiz, Author Your Legacy — Monday July 08, 2024
Jump to navigationJump to search

Template:Module rating This module is supposed to take all the normal File:/Image: parameters from the call to a parent template (though it will might use its own args in preference for debug purposes) and return them back one by one for use by a template. Most important, it figures out by a process of elimination which of these is the caption.

Usage

This is designed to be used with a template like Template:Pseudo image which receives the parameters that used to go to a File: or Image: thumbnail. For example,

[[File:Abortionmethods.png|thumb|right|220px|Example: chart of abortion times]]

is replaced in the template by

{{Pseudo image|thumb|right|220px|link=:File:Abortionmethods.png|Example: chart of abortion times|image={{#invoke:Block diagram|main|''a bunch of stuff to draw the diagram as divs''}}}}

The current division of labor in the process is that the Template:Pseudo image accepts this call, and outputs the graphics, while this module is limited to sorting out the parameters. Therefore, calls to the module itself accept an extra parameter query=(field name) and return the desired field for use within the template. The field names presently supported are:

  • thumb - returns the value "yes" if there a |thumb| or |thumbnail| is given.
  • frame - returns "yes" if there is a |frame|
  • px - returns the horizontal pixel size ### if there is a |###px| or |###x##px|
  • xpx - returns the vertical pixel size ### if there is a |##x###px| or |x###px|
  • float - returns left, right, center, none if any one of these is present
  • vertical - returns baseline,middle,sub,super,text-top,text-bottom,top, or bottom if any of these is present
  • caption - returns the first field that isn't any one of the preceding. Note: as a convenience, it also ignores blank fields and picks a later nonblank field instead, though that shouldn't happen anyway.
  • If two different values are given for the same field (Pseudo image|thumb|left|right|center|, whichever comes later as they're listed above should dominate, but let's call it "undefined", i.e. don't do this. ;)
  • Note that this module ignores fields like alt=, link= etc. for determining the caption because they are not unlabeled fields, but should actually return the values if queried anyway. But there's no obvious reason not to use those as simply {{{alt|}}},{{{link|}}} etc. without #invoking anyway.

--- This module is supposed to take all the normal File:/Image: parameters from the call
--- to a parent template (though it will might use its own args in preference for debug purposes)
--- and return them back one by one for use by a template.  Most important, figure which is the caption.
--- (This is a bit experimental - I haven't really ''used'' the parent .args much, 
--- and there's a chance the whole template will just get wrapped up into one Lua script in the end)
--- usage: {{#invoke Pseudo-image:main|thumb}}, etc.

local p={}

function initialize(frame)
    local parent=frame:getParent() or {}
    local args=frame.args or {}
    local pargs=parent.args or {}
    return args,pargs
end

function p.main(frame,query)
    local args,pargs=initialize(frame)
    local query=query or args.query or pargs.query -- this leaves the door open for specifying query as a function
    local showdebug=args.debug or pargs.debug -- wasn't shutting off under name "debug"...
    local thumb,frame,px,float,border,vertical
    local debuglog=#pargs or "no#pargs"
    local debuglog=debuglog.."q="..tostring(query)
    local floatoptions = {left=true,right=true,center=true,none=true}
    local verticaloptions = {baseline=true,middle=true,sub=true,super=true,['text-top']=true,['text-bottom']=true,top=true,bottom=true}
    local default = {float='right',vertical='middle'}
    local output={}
    debuglog=debuglog..tostring(default['float'])
    for i, parm in ipairs(pargs) do
            debuglog=debuglog..i..tostring(pargs[i])
            local parm=pargs[i] or ""
            parm=mw.ustring.match(parm,"^%s*(%S.*)$") or parm -- strip leading space (not sure if there can be any)
            parm=mw.ustring.match(parm,"^(.*%S)%s*") or parm -- strip lagging space (" " ")
            if parm == "thumb" or parm == "thumbnail" then
                output.thumb = "yes"
            elseif parm == "frame" then
                output.frame = true
            elseif floatoptions[parm] then
                output.float = parm
            elseif parm == "border" then
                output.border = true -- technically this may need fine-tuning - does thumb with border make "border" the caption?
            elseif verticaloptions[parm] then
                output.vertical = parm
            elseif mw.ustring.match(parm,"^(%d+)px$") then
                output.px = mw.ustring.match(parm,"(%d+)px") -- there must be a better way to do this two-liner
            elseif mw.ustring.match(parm,"^(%d*)x(%d+)px$") then
                output.px,output.xpx = mw.ustring.match(parm,"^(%d*)x(%d+)px$") -- " "
            else output.caption = output.caption or parm
                 if output.caption == "" then output.caption = nil end -- for now I choose to auto-rescue if the caption isn't the first unidentified misc parameter
            end -- the mess of cases
    end -- for i,parms in ipairs(pargs)
    for k,v in pairs(pargs) do
        output[k]=v
    end
    for k,v in pairs(args) do --- note these allow the pseudo-parameters to be overridden in the Lua call
        output[k]=v
    end
    if showdebug then return debuglog..tostring(default[query])..tostring(output[query]) else return output[query] or default[query] end
end

return p