Marvel Wiki
Registre-se
Advertisement
Marvel Wiki
5 117
páginas

A documentação para este módulo pode ser criada em Módulo:HF/doc

local p = mw.InfoboxBuilderHF
local getArgs = require('Module:Arguments').getArgs

--------------------------------------------------------------------------------------------------
-- retorna o conteúdo da página
function p.get_content(pagename)
	local output = ''
	
	if not p.isempty(pagename) then
		local page = mw.title.new(p.break_link(pagename,1))
		if not page then
			return ''
		end
		output = page:getContent() or ''
	end
	
	return output
end

--------------------------------------------------------------------------------------------------
-- retorna o valor de 'campo' de 'page_content'
function p.get_field_value(page_content, field)
	local output = ''
	
	if not p.isempty(page_content) and not p.isempty(field)
		then 
			page_content = string.gsub(page_content, '}}', '|}}') -- added to get value from last field
			output = string.match(page_content, '|%s-'..field..'%s-=%s-(.-)\n|')
			if not p.isempty(output)
				then output = p.trim(output)
			end
	end

	return output
end

--------------------------------------------------------------------------------------------------
-- Verifique se 'texto' faz parte da 'lista' ou não
function p.in_list(list, text)
	local output = false

	if not p.isempty(list) and not p.isempty(text)
		then
			for i, v in ipairs( list ) do
				if v == text
					then 
						output = true 
						break
				end
			end
	end

	return output
end

--------------------------------------------------------------------------------------------------
-- adiciona todos os elementos de 'tabela2' em 'tabela1'; primeiro, segundo, ambos ou nenhum deles pode ser tabelas ou strings
function p.join_tables(table1, table2)
	local output = {}
	
	if not p.isempty(table1)
		then 
			if type(table1) == "table"  
				then output = table1
				else table.insert(output, table1)
			end
	end

	if not p.isempty(table2)
		then
			if type(table2) == "table"
				then
					for i = 1, #table2 do
						table.insert(output, table2[i])
					end
				else
					table.insert(output, table2)
			end
	end

	return output
end

--------------------------------------------------------------------------------------------------
--verifica se a 'página' existe ou não
function p.exists(page)
	local title
	local output = false
	
	if not p.isempty(page) 
		then 
			title = mw.title.new(page)
			if title ~= nil and title.exists
				then output = true
			end
	end
	title = nil
	return output
end

--------------------------------------------------------------------------------------------------
--retorna verdadeiro se o texto for um link wiki ou falso caso contrário
function p.is_link(link)
	local i
	local j
	local output = false
 
	if not p.isempty(link)
		then
			i = string.find(link, "^%[%[")
			j = string.find(link, "%]%]$")
			if i ~= nil and j ~= nil
				then output = true
			end
	end
	
	return output
end

--------------------------------------------------------------------------------------------------
--Verifique se 'link' é um wikilink. Em caso afirmativo, verifique se | dentro. Se sim, então devolva 'parte' dele. Se 'link' não for um wikilink, retorne 'link'.
function p.break_link(link, part)
	local i
	local j
	local k
	local output = ''
	
	if not p.isempty(link) and p.is_link(link)
		then
			i = string.find(link, "[[",1,true)
			j = string.find(link, "|",1,true)
			k = string.find(link, "]]",1,true)
			if j == nil 
				then output = string.sub(link, i+2, k-1) 
				elseif j < k
					then 
						if part == 2 
							then output = string.sub(link, j+1, k-1)
							else output = string.sub(link, i+2, j-1)
						end
				else output = string.sub(link, i+2, k-1)  
			end
		else output = link
	end

	return output
end


function p.breaklink(frame)
	return p.break_link(frame.args[1], tonumber(frame.args[2]))
end

--------------------------------------------------------------------------------------------------
-- retorna o número de páginas em "category", "tipodepagina" pode ser um de '*', 'all', 'pages', 'subcats', or 'files' 
function p.pages_in_category(category, pagetype)
	local output = 0
	pagetype = pagetype or 'pages'

	if not p.isempty(category)
		then
		output = string.gsub(category, '&#39;',"'")
		output = mw.site.stats.pagesInCategory(output, pagetype)
	end

	return output
end

--------------------------------------------------------------------------------------------------
-- adiciona cada elemento da tabela de 'categorias' como categoria com 'sortname'.
-- Elements of 'categories' table could be strings or tables themselves with second element being a different sortname
function p.add_categories(categories, sortname)
	local i
	local category
	local output = {}
 
	if not p.isempty(categories) and type(categories) == 'table'
		then
			for i = 1,#categories do
				category = categories[i]
				if not p.isempty(category)
					then 
						if type(category) == 'table'
							then table.insert( output, p.Category(category[1], category[2]) )
							else table.insert( output, p.Category(category, sortname) )
						end
				end
			end
	end
 
	return table.concat(output)
end


--------------------------------------------------------------------------------------------------
--transforme 'número' em número ordinal, por exemplo '1' em 'primeiro'
function p.numero_ordinal(numero)
	local output = ''
	
	numero = tonumber(numero)
	if not p.isempty(numero)
		then
			if numero % 10 == 1 and numero ~= 11 -- in (1,21,31,41,51,61,71)
				then output = numero..'ª'
				elseif numero % 10 == 2 and numero ~= 12 -- in (2,22,32,42,52,62,72)
					then output = numero..'ª'
					elseif numero % 10 == 3 and numero ~= 13 --in (3,23,33,43,53,63,73)
						then output = numero..'ª'
						else output = numero..'ª'
			end
	end
	
	return output
end

--------------------------------------------------------------------------------------------------
--transforme o tipo de página como 'personagem' no plural 'personagens' (usado no Módulo:Design. Adicionado aqui em vez de Módulo:TipoDePágina para não chamá-lo de lá)
function p.get_tipo_de_pagina_no_plural(tipo_de_pagina)
	local output = ''
	
	if tipo_de_pagina == 'Organização'
		then output = 'Organizações'
		elseif tipo_de_pagina == 'Local'
			then output = 'Locais'
		elseif tipo_de_pagina == 'Personagem'
		then output = 'Personagens'
		else output = tipo_de_pagina..'s'
	end

	return output
end

--------------------------------------------------------------------------------------------------
function p._HelpButton( args )
  if p.isempty( args.buttonsize ) then args.buttonsize = "10px" end
  local target = args.ArticleTarget or "Clique aqui para obter ajuda neste campo"
	local link = string.format(
		"[[Arquivo:Information-silk.png|%s|link=Clique aqui para obter ajuda neste campo#%s]] %s",
		args.buttonsize,
		args.Section or args.Label or '',
	args.Label or ''
	)
  return link
end


function p.HelpButton( frame )
	local args = getArgs( frame )
	return p._HelpButton( args )
end

--------------------------------------------------------------------------------------------------
function p.AddZeros( s, len )
  local output = ""

  local sLength = string.len( tostring( s ) )
  local diff = tonumber( len ) - tonumber( sLength )

  if diff > 0 then
    for i = 1, diff do
      output = string.format('%s0', output)
    end
  end

  output = string.format('%s%s', output, s)

  return output
end

return p
Advertisement