모듈:Style

삼쩌모 (토론 | 기여)님의 2025년 11월 29일 (토) 17:11 판

이 모듈에 대한 설명문서는 모듈:Style/설명문서에서 만들 수 있습니다

-- Module:스타일
local p = {}

-- 단일 속성 매핑
local mapSingle = {
	c = "color",
	bg  = "background",
	bgc = "background-color",
	fs  = "font-size",
	fw  = "font-weight",
	ff  = "font-family",
	lh  = "line-height",
	ls  = "letter-spacing",
	ta  = "text-align",

	w   = "width",
	h   = "height",
	maxw = "max-width",
	minw = "min-width",

	p   = "padding",
	pt  = "padding-top",
	pr  = "padding-right",
	pb  = "padding-bottom",
	pl  = "padding-left",

	m   = "margin",
	mt  = "margin-top",
	mr  = "margin-right",
	mb  = "margin-bottom",
	ml  = "margin-left",

	ovf = "overflow",
	ovfx = "overflow-x",
	ovfy = "overflow-y",
	dsp = "display",
	pos = "position",
	zi  = "z-index",
}

-- border 방향 매핑
local borderMap = {
	bd  = "border",
	bdt = "border-top",
	bdr = "border-right",
	bdb = "border-bottom",
	bdl = "border-left",
}

-- border 스타일 축약 변환
local borderTypeMap = {
	sld = "solid",
	dsh = "dashed",
	dot = "dotted",
	db  = "double",
	grv = "groove",
	rdg = "ridge",
	ins = "inset",
	ous = "outset",
}

local function addStyle(list, name, value)
	if not name or name == "" then return end
	if not value or value == "" then return end
	table.insert(list, name .. ":" .. value .. ";")
end

local function buildBorder(token, cssName, styles)
	-- token 예: "bd-1px-sld-#000"
	local parts = mw.text.split(token, "-")

	-- parts[1] = "bd" 혹은 "bdt" 등. 이미 처리됨.
	table.remove(parts, 1)

	local result = {}

	for _, part in ipairs(parts) do
		if borderTypeMap[part] then
			table.insert(result, borderTypeMap[part])
		else
			table.insert(result, part)
		end
	end

	addStyle(styles, cssName, table.concat(result, " "))
end

function p.build(frame)
	local args = frame.args
	if frame:getParent() then
		for k, v in pairs(frame:getParent().args) do
			if (args[k] == nil or args[k] == "") then
				args[k] = v
			end
		end
	end

	local spec = args[1] or ""
	spec = mw.text.trim(spec)
	if spec == "" then return "" end

	local styles = {}

	for _, token in ipairs(mw.text.split(spec, "%s+")) do
		token = mw.text.trim(token)
		if token ~= "" then
			local dashPos = token:find("-")
			if dashPos then
				local key = token:sub(1, dashPos - 1)
				local val = token:sub(dashPos + 1)

				-- padding/margin 축약(px, py, mx, my)
				if key == "px" then
					addStyle(styles, "padding-left",  val)
					addStyle(styles, "padding-right", val)
				elseif key == "py" then
					addStyle(styles, "padding-top",    val)
					addStyle(styles, "padding-bottom", val)
				elseif key == "mx" then
					addStyle(styles, "margin-left",  val)
					addStyle(styles, "margin-right", val)
				elseif key == "my" then
					addStyle(styles, "margin-top",    val)
					addStyle(styles, "margin-bottom", val)

				-- border 처리
				elseif borderMap[key] then
					buildBorder(token, borderMap[key], styles)

				else
					-- 일반 단일 속성 매핑
					local cssName = mapSingle[key]
					if cssName then
						addStyle(styles, cssName, val)
					end
				end
			end
		end
	end

	return table.concat(styles, " ")
end

return p