Marvel Database
Advertisement

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

local p = {}
local getArgs = require('Dev:Arguments').getArgs
local h = require("Module:HF")
local standard = require("Module:StandardizedName")
local m_date = require("Module:Date")
local SC = require("Module:StaffCorrection")
local pagetype = require("Module:PageType")
local design = require("Module:Design")
local function get_pagename()
	return mw.title.getCurrentTitle().text
end
local function positions_in_list(list, value)
	local i
	local output = {}
	for i = 1, #list do
		if list[i] == value
			then table.insert(output, i)
		end
	end
	return output
end


------------------------------------------------------------------------------------------
function p.main(frame)
	local args = getArgs(frame)
	local pagename = get_pagename()
	local list_of_handbooks
	local handbook
	local story_contributors
	local reprints
	local adaptation
	local part_of_info
	local release_info = m_date.lua_get_release_date_info(args['ReleaseDate'])
	local released = m_date.lua_date_comparison(release_info.date)
	local canceled = p.lua_is_canceled(args)
	local i
	local number_of_stories = p.lua_get_number_of_stories(args)
	local categories
	local value
	local collapse = 'open'
	local no_credits = ''
	local appearances_and_synopses = {}
	local output_categories = {}
	local output = {}
	
	categories = p.lua_get_outdated_fields(args)
	output_categories = h.join_tables(output_categories, categories)
	
	-- check if this comic issue is handbook or not
	list_of_handbooks = '{{#dpl:|category = Handbooks|namespace = |mode = userformat|listseparators = ,%PAGE%,@@@}}'
	list_of_handbooks = mw.text.split( frame:preprocess(list_of_handbooks), '@@@' )
	handbook = p.lua_is_handbook(list_of_handbooks)

	reprints, categories = p.lua_get_reprints_content(args)
	output_categories = h.join_tables(output_categories, categories)

-- add credits, appearances and synopses
	for i = 1, number_of_stories do
		if i > 1
			then collapse = 'closed'
		end
		
		story_contributors, categories = p.lua_get_story_contributors(args, reprints[i], i, released, canceled)
		output_categories = h.join_tables(output_categories, categories)

		if table.concat(story_contributors) == ''
			then no_credits = 'Credits not yet listed'
			else no_credits = ''
		end
		
		adaptation, categories = p.lua_adopted_story(args, i)
		output_categories = h.join_tables(output_categories, categories)
		
		part_of_info, categories = p.lua_get_part_of_info(args, i)
		output_categories = h.join_tables(output_categories, categories)

		value = frame:expandTemplate{ 
			title = 'Marvel Database:Comic Template/Credits', 
			args = { 
					Collapse		= collapse,
					StoryTitle		= p.lua_get_story_title(args, reprints[i], i, true),
					NoCredits		= no_credits, -- if all credits are empty, but some other field(s) for story is filled, this message is shown 
					ReprintOf		= p.lua_reprint_message(args, reprints[i], i, true),
					AdaptedFrom		= adaptation,
					Writers 		= story_contributors[1],
					Pencilers		= story_contributors[2],
					Inkers			= story_contributors[3],
					Colorists		= story_contributors[4],
					Letterers		= story_contributors[5],
					Editors			= story_contributors[6],
					PartOf			= part_of_info[1],
					PartOf_Previous	= part_of_info[2],
					PartOf_Next		= part_of_info[3],
					} 
		}
		table.insert(output, value)
		
		value, categories = p.lua_get_story_appearances_and_synopsis(args, reprints[i], i, released, canceled, handbook)
		table.insert(appearances_and_synopses, value)
		output_categories = h.join_tables(output_categories, categories)
	end

-- add sections	
	value, categories = p.lua_add_quote(args)
	output_categories = h.join_tables(output_categories, categories)
	table.insert(output, value)
	
	if released and not canceled
		then table.insert(output, table.concat(appearances_and_synopses) )
	end
	table.insert(output, design.add_section('Solicit Synopsis', args['Solicit'], 2)) 
	table.insert(output, design.add_section('Notes', args['Notes'], 2))
	table.insert(output, design.add_section('Trivia', args['Trivia'], 2))
	
	table.insert(output, design.add_header('See Also', 2))
	value, categories = design.links_to_subcategories(pagename..'/Images', 'files', pagename, ' image(s) from ', 'Images Category Needed')
	output_categories = h.join_tables(output_categories, categories)
	table.insert(output, value)
	value, categories = design.links_to_subcategories(pagename..'/Reprints', 'pages', pagename, ' reprint(s) of ', 'Reprints Category Needed')
	output_categories = h.join_tables(output_categories, categories)
	table.insert(output, value)

	table.insert(output, design.add_section('Recommended Reading', args['Recommended'], 2) )
	table.insert(output, design.add_links_and_references(args, pagename) )
	output = table.concat(output)

	return frame:preprocess(output)..h.add_categories(output_categories)
end


------------------------------------------------------------------------------------------
function p.lua_get_outdated_fields(args)
	local i
	local j
	local k
	local output = {}
	
	for j = 1, 90 do
		if not h.isempty(args['CustomRole'..j]) or not h.isempty(args['CustomMembers'..j])
			then table.insert(output, 'Outdated Fields/CustomRole')
		end
		for k = 1, 50 do
			if not h.isempty(args['CustomRole'..j..'_'..k]) or not h.isempty(args['CustomMembers'..j..'_'..k])
				then table.insert(output, 'Outdated Fields/CustomRole')
			end
		end
	end
