มอดูล:FreedImg
หน้าตา
เอกสารการใช้งานสำหรับมอดูลนี้อาจสร้างขึ้นที่ มอดูล:FreedImg/doc
-- This is an module to implement FreedImg
-- Argument to the functions are as described on [[Template:FI]] and [[Template:FIS]].
--
-- 2021-01-13: Implementation copying original FI and FIS templates, but with
-- added max-width handing to reduce image sizes for very large images
local p = {} --p stands for package
-- this is a limit on the max default image size to avoid multi-MB full-size images
-- being loaded. if the image is smaller than this, it is loaded at full resolution
local max_image_size = 1000
-- this is a nominal "max" screen size used when computing the maximum size of an
-- image with a width in percent. For example, on a 2048px screen, an image at
-- 10% will be 204px at most, so there's no point loading a 1000px image.
local max_screen_size = 2048
function arg_or_nil(args, name)
if args[name] ~= nil and args[name] ~= "" then
return args[name]
end
return nil
end
-- add to a table if the variable is not nil or empty
function add_if(t, key, var)
if var ~= nil and var ~= "" then
t[key] = var
end
end
-- append a CSS style to a table so we can use mw.html:css on it
function add_style_to_table(t, s)
if s == nil or s == "" then
return
end
for rule in string.gmatch(s, "([^;]+)") do
local idx, _
idx, _ = string.find(rule, ":")
if idx then
t[string.sub(rule, 0, idx - 1)] = string.sub(rule, idx + 1)
end
end
end
function construct_image_markup(img_name, args)
-- get the image info
-- NOTE: this is an expensive function
local img_title = mw.title.makeTitle("File", img_name)
local img_width_px
if arg_or_nil(args, "imgwidth") then
-- the user told us what they want
img_width_px = string.gsub(args['imgwidth'], "px", "")
elseif arg_or_nil(args, "width") then
-- if the width parameter is in px or %, use that to get the image as the
-- image is at most the size of the container
local arg_px_width = string.match(args["width"], '(%d+)px$')
local arg_pc_width = string.match(args["width"], '(%d+)%%$')
if arg_px_width then
-- use what the parameter said
img_width_px = arg_px_width
elseif arg_pc_width then
-- use a nominal huge screen and find the image size upper bound
img_width_px = math.floor((max_screen_size * tonumber(arg_pc_width)) / 100)
-- still limit to the max size as well
img_width_px = math.min(max_image_size, img_width_px)
end
end
if img_width_px == nil then
-- use the default size, or the image size, whichever is smaller
img_width_px = math.min(img_title.file["width"], max_image_size)
end
-- construct the image markup we will use
local img_markup = "[[File:" .. img_name .. "|" .. img_width_px .. "px"
if arg_or_nil(args, "alt") then
img_markup = img_markup .. "|alt=" .. args["alt"]
end
if arg_or_nil(args, "link") then
img_markup = img_markup .. "|link=" .. args["link"]
end
if arg_or_nil(args, "page") then
img_markup = img_markup .. "|page=" .. args["page"]
end
img_markup = img_markup .. "|class=freedImg"
if args["imgclass"] then
img_markup = img_markup .. " " .. args["imgclass"]
end
img_markup = img_markup .. "]]"
return img_markup
end
function construct_caption(parent, is_div, args)
local tag = is_div and "p" or "span"
local pstyle = {}
add_if(pstyle, "display", args["tdisplay"])
add_if(pstyle, "text-align", args["talign"])
add_style_to_table(pstyle, arg_or_nil(args, "tstyle"))
local sstyle = {}
add_if(sstyle, "margin-right", args["tmright"])
add_if(sstyle, "margin-left", args["tmleft"])
add_if(sstyle, "display", args["sdisplay"])
add_if(sstyle, "text-indent", args["indent"])
local para = parent:tag(tag)
para:css(pstyle):addClass("imgCaption")
local span = para:tag("span")
span:css(sstyle):wikitext(args["caption"])
return span
end
function freedImg(is_div, args)
local img_markup
-- construct the image markup we will use
if args['type'] == "math" or args['type'] == "score" or args['type'] == "user" then
-- math, score and user just place the markup directly
img_markup = args["file"]
else
img_markup = construct_image_markup(args["file"], args)
end
local outer_tag = is_div and "div" or "span"
local caption_tag = is_div and "p" or "span"
local outer_div_class = {"freedImgOuter"}
if arg_or_nil(args, "cclass") then
table.insert(outer_div_class, args["cclass"])
end
local outer_div_style = {}
add_if(outer_div_style, "width", args["width"])
add_if(outer_div_style, "margin-right", args["margin-right"])
add_if(outer_div_style, "margin-left", args["margin-left"])
add_if(outer_div_style, "float", args["float"])
add_if(outer_div_style, "clear", args["clear"])
if not is_div then
outer_div_style["display"] = "inline-block"
end
add_style_to_table(outer_div_style, args["cstyle"])
outer = mw.html.create(outer_tag)
outer:addClass(table.concat(outer_div_class, " "))
:css(outer_div_style)
local inner_style = {}
inner_style["width"] = "100%"
local inner = outer:tag(outer_tag)
inner:addClass("freedImg"):css(inner_style)
if is_div then
local pstyle = {}
add_style_to_table(pstyle, args["imgstyle"])
local para = inner:tag("p")
para:addClass("freedImg")
:css(pstyle)
:wikitext(img_markup)
else
inner:css("display", "block")
inner:wikitext(img_markup)
end
-- add the caption, if there is one, to the outer div
if arg_or_nil(args, 'caption') then
construct_caption(outer, is_div, args)
end
return tostring(outer)
end -- function freedimg
--[=[
Construct a "block" FreedImg
]=]
function p.freedImg(frame)
return freedImg(true, frame:getParent().args)
end
--[=[
Construct an "inline" FreedImg
]=]
function p.freedImg_span(frame)
return freedImg(false, frame:getParent().args)
end
return p