وحدة:DemoTemplate2

من أرابيكا، الموسوعة الحرة
اذهب إلى التنقل اذهب إلى البحث

require("Module:No globals")

--local newBuffer = require('Module:OutputBuffer')
local mt = {}

local function isvalid(x)
	if x and x ~= "" then
		return x
	end
	return nil
end

function mt.__index(t, title)
	return function(frame)
		local false_args = {
			["_sandbox_"] = "",
			["_compare_"] = "",
			["demotitle"] = "",
			["_dir_"] = "",
			["_demonewline_"] = "",
			["demonewline"] = "",
			["syntaxhighlight"] = ""
		}
		local space = " "
		local line = ""
		if isvalid(frame.args["_demonewline_"]) or isvalid(frame.args["demonewline"]) then
			line = "\n"
			space = ""
		end

		local width50 = "width:50%;"
		local text = ""
		local text2 = ""
		local title2 = ""
		local compare
		if isvalid(frame.args["_sandbox_"]) then
			title2 = frame.args["_sandbox_"]
			width50 = "width:33%;"
			if isvalid(frame.args["_compare_"]) then
				compare = true
			end
		end

		local tit1 = "يظهر"
		local tit2 = ""
		if isvalid(title2) then
			-- find "#invoke:" in start of tit1 and remove it
			tit1 = title
			tit2 = title2
			local pos = string.find(tit1, "#invoke:") or string.find(tit1, "#استدعاء:")
			if pos then
				tit1 = string.gsub(tit1, "#invoke:", "")
				tit1 = string.gsub(tit1, "#استدعاء:", "")
				tit1 = "[[وحدة:" .. tit1 .. "]]"
			else
				tit1 = "[[قالب:" .. tit1 .. "]]"
			end
			local pos2 = string.find(tit2, "#invoke:") or string.find(tit2, "#استدعاء:")
			if pos2 then
				tit2 = string.gsub(tit2, "#invoke:", "")
				tit2 = string.gsub(tit2, "#استدعاء:", "")
				tit2 = "[[وحدة:" .. tit2 .. "]]"
			else
				tit2 = "[[قالب:" .. tit2 .. "]]"
			end
		end

		--local getBuffer, print, printf = newBuffer()
		--printf('{{%s' .. line, title)
		text = text .. "{{" .. title .. line
		text2 = text2 .. "{{" .. title2 .. line
		local ipairsArgs = {}
		for k, v in ipairs(frame.args) do
			if string.find(v, "=", 1, true) then
				break
			end
			ipairsArgs[k] = true
			--printf('|%s' .. line, v)
			text = text .. "|" .. v .. line
			text2 = text2 .. "|" .. v .. line
		end
		for k, v in pairs(frame.args) do
			if not false_args[k] and not ipairsArgs[k] then
				--printf('|%s=%s' .. line, string.gsub(k, '=', '{{=}}'), v)
				text = text .. "|" .. space .. string.gsub(k, "=", "{{=}}") .. "=" .. v .. line
				text2 = text2 .. "|" .. space .. string.gsub(k, "=", "{{=}}") .. "=" .. v .. line
			end
		end
		--print('}}')
		text = text .. "}}"
		text2 = text2 .. "}}"
		mw.log(text .. "\n")
		--local buffer = getBuffer()
		local style = "table-layout:fixed; width:100%; margin-top:0;margin-left:0; border-width:medium; padding:0;"
		local tab = mw.html.create("table"):
			--:addClass('sortable')
			cssText(style)
		if frame.args.demotitle and frame.args.demotitle ~= "" then
			tab:tag("caption"):tag("b"):wikitext(frame.args.demotitle)
		end
		local dir = isvalid(frame.args["_dir_"]) or "ltr"

		local tr = tab:tag("tr")
		tr:tag("th")
			:attr("scope", "col")
			:cssText(width50)
			:wikitext("توصيف")
		
		tr:tag("th")
			:attr("scope", "col")
			:cssText(width50)
			:wikitext(tit1)

		if isvalid(title2) then
			tr:tag("th")
				:attr("scope", "col")
				:cssText(width50)
				:wikitext(tit2)
				
			if compare then
				tr:tag("th")
					:attr("scope", "col")
					:cssText("width:10%;")
					:wikitext('مطابق؟')
			end			
		end
		
		local td1style = "width:50%; background:#f9f9f9; border-width:1px;border-style:solid none none solid;" ..
			"border-color:#ddd;padding:9px 5px 5px; vertical-align:top;"
		local prestyle = "margin:0;border:none;padding:0; word-wrap:break-word;" ..
			"white-space:-moz-pre-wrap;white-space:-o-pre-wrap;white-space:-pre-wrap;white-space:pre-wrap;"

		local td2style = "width:50%; background:#f9f9f9;border-width:1px;border-style:solid solid none solid;" ..
			"border-color:#ddd; padding:5px; vertical-align:top;"

		local nowikitext = mw.text.nowiki(text)
		if isvalid(frame.args.syntaxhighlight) then
			nowikitext = frame:extensionTag("syntaxhighlight", text, {lang = "html+handlebars"})
		end
		text = frame:preprocess(text)
		local tr2 = tab:tag("tr")
		tr2:tag("td")
			:cssText(td1style)
			:tag("pre")
			:attr("dir", dir)
			:cssText(prestyle)
			:wikitext(nowikitext)

		tr2:tag("td")
			:cssText(td2style)
			:tag("span")
			:wikitext(text)

		if isvalid(title2) then
			text2 = frame:preprocess(text2)
			tr2:tag("td")
			:cssText(td2style)
			:tag("span")
			:wikitext(text2)
			if compare then
				local result = 'false'
				local color  = '#fcc0c0'
				if text2 == text then
					result = 'true'
					color  = '#c0fcc0'
				end
				tr2:tag("td")
				-- :cssText("background:" .. color .. ";")
				:css("background", color)
				:tag("span")
				:wikitext(result)
				
			end
		end
		
		return tostring(tab)
	end
end

return setmetatable({}, mt)