--[[
	if  not h.isempty(args.Event)
		or not h.isempty(args.EventName)
		or not h.isempty(args.StoryArc)
		or not h.isempty(args.StoryArcName)
		or not h.isempty(args.Storyline)
		or not h.isempty(args.StorylineName)
		then table.insert(output, 'Outdated Fields/Comic Events and Stories')
	end	
	for i = 2, 10 do
		if  not h.isempty(args['Event'..i..'Name']) 
			or not h.isempty(args['StoryArc'..i..'Name']) 
			or not h.isempty(args['Storyline'..i..'Name']) 
			then table.insert(output, 'Outdated Fields/Comic Events and Stories')
		end
	end
	
	if  not h.isempty(args.Title)
		or not h.isempty(args.Title2)
		or not h.isempty(args.Volume)
		or not h.isempty(args.Issue)
		or not h.isempty(args.HeadTitle)
		then table.insert(output, 'Outdated Fields/Titles')
	end
	
	if  not h.isempty(args.ChiefCreativeOfficer)
		or not h.isempty(args.CCO)
		then table.insert(output, 'Outdated Fields/CCO')
	end
	
	if  not h.isempty(args.NotesHeader)
		or not h.isempty(args.TriviaHeader)
		or not h.isempty(args.CustomSection1)
		or not h.isempty(args.CustomText1)
		or not h.isempty(args.CustomSection2)
		or not h.isempty(args.CustomText2)
		or not h.isempty(args.SeeAlsoHeader)
		or not h.isempty(args.RecommendedHeader)
		or not h.isempty(args.LinksHeader)
		then table.insert(output, 'Outdated Fields/Headers')
	end

	if  not h.isempty(args.Season)
		then table.insert(output, 'Outdated Fields/Season')
	end
]]--
	if  not h.isempty(args.Image)
		or not h.isempty(args.Image1Text)
		or not h.isempty(args.Appearing1_1)
		or not h.isempty(args.ImageText)
		or not h.isempty(args.ImageSize)
		or not h.isempty(args.Gallery)
		or not h.isempty(args.Synopsis1Header)
		or not h.isempty(args.Contributors1Header)
		or not h.isempty(args.Appearing1Header)
		or not h.isempty(args.AppearingCategories)
		or not h.isempty(args.Race1)
		or not h.isempty(args.Location1)
		or not h.isempty(args.Item1)
		or not h.isempty(args.Vehicle1)
		or not h.isempty(args.Appearing)
		or not h.isempty(args.Reprint_CoverArtists)
			then table.insert(output, 'Outdated Fields')
	end

	return output	
end


------------------------------------------------------------------------------------------
-- counts number of stories in issue based on filled fields
function p.lua_get_number_of_stories(args)
	local i
	local output = 1
	
	for i = 2,200 do
		if	not h.isempty(args['ReprintOf'..i])
			or not h.isempty(args['ReprintOfStory'..i])
			or not h.isempty(args['StoryTitle'..i])
			or ( not h.isempty(args['Appearing'..i]) and not p.lua_appearances_is_empty(args['Appearing'..i]) )
			or not h.isempty(args['Synopsis'..i])
			or not h.isempty(args['Writer'..i..'_1'])
			or not h.isempty(args['Penciler'..i..'_1'])
			or not h.isempty(args['Inker'..i..'_1'])
			or not h.isempty(args['Colorist'..i..'_1'])
			or not h.isempty(args['Letterer'..i..'_1'])
			or not h.isempty(args['Editor'..i..'_1'])
			or not h.isempty(args['AdaptedFrom'..i..'_1'])
			or not h.isempty(args['PartOf'..i])
			or not h.isempty(args['PartOf'..i..'_Previous'])
			or not h.isempty(args['PartOf'..i..'_Next'])
			then output = output + 1
			else break
		end
	end
			
	return output
end


------------------------------------------------------------------------------------------
--returns story title, story title from reprint or story number if none of them exists
function p.lua_get_story_title(args, reprint_info, story_number, return_link)
	local story_title = args['StoryTitle'..story_number] or ''
	local reprint_story_title = ''
	local ordinal_number = h.ordinal_number(story_number)
	local appearing = get_pagename()..'#Appearing in '
	local link = ''
	local output = '' 
	
	if reprint_info ~= false
		then 
			reprint_story_title = h.get_field_value(reprint_info.content, 'StoryTitle'..reprint_info.story) or ''
			if story_title ~= ''
				then output = story_title
				elseif reprint_story_title ~= ''
					then output = reprint_story_title
			end
			if output ~= ''
				then 
					output = '"'..string.gsub(output,'"','')..'"'
					link = h.Link(appearing..output, story_number..'. '..design.span(output).italic)
				else 
					output = ordinal_number..' story'
					link = h.Link(appearing..output, design.span(output).italic)
			end
		elseif story_title ~= ''
			then 
				output = '"'..string.gsub(story_title,'"','')..'"'
				link = h.Link(appearing..output, story_number..'. '..output)
		else 
			output = ordinal_number..' story'
			link = h.Link(appearing..output, output)
	end
	
	if return_link == true
		then output = link
	end

	return output
end


