モジュール:サンドボックス/JuthaDDA/LuaTest
表示
モジュールの解説[作成]
local p = {}
local args = mw.getCurrentFrame():getParent().args
function p.main ()
local div = mw.html.create('div')
div:addClass('columns-list__wrapper')
if args["plainlist"] == "1" then
div:addClass("plainlist")
end
if args["small"] == "1" then
div:addClass("columns-list__wrapper--small-font")
end
div = p.set_styles(div)
div:wikitext(p.get_content())
return tostring(div)
end
function p.set_styles(elm)
local column_width = p.get_column_width()
if column_width then
elm:cssText(p.add_vender_prefix_to("column-width", column_width))
else
local column_count = p.get_column_count()
if column_count then
elm:cssText(p.add_vender_prefix_to("column-count", column_count))
end
end
if args["rules"] then
elm:cssText(p.add_vender_prefix_to("column-rule", args["rules"]))
end
if args["gap"] then
elm:cssText(p.add_vender_prefix_to("column-gap", args["gap"]))
end
local arg_style = -- style引数から引用符を除去
args["style"] and string.gsub(args["style"], "\"", "")
if arg_style then
elm:cssText(arg_style)
else -- {{columns-list|2;width:50%;}}のような指定に対応
local css_params_in_arg1 = args[1] and p.get_css_params_from(args[1])
if css_params_in_arg1 then
elm:cssText(css_params_in_arg1)
end
end
return elm
end
function p.get_column_width()
local colwidth = args["colwidth"] or args["col-width"] or args["column-width"]
if colwidth then
return colwidth
end
local arg1 = args[1] and p.remove_css_params_from(args[1])
if tonumber(arg1) then
return nil
end
return arg1:gsub("[\n\r]","")
end
function p.get_column_count()
local arg1 = args[1] and p.remove_css_params_from(args[1])
if tonumber(arg1) then
return arg1:gsub("[\n\r]","")
end
return nil
end
function p.get_content()
if args["content"] then
local wikitext = string.gsub(args["content"],"^(%*)", "\n*")
return wikitext
end
if args[2] then
return args[2]
end
if args[1] then
return args[1]
end
return ""
end
-- {{columns-list|2;width:50%;}}のような指定に対応
function p.remove_css_params_from(str)
return string.gsub(str, ";.*", "")
end
-- {{columns-list|2;width:50%;}}のような指定に対応
function p.get_css_params_from(str)
local col_count = p.remove_css_params_from(str)
if not tonumber(col_count) then
return nil
end
local has_params = str and string.find(str, ";");
if not has_params then
return nil
end
return string.gsub(str, "^%d-;", "")
end
function p.add_vender_prefix_to(property, value)
local webkit = "-webkit-" .. property .. ": " .. value .. "; "
local moz = "-moz-" .. property .. ": " .. value .. "; "
return webkit .. moz .. property .. ": " .. value .. ";"
end
function p.has_no_column_width()
if args["colwidth"] or args["col-width"] or args["column-width"] then
return nil
end
if not tonumber(args[1] and p.remove_css_params_from(args[1])) then
return nil
end
return true
end
function p.has_css_in_arg1()
return args[1] and p.get_css_params_from(args[1]) and true or nil
end
return p