Marvel Database
Advertisement
Marvel Database

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

-- module to form Marvel Database:Volume Template
local p = {}
local list_of_series = mw.loadData( 'Module:Volume Template/Series' )
local h = require("Module:HF")
local standard = require("Module:StandardizedName")
local m_date = require("Module:Date")
local design = require("Module:Design")
local getArgs = require('Module:Arguments').getArgs


function p.main(frame)
	local args = getArgs (frame)
	local pagename = args['category'] or mw.title.getCurrentTitle().text
	local previous_volume = args['PreviousVol'] 
	local next_volume = args['NextVol']
	local relaunches = {}
	local issue_list = args['IssueList'] or ''
	local publisher = p.lua_get_publisher(args['publisher'])
	local name_info = standard.lua_get_title_volume_issue(pagename, 'Vol')
	local volume_type
	local volume_type_category
	local volume_format
	local volume_format_category
	local volume_status
	local volume_status_category
	local genres
	local genres_categories
	local main_volume
	local main_volume_category
	local list_of_canceled_issues
	local categories = {}
	local category = ''
	local dpl_string = ''
	local list = {}
	local first_by_date
	local last_by_date
	local next_issue
	local top_message = ''
	local total_issues = 0
	local number_of_canceled_issues = 0
	local display_title = pagename
	local images_category = pagename..'/Images'
	local output = {}

	-- create list of all issues from category with DPL
	dpl_string = frame:preprocess( p.lua_dpl_list(pagename, _, false)..p.lua_dpl_list(pagename, 500, false) )
	list_of_canceled_issues = mw.text.split( frame:preprocess('{{#dpl:|category = Canceled Comics|namespace = |mode = userformat|listseparators = ,%PAGE%,@@@}}'), '@@@' )
	list = p.lua_transform_dpl_string_into_list(dpl_string, args['exclude'], list_of_canceled_issues)
	
	-- add gallery of issues
	if h.isempty(issue_list)
		then issue_list = p.lua_create_issue_list(list, args)
	end
	
	-- add top message
	first_by_date, last_by_date, next_issue = p.lua_get_first_and_last_by_date(list)
	volume_type, volume_type_category = p.lua_get_volume_type(args['type'])
	volume_format, volume_format_category = p.lua_get_volume_format(args['format'], #list, last_by_date, pagename)
	volume_status, volume_status_category = p.lua_get_volume_status(args['status'], first_by_date, last_by_date)
	relaunches = p.lua_get_relaunches(list, args['relaunches'])
	genres, genres_categories = p.lua_get_genres(args['genres'])
	number_of_canceled_issues = p.lua_get_number_of_canceled_issues(list)
	total_issues = p.lua_get_number_of_issues(#list, number_of_canceled_issues)
	previous_volume, next_volume = p.lua_get_previous_and_next_volume(name_info, previous_volume, next_volume)
	
	main_volume, main_volume_category = p.lua_get_main_volume(args['main_volume'], volume_format)
	
	
	top_message = p.lua_top_message(pagename, first_by_date, last_by_date, next_issue, genres, main_volume,
									volume_format, volume_status, volume_type, total_issues, relaunches, publisher,
									previous_volume, next_volume, args)
	

	-- add categories
	if h.exists('Category:'..pagename) == false and h.pages_in_category(pagename, 'pages') > 0
		then table.insert(categories, 'Volume Category Needed')
	end
	if h.exists('Category:'..images_category) == false and h.pages_in_category(images_category, 'files') > 0
		then table.insert(categories, 'Volume Images Category Needed')
	end
	--if h.isempty(args['type'])
	--	then table.insert(categories, 'Volume Type Needed')
	--end
	if h.isempty(args['genres'])
		then table.insert(categories, 'Volume Genre Needed')
	end
	--if h.isempty(args['featured'])
	--	then table.insert(categories, 'Volume Featured Needed')
	--end
	if h.isempty(args['volume_logo'])
		then table.insert(categories, 'Volume Logo Needed')
	end
	if h.isempty(args['format'])
		then table.insert(categories, 'Volume Format Needed')
	end
	table.insert(categories, 'Volumes')
	table.insert(categories, p.lua_volume_by_title(name_info.sortname.title) )
	table.insert(categories, publisher)
	table.insert(categories, volume_format_category)
	categories = h.join_tables(categories, main_volume_category)
	table.insert(categories, volume_type_category)
	table.insert(categories, volume_status_category)
	categories = h.join_tables(categories, genres_categories)
	table.insert(categories, p.lua_volumes_by_number_of_issues(total_issues, canceled) )
	table.insert(categories, p.lua_volume_debut(first_by_date, volume_status))
	if relaunches ~= ''
		then table.insert(categories, 'Relaunched Volumes')
	end
	table.insert(categories, p.lua_volume_end(last_by_date, volume_status))
	if  not h.isempty(args['IssueList']) or 
		not h.isempty(args['VolumeLogo']) or
		not h.isempty(args['Featured'])
		then table.insert(categories, 'Outdated Fields')
	end
	categories = h.join_tables( categories, p.lua_get_comic_series(pagename, args['featured']) )
	categories = h.add_categories(categories, name_info.sortname.noissue)

	-- add everything to page
	running_years = p.lua_get_running_years(first_by_date, last_by_date, volume_status, volume_format)
	display_title = '{{DISPLAYTITLE:'..pagename..running_years..'}}'
	table.insert(output, display_title)
	table.insert(output, top_message)
	table.insert(output, issue_list)
	table.insert(output, design.add_section('See Also', args['SeeAlso'], 2) )
	table.insert(output, design.add_section('Notes', args['Notes'], 2) )
	table.insert(output, '<references/>')
	table.insert(output, categories)

	return frame:preprocess( table.concat(output) )
end


function p.lua_volume_by_title(pagename)
	local first_letter = ''
	local output = ''
	
	first_letter = string.sub(pagename,1,1)
	if string.match(first_letter, '%d') ~= nil
		then output = 'Volumes 0-9'
		elseif string.match(first_letter, '%a') ~= nil
			then output = 'Volumes '..first_letter
		else output = 'Volumes &'
	end
	
	return output
end

function p.lua_volumes_by_number_of_issues(total_issues, canceled)
	local first
	local last
	local i
	local output = ''
	
	i = tonumber( string.match(total_issues, '%d+') )
	
	if not canceled and i > 1
		then
			if i > 1 and i <= 9
				then 
					first = 2
					last = 10
				elseif i < 100
					then 
						first = 10
						last = 20
						while last <= i do
							first = first + 10
							last = last + 10
						end
				else
					first = 100
					last = 150
					while last <= i do
						first = first + 50
						last = last + 50
					end
			end
			output = 'Volumes With '..first..' to '..(last-1)..' Issues'
	end
	
	return output
end


function p.lua_get_number_of_canceled_issues(list)
	local output = 0
	
	for i = 1, #list do
		if list[i].canceled
			then output = output + 1
		end
	end
	
	return output
end


function p.lua_get_number_of_issues(total_issues, number_of_canceled_issues)
	local text
	local n
	local output
	
	if number_of_canceled_issues > 0
		then 
			if total_issues == number_of_canceled_issues
				then 
					if total_issues > 1
						then text = ' issues (all canceled)'
						else text = ' issue (canceled)'
					end
					output = number_of_canceled_issues..text
				else 
					n = total_issues - number_of_canceled_issues
					if n > 1
						then text = ' issues'
						else text = ' issue'
					end
					output = n..text..' (+'..number_of_canceled_issues..' canceled)'
			end
		else 
			if total_issues > 1
				then text = ' issues'
				else text = ' issue'
			end
			output = total_issues..text
	end
	
	return output
end


function p.lua_get_publisher(publisher)
	local output = 'Marvel Comics'
	
	if not h.isempty(publisher) 
		then output = h.break_link(publisher, 1)
	end
	
	return output
end


function p.lua_get_previous_and_next_volume(name_info, previous_volume, next_volume)
	local function link_to_volume(volume, previous)
		local arrow
		local output
		if volume ~= '' and previous == true
			then
				arrow = '[[File:Red Previous-icon.png|15px|link='..volume..']] '
				output = arrow..h.Link(volume, 'Previous Volume')
		elseif volume ~= ''
			then
				arrow = ' [[File:Red Next-icon.png|15px|link='..volume..']]'
				output = h.Link(volume, 'Next Volume')..arrow
		end
		return output
	end
	local function get_volume(volume, volume_n)
		local output = ''
		if not h.isempty(volume)
			then output = h.break_link(volume, 1)
			elseif volume_n ~= '' and h.exists(volume_n)
				then output = volume_n
		end
		return output
	end
	local s1 = ''
	local s2 = ''
	local current_volume = tonumber(name_info.volume)
	
	if current_volume ~= nil
		then 
			s1 = name_info.title..' Vol '..tostring(current_volume-1)
			s2 = name_info.title..' Vol '..tostring(current_volume+1)
	end
	previous_volume = link_to_volume(get_volume(previous_volume, s1), true)
	next_volume = link_to_volume(get_volume(next_volume, s2), false)

	return previous_volume, next_volume
end


function p.lua_get_relaunches(list, relaunches)
	local i
	local j
	local n
	local output = {}
	
	if not h.isempty(relaunches)
		then
			relaunches = p.lua_divide_list(relaunches, false)
			
			for i = 1, #list do
				for j = 1, #relaunches do
					if list[i].pagename == relaunches[j]
						then table.insert(output, h.Link(list[i].pagename, list[i].year))
					end
				end
			end
	end
	
	n = #output
	if n > 0
		then output = mw.text.listToText(output)
		else output = ''
	end

	return output
end

function p.lua_get_genres(genres)
	local i
	local s
	local categories = {}
	local output = {}

	if not h.isempty(genres)
		then
			genres = p.lua_divide_list(genres, false)
			for i = 1, #genres do
				s = genres[i]..' Genre'
				table.insert(categories, s)
				s = h.LinkToCategory(s, genres[i])
				table.insert(output, s)
			end
			output = mw.text.listToText(output, ', ', ', ')
		else
			output = ''
	end
	
	return output, categories
end


function p.lua_get_logo(volume_logo, publisher, logo_size)
	local output = 'Marvel Logo.png'
	
	if h.isempty(logo_size)
		then logo_size = '180px'
	end
	
	if not h.isempty(volume_logo) 
		then output = volume_logo
		elseif publisher ~= 'Marvel Comics'
			then
				if h.in_list({'Atlas Comics', 'Timely Comics', 'Icon Comics', 'CrossGen', 'Marvel Music', 'Marvel Absurd', 'Amalgam Comics', 'Image Comics', 'Marvel Edge', 'Disney Comics', 'Nelson Comics'},  publisher)
					then logo_size = '100px'
				end
				if h.exists('File:'..publisher..'.png')
					then output = publisher..'.png'
					elseif h.exists('File:'..publisher..'.jpg')
						then output = publisher..'.jpg'
				end
	end

	return '<td style="width: 20%; padding-right: 10px; text-align: center;">[[File:'..output..'|'..logo_size..'|center]]</td>'
end


function p.lua_top_message(pagename, first_by_date, last_by_date, next_issue, genres, main_volume,
							volume_format, volume_status, volume_type, total_issues, relaunches, publisher,
							previous_volume, next_volume, args)
	local function tag_td(text, width, font_size)
		return tostring(mw.html.create( 'td' )
				:css('width', width)
				:css('font-size', font_size)
				:wikitext(text)
				:done() )
	end
	local function add_row(label, value)
		local output = nil
		if not h.isempty(value)
			then output = design.span(label..': ').bold..value..'<br>'
		end
		return output
	end
	local volume_logo = p.lua_get_logo(args['volume_logo'], publisher, args['volume_logo_size'])
	local featured = p.lua_divide_list(args['featured'] or '', true)
	local message = ''
	local second_row = {}
	local first_link = first_by_date.link
	local last_link = last_by_date.link
	local first_link2 = string.match(first_link, '(%[%[.+%]%]),')
	local dates
	local rows = {}
	local s = nil
	local output = {}
	
	table.insert(rows, add_row('Publisher', h.Link(publisher) ) )
	if volume_type ~= ''
		then volume_type = ' ('..volume_type..')'
	end
	table.insert(rows, add_row('Type', volume_format..volume_type) )
	table.insert(rows, add_row('Main Volume', main_volume) )
	table.insert(rows, add_row('Genre', genres) )
	table.insert(rows, add_row('Featuring', mw.text.listToText(featured)) )
	message = tag_td(table.concat(rows), '40%', '85%')
	
	
	rows = {}
	if volume_status == 'Finished'
		then
			if volume_format == 'One Shot' or first_link == last_link
				then dates = first_link
				elseif first_by_date.year ~= '' 
					   and first_by_date.year == last_by_date.year 
					   and first_by_date.month ~= ''
					then dates = first_link2..'—'..last_link
					else dates = first_link..'—'..last_link
			end
		elseif volume_status == 'Active' or volume_status == 'Announced'
			then 
				if volume_format == 'One Shot'
					then dates = first_link
					else dates = first_link..'—onwards'
				end
				if volume_status == 'Announced'
					then s = 'Announced Publication Date'
				end
		elseif volume_status == 'Canceled'
			then 
				dates = first_by_date.release_date
				if h.isempty(dates)
					then dates = first_link
				end
				s = 'Intended Publication Date'
	end
	table.insert(rows, add_row('Status', volume_status) )
	table.insert(rows, add_row(s or 'Publication Date', dates) )
	table.insert(rows, add_row('Relaunched in', relaunches) )
	if volume_status == 'Active' and next_issue ~= ''
		then
			s = standard.lua_standardized_link(next_issue.pagename, 'Comic')
			s = h.Link(next_issue.pagename, string.match(s, '(#.+)]]'))
			if next_issue.release_date ~= ''
				then s = s..' ('..next_issue.release_date..')'
				else s = s..' ('..next_issue.link..')'
			end
			table.insert(rows, add_row('Next Release', s) )
	end
	if last_by_date.release_date ~= '' and last_by_date.released == false
		then 
			s = standard.lua_standardized_link(last_by_date.pagename, 'Comic')
			s = h.Link(last_by_date.pagename, string.match(s, '(#.+)]]'))
			s = s..' ('..last_by_date.release_date..')'
			table.insert(rows, add_row('Latest Solicited Issue', s) )
	end
	message = message..tag_td(table.concat(rows), '40%', '85%')


	table.insert(output, '<table align="center" style="width: 100%; text-align:left;"><tr>'..volume_logo..message..'</tr>')
	table.insert(output, '</table>')
	table.insert(output, '<table align="center" style="width: 100%; text-align:center;">')
	
	table.insert(second_row, h.LinkToCategory(pagename, total_issues))
	table.insert(second_row, p.lua_get_number_of_pages_in_subcategory(pagename, 'Images', 'files', 'image') )
	table.insert(second_row, p.lua_get_number_of_pages_in_subcategory(pagename, 'Annuals', 'pages', 'annual') )
	table.insert(second_row, p.lua_get_number_of_pages_in_subcategory(pagename, 'Specials', 'pages', 'special') )
	table.insert(second_row, p.lua_get_number_of_pages_in_subcategory(pagename, 'TPB', 'pages', 'TPB edition') )
	second_row = mw.text.listToText(second_row, '&nbsp;•&nbsp;', '&nbsp;•&nbsp;')
	previous_volume = tag_td(previous_volume, '20%', '100%')
	next_volume = tag_td(next_volume, '20%', '100%')
	table.insert(output, '<tr>'..previous_volume..'<td style="width:60%;">'..second_row..'</td>'..next_volume..'</tr>')
	table.insert(output, '</table>')

	return design.messagebox( { table.concat(output), padding = '10px' } )
end


function p.lua_get_number_of_pages_in_subcategory(pagename, subcategory, page_type, link_name)
	local category = pagename..'/'..subcategory
	local category_counter = h.pages_in_category(category, page_type)
	local output = nil
	
	if category_counter > 0
		then 
			if category_counter == 1
				then output = ' '..link_name
				else output = ' '..link_name..'s'	
			end
			output = h.LinkToCategory(category, category_counter..output)
	end
	
	return output
end


function p.lua_get_volume_status(volume_status, first_by_date, last_by_date)
	local s = ''
	local first = first_by_date.sort_date
	local last = last_by_date.sort_date
	local current_date = tonumber( m_date.lua_date_converter('now', 'Ym') ) 
	local date_of_last_solicit = tonumber( m_date.lua_date_converter('now + 4 months', 'Ym') ) 
		-- +4 months because pages for new comics normally created shortly after publication of solicitations 
		-- solicitations appear 2 months before the release date
		-- publication is 2 months after release
	local output = ''
	local category = ''

	if not h.isempty(volume_status)
		then
			s = string.lower(volume_status)
			if s == 'active'
				then
					output = 'Active'
					category = 'Active Volumes'
				elseif s == 'finished'
					then 
						output = 'Finished'
						category = 'Finished Volumes'
				elseif s == 'canceled'
					then
						output = 'Canceled'
						category = 'Canceled Volumes'
				else output = volume_status
			end
		elseif first >= current_date and first_by_date.released == false
			then
				output = 'Announced'
				category = 'Announced Volumes'
		elseif last < 999901 and ( (last_by_date.release_date ~= '' and not last_by_date.released) or not last_by_date.published ) --last >= date_of_last_solicit
			then
				output = 'Active'
				category = 'Active Volumes'
		elseif first_by_date.canceled
			then 
				output = 'Canceled'
				category = 'Canceled Volumes'
		else 
			output = 'Finished'
			category = 'Finished Volumes'
	end
	
	return output, category
end
	

function p.lua_get_volume_format(volume_format, total_issues, last_by_date, pagename)
	local s = ''
	local last = last_by_date.sort_date
	local current_date = tonumber( m_date.lua_date_converter('now + 5 months', 'Ym') )
	local output = ''
	local category = ''
	
	total_issues = tonumber(total_issues)
	
	if not h.isempty(volume_format)
		then
			s = string.lower(volume_format)
			if s == 'limited' or s == 'limited series'
				then 
					output = 'Limited Series'
					category = output
				elseif s == 'one shot' or s == 'one-shot'
					then 
						output = 'One Shot'
						category = 'One Shots'
				elseif s == 'ongoing' or s == 'ongoing series'
					then output = 'Ongoing Series'
				elseif s == 'reprint'
					then
						output = 'Reprint Series'
						category = 'Reprint Series'
				elseif s == 'annual'
					then
						if total_issues == 1 and last < 999901 and last < current_date
							then output = 'Annual'
							else output = 'Annual Series' 
						end
						category = 'Annuals'
				elseif s == 'handbook'
					then
						if total_issues == 1 and last < 999901 and last < current_date
							then output = 'Handbook'
							else output = 'Handbook Series' 
						end
						category = 'Handbooks'
				elseif s == 'tpb' or s == 'hc'
					then
						output = 'TPB Edition'
						category = 'Trade Paperbacks'
				else output = volume_format
			end
		elseif total_issues == 1 and last < 999901 and last < current_date
			then 
				output = 'One Shot'
				category = 'One Shots'
		elseif total_issues <= 5 and last < 999901 and last < current_date
			then 
				output = 'Limited Series'
				category = output
		else output = 'Ongoing Series'
	end
	
	-- the following variants override any previous value
	-- check if Annual
	if string.find(pagename, 'Annual', 1, true) ~= nil
		then
			if output == 'One Shot'
				then output = 'Annual'
				else output = 'Annual Series' 
			end
			category = 'Annuals'
	end
	
	-- check if Handbook
	if string.find(pagename, 'Handbook', 1, true) ~= nil or
	   string.find(pagename, 'OHOTMU', 1, true) ~= nil or
	   string.find(pagename, 'OHOTUMU', 1, true) ~= nil
		then
			if output == 'One Shot'
				then output = 'Handbook'
				else output = 'Handbook Series' 
			end
			category = 'Handbooks'
	end
	
	-- check if TPB 
	if string.find(pagename, ' TPB ', 1, true) ~= nil or
	   string.find(pagename, ' HC ', 1, true) ~= nil
		then 
			output = 'TPB Edition'
			category = 'Trade Paperbacks'
	end

	return output, category
end


function p.lua_get_volume_type(volume_type)
	local s
	local category = ''
	local output = ''
	
	if not h.isempty(volume_type)
		then
			s = string.lower(volume_type)
			if s == 'solo'
				then
					category = 'Solo Volumes'
					output = 'Solo'
				elseif s == 'team'
					then
						category = 'Team Volumes'
						output = 'Team'
				elseif s == 'team-up' or s == 'team up'
					then
						category = 'Team-Up Volumes' 
						output = 'Team-Up'
				elseif s == 'event'
					then
						output = 'Event'
						category = 'Event Volumes' 
				elseif s == 'adaptation'
					then
						output = 'Adaptation'
						category = 'Adaptation Volumes' 
				else
					output = volume_type
			end
	end
	
	return output, category
end	


function p.lua_get_main_volume(main_volume, volume_format)
	local categories = {}
	local output = ''
	
	if not h.isempty(main_volume)
		then
			output = p.lua_divide_list(main_volume)
			for i = 1,#output do
				if volume_format == 'TPB Edition'
					then table.insert(categories, output[i]..'/TPB')
					elseif string.find(volume_format, 'Annual') ~= nil
						then table.insert(categories, output[i]..'/Annuals')
					else table.insert(categories, output[i]..'/Specials')
				end
				output[i] = h.Link(output[i])
			end
			output = mw.text.listToText(output)
	end
	
	return output, categories
end	


function p.lua_divide_list(list, return_links)
	local link = ''
	local text = ''
	local s
	local i
	local output = {}

	if not h.isempty(list)
		then 
			if string.find(list, ';', 1, true) ~= nil 
				then
					list = mw.text.split(list, ';')
					for i = 1,#list do
						s = mw.text.trim(list[i])
						if return_links
							then
								link = h.break_link(s, 1)
								text = h.break_link(s, 2)
								s = h.Link(link, text)
						end
						table.insert(output, s)
					end
				elseif not h.is_link(list) and h.exists(list)
					then
						list = mw.text.trim(list)
						if return_links
							then s = h.Link(list, list)
							else s = list
						end
						table.insert(output, s)
					else table.insert(output, mw.text.trim(list))
			end
	end
	
	return output
end


function p.lua_create_issue_list(list, args)
	local parts = {}
	local above = {}
	local below = {}
	local i
	local j
	local k
	local s = {}
	local output = {}
	
	for i = 1,9 do
		s = args['part'..i]
		if not h.isempty(s)
			then table.insert(parts, tonumber(standard.lua_padded_issue(s)))
		end
		table.insert(above, args['part'..i..'_above'] or '')
		table.insert(below, args['part'..i..'_below'] or '')
	end
	
	if h.isempty(parts)
		then output = { p.lua_create_gallery(list, above[1], below[1]) }
		else
			if parts[#parts] < tonumber(list[#list].padded_issue)
				then table.insert(parts, tonumber(list[#list].padded_issue) )
			end
			k = 1
			for i = 1,#parts do
				s = {}
				for j = k,#list do
					if tonumber(list[j].padded_issue) <= parts[i]
						then table.insert(s, list[j])
						else
							k = j
							break
					end
				end
				table.insert(output, p.lua_create_gallery(s, above[i], below[i]) )
			end
	end
	
	return table.concat(output)
end


function p.lua_create_gallery(list, above, below)
	local s
	local output = {}
	above = above or ''
	below = below or ''

	for i = 1,#list do
		s = p.lua_link_dates_storytitle(list[i])
		table.insert(output, '\n'..list[i].image..'{{!}}'..s)
	end
	output = table.concat(output)
	output = '{{#tag:gallery|'..output..'|position=center|captionalign=center|hideaddbutton=true|widths=200px}}'
	
	return above..output..below
end


function p.lua_transform_dpl_string_into_list(dpl_string, exclude, list_of_canceled_issues)
	local list = {}
	local issue_info = {}
	local image = ''
	local storytitle = ''
	local release_date = ''
	local pagename = ''
	local canceled = false
	local sort_date = ''
	local month
	local year
	local link
	local s = ''
	local output = {}
	
	if not h.isempty(dpl_string) and dpl_string ~= 'no_resultsno_results'
		then
			dpl_string = string.gsub(dpl_string, 'no_results', '')
			list = mw.text.split(dpl_string, '@@@', true)
			
			if not h.isempty(exclude)
				then exclude = mw.text.split(exclude, ";", true)
				else exclude = nil
			end
			
			for i = 1, #list do
				if not h.isempty(list[i])
					then
						s = mw.text.split(list[i], '~', true)
						pagename = s[1]
						if exclude ~= nil and exclude[pagename] ~= nil
							then s = ''
							else
								canceled = h.in_list(list_of_canceled_issues, pagename)
								issue_info = standard.lua_get_title_volume_issue(pagename)
								year = s[2]
								month = m_date.get_month_name({s[3]}) or ''
								image = p.lua_cover(s[4], issue_info)
								if not h.isempty(s[5])
									then release_date = m_date.lua_date_converter(s[5], 'F j, Y')
									else release_date = ''
								end
								storytitle = s[6]
								if h.isempty(year)
									then year = ''
								end
								link = m_date.lua_get_publication_category(year, month)
								sort_date = p.lua_get_sort_date(year, month)
								s ={['pagename'] = pagename, 
									['image'] = image, 
									['storytitle'] = storytitle, 
									['year'] = year,
									['month'] = month, 
									['release_date'] = release_date,
									['link'] = link,
									['padded_issue'] = issue_info.padded_issue,
									['canceled'] = canceled,
									['sort_date'] = sort_date,
								}
								table.insert(output, s)
						end
				end
			end
		table.sort(output, p.lua_sort_by_issue)
	end
	
	return output
end


function p.lua_dpl_list(pagename, offset)
	local template = '{Marvel Database:Comic Template}'
	local image = template..':Image, '
	local year = template..':Year, '
	local month = template..':Month, '
	local release_date = template..':ReleaseDate, '
	local story_title = template..':StoryTitle1'
	local secseparators = '%PAGE%~,~,,~,,~,,~,,@@@'
	local category = ''
	local output = {}

	if string.find(pagename, '&', 1, true) ~= nil
		then 
			pagename = string.gsub(pagename, '%(', '\\(')
			pagename = string.gsub(pagename, '%)', '\\)')
			pagename = string.gsub(pagename, '%?', '\\?')	
			category = '|categoryregexp = ^'..pagename..'$'
		else category = '|category = '..pagename
	end

	table.insert(output, '{{#dpl:')
	table.insert(output, category)
	table.insert(output, '|namespace = ')
	table.insert(output, '|include = '..year..month..image..release_date..story_title)
	table.insert(output, '|mode = userformat')
	table.insert(output, '|secseparators = '..secseparators)
	table.insert(output, '|noresultsheader = no_results')
	table.insert(output, '|allowcachedresults = true')
	if not h.isempty(offset)
		then table.insert(output, '|offset ='..offset)
	end
	table.insert(output, '}}')

	return table.concat(output)
end


function p.lua_sort_by_issue(a, b)
	local issue1 = a.padded_issue
	local issue2 = b.padded_issue
	local output
	
	if h.isempty(issue1) or h.isempty(issue2)
		then output = false
		else
			if tonumber(issue1) ~= nil
				then
					if tonumber(issue2) ~= nil
						then output = tonumber(issue1) < tonumber(issue2)
						else output = true
					end
				elseif tonumber(issue2) ~= nil
					then output = false
					else output = issue1 < issue2
			end
	end

	return output
end


function p.lua_sort_by_date(a, b)
	if a.sort_date == b.sort_date
		then return p.lua_sort_by_issue(a, b)
		else return a.sort_date < b.sort_date
	end
end


function p.lua_get_sort_date(year, month)
	if h.isempty(year)
		then year = '9999'
	end
	month = m_date.get_month_number({month}) or '01'

	return tonumber(year..month)
end


function p.lua_get_first_and_last_by_date(list)
	local function is_released(release_date)
		local year
		local output = true
		
		if release_date ~= ''
			then 
				year = string.match(release_date, '%d%d%d%d')
				if tonumber(year) >= 2020
					then output = m_date.lua_date_comparison(release_date)
				end
		end
		
		return output
	end
	local list_sorted_by_date = {}
	local first_by_date
	local last_by_date = ''
	local next_issue = ''
	local j
	local release_date
	local released
	local published
	local info
	
	if not h.isempty(list)
		then
			list_sorted_by_date = mw.clone(list)
			table.sort(list_sorted_by_date, p.lua_sort_by_date)
			first_by_date = list_sorted_by_date[1]
			info = m_date.lua_get_publication_date_info(first_by_date.year, first_by_date.month)
			if tonumber(info.year) >= 2020
				then published = m_date.lua_date_comparison(info.month..' 1,'..info.year)
				else published = true
			end
			first_by_date.published = published
			first_by_date.released = is_released(first_by_date.release_date)

			if #list_sorted_by_date == 1
				then last_by_date = first_by_date
				else
					for i = #list_sorted_by_date,1,-1 do
						if list_sorted_by_date[i].sort_date < 999901 and not list_sorted_by_date[i].canceled
							then 
								last_by_date = list_sorted_by_date[i]
								j = i
								break
						end
					end
					
					if last_by_date == ''
						then last_by_date = first_by_date
						else
							info = m_date.lua_get_publication_date_info(last_by_date.year, last_by_date.month)
							if tonumber(info.year) >= 2020
								then published = m_date.lua_date_comparison(info.month..' 1,'..info.year)
								else published = true
							end
							last_by_date.published = published
							last_by_date.released = is_released(last_by_date.release_date)
					end
					
					if not last_by_date.released and not last_by_date.published
						then
							for i = j, 1, -1 do
								release_date = list_sorted_by_date[i].release_date
								if release_date ~= ''
									then
										released = is_released(release_date)
										if released == true
											then next_issue = list_sorted_by_date[i+1]
										end
									else
										info = m_date.lua_get_publication_date_info(last_by_date.year, last_by_date.month)
										if tonumber(info.year) >= 2020
											then published = m_date.lua_date_comparison(info.month..' 1,'..info.year)
											else published = true
										end
										if published == true
											then next_issue = list_sorted_by_date[i+1]
										end
								end
								
								if next_issue ~= ''
									then break
								end
							end
					end
			end
	end
	
	return first_by_date, last_by_date, next_issue
end


function p.lua_get_running_years(first_by_date, last_by_date, volume_status, volume_format)
	local first_year = first_by_date.year
	local last_year = last_by_date.year
	local output = ''
	
	if first_year ~= '' and last_year ~= ''
		then
			if volume_status == 'Canceled'
				then output = volume_status
				elseif volume_format == 'One Shot'
					then output = first_year
				elseif volume_status == 'Active'
					then output = first_year..'–...'
				elseif first_year == last_year
					then output = first_year
				else output = first_year..'–'..last_year
			end
			output = tostring( mw.html.create('span')
				:css('font-size', '80%')
				:css('font-style', 'italic')
				:css('font-family', 'monospace')
				:wikitext(' ('..output..')') 
			)
	end
	
	return output
end


-- checks if volume title is in list of known comics books series (for example, "Wolverine Comics Books", "Spider-Man Comics Books", etc.)
function p.lua_get_comic_series(pagename, featured)
	local series
	local title
	local names = {}
	local link = ''
	local text = ''
	local output = {}

	--table.insert(names, pagename)
	if not h.isempty(featured)
		then 
			featured = p.lua_divide_list(featured)
			for i = 1,#featured do
				if not h.isempty(featured[i])
					then
						link = string.gsub(h.break_link(featured[i], 1), ' %(.-%)', '')
						text = string.gsub(h.break_link(featured[i], 2), ' %(.-%)', '')
						table.insert(names, link)
						--table.insert(names, text)
				end
			end
	end   
	
	--series = list_of_series[pagename]
	--if series == nil
		--then
			for title, series in pairs(list_of_series) do
				for i = 1, #names do
					if string.lower(names[i]) == string.lower(title)
						then table.insert(output, series..' Comic Books')
					end
				end
			end
	--end 
	table.sort(output)

	return output
end


-- return "image", if not empty, otherwise check if main cover or textless cover exists, if not, then return 'No Image Cover.jpg'
function p.lua_cover(image, issue_info)
	local cover = ''
	local textless = ''
	local output = ''

	if not h.isempty(image)
		then output = string.gsub(image, '%+', '%%2B')
		else
			cover = issue_info.filename.all
			textless = cover..' Textless.jpg'
			cover = cover..'.jpg'
			if h.exists('File:'..cover)
				then output = cover
				elseif h.exists('File:'..textless)
					then output = textless
					else output = 'No Image Cover.jpg'
			end
	end
	
	return output
end


function p.lua_storytitle(storytitle)
	local output = ''
	local tag = mw.html.create( 'span' )
				:css( 'color', 'grey' )
				:css( 'font-style', 'italic' )
				
	if not h.isempty(storytitle)
		then 
			storytitle = string.gsub(storytitle, '"', '')
			output = '<br />'..tostring( tag:wikitext( '"'..storytitle..'"' ):done() )
	end

	return output
end


function p.lua_link(pagename)
	local output = ''
	local tag = mw.html.create( 'span' )
				:css( 'font-weight', 'bold' )
	
	if not h.isempty(pagename)
		then
			output = tag:wikitext( standard.lua_standardized_link(pagename, 'Comic') ):done()
			output = tostring(output)
	end
	
	return output
end


function p.lua_volume_debut(first_by_date, volume_status)
	local month = first_by_date.month
	local year = first_by_date.year
	local output = ''
	
	if volume_status ~= 'Canceled'
		then
			if year ~= ''
				then
					if month ~= ''
						then output = month..' '..year..' Volume Debut'
						else output = year..' Volume Debuts'
					end
			end
	end
	
	return output
end


function p.lua_volume_end(last_by_date, volume_status)
	local month = last_by_date.month
	local year = last_by_date.year
	local output = ''
	
	if volume_status == 'Finished'
		then
			if year ~= ''
				then
					if month ~= ''
						then output = month..' '..year..' Volume End'
						else output = year..' Volume Ends'
					end
			end
	end
	
	return output
end


function p.lua_link_dates_storytitle(issue_info)
	local publication_date = issue_info.link
	local release_date = issue_info.release_date
	local dates = ''
	local link = p.lua_link(issue_info.pagename)
	local storytitle = p.lua_storytitle(issue_info.storytitle)
	local s1
	local s2
	local output

	if not h.isempty(publication_date)
		then publication_date = '<br />'..design.span('Cover date: ').bold..publication_date
		else publication_date = ''
	end
	if release_date ~= ''
		then 
			release_date = m_date.lua_get_link_to_release_week(release_date)
			release_date = '<br />'..design.span('Release date: ').bold..release_date
		else release_date = ''
	end

	if issue_info.canceled 
		then dates = '<br />Canceled'
		else dates = release_date..publication_date
	end

	output = storytitle..dates

	if output ~= ''
		then output = tostring(mw.html.create('span')
						:css('font-size', '80%')
						:css('font-family', 'monospace')
						:wikitext(output)
						:done()
					)
	end

	output = '<center>'..tostring(mw.html.create('div')
						:css('line-height', '90%')
						:wikitext(link..output)
						:done()
					)..'</center>'

	return output
end


---------------------------------------------------------
-- for Template:Volumes by Number of Issues
function p.volumes_by_number_of_issues_category(frame)
	local pagename = mw.title.getCurrentTitle().text
	local previous_arrow = '[[File:Red Previous-icon.png|15px]] '
	local next_arrow = ' [[File:Red Next-icon.png|15px]]'
	local previous_category = ''
	local next_category = ''
	local first
	local last
	local category = ''
	local output = ''
	
	if pagename == 'One Shots'
		then 
			category = h.Category('Volumes by Number of Issues', '001')
			previous_category = ''
			next_category = h.LinkToCategory('Volumes With 2 to 9 Issues', '002-009')
			output = 'One Shots'
		elseif string.find(pagename, 'Volumes With') ~= nil
			then
				first, last = string.match(pagename, 'Volumes With (%d+) to (%d+) Issues')
				last = string.rep('0', 3-#tostring(last))..last
				if #first == 1
					then 
						first = '00'..first
						previous_category = 'One Shots'
						next_category = 'Volumes With 10 to 19 Issues'
						previous_category = h.LinkToCategory(previous_category, 'One Shots')
						next_category = h.LinkToCategory(next_category, '010-019')
					elseif pagename == 'Volumes With 10 to 19 Issues'
						then
							previous_category = 'Volumes With 2 to 9 Issues'
							next_category = 'Volumes With 20 to 29 Issues'
							previous_category = h.LinkToCategory(previous_category, '002-009')
							next_category = h.LinkToCategory(next_category, '020-029')
							first = '010'
					elseif first == '90'
						then
							previous_category = 'Volumes With 80 to 89 Issues'
							next_category = 'Volumes With 100 to 149 Issues'
							previous_category = h.LinkToCategory(previous_category, '080-089')
							next_category = h.LinkToCategory(next_category, '100-149')
							first = '090'
					elseif #first == 2
						then
							first = tonumber(first)
							previous_category = 'Volumes With ' .. first-10 .. ' to ' .. first-1 .. ' Issues'
							next_category = 'Volumes With ' .. first+10 .. ' to ' .. first+19 .. ' Issues'
							previous_category = h.LinkToCategory(previous_category, string.rep('0', 3-#tostring(first-10))..first-10 ..'-'.. string.rep('0', 3-#tostring(first-1))..first-1)
							next_category = h.LinkToCategory(next_category, string.rep('0', 3-#tostring(first+10))..first+10 ..'-'.. string.rep('0', 3-#tostring(first+19))..first+19 )
							first = '0'..first
					elseif first == '100'
						then
							previous_category = 'Volumes With 90 to 99 Issues'
							next_category = 'Volumes With 150 to 199 Issues'
							previous_category = h.LinkToCategory(previous_category, '090-099')
							next_category = h.LinkToCategory(next_category, '150-199')
					else
						first = tonumber(first)
						previous_category = 'Volumes With ' .. first-50 .. ' to ' .. first-1 .. ' Issues'
						next_category = 'Volumes With ' .. first+50 .. ' to ' .. first+99 .. ' Issues'
						previous_category = h.LinkToCategory(previous_category, first-50 ..'-'.. first-1)
						next_category = h.LinkToCategory(next_category, first+50 ..'-'.. first+99)
				end
				output = first..'-'..last
				category = h.Category('Volumes by Number of Issues', first)
	end
	
	
	output = tostring(mw.html.create('td')
				:css('width', '40%')
				:wikitext(output)
				:done())

	if previous_category ~= ''
		then previous_category = previous_arrow..previous_category
	end
	previous_category = tostring(mw.html.create('td')
				:css('width', '30%')
				:wikitext(previous_category)
				:done())
	
	next_category = tostring(mw.html.create('td')
				:css('width', '30%')
				:wikitext(next_category..next_arrow)
				:done())

	output = tostring(mw.html.create('table')
				:css('width', '100%')
				:css('font-size', '18px')
				:wikitext(previous_category..output..next_category)
				:done())
	
	output = design.messagebox({output})..category
	
	return output
end


---------------------------------------------------------
-- for Template:Volume Debut and Template:Volume End
function p.volume_debut_or_end_category(frame)
	local pagename = mw.title.getCurrentTitle().text
	local s_type = ''
	local year
	local month
	local sort_name = pagename
	local message = 'A listing of all the comic volumes which '
	local categories = {}
	local output = ''

	if string.find(pagename, 'Debut') ~= nil
		then s_type = 'Debut'
		elseif string.find(pagename, 'End') ~= nil
			then s_type = 'End'
	end
	message = message..string.lower(s_type)..'ed in '
	
	year = string.match(pagename, '(%d+) Volume '..s_type..'s')

	if year ~= nil
		then
			table.insert(categories, 'Volume '..s_type..'s')
			table.insert(categories, year)
			output = message..h.LinkToCategory(year, year)..'.'
		else
			month, year = string.match(pagename, '(.+) (%d+) Volume '..s_type)
			table.insert(categories, year..' Volume '..s_type..'s')
			table.insert(categories, year..', '..month)
			sort_name = ' '..m_date.get_month_number({month})..pagename
			output = message..h.LinkToCategory(year..', '..month, month..', '..year)..'.'
	end
	
	output = design.messagebox({output})
	categories = h.add_categories(categories, sort_name)

	return output..categories
end


return p
Advertisement