------------------------------------------------------------------------------------------
function p.lua_get_story_contributors(args, reprint_info, story_number, released, canceled)
	local jobs = {'Writer', 'Penciler', 'Inker', 'Colorist', 'Letterer', 'Editor'}
	local i
	local j
	local list = {}
	local value
	local categories
	local output_categories = {}
	local output = {}
	
	-- creates empty tables for each job
	for i = 1, #jobs do
		table.insert(output, {})
	end
	
	-- fetch credits from original story, if it is a reprint
	if reprint_info ~= false
		then
			for i = 1, #jobs do
				list = {}
				for j = 1,200 do
					value = h.get_field_value(reprint_info.content, jobs[i]..reprint_info.story..'_'..j)
					if not h.isempty(value)
						then 
							value = SC.lua_get_staff_link_and_category(value)
							table.insert(list, design.span(value).italic)
					end
				end
				output[i] = list
			end
	end
	
	-- add credits listed in template
	for i = 1, #jobs do
		list, categories = SC.lua_get_list_of_staff_from_args(args, jobs[i]..story_number..'_', jobs[i])
		output_categories = h.join_tables(output_categories, categories)
		output[i] = h.join_tables(output[i], list)
	end
	
	-- form the list of people for each job
	for i = 1, #jobs do
		list = output[i]
		if not h.isempty(list)
			then output[i] = '\n'..mw.text.listToText(list, '<br>', '<br>')
			else 
				output[i] = ''
				if released and not canceled and reprint_info == false
					then table.insert(output_categories, jobs[i]..' Credit Needed')
				end
		end
	end

	return output, output_categories
end


------------------------------------------------------------------------------------------
function p.lua_get_story_appearances_and_synopsis(args, reprint_info, story_number, released, canceled, handbook)
	local appearances = args['Appearing'..story_number]
	local synopsis = args['Synopsis'..story_number]
	local reprint_message = ''
	local story_title = p.lua_get_story_title(args, reprint_info, story_number, false)
	local output_categories = {}
	local output = {}

	if reprint_info ~= false
		then
			reprint_message = p.lua_reprint_message(args, reprint_info, story_number, false)
			reprint_message = design.messagebox({Message = reprint_message, width = '40%'})
			appearances = h.get_field_value(reprint_info.content, 'Appearing'..reprint_info.story)
			appearances = p.lua_get_appearances_from_reprint(appearances)
			synopsis = h.get_field_value(reprint_info.content, 'Synopsis'..reprint_info.story)
		elseif p.lua_appearances_is_empty(appearances) == true
			then appearances = ''
		elseif handbook == true
			then
				appearances = string.gsub(appearances, '/Appearances', '/Handbook Appearances')
				appearances = string.gsub(appearances, '/Minor Appearances', '/Handbook Appearances')
				appearances = string.gsub(appearances, '/Mentions', '/Handbook Mentions')
				appearances = string.gsub(appearances, '/Invocations', '/Handbook Mentions')
	end
	
	if h.isempty(appearances) 
		then 
			appearances = '* Appearances not yet listed'
			if released and not canceled and reprint_info == false -- not reprint
				then table.insert(output_categories, 'Appearances Needed')
			end
	end
	
	if h.isempty(synopsis) 
		then 
			synopsis = '* Synopsis not yet written'
			if released and not canceled and reprint_info == false -- not reprint
				then table.insert(output_categories, 'Comic Synopsis Needed')
			end
	end

	table.insert(output, design.add_header('Appearing in '..story_title, 2))
	table.insert(output, reprint_message)
	table.insert(output, '\n'..appearances)

	table.insert(output, design.add_header('Synopsis for '..story_title, 2))
	table.insert(output, reprint_message)
	table.insert(output, '\n'..synopsis)

	return table.concat(output), output_categories
end


------------------------------------------------------------------------------------------
-- creates a list with content from reprints
function p.lua_get_reprints_content(args)
	local reprint_issue
	local reprint_story
	local i
	local value
	local list = {}
	local list_of_comics = {}
	local output_categories = {}
	local output = {}
	
	for i = 1, 200 do
		reprint_issue = args['ReprintOf'..i]
		reprint_story = args['ReprintOfStory'..i]
		if not h.isempty(reprint_issue)
			then
				table.insert(output_categories, 'Reprints')
				reprint_issue = standard.lua_standardized_comics_name(reprint_issue)
				table.insert(output_categories, reprint_issue..'/Reprints')
				if h.isempty(reprint_story)
					then 
						reprint_story = '1'
						table.insert(output_categories, 'Reprint Story Number Needed')
				end
				table.insert(list_of_comics, reprint_issue) -- an additional table to store only names of issues. used below to reduce number of expensive calls, for issues with many reprints
				table.insert(list, {story_number = i, issue = reprint_issue, story = reprint_story, content = ''})
			else
				-- this story is not a reprint
				table.insert(list_of_comics, false)
				table.insert(list, false)
		end
	end
	
	-- if there any reprints, then get content from original issues
	for i = 1, #list do
		if list[i] ~= false
			then
				value = positions_in_list(list_of_comics, list[i].issue)
				if #value > 0
					then
						if #value == 1 or i == value[1]
							then 
								list[i].content = h.get_content(list[i].issue)
								table.insert(output, list[i])
							else
								-- since getContent() is expensive, to reduce number of time it is called, 
								-- check if there are several reprints from the same issue and get content from the previosly stored variable
								list[i].content = list[value[1]].content
								table.insert(output, list[i])
						end
				end
			else
				table.insert(output, list[i])
		end
	end

	return output, output_categories
end


