Marvel Database
Register
Advertisement

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

local p = {}
local h = require("Module:HF")
local getArgs = require('Dev:Arguments').getArgs
---- //*** Functions for transforming pagenames of comics/episodes into desirable format ***// ----


--------------------------------------------------------------------------------------------------
-- divides complex issue number like "4AU", "5 (Fall)", "6.R", etc.
function p.lua_get_clean_issue_number_and_remainder(issue)
	local remainder_issue = ''
	local clean_issue = ''
	if not h.isempty(issue) and tonumber(issue) == nil
		then clean_issue, remainder_issue = string.match(issue, '(%d+)(%D+)')
	end
	return clean_issue, remainder_issue
end
	
	
--------------------------------------------------------------------------------------------------
-- returns transformed issue/episode number that can be used for sorting in categories as numbers, instead of text, including "point one" issues and "-1"/"negative" issues
function p.lua_padded_issue(issue)
	local minus = '5'
	local point_one = '00'
	local i = ''
	local j = ''
	local output = ''
	local clean_issue = ''
	local remainder_issue = ''
	
	if not h.isempty(issue)
		then
			if issue == '½'
				then issue = '0.5'
			end
			
			if string.find(issue, '%d+: ') ~= nil
				then issue = string.match(issue, '(%d+): ')
			end
			
			clean_issue, remainder_issue = p.lua_get_clean_issue_number_and_remainder(issue)
			if not h.isempty(remainder_issue)
				then issue = clean_issue..'.'..remainder_issue
			end
			
			if string.match(issue, '^%-*%d', 1) ~= nil --check if issue starts with number (including negative numbers like -1)
				then
					-- -1 issues
					j = string.find(issue,"-",1,true)
					if not h.isempty(j)
						then
							i = string.sub(issue,2,#issue)
							j = string.find(i,".",1,true)
							if not h.isempty(j) 
								then minus = 5 - #string.sub(i,1,j-1)
								else minus = 5 - #i
							end
						else
							i = issue
					end
	
					-- point one issues
					j = string.find(i,".",1,true) 
					if not h.isempty(j)
						then
							point_one = string.sub(i,j+1,#i)
							if tonumber(point_one) == nil
								then point_one = '99' -- for issues with letters after . instead of just numbers (for example, Amazing Spider-Man Vol 5 #16.HU)
							end
							point_one = string.rep("0", 2-#point_one)..point_one
							i = string.sub(i,1,j-1)
					end
	
					output = minus..string.rep("0", 4-#i)..i..point_one
				else -- issue is not a number
					output = issue --'999999999'
			end
	end
	
	return output
end


--------------------------------------------------------------------------------------------------
-- returns transformed volume/season number that can be used for sorting in categories as numbers, instead of text
function p.lua_padded_volume(volume)
	local output = ''
	
	if not h.isempty(volume)
		then output = string.rep("0", 6-#volume)..volume
	end
	
	return output
end


--------------------------------------------------------------------------------------------------
-- returns transformed title, with replaced characters like ":" or "/" that can be used for names of files (for example, covers)
function p.lua_title_for_files(title)
	local output = title
	
	output = string.gsub(output,'/',' ')
	output = string.gsub(output,':','')
	output = string.gsub(output,'&','&')
	output = string.gsub(output,''',"'") 

	return output
end


--------------------------------------------------------------------------------------------------
-- break down pagename of comics/episodes into parts - title, volume/season, issue/episode. 
---- Also returns transformed volume/season number and issue/episode number that can be used for sorting in categories as numbers, instead of text, including "point one" issues and "-1"/"negative" issues
---- Also returns transformed title, with replaced characters like ":" or "/" that can be used for names of files (for example, covers)
function p.lua_get_title_volume_issue(pagename, pagetype)
	local title = ''
	local volume = ''
	local issue = ''
	local padded_issue = ''
	local padded_volume = ''
	local title_for_files = ''
	local remainder_issue = ''
	local clean_issue = ''
	local link_part1 = ''
	local link_part2 = ''
	local link_part2_noissue = ''
	local design = require("Module:Design")
	local j
	local k
	local s
	
	if h.isempty(pagetype)
		then pagetype = 'Vol'
	end
	
	j,k = string.find(pagename, ' '..pagetype..' %d+ ')
	if not h.isempty(j)
		then
			title = string.sub(pagename, 1, j-1)
			issue = string.gsub(string.sub(pagename, k+1, #pagename), '#', '')
			j,k,volume = string.find(pagename, ' '..pagetype..' (%d+) ')
		else
			j,k = string.find(pagename, ' #')
			if h.isempty(j)
				then
					j,k = string.find(pagename, ' '..pagetype..' %d+')
					if not h.isempty(j)
						then
							title = string.sub(pagename, 1, j-1)
							volume = string.sub(pagename, j+#pagetype+2, k)
						else return ''
					end
				else
					title = string.sub(pagename, 1, j-1)
					issue = string.sub(pagename, k+1, #pagename)
					volume = '1'
			end
	end
	
	clean_issue, remainder_issue = p.lua_get_clean_issue_number_and_remainder(issue)

	if not h.isempty(title)
		then
			if not h.isempty(issue)
				then
					if pagetype == 'Season'
						then link_part2 = 'E'..string.rep('0', 2-#issue)..issue
						else
							link_part1, link_part2 = string.match(issue, '(%d+): (.+)')
							if h.isempty(link_part2)
								then link_part2 = ' #'..issue
								else link_part2 = design.span(': '..link_part2).italic
							end
					end
					link_part1 = title..' '..pagetype..' '..volume..' '..issue
				else
					link_part1 = title..' '..pagetype..' '..volume
			end
			if pagetype == 'Season'
				then
					s = string.gsub(title, ' %(.+%)', '')
					link_part2 = design.span(s).italic..' S'..volume..link_part2
				else
					link_part2_noissue = design.span(title).italic
					if volume ~= '1'
						then link_part2_noissue = link_part2_noissue..' ('..pagetype..'. '..volume..')'
					end
					link_part2 = link_part2_noissue..link_part2
			end
	end

	padded_volume = p.lua_padded_volume(volume)
	padded_issue = p.lua_padded_issue(issue)
	title_for_files = p.lua_title_for_files(title)
	return {  ['title'] = title, 
			  ['volume'] = volume,
			  ['issue'] = issue,
			  ['clean_issue'] = clean_issue or '',
			  ['remainder_issue'] = remainder_issue or '',
			  ['padded_volume'] = padded_volume,
			  ['padded_issue'] = padded_issue,
			  ['filename'] = {
				  ['title'] = title_for_files,
				  ['noissue'] = title_for_files..' '..pagetype..' '..volume,
				  ['all'] = title_for_files..' '..pagetype..' '..volume..' '..issue,
			  },
			  ['sortname'] = {
				  ['title'] = p.lua_remove_the(title),
				  ['noissue'] = p.lua_remove_the(title)..' '..pagetype..' '..padded_volume,
				  ['all'] = p.lua_remove_the(title)..' '..pagetype..' '..padded_volume..' '.. padded_issue,
			  },
			  ['link'] = {
				  ['part1'] = link_part1,
				  ['part2'] = link_part2,
				  ['all'] = h.Link(link_part1, link_part2),
				  ['part2_noissue'] = link_part2_noissue,
			  },
			  ['noissue'] = title..' '..pagetype..' '..volume,
			  ['all'] = title..' '..pagetype..' '..volume..' '..issue,
			}
end

--------------------------------------------------------------------------------------------------
-- removes "The " from the start of pagename
function p.lua_remove_the(pagename)
	if not h.isempty(pagename) and string.lower( string.sub(pagename, 1, 4) ) == 'the '
		then pagename = string.sub(pagename, 5, #pagename)
	end
	return pagename
end


function p.remove_the(frame)
	local args = getArgs(frame)
	return p.lua_remove_the(args[1])
end


--------------------------------------------------------------------------------------------------
-- automatic standardization of comics names
function p.lua_standardized_comics_name(pagename)
	local info = {}
	local output = pagename

	if not h.isempty(pagename)
		then 
			info = p.lua_get_title_volume_issue(pagename, 'Vol')
			if not h.isempty(info.title)
				then output = info.all
			end
	end
	
	return output
end


function p.standardized_comics_name(frame)
	local args = getArgs(frame)
	return p.lua_standardized_comics_name(args[1])
end


--------------------------------------------------------------------------------------------------
-- automatic standardization of episodes names
function p.lua_standardized_episode_name(pagename)
	local info = {}
	local output = pagename

	if not h.isempty(pagename)
		then 
			info = p.lua_get_title_volume_issue(pagename, 'Season')
			if not h.isempty(info.title)
				then output = info.all
			end
	end
	
	return output
end


function p.standardized_episode_name(frame)
	local args = getArgs(frame)
	return p.lua_standardized_episode_name(args[1])
end


--------------------------------------------------------------------------------------------------
-- returns link after standardization of its name
function p.lua_standardized_link(pagename, pagetype, text, to_issue, and_issue)
	local design = require("Module:Design")
	local link = ''
	local issue = ''
	local info = {}
	local output = ''
	
	if not h.isempty(pagename)
		then
			pagename = p.lua_replace_number_sign(pagename, pagetype)
			if string.find(pagename, '%]%].+') ~= nil
				then return pagename
				else pagename = h.break_link(pagename, 1)
			end
			if h.isempty(pagetype)
				then pagetype = require("Module:PageType").get_page_type(pagename)
			end
			if not h.isempty(pagetype)
				then
					if pagetype == 'Comic' or pagetype == 'Volume' or pagetype == 'Episode' 
						then
							if pagetype == 'Comic' or pagetype == 'Volume'
								then pagetype = 'Vol'
								else pagetype = 'Season'
							end

							info = p.lua_get_title_volume_issue(pagename, pagetype)
							if not h.isempty(text)
								then output = h.Link(info.link.part1, text)
								else output = info.link.all
							end
							if not h.isempty(to_issue)
								then output = output .. '–'.. h.Link(info.noissue..' '..to_issue, to_issue)
							end
							if not h.isempty(and_issue)
								then output = output .. ' and #'.. h.Link(info.noissue..' '..and_issue, and_issue)
							end
						elseif h.in_list({'Board Game', 'Film', 'Novel', 'Series', 'Video Game'}, pagetype)
							then
								link = pagename
								text = string.gsub(pagename, ' %(.+%)', '')
								output = h.Link(link, design.span(text).italic)
						else output = h.Link(pagename)
					end
				else output = h.Link(pagename)
			end
	end
	
	return output
end


--------------------------------------------------------------------------------------------------
function p.comics_link(frame)
	local args = getArgs(frame)
	local arg1 = args[1]
	local output
	
	if not h.isempty(arg1)
		then
			if h.in_list({'-', '--', '---', '—', '—'}, arg1)
				then output = '—'
				else output = p.lua_standardized_link(arg1, 'Comic', args[2])
			end
	end
	
	return output
end


--------------------------------------------------------------------------------------------------
-- returns name transformed into format for sorting 
---- removes "The" from the begining
---- for comics/episodes changes volume/season and issue/episode into numbers, instead of text
function p.lua_standardized_name_for_sorting(pagename, pagetype)
	local info = {}
	local output = pagename

	if not h.isempty(pagename) and not h.isempty(pagetype)
		then
			if pagetype == 'Comic' or pagetype == 'Episode'
				then
					if pagetype == 'Comic'
						then pagetype = 'Vol'
						else pagetype = 'Season'
					end
					info = p.lua_get_title_volume_issue(pagename,pagetype)
					if not h.isempty(info.title)
						then output = info.sortname.all
					end
				elseif h.in_list({'Board Game', 'Film', 'Novel', 'Series', 'Video Game'}, pagetype)
					then output = p.lua_remove_the(pagename)
			end
	end
	
	return output
end


function p.comics_name_for_sorting(frame)
	local args = getArgs(frame)
	return p.lua_standardized_name_for_sorting(args[1], 'Comic')
end


--------------------------------------------------------------------------------------------------
-- function for sorting characters, teams, items, etc. by reality number
function p.name_for_sorting(frame)
	local args = getArgs(frame)
	local module_reality = require('Module:Reality')
	local output = p.lua_remove_the(args[1])
	
	return module_reality.sort_by_reality({output})
end

--------------------------------------------------------------------------------------------------
function p.lua_get_part(pagename,pagetype,part)
	local output = ''
	local info = {}
	
	if not h.isempty(pagename) and not h.isempty(pagetype)
		then 
			if pagetype == 'Comic' or pagetype == 'Episode'
				then
					if pagetype == 'Comic'
						then pagetype = 'Vol'
						else pagetype = 'Season'
					end
					
					info = p.lua_get_title_volume_issue(pagename,pagetype)
					if not h.isempty(title)
						then 
							if part == 'Title'
								then output = info.title
								elseif part == 'Volume'
									then output = info.volume
									elseif part == 'Issue'
										then output = info.issue
							end
					end
			end
	end
	
	return output
end


function p.get_part(frame)
	local args = getArgs(frame)
	return p.lua_get_part(args[1], args[2], args[3])
end


function p.get_title_and_volume(frame)
	local args = getArgs(frame)
	local pagename = args[1]
	local info = {}
	local output = ''
	
	if not h.isempty(pagename)
		then 
			info = p.lua_get_title_volume_issue(pagename, 'Vol')
			output = info.title .. ' Vol ' .. info.volume
	end
	
	return output
end


--------------------------------------------------------------------------------------------------
function p.lua_get_comic_issue_date_and_story_title(pagename, add_story_title)
	local m_date = require("Module:Date")
	local design = require("Module:Design")
	local date = ''
	local story_title = ''
	local div = mw.html.create( 'div' ):attr( 'align', 'center' ):done()
	local output = ''
	
	if not h.isempty(pagename)
		then
			output = p.lua_standardized_link(pagename, 'Comic')
			output = design.span(output).bold
			date, story_title = p.lua_get_release_date(pagename, 'Comic', add_story_title)

			if not h.isempty(date)
				then output = output..story_title..'<br />('..date..')'
			end

			output = tostring( div:wikitext(output) )
	end

	return output
end

-- used in Template:Cid
function p.cid(frame)
	local args = getArgs(frame)
	return p.lua_get_comic_issue_date_and_story_title(args[1], false)
end

-- used in Template:Cis
function p.cis(frame)
	local args = getArgs(frame)
	return p.lua_get_comic_issue_date_and_story_title(args[1], true)
end


--------------------------------------------------------------------------------------------------
-- replaces "#" with "Vol" or "Season"
function p.lua_replace_number_sign(pagename, pagetype)
	local output = ''
	
	if not h.isempty(pagename) 
		then
			if h.isempty(pagetype) 
				then pagetype = ''
			end
			
			if string.find(pagename, '#') ~= nil 
				then
					if string.find(pagename, ' Vol ') ~= nil or string.find(pagename, ' Season ') ~= nil
						then output = string.gsub(pagename, '#', '')
						elseif pagetype == 'Comic' or h.exists( string.gsub(pagename, '#', 'Vol 1 ') )
							then output = string.gsub(pagename, '#', 'Vol 1 ')
						elseif pagetype == 'Episode' or h.exists( string.gsub(pagename, '#', 'Season 1 ') )
							then output = string.gsub(pagename, '#', 'Season 1 ')
					end
				else
					output = pagename
			end
	end
	
	return output		
end

--------------------------------------------------------------------------------------------------
-- returns release date (or cover date if release date is not filled on page). Optionally can also add a story title (for comic issues only)
function p.lua_get_release_date(pagename, pagetype, add_story_title)
	local m_date = require("Module:Date")
	local design = require("Module:Design")
	local page_content = ''
	local release_date = ''
	local year = ''
	local month = ''
	local day = ''
	local story_title = ''
	local output = ''

	if not h.isempty(pagename)
		then
			pagename = p.lua_replace_number_sign(pagename, pagetype)
			if h.isempty(pagetype)
				then pagetype = require("Module:PageType").get_page_type(pagename)
			end
			page_content = h.get_content(pagename)
			if page_content ~= ''
				then
					if pagetype == 'Comic'
						then
							release_date = h.get_field_value(page_content, 'ReleaseDate')
							if not h.isempty(release_date)
								then
									info = m_date.lua_get_release_date_info(release_date)
									output = h.Link(info.week.text, info.no_day)
								else
									month = h.get_field_value(page_content, 'Month')
									year = h.get_field_value(page_content, 'Year')
									info = m_date.lua_get_publication_date_info(year, month, false)
									output = string.gsub(info.link, '%]%], %[%[:Category:%d+|', ', ')
							end
							if add_story_title
								then 
									story_title = h.get_field_value(page_content, 'StoryTitle1')
									story_title = design.lua_get_comic_issue_story_title(story_title)
							end
						elseif pagetype == 'Episode'
							then 
								year = h.get_field_value(page_content, 'Year')
								month = h.get_field_value(page_content, 'Month')
								day = h.get_field_value(page_content, 'Day')
								if not h.isempty(year) and not h.isempty(month)
									then 
										if not h.isempty(day)
											then day = ' '..day
											else day = ''
										end
										month = m_date.get_month_name({month})
										output = h.LinkToCategory(year..', '..month, month..day..', '..year)
								end
						elseif h.in_list({'Board Game', 'Film', 'Novel', 'Video Game'}, pagetype)
							then output = h.get_field_value(page_content, 'Release Date')
					end
			end
	end
	
	return output, story_title
end


--------------------------------------------------------------------------------------------------
function p.lua_get_link_and_release_date(pagename, pagetype, text, release_date)
	local output = ''
	
	if not h.isempty(pagename)
		then
			pagename = p.lua_replace_number_sign(pagename, pagetype)
			if h.isempty(pagetype)
				then pagetype = require("Module:PageType").get_page_type(pagename)
			end
			output = p.lua_standardized_link(pagename, pagetype, text)
			if h.isempty(release_date)
				then release_date = p.lua_get_release_date(pagename, pagetype)
			end
			if not h.isempty(release_date)
				then output = output..'<br>('..release_date..')'
			end
			
			output = output..'<br>'
	end
	
	return output, release_date
end

--------------------------------------------------------------------------------------------------
-- used in Template:Sl
function p.get_link(frame)
	local args = getArgs(frame)
	local pagename = args[1]
	local text = args[2]
	local to_issue = args['to']
	local and_issue = args['and']
	local pagetype = args['type']
	local output = ''
	
	if not h.isempty(pagename)
		then
			pagename = p.lua_replace_number_sign(pagename, pagetype)
			if h.isempty(pagetype)
				then pagetype = require("Module:PageType").get_page_type(pagename)
			end
			output = p.lua_standardized_link(pagename, pagetype, text, to_issue, and_issue)
	end
	if not h.isempty(text)
		then output = output .. '[[Category:Outdated Fields/Links]]'
	end
	
	return output
end


--------------------------------------------------------------------------------------------------
-- used in Template:Sld
function p.get_link_and_release_date(frame)
	local args = getArgs(frame)
	local pagename = args[1]
	local text = args[2]
	local pagetype = args['type']
	local release_date = args['date']
	
	local output = p.lua_get_link_and_release_date(pagename, pagetype, text, release_date)

	return output
end


--------------------------------------------------------------------------------------------------
-- returns link to episode, but replaces text with episode's title
-- used in Template:Elt
function p.get_link_to_episode_with_title(frame)
	local args = getArgs(frame)
	local pagename = args[1]
	local text
	local pagetype = 'Episode'
	local output = ''
	
	if not h.isempty(pagename)
		then
			pagename = p.lua_replace_number_sign(pagename, pagetype)
			text = p.lue_get_episode_title(pagename)
			if text == pagename
				then output = p.lua_standardized_link(pagename, pagetype)
				else output = p.lua_standardized_link(pagename, pagetype, text)
			end
	end
	
	return output
end


--------------------------------------------------------------------------------------------------
-- used in Template:Eltd
function p.get_link_and_release_date_to_episode_with_title(frame)
	local args = getArgs(frame)
	local pagename = args[1]
	local text
	local release_date = args['date']
	local pagetype = 'Episode'
	local output = ''

	if not h.isempty(pagename)
		then
			pagename = p.lua_replace_number_sign(pagename, pagetype)
			text = p.lue_get_episode_title(pagename)
			if text == pagename
				then output = p.lua_standardized_link(pagename, pagetype)
				else output = p.lua_standardized_link(pagename, pagetype, text)
			end
			if h.isempty(release_date)
				then release_date = p.lua_get_release_date(pagename, pagetype)
			end
			if not h.isempty(release_date)
				then output = output..'<br>('..release_date..')'
			end
	end
	
	return output 
end

--------------------------------------------------------------------------------------------------
function p.lue_get_episode_title(pagename)
	local page_content = ''
	local output = ''
	
	if not h.isempty(pagename)
		then
			pagename = p.lua_replace_number_sign(pagename, 'Episode')
			page_content = h.get_content(pagename)
			if page_content ~= ''
				then output = h.get_field_value(page_content, 'EpisodeTitle')
			end
			
			if output ~= ''
				then output = '"'..output..'"'
				else output = pagename
			end
	end
	
	return output
end

return p
Advertisement