Sari la conținut

Modul:Infobox/teste

De la Wikipedia, enciclopedia liberă

Documentația acestui modul poate fi creată la Modul:Infobox/teste/doc

--
-- This module implements {{Infobox}}
--
 
local p = {}
local getArgs = require('Modul:Arguments').getArgs
local StringUtils = require('Modul:StringUtils')
local TableTools = require('Modul:TableTools')
local args = {}

local function containsEmbed(cellData)
	local check = { mw.ustring.find(cellData, "^%s*<%s*[Tt][Rr](%s*)([^<>]*)>%s*<%s*[Tt][DdHh](%s*)([^<>]*)>") }
	if check[1] == nil or (check[3] == "" and check[4] ~= "") or (check[5] == "" and check[6] ~= "") then return false end
	check = { mw.ustring.find(cellData, "</[Tt][DdHh]%s*>%s*</[Tt][Rr]%s*>%s*$") }
	if check[1] == nil then return false else return true end
end

p._infobox = function(origArgs)

	local child = origArgs["child"] or origArgs["embed"] or "no"
	local bodyclass = origArgs["bodyclass"] or "infocaseta"
	local antet = origArgs["antet"] or "default"
	local aboveclass = origArgs["aboveclass"] or antet
	local abovestyle = origArgs["abovestyle"] or ""
	local culoare_cadru = origArgs["culoare cadru"] or "F5F5DC"
	if mw.ustring.match(culoare_cadru, '^%x%x%x$') or mw.ustring.match(culoare_cadru, '^%x%x%x%x%x%x$') then culoare_cadru = StringUtils._prependIfMissing({culoare_cadru, '#'}) end
	local culoare_text = origArgs["culoare text"] or "000000"
	if mw.ustring.match(culoare_text, '^%x%x%x$') or mw.ustring.match(culoare_text, '^%x%x%x%x%x%x$') then culoare_text = StringUtils._prependIfMissing({culoare_text, '#'}) end
	local titlestyle = origArgs["titlestyle"] or ""
	local title = origArgs["title"] or origArgs["titlu"] or ""
	title = StringUtils._capitalize({title})
	local showtitle = origArgs["showtitle"] or "yes"
	local above = origArgs["above"] or ""
	local parentColSpanArg = origArgs['parent_colspan']
	local parentColSpan
	if parentColSpanArg and mw.ustring.gsub(parentColSpanArg, '%d+', '', 1) == '' then
		parentColSpan = tonumber(parentColSpanArg)
	else
		parentColSpan = 2
	end
	
	local wikidataEnabled = origArgs["wikidata"] or ""
	local enclose = origArgs["enclose"]
	
	if type(enclose) ~= "string" then enclose = "auto" end
	
	if enclose == "begin" then enclose = { true, false }
	elseif enclose == "end" then enclose = { false, true }
	elseif enclose == "both" then enclose = { true, true }
	elseif enclose == "none" or child == "yes" then enclose = { false, false }
	else enclose = { true, true }
	end
	
	local out = ""
	
	-- open box
	if enclose[1] then
		out = out .. "<table class=\"" .. bodyclass .. "\">"
	end
	
	if child ~= "yes" then
		-- caption
		if showtitle == "yes" then
			out = out .. tostring(mw.html.create('tr')
								:tag('td')
								:attr('colspan', tostring(parentColSpan))
								:addClass('antet ' .. aboveclass)
								:css('background-color', culoare_cadru)
								:css('color', culoare_text)
								:cssText(titlestyle)
								:wikitext(title)
								:allDone())
		end
		-- header
		if above ~= "" then
			local aboveTr = mw.html.create('tr'):tag('td')
				:attr('colspan', tostring(parentColSpan))
				:addClass(aboveclass)
				:css('text-align', 'center')
				:css('font-size', '125%')
				:css('font-weight', 'bold')
				:cssText(abovestyle)
				:wikitext(above):allDone()
			out = out .. tostring(aboveTr)
		end
	else
		if showtitle == "yes" and title ~= "" then
			local newTr = mw.html.create('tr'):tag('td')
				:attr('colspan', tostring(parentColSpan))
				:css('background-color', culoare_cadru)
				:css('color', culoare_text)
				:css('font-size', '125%')
				:css('font-weight', 'bold')
				:css('margin-bottom', '2')
				:css('text-align', 'center')
				:css('line-height', '1.2em')
				:cssText(titlestyle)
				:wikitext(title):allDone()
			out = out .. tostring(newTr)
		end
	end

	-- subheaders
	local subheaders = {}
	subheaders[1] = origArgs["subheader"] or origArgs["subheader1"] or ""
	local subhIndex = 2
	while (origArgs["subheader" .. tostring(subhIndex)] or "") ~= "" do
		subheaders[subhIndex] = origArgs["subheader" .. tostring(subhIndex)] or ""
		subhIndex = subhIndex + 1
	end
	local subheaderstyle = origArgs["subheaderstyle"] or ""
	local subheaderclass = origArgs["subheaderclass"] or ""
	for subHeaderIdx = 1,#subheaders do
		if subheaders[subHeaderIdx] ~= "" then
			local subhTr = mw.html.create('tr'):tag('td'):attr('colspan', tostring(parentColSpan))
				:addClass(subheaderclass)
				:css('text-align', 'center')
				:cssText(subheaderstyle)
				:wikitext(subheaders[subHeaderIdx]):allDone()
			out = out .. tostring(subhTr)
		end
	end
	
	-- images
	local imageIndices = TableTools.affixNums(origArgs, 'image')
	local images = {}
	local captions = {}
	if origArgs['image'] then
		table.insert(images, origArgs['image'] or '')
		table.insert(captions, origArgs['caption'] or '')
	end
	for _,imgIndex in ipairs(imageIndices) do
		table.insert(images, origArgs['image' .. tostring(imgIndex)])
		table.insert(captions, origArgs['caption' .. tostring(imgIndex)] or '')
	end

	local imageclass = origArgs["imageclass"]
	local imagestyle = origArgs["imagestyle"]
	local captionstyle = origArgs["captionstyle"]
	for i = 1,#images do
		if images[i] ~= "" then
			local imageRow = mw.html.create('tr')
			local imageTd = imageRow
				:tag('td'):attr('colspan', tostring(parentColSpan))
					:addClass(imageclass)
					:css('text-align', 'center')
					:cssText(imagestyle)
					:wikitext(images[i])
			if captions[i] ~= "" then
				imageTd:tag('br')
				imageTd:tag('div'):cssText('margin-top: 3px')
				imageTd:tag('span'):cssText(captionstyle)
					:wikitext(captions[i])
			end
			out = out .. tostring(imageRow)
		end
	end
	
	-- rows
	local labelstyle = origArgs["labelstyle"] or ""
	local datastyle = origArgs["datastyle"] or ""
	local headerstyle = origArgs["headerstyle"] or ""
	local elementIndex = 1
	local headers = {}
	local data = {}
	local labels = {}
	local classes = {}
	local styles = {}
	local lblstyles = {}
	local processingOrder = {}

	for k,v in pairs(origArgs) do
		local headerStart
		local headerEnd
		local labelStart
		local labelEnd
		local dataStart
		local dataEnd
		local styleStart
		local styleEnd
		local lblStyleStart
		local lblStyleEnd
		headerStart, headerEnd = mw.ustring.find(k, "header")
		labelStart, labelEnd = mw.ustring.find(k, "label")
		dataStart, dataEnd = mw.ustring.find(k, "data")
		styleStart, styleEnd = mw.ustring.find(k, "style")
		lblStyleStart, lblStyleEnd = mw.ustring.find(k, "lblstyle")
		
		local nr = ""
		if dataStart == 1 then
			nr = mw.ustring.sub(k, 1 + dataEnd, mw.ustring.len(k))
		elseif labelStart == 1 then
			nr = mw.ustring.sub(k, 1 + labelEnd, mw.ustring.len(k))
		elseif headerStart == 1 then
			nr = mw.ustring.sub(k, 1 + headerEnd, mw.ustring.len(k))
		elseif styleStart == 1 then
			nr = mw.ustring.sub(k, 1 + styleEnd, mw.ustring.len(k))
		elseif lblStyleStart == 1 then
			nr = mw.ustring.sub(k, 1 + lblStyleEnd, mw.ustring.len(k))
		end

		if nr ~= "" and processingOrder[nr] == nil and tonumber(nr) ~= nil then
			headers[elementIndex] = origArgs["header" .. nr] or ""
			labels[elementIndex] =  origArgs["label" .. nr] or ""
			data[elementIndex] = origArgs["data" .. nr] or ""
			classes[elementIndex] =  origArgs["class" .. nr] or ""
			styles[elementIndex] = origArgs["style" .. nr] or ""
			lblstyles[elementIndex] = origArgs["lblstyle" .. nr] or ""
			processingOrder[tonumber(nr)] = elementIndex
			elementIndex = elementIndex + 1
		end
	end

	local processingElement = 1
	while processingElement <= table.maxn(processingOrder) do
		elementIndex = processingOrder[processingElement]
		if elementIndex ~= nil then
			local crtHeader = headers[elementIndex]
			local crtData = data[elementIndex]
			local crtLabel = labels[elementIndex]
			local crtLblStyle = lblstyles[elementIndex]
			local crtClass = classes[elementIndex]
			local crtStyle = styles[elementIndex]
			if crtHeader ~= "" then
				local headerTr = mw.html.create('tr')
				local headerTh = headerTr:tag('th')
					:attr('colspan', tostring(parentColSpan))
					:css('text-align', 'center')
					:css('background-color', culoare_cadru)
					:css('color', culoare_text)
					:addClass(crtClass)
					:cssText(headerstyle)
					:wikitext(crtHeader)
				out = out .. tostring(headerTr)
			elseif crtLabel ~= "" then
				if crtData ~= "" then
					local dataAndLabelTr = mw.html.create('tr')
						:tag('th'):cssText(labelstyle):cssText(crtLblStyle)
							:wikitext(crtLabel):done()
						:tag('td')
							:attr('colspan', tostring(parentColSpan - 1))
							:addClass(crtClass)
							:cssText(datastyle):cssText(crtStyle)
							:wikitext(crtData):allDone()
					out = out .. tostring(dataAndLabelTr)
				end
			elseif crtData ~= "" then
				crtData = mw.getCurrentFrame():preprocess(crtData)
				if containsEmbed(crtData) then
					out = out .. crtData
				else
					local dataTr = mw.html.create('tr')
						:tag('td'):attr('colspan', tostring(parentColSpan))
							:addClass(crtClass)
							:css('text-align', 'center')
							:cssText(datastyle)
							:cssText(crtStyle)
							:wikitext(crtData):allDone()
					out = out .. tostring(dataTr)
				end
			end
		end
		processingElement = processingElement + 1
	end
	
	-- below
	local belowstyle = origArgs["belowstyle"] or ""
	local below = origArgs["below"] or ""
	if below ~= "" then
		out = out .. tostring(mw.html.create('tr')
								:tag('td')
									:attr('colspan', tostring(parentColSpan))
									:css('text-align', 'center')
									:cssText(belowstyle)
									:wikitext(below):allDone())
	end
	
	-- tnavbar
	local name = origArgs["name"] or ""
	local navbar = origArgs["navbar"]
	if name ~= "" and navbar ~= 'false' then
		local navBarTr = mw.html.create('tr'):tag('td')
			:css('text-align', 'right')
			:attr('colspan', tostring(parentColSpan))
			:wikitext(mw.getCurrentFrame():expandTemplate{title = "Tnavbar", args = { name }}):done()
		out = out .. tostring(navBarTr)
	end
	if child ~= "yes" then
		local doc = origArgs["doc"] or ""
		if doc ~= "" then
			local infodocTr = mw.html.create('tr'):wikitext(mw.getCurrentFrame():expandTemplate{title = "infodoc", args = {colspan = tostring(parentColSpan), culoare = culoare_cadru, link = doc, wikidata = wikidataEnabled }})
			out = out .. tostring(infodocTr)
		end
	end
	
	-- close box
	if enclose[2] then
		out = out .. "</table>" .. mw.getCurrentFrame():extensionTag{ name = 'templatestyles', args = { src = 'Modul:Infobox/styles.css'} } 
	end
	
	return out
end

p.infobox = function(frame)
	local origArgs = getArgs(frame)
	return p._infobox(origArgs)
end
return p