------------------------------------------------------------------------------------------
-- forms a message about story being a reprint
function p.lua_reprint_message(args, reprint_info, story_number, message_for_credits)
	local story_title = ''
	local reprint_story_title = ''
	local link
	local help_message = ''
	local output = ''
	
	if message_for_credits == true
		then
			help_message = '<br />(original credits are '..design.span('italicized').italic..')'
			help_message = tostring(mw.html.create('span'):css('font-family', 'monospace'):wikitext(help_message))
	end
	
	if reprint_info ~= false
		then 
			link = standard.lua_standardized_link(reprint_info.issue, 'Comic')
			story_title = args['StoryTitle'..story_number] or ''
			reprint_story_title = h.get_field_value(reprint_info.content, 'StoryTitle'..reprint_info.story) or ''
			if story_title ~= '' and story_title ~= reprint_story_title
				then
					if reprint_story_title ~= ''
						then story_title = '(originally printed as '..reprint_story_title..')'
						else story_title = '(originally printed without story title)'
					end
					story_title = '<br />'..story_title
			end
			output = 'Reprint of the '..h.ordinal_number(reprint_info.story)..' story from <br />'..link..story_title..help_message
	end
	
	return output
end


------------------------------------------------------------------------------------------
function p.lua_adopted_story(args, story_number)
	local output_categories = ''
	local output = ''
	
	output, output_categories = SC.lua_get_list_of_staff_from_args(args, 'AdaptedFrom'..story_number..'_', 'Adapted Stories')
	if not h.isempty(output)
		then output = 'Story is an adaptation of the original material by '..mw.text.listToText(output)
		else output = ''
	end
	
	return output, output_categories
end


------------------------------------------------------------------------------------------
-- returns information about "stories that are part of the storyarc/storyline/event (specifically for stories, in which previous/next issues are not in the same volume)
function p.lua_get_part_of_info(args, story_number)
	local part_of_name = args['PartOf'..story_number]
	local part_of_previous = ''
	local part_of_next = ''
	local part_of_number = ''
	local part_of_type
	local category = ''
	local output = ''

	--part_of_previous = standard.comics_link({args['PartOf'..story_number..'_Previous']})
	--part_of_next = standard.comics_link({args['PartOf'..story_number..'_Next']})
	part_of_previous = args['PartOf'..story_number..'_Previous']
	part_of_next = args['PartOf'..story_number..'_Next']
	if not h.isempty(part_of_previous) 
		then 
			if h.isempty(part_of_next)
				then part_of_next = '—'
			end
		elseif not h.isempty(part_of_next) 
			then part_of_previous = '—'
	end
	
	if not h.isempty(part_of_name)
		then
			if h.is_link(part_of_name)
				then category = h.break_link(part_of_name, 1)
				else
					category = part_of_name
					part_of_name = h.Link(part_of_name, part_of_name)
			end
			part_of_type = args['PartOf'..story_number..'_Type'] or require("Module:PageType").get_page_type(category) 
			if h.isempty(part_of_type) or part_of_type == 'Unknown'
				then part_of_type = ''
				else part_of_type = ' '..string.lower(part_of_type)
			end
			part_of_number = args['PartOf'..story_number..'_Number']
			if not h.isempty(part_of_number)
				then part_of_number = '#'..part_of_number..' '
				else part_of_number = ' '
			end
			
			output = 'Part '..part_of_number..'of '..part_of_name..string.lower(part_of_type)..'.', part_of_previous, part_of_next
	end
	
	return {output, part_of_previous, part_of_next}, category
end	


------------------------------------------------------------------------------------------
function p.lua_is_canceled(args)
	local output = false -- not canceled
	
	if not h.isempty(args.Canceled)
		then output = true -- canceled
	end
	
	return output
end


------------------------------------------------------------------------------------------
-- check if appearances section is filled only with subheaders (Featured Characters, etc.) without any actual appearances
function p.lua_appearances_is_empty(value)
	local subheaders = { "'''Featured Characters:'''", "'''Supporting Characters:'''",  
						 "'''Antagonists:'''", "'''Other Characters:'''", "'''Races and Species:'''", 
						 "'''Locations:'''", "'''Items:'''", "'''Vehicles:'''", }
	local s
	local count = 0
	local output = true --empty 
	
	if not h.isempty(value)
		then
			s = mw.text.split(value, '\n', true)
			for i = 1,#s do
				if h.in_list(subheaders, s[i]) 
				   or string.match(s[i], "^%*%s*$") ~= nil 
				   or string.match(s[i], "^%*%s*<br%s-/-%s->%s-$") ~= nil
				   or string.match(s[i], "^%s*$") ~= nil
						then count = count + 1
				end
			end
			
			if count ~= #s 
				then output = false --not empty
			end
	end

	return output
end


------------------------------------------------------------------------------------------
-- get the list of appearances formed by dpl query from a comics issue and replace/move all tags to suppress all categoriztion.
function p.lua_get_appearances_from_reprint(appearances)
	local pagename = get_pagename()
	local i
	local j
	local k
	local f
	local s1
	local s2
	local s3
	local str
	local output = {}
	
	if not h.isempty(appearances)
		then
			appearances = mw.text.split(appearances,'\n')

			for i = 1,#appearances do
				str = appearances[i]
				str = mw.ustring.gsub(str, '{{[Aa]|(%[%[.-%]%])|}}', '%1' )
				str = mw.ustring.gsub(str, '{{[Aa]|(.-)|}}', '[[%1]]' )
				str = mw.ustring.gsub(str, '{{[Aa]pn|(%[%[.-%]%]).-|}}', '%1' )
				str = mw.ustring.gsub(str, '{{[Aa]pn||.-|}}', '' )
				str = mw.ustring.gsub(str, '{{1stas|(%[%[.-%]%])|(.-)|}}', '%1 {{g|First appearance as %2}}' )
				str = mw.ustring.gsub(str, '{{[Pp]ossessed|(%[%[.-%]%])|(.-)|}}', '%1 {{g|Possessed by %2}}' )
				str = mw.ustring.gsub(str, '{{[Pp]ossessedBy|(%[%[.-%]%])|(.-)|}}', '%1 {{g|Possessed by %2}}' )
				str = mw.ustring.gsub(str, '{{[Pp]ossesses|(%[%[.-%]%])|(.-)|}}', '%1 {{g|Possesses %2}}' )
				str = mw.ustring.gsub(str, '{{[Ii]mpersonates|(.-)|}}', '{{g|Impersonates %1}}' )
				str = mw.ustring.gsub(str, '{{[Mm]inor|||(.-)|}}', '{{g|%1}}' )
				j, k = mw.ustring.find(string.lower(str), '{{minor|')
				if j ~= nil 
					then
						s1 = mw.ustring.sub(str, 1, j-1)
						f = mw.ustring.find(str, '|}}')
						s2 = mw.ustring.sub(str, k+1, f-1)
						s3 = mw.ustring.sub(str, f+3, #str)
						j = mw.ustring.find(s2, '[[',1,true)
						k = mw.ustring.find(s2, ']]',1,true)
						f = mw.ustring.find(s2, '|',1,true)
						if j ~= nil 
							then
								if f ~= nil
									then
										s2 = h.Link(mw.ustring.sub(s2, j+2, f-1), mw.ustring.sub(s2, f+1, k-1))
									else
										s2 = mw.ustring.sub(s2, j+2, k-1)
										s2 = h.Link(s2, s2)
								end
							else
								s2 = mw.text.split(s2, "|", true)
								if #s2 == 3
									then s2 = h.Link(s2[1], s2[2])..' {{g|'..s2[3]..'}}'
									elseif #s2 == 2
										then s2 = s2[1]..' {{g|'..s2[2]..'}}'
										else s2 = s2[1]
								end
						end
						str = s1..s2..s3
				end
				str = mw.ustring.gsub(str, '{{[Gg]|(%[%[.-%]%])|(.-)|}}', '%1 {{g|%2}}' )
				str = mw.ustring.gsub(str, '{{[Gg]|(%[%[.-%]%])|}}', '%1' )
				str = mw.ustring.gsub(str, '{{[Gg]reen|(%[%[.-%]%])|(.-)|}}', '%1 {{g|%2}}' )	
				str = mw.ustring.gsub(str, '{{[Gg]reen|(%[%[.-%]%])|}}', '%1' )
				str = mw.ustring.gsub(str, '({{.-)|(%[%[.-%]%])|}}', '%2 %1}}' )
				str = mw.ustring.gsub(str, '{{[Cc]hronology}}', '{{g|[['..pagename..'#Chronology Notes|See chronology]]}}' )
				str = mw.ustring.gsub(str, '{{[Cc]hronoFB}}', '{{g|[['..pagename..'#Chronology Notes|Appears in flashback]]}}' )
				--str = mw.ustring.gsub(str, '{{g|[[#Continuity Notes|See Continuity Notes]]}}', '{{g|[['..pagename..'#Continuity Notes|See Continuity Notes]]}}' )
				j, k = mw.ustring.find(string.lower(str), '{{g|[[#Continuity Notes|See Continuity Notes]]}}', 1, true)
				if j ~= nil 
					then
						s1 = string.sub(str, 1, j-1)
						s2 = '{{g|[['..pagename..'#Continuity Notes|See Continuity Notes]]}}'
						s3 = string.sub(str, k+1, #str)
						str = s1..s2..s3
				end   
				str = mw.ustring.gsub(str, '%[%[Category:.-%]%]', '' )
				str = mw.ustring.gsub(str, '&lt;', '<' )
				str = mw.ustring.gsub(str, '&gt;', '>' )
				table.insert(output, '\n'..str)
			end
	end
	
	return table.concat(output)
end


--------------------------------------------------------------------------------
function p.lua_add_quote(args)
	local quote = require("Module:Quote")
	local reprint_content = ''
	local quotation = args.Quotation
	local speaker = args.Speaker
	local output_categories = {}
	local output = ''
	
	if not h.isempty(args.ReprintQuote)
		then 
			reprint_content = h.get_content(args.ReprintQuote)
			if not h.isempty(reprint_content)
				then
					quotation = h.get_field_value(reprint_content, 'Quotation')
					speaker = h.get_field_value(reprint_content, 'Speaker')
					if not h.isempty(quotation)
						then output, _ = quote.main(quotation, speaker, _, true)
					end
			end
		elseif not h.isempty(quotation)
			then output, output_categories = quote.main(quotation, speaker, args.QuoteSource, true)
		elseif not h.isempty(args.BlockQuote)
			then 
				output = args.BlockQuote
				table.insert(output_categories, 'BlockQuotes')
		else
			table.insert(output_categories, 'Comic Quote Needed')
	end
	
	for i = 1, #output_categories do
		if output_categories[i] == 'Quote Source Needed'
			then output_categories[i] = ''
		end
	end
	
	return output, output_categories
end


------------------------------------------------------------------------------------------
function p.lua_is_handbook(list_of_handbooks)
	local issue_info = standard.lua_get_title_volume_issue(get_pagename(), 'Vol')
	return h.in_list(list_of_handbooks, issue_info.noissue)
end


--****************************************************************************************************************
-- Functions to be called directly from template
--****************************************************************************************************************
------------------------------------------------------------------------------------------
function p.top_message(frame)
	local args = getArgs(frame)
	local pagename = get_pagename()
	local issue_info = standard.lua_get_title_volume_issue(pagename)
	local canceled = p.lua_is_canceled(args)
	local release_info = m_date.lua_get_release_date_info(args['ReleaseDate'])
	local publication_info = m_date.lua_get_publication_date_info(args['Year'], args['Month'], canceled)
	local released = m_date.lua_date_comparison(release_info.date)
	local sortname = publication_info.sortdate..' '
	local day
	local output_categories = {}
	local output = ''

	if release_info.date ~= ''
		then 
			day = release_info.day
			day = string.rep('0', 2-#day)..day
			sortname = sortname..release_info.year..m_date.get_month_number({release_info.month})..day..' '
	end
	sortname = '{{DEFAULTSORT:&nbsp;'..sortname..issue_info.sortname.all..'}}'

	if canceled
		then
			table.insert(output_categories, 'Canceled Comics')
			output = 'This comic will not be released. It was canceled prior to its release date, '..release_info.week.link
			output = design.messagebox( {Message = output, background = 'rgb(255, 102, 102)', border = 'rgb(255, 20, 20)', extrastyle = 'color:white;'})
		elseif not released
			then
				table.insert(output_categories, 'Solicitations')
				output = design.messagebox( {Message = 'This comic will be released on '.. release_info.week.link})
		elseif release_info.recent
			then 
				table.insert(output_categories, 'Spoilers')
				output = design.spoiler_warning()
	end

	return frame:preprocess(sortname..output..'__NOTOC__')..h.add_categories(output_categories)
end


------------------------------------------------------------------------------------------
function p.get_link_to_volume(frame)
	local pagename = get_pagename()
	local issue_info = standard.lua_get_title_volume_issue(pagename, 'Vol')
	local output_categories = {}
	local output = h.Link(issue_info.noissue, issue_info.noissue)..' #'..issue_info.issue

	table.insert(output_categories, 'Comics')
	table.insert(output_categories, issue_info.noissue) -- volume category
	
	return output..h.add_categories(output_categories)
end


------------------------------------------------------------------------------------------
function p.get_main_cover(frame)
	local args = getArgs(frame)
	local pagename = get_pagename()
	local issue_info = standard.lua_get_title_volume_issue(pagename, 'Vol')
	local main_cover = issue_info.filename.all..'.jpg'
	local main_cover_textless = issue_info.filename.all..' Textless.jpg'
	local output = args.Image1

	if h.isempty(output)
		then 
			if h.exists('File:'..main_cover)
				then output = main_cover
				elseif h.exists('File:'..main_cover_textless)
					then output = main_cover_textless
				else output = 'No Image Cover.jpg'
			end
	end

	return output
end


------------------------------------------------------------------------------------------
function p.get_textless_cover(frame)
	local pagename = get_pagename()
	local issue_info = standard.lua_get_title_volume_issue(pagename, 'Vol')
	local main_cover_textless = issue_info.filename.all..' Textless.jpg'
	local output = ''
	
	if h.exists('File:'..main_cover_textless)
		then output = main_cover_textless
	end
	
	return output
end


------------------------------------------------------------------------------------------
function p.get_cover_artists(frame)
	local function get_all_images(args)
		local value
		local i
		local output = {}

		for i = 1,100 do
			value = args['Image'..i]
			if not h.isempty(value)
				then 
					value = string.sub(value, 1, -5) -- removes file extension (.jpg, .png, etc)
					table.insert(output, value)
				else break
			end
		end
		
		return output
	end
	local args = getArgs(frame)
	local pagename = get_pagename()
	local issue_info = standard.lua_get_title_volume_issue(pagename, 'Vol')
	local main_cover_textless = issue_info.filename.all..' Textless.jpg'
	local cover_number = args.cover_number
	local all_images = get_all_images(args)
	local i
	local j
	local category = ''
	local value = ''
	local list = {'Penciler', 'Inker', 'Colorist', 'Letterer'}
	local reprint_of = args['Image'..cover_number..'_ReprintOf']
	local content = ''
	local reprint_artists = {}
	local reprint_source = ''
	local output_categories = {}
	local output = ''

	output, output_categories = SC.lua_get_list_of_staff_from_args(args, 'Image'..cover_number..'_Artist', 'Cover Artist')
	
	if not h.isempty(reprint_of)
		then
			table.insert(output_categories, 'Comics With Reprinted Cover')
			reprint_of = 'File:'..string.gsub(reprint_of, 'File:', '')
			content = h.get_content(reprint_of)
			if not h.isempty(content)
				then
					reprint_source = h.get_field_value(content, 'Source')
					if not h.isempty(reprint_source) and string.find(reprint_source, 'http') == nil
						then 
							reprint_source = standard.lua_standardized_link(reprint_source)
							reprint_source = ' from '..reprint_source
					end

					for i = 1,10 do --10 is a maximum number of credits for each field allowed in Module:Image Template
						value = h.get_field_value(content, 'CoverArtist'..i)
						if not h.isempty(value)
							then 
								value = SC.lua_get_staff_link_and_category(value, '')
								table.insert(reprint_artists, value)
						end
					end
					
					if table.concat(reprint_artists) == ''
						then
							for j = 1, #list do
								for i = 1,10 do
									value = h.get_field_value(content, list[j]..i) or nil
									if not h.isempty(value)
										then 
											value = SC.lua_get_staff_link_and_category(value, '')
											table.insert(reprint_artists, value)
									end
								end
							end
					end
			end
			
			reprint_of = 'Reprint of an '..h.Link(':'..reprint_of, 'image')..reprint_source
			if table.concat(reprint_artists) ~= ''
				then 
					reprint_artists = mw.text.listToText(reprint_artists)
					reprint_artists = '. Original art by: '..reprint_artists
					if h.isempty(output)
						then output = reprint_of..reprint_artists
						else output = reprint_of..reprint_artists..', with modifications by '..mw.text.listToText(output)
					end
			end
		elseif not h.isempty(output)
			then output = 'Art by: '..mw.text.listToText(output)
		elseif args.cover_number == '1' and not h.isempty(args.Image1)
			then
				output = 'Cover artist '..h.LinkToCategory('Cover Artist Credit Needed', 'not yet listed')
				table.insert(output_categories, 'Cover Artist Credit Needed')
		elseif not h.isempty(args['Image'..cover_number])
			then
				output = {}
				content = h.get_content('File:'..string.gsub(args['Image'..cover_number], 'File:', ''))
				for i = 1,10 do --10 is a maximum number of credits for each field allowed in Module:Image Template
						value = h.get_field_value(content, 'CoverArtist'..i)
						if not h.isempty(value)
							then 
								value, category = SC.lua_get_staff_link_and_category(value, 'Cover Artist')
								table.insert(output, value)
								table.insert(output_categories, category)
						end
				end
				if table.concat(output) ~= ''
					then output = 'Art by: '..mw.text.listToText(output)
					else
						output = 'Cover artist '..h.LinkToCategory('Cover Artist Credit Needed', 'not yet listed')
						table.insert(output_categories, 'Cover Artist Credit Needed')
				end
		else
			output = ''
	end

	return output..h.add_categories(output_categories)
end


------------------------------------------------------------------------------------------
-- forms a gallery of all alternate covers
function p.get_all_covers(frame)
	local args = getArgs(frame)
	local pagename = get_pagename()
	local issue_info = standard.lua_get_title_volume_issue(pagename, 'Vol')
	local main_cover = issue_info.filename.all..'.jpg'
	local main_cover_textless = issue_info.filename.all..' Textless.jpg'
	local i
	local image
	local image_text
	local value
	local category = ''
	local output = {}

	value = args.Image1
	if h.isempty(value) or h.in_list( {'No Image Cover.jpg', 'Needed.png', 'Needed.jpg'}, value)
		then category = h.Category('Cover Image Needed')
		elseif not h.in_list( {main_cover, main_cover_textless}, value)
			then category = h.Category('Cover Image Correction Needed')
	end

	for i = 2, 100 do
		image = args['Image'..i]
		image_text = args['Image'..i..'_Text']
		if not h.isempty(image)
			then
				if not h.isempty(image_text)
					then value = image..'|'..(i - 1)..' - '..image_text
					elseif i == 2 and image == main_cover_textless
						then value = image..'|'..(i - 1)..' - Textless'
					else value = image..'|'..(i - 1)
				end
				table.insert(output, value)
			elseif i == 2 and h.exists('File:'..main_cover_textless)
				then 
					value = main_cover_textless..'|1 - Textless'
					table.insert(output, value)
		end
	end
				
	if not h.isempty(output)
		then 
			output = '\n'..mw.text.listToText(output, '\n', '\n')
			output = '<gallery widths="68px" position="center" captionalign="center" spacing="small">'..output..'</gallery>'
		else
			output = ''
	end
	
	return frame:preprocess(output)..category
end


------------------------------------------------------------------------------------------
function p.get_release_date(frame)
	local args = getArgs(frame)
	local release_info = m_date.lua_get_release_date_info(args['ReleaseDate'])
	local category = ''
	local output = ''

	output = release_info.week.link
	category = string.gsub(release_info.week.text, ':Category:', '')

	return output..h.Category(category)
end


------------------------------------------------------------------------------------------
function p.get_cover_date(frame)
	local args = getArgs(frame)
	local publication_info
	local canceled = p.lua_is_canceled(args)
	local output_categories = {}
	local output = ''

	publication_info, output_categories = m_date.lua_get_publication_date_info(args['Year'], args['Month'], canceled)

	return publication_info.link..h.add_categories(output_categories)
end


------------------------------------------------------------------------------------------
function p.get_rating(frame)
	local args = getArgs(frame)
	local ratings = {
		['all ages'] = { 'All Ages (for all ages)', 'All Ages (for all ages)' },
		['a'] = { 'A (9 and up)', 'A (9 and up)' },
		['psr'] = { 'PSR (9 and up)', 'PSR (9 and up)' },
		['pg'] = { 'PG (9 and up)', 'PG (9 and up)' },
		['a-'] = { 'A- (9 and up)', 'A- (9 and up)' },
		['a+'] = { 'A+ (9 and up)', 'A+ (9 and up)' },
		['t'] = { 'T (13 and up)', 'T (13 and up)' },
		['teen'] = { 'Teen (13 and up)', 'T (13 and up)' },
		['psr+'] = { 'PSR+ (13 and up)', 'PSR+ (13 and up)' },
		['pg+'] = { 'PG+ (13 and up)', 'PG+ (13 and up)' },
		['t-'] = { 'T- (13 and up)', 'T- (13 and up)' },
		['t+'] = { 'T+ (13 and up)', 'T+ (13 and up)' },
		['pa'] = { 'Parental Advisory (15 and up)', 'Parental Advisory (15 and up)' },
		['parental advisory'] = { 'Parental Advisory (15 and up)', 'Parental Advisory (15 and up)' },
		['pa/ec'] = { 'Parental Advisory (15 and up)', 'PA/EC (15 and up)' },
		['explicit content'] = { 'Parental Advisory (15 and up)', 'Explicit Content (15 and up)' },
		['parental advisory/explicit content'] = { 'Parental Advisory (15 and up)', 'Parental Advisory/Explicit Content (15 and up)' },
		['mature'] = { 'MAX: Explicit Content (18 and up)', 'Mature' },
		['max'] = { 'MAX: Explicit Content (18 and up)', 'MAX: Explicit Content (18 and up)' },
		['maxx'] = { 'MAXX: Explicit Content (18 and up)', 'MAXX: Explicit Content (18 and up)' }
	}
	local value = args.Rating
	local category = ''
	local output = ratings[string.lower(value)]
	
	output = ratings[string.lower(value)] 
	if output ~= nil
		then category = output[1]
		else output = {value, value}
	end
	output = h.LinkToCategory(output[1], output[2])

	return output..h.Category(category)
end


------------------------------------------------------------------------------------------
function p.get_date_label(frame)
	local args = getArgs(frame)
	local canceled = p.lua_is_canceled(args)
	local field_name = args.field_name
	local output = 'Intended '

	if canceled
		then output = output..field_name
		else output = field_name
	end	
	
	return output
end


------------------------------------------------------------------------------------------
function p.get_previous_issue(frame)
	local args = getArgs(frame)
	local pagename = get_pagename()
	local issue_info = standard.lua_get_title_volume_issue(pagename)
	local issue = issue_info.issue
	local remainder_issue = issue_info.remainder_issue
	local previous_issue = args['PreviousIssue']
	local r_issue
	local p_issue = ''
	local list = {'-', '--', '---', '—', '&mdash;'}
	local output = ''

	if h.isempty(previous_issue)
		then
			if issue ~= nil 
				then
					if string.match(issue, '%d+%.(%d+)') ~= nil
						then p_issue = issue - 0.1
						elseif not h.isempty(remainder_issue)
							then p_issue = tonumber(issue_info.clean_issue) - 1
						elseif tonumber(issue) > 1
							then p_issue = issue - 1
					end
			end
			if p_issue ~= ''
				then 
					p_issue = issue_info.noissue..' '..p_issue
					p_issue = standard.lua_standardized_link(p_issue, 'Comic')
			end
			if p_issue ~= ''
				then output = p_issue
				else output = '—'
			end
		elseif h.in_list(list, previous_issue)
			then output = '—'
		else
			if h.is_link(previous_issue)
				then output = standard.lua_standardized_link(h.break_link(previous_issue, 1), 'Comic')
				elseif h.exists(standard.lua_replace_number_sign(previous_issue, 'Comic'))
					then output = standard.lua_standardized_link(previous_issue, 'Comic')
					else output = previous_issue
			end
	end

	return output
end


------------------------------------------------------------------------------------------
function p.get_next_issue(frame)
	local args = getArgs(frame)
	local pagename = get_pagename()
	local issue_info = standard.lua_get_title_volume_issue(pagename)
	local issue = issue_info.issue
	local remainder_issue = issue_info.remainder_issue
	local next_issue = args['NextIssue']
	local r_issue
	local n_issue = ''
	local list = {'-', '--', '---', '—', '&mdash;'}
	local output = ''

	if h.isempty(next_issue)
		then 
			if issue ~= nil 
				then
					if string.match(issue, '%d+%.(%d+)') ~= nil
						then n_issue = issue + 0.1
						elseif not h.isempty(remainder_issue)
							then n_issue = tonumber(issue_info.clean_issue) + 1
						else n_issue = issue + 1
					end
			end
			if n_issue ~= ''
				then 
					n_issue = issue_info.noissue..' '..n_issue
					n_issue = standard.lua_standardized_link(n_issue, 'Comic')
			end
			if n_issue ~= ''
				then output = n_issue
				else output = '—'
			end
		elseif h.in_list(list, next_issue)
			then output = '—'
		else
			if h.is_link(next_issue)
				then output = standard.lua_standardized_link(h.break_link(next_issue, 1), 'Comic')
				elseif h.exists(standard.lua_replace_number_sign(next_issue, 'Comic'))
					then output = standard.lua_standardized_link(next_issue, 'Comic')
					else output = next_issue
			end	
	end

	return output
end


------------------------------------------------------------------------------------------
function p.get_events_and_arcs(frame)
	local args = getArgs(frame)
	local pagename = get_pagename()
	local types = {'Event', 'StoryArc', 'Storyline', 'Tie-in'}
	local names = {'event', 'arc', 'storyline', 'Tie-in'}
	local list
	local i
	local j
	local value
	local category
	local link_text
	local output_categories = {}
	local output = {}

	for i = 1,#types do
		list = {}
		for j = 1,10 do
			value = args[types[i]..j]
			if not h.isempty(value)
				then
					category = h.break_link(value, 1)
					link_text = h.break_link(value, 2)
					link_text = string.gsub(link_text, '^The ', '')
					link_text = string.gsub(link_text, '%(Event%)', '')
					link_text = string.gsub(link_text, '%(Story%)', '')
					link_text = string.gsub(link_text, '%(Story Arc%)', '')
					link_text = string.gsub(link_text, '%(Arc%)', '')
					link_text = string.gsub(link_text, '%(Storyline%)', '')
					table.insert(output_categories, category)
					value = h.Link(category, link_text)
					value = design.span(value).italic
					table.insert(list, value)
			end
		end
		if #list > 0
			then
				if i == 4 -- 'Tie-in'
					then table.insert(output, 'Tie-in to the '..mw.text.listToText(list)..' event.')
					else
						if #list == 1
							then value = names[i]
							else value = names[i]..'s'
						end
						table.insert(output, 'Part of the '..mw.text.listToText(list)..' '..value)
				end
		end
	end
	
	output = mw.text.listToText(output, '<br>', '<br>')
	
	return output..h.add_categories(output_categories)
end

return p
Advertisement