Module:Stilizatoe

Èn årtike di Wikisource.

La documentation pour ce module peut être créée à Module:Stilizatoe/doc

local p = {}

-- to place in a module 'table' ?
function in_table (val, tab)
    for index, value in pairs(tab) do
        if value == val then
            return true
        end
    end

    return false
end

function unit (val, unit)
    if unit == nil then unit = 'em' end
    if tonumber(val) ~=nil then
    	val = val .. unit
    end
    return val
end

function openclose (str, openTag, closeTag)
	
    if mw.title.getCurrentTitle().namespace == 250 then
    	return str
    end
    
    if type(str) ~= "string" then
    	str = tostring(str)
    end
    
	if not openTag then
		local i = string.find( str, '>', 1, true )
		if i ~= nil then
			str = string.sub( str, i+1 )
		end
	end
	if not closeTag then
		local i = string.find( string.reverse(str), '/<', 0, true )
		if i ~= nil then
			str = string.sub( str, 1, -i-2 )
		end
	end
	
    return str
end

function p.init(frame)
	local args = frame.args
	local permelems = {"div", "p", "span", "h2", "h3", "h4", "h5", "h6"} -- not h1 (page title)
	local elem = 'div'
	local class = 'alignmint'
	local openTag = true
	local closeTag = true
	local isprose = false;
	
	if args.elem ~= nil and args.elem ~= "" then 
		if in_table(args.elem, permelems) then
			elem = args.elem
		else
			return "L’ elemint " ..args.elem.. " n’ est nén rkinoxhou."
		end
	end
	
	if args[1] == nil or args[1] == "" then
		return "I gn a pont d’ contnou a stitchî dvins l’ elemint."
	end
	
	if args['open'] ~= nil and args['open'] == "no" then
		openTag = false
	end
	
	if args['close'] ~= nil and args['close'] == "no" then
		closeTag = false
	end
	
	if args['class'] ~= nil and args['class'] ~= "" then
		class = args['class']
	end
	
	html = mw.html.create(elem)
	html:wikitext(mw.text.trim(args[1]))
	html:attr('class', class) -- vey Mediawiki:Common.css
	
	-- LANGUAGE
	-- FIXME we should test if language is a correct value
	if args.lang ~= nil and args.lang ~= "" then
		html:attr("lang", args.lang)
	end
	
	-- FONT-SIZE
	-- FIXME we should test unit (%,em,px,...) or value (smaller,...)
	if args['font-size'] ~= nil and args['font-size'] ~= "" then
		local fs_unit = 'px'
		if class == 'tite' then fs_unit = '%' end
		html:css("font-size", unit(args['font-size'], fs_unit))
	end
	
	-- FONT-STYLE
	if args['font-style'] ~= nil and args['font-style'] ~= "" then
		local permfst = {i="italic"}
		local fst = args['font-style']
		if permfst[args['font-style']] ~= nil then 
			fst = permfst[args['font-style']]
		elseif not in_table( fst, permfst ) then
			return "L’ atribut font-style ni rkinoxhe nén l’ valixhance "..fst.."."
		end
		html:css("font-style", fst)
	end
	
	-- TEXT-DECORATION 
	if args['text-decoration'] ~= nil and args['text-decoration'] ~= "" then
		local permfd = {u="underline"}
		local fd = args['text-decoration']
		if permfd[args['text-decoration']] ~= nil then 
			fd = permfd[args['text-decoration']]
		elseif not in_table( fd, permfd ) then
			return "L’ atribut text-decoration ni rkinoxhe nén l’ valixhance "..fd.."."
		end
		html:css("text-decoration", fd)
	end
	
	-- TEXT-INDENT
	if args['text-indent'] ~= nil and args['text-indent'] ~= "" then
		local ti = unit(args['text-indent'])
		html:css("text-indent", ti)
	end
	
	-- FONT-VARIANT 
	if args['font-variant'] ~= nil and args['font-variant'] ~= "" then
		local permfv = {sc="small-caps", asc="all-small-caps"}
		local fv = args['font-variant']
		if permfv[args['font-variant']] ~= nil then 
			fv = permfv[args['font-variant']]
		elseif not in_table( fv, permfv ) then
			return "L’ atribut font-variant ni rkinoxhe nén l’ valixhance "..fv.."."
		end
		html:css("font-variant", fv)
	end
	
	-- TEXT-ALIGN 
	if args['text-align'] ~= nil and args['text-align'] ~= "" then
		if args['text-align'] == "prôze" then
			html:css("text-align", "left")
			isprose = true
		elseif not in_table( args['text-align'], {"left", "right", "center", "justify"} ) then
			return "L’ atribut text-align ni rkinoxhe nén l’ valixhance "..args['text-align'].."."
		elseif elem == "span" then
			return "L’ atribut text-align ni rote nén avou l’ elemint span."
		else
			html:css("text-align", args['text-align'])
		end
	end
	
	-- MARGIN
	-- FIXME we should test unit (%,em,px,...)
	if args.margin ~= nil and args.margin ~= "" then
		if elem == "span" then
			return "L’ atribut margin ni rote nén avou l’ elemint span."
		end
		html:css("margin", unit(args.margin))
	end
	if args['margin-top'] ~= nil and args['margin-top'] ~= "" then
		if elem == "span" then
			return "L’ atribut margin-top ni rote nén avou l’ elemint span."
		end
		html:css("margin-top", unit(args['margin-top']))
	end
	if args['margin-bottom'] ~= nil and args['margin-bottom'] ~= "" then
		if elem == "span" then
			return "L’ atribut margin-bottom ni rote nén avou l’ elemint span."
		end
		html:css("margin-bottom", unit(args['margin-bottom']))
	end
	if args['margin-left'] ~= nil and args['margin-left'] ~= "" then
		if elem == "span" then
			return "L’ atribut margin-left ni rote nén avou l’ elemint span."
		end
		html:css("margin-left", unit(args['margin-left']))
	end
	if args['margin-right'] ~= nil and args['margin-right'] ~= "" then
		if elem == "span" then
			return "L’ atribut margin-right ni rote nén avou l’ elemint span."
		end
		html:css("margin-right", unit(args['margin-right']))
	end
	
	-- PADDING
	-- FIXME we should test unit (%,em,px,...)
	if args.padding ~= nil and args.padding ~= "" then
		if elem == "span" then
			return "L’ atribut padding ni rote nén avou l’ elemint span."
		end
		html:css("padding", unit(args.padding))
	end
	if args['padding-top'] ~= nil and args['padding-top'] ~= "" then
		if elem == "span" then
			return "L’ atribut padding-top ni rote nén avou l’ elemint span."
		end
		html:css("padding-top", unit(args['padding-top']))
	end
	if args['padding-bottom'] ~= nil and args['padding-bottom'] ~= "" then
		if elem == "span" then
			return "L’ atribut padding-bottom ni rote nén avou l’ elemint span."
		end
		html:css("padding-bottom", unit(args['padding-bottom']))
	end
	if args['padding-left'] ~= nil and args['padding-left'] ~= "" then
		if elem == "span" then
			return "L’ atribut padding-left ni rote nén avou l’ elemint span."
		end
		html:css("padding-left", unit(args['padding-left']))
	end
	if args['padding-right'] ~= nil and args['padding-right'] ~= "" then
		if elem == "span" then
			return "L’ atribut padding-right ni rote nén avou l’ elemint span."
		end
		html:css("padding-right", unit(args['padding-right']))
	end
	
	-- LETTER-SPACING
	-- FIXME we should test unit (%,em,px,...)
	if args['letter-spacing'] ~= nil and args['letter-spacing'] ~= "" then
		html:css("letter-spacing", unit(args['letter-spacing'], 'px'))
	end
	
	-- WORD-SPACING
	-- FIXME we should test unit (%,em,px,...)
	if args['word-spacing'] ~= nil and args['word-spacing'] ~= "" then
		html:css("word-spacing", unit(args['word-spacing'], 'px'))
	end
	
	-- LINE-HEIGHT
	-- FIXME we should test unit (%,em,px,...)
	if args['line-height'] ~= nil and args['line-height'] ~= "" then
		if elem == "span" then
			return "L’ atribut line-height ni rote nén avou l’ elemint span."
		end
		html:css("line-height", args['line-height'])
	end
	
	-- COLOR
	if args.color ~= nil and args.color ~= "" then
		html:css("color", args.color)
	end
	
	-- BACKGROUND-COLOR
	if args['background-color'] ~= nil and args['background-color'] ~= "" then
		html:css("background-color", args['background-color'])
	end
	
	-- BORDER
	-- eg.	border:1px solid green
	if args.border ~= nil and args.border ~= "" then
		html:css("border", args.border)
	end
	if args['border-top'] ~= nil and args['border-top'] ~= "" then
		html:css("border-top", args['border-top'])
	end
	if args['border-bottom'] ~= nil and args['border-bottom'] ~= "" then
		html:css("border-bottom", args['border-bottom'])
	end
	if args['border-left'] ~= nil and args['border-left'] ~= "" then
		html:css("border-left", args['border-left'])
	end
	if args['border-right'] ~= nil and args['border-right'] ~= "" then
		html:css("border-right", args['border-right'])
	end
	
	if openTag == false or closeTag == false then
		html = openclose(html, openTag, closeTag)
	end
	
	-- PRÔZE
	if isprose then
		content = html
		html = mw.html.create("div")
		html:css({
			display = 'flex',
			['justify-content'] = 'center'
		})
		html:node(content)
		
		if openTag == false or closeTag == false then
			html = openclose(html, openTag, closeTag)
		end
	end
	
	return html
end

return p