Marvel Database

Due to recent developments, please be aware that the use of large language model or generative AIs in writing article content is strictly forbidden. This caveat has now been added to the Manual of Style and Blocking Policy.

READ MORE

Marvel Database
Advertisement

Documentation for this module may be created at Module:HF/doc

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

--------------------------------------------------------------------------------------------------
-- returns content of the page
function p.get_content(pagename)
	local output = ''
	
	if not p.isempty(pagename)
		then output = mw.title.new(p.break_link(pagename,1)):getContent() or ''
	end
	
	return output
end

--------------------------------------------------------------------------------------------------
-- returns value of 'field' from '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

--------------------------------------------------------------------------------------------------
-- Check if 'text' is part of the 'list' or not
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

--------------------------------------------------------------------------------------------------
-- adds all elements from 'table2' into 'table1'; first, second, both or none of them could be tables or 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

--------------------------------------------------------------------------------------------------
--checks if 'page' exists or not
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

--------------------------------------------------------------------------------------------------
--returns true if text is a wikilink or false otherwise
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

--------------------------------------------------------------------------------------------------
--Check if 'link' is a wikilink. If yes, then check if it has | inside. If yes, then return 'part' part of it. If 'link' is not a wikilink return '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

--------------------------------------------------------------------------------------------------
-- returns number of pages in the "category", "pagetype" can be one of '*', '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

--------------------------------------------------------------------------------------------------
-- adds each element of 'categories' table as category with '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


--------------------------------------------------------------------------------------------------
--transform 'number' into ordinal number, for example '1' into '1st'
function p.ordinal_number(number)
	local output = ''
	
	number = tonumber(number)
	if not p.isempty(number)
		then
			if number % 10 == 1 and number ~= 11 -- in (1,21,31,41,51,61,71)
				then output = number..'st'
				elseif number % 10 == 2 and number ~= 12 -- in (2,22,32,42,52,62,72)
					then output = number..'nd'
					elseif number % 10 == 3 and number ~= 13 --in (3,23,33,43,53,63,73)
						then output = number..'rd'
						else output = number..'th'
			end
	end
	
	return output
end


--------------------------------------------------------------------------------------------------
--transform page type like 'character' into plural form 'characters' (used in Module:Design. Added here insted of Module:PageType to not call it there)
function p.get_plural_page_type(page_type)
	local output = ''
	
	if page_type == 'Reality'
		then output = 'Realities'
		elseif page_type == 'Gallery'
			then output = 'Galleries'
		elseif page_type == 'Series'
			then output = page_type
		else output = page_type..'s'
	end

	return output
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


--------------------------------------------------------------------------------------------------
-- used in Module:List_of_Powers
function p.LinkToWikipedia(link, text)
	if p.isempty(text)
		then text = link
	end
	return p.Link('Wikipedia:'..link, text)
end


return p
Advertisement