Marvel Database
No edit summary
No edit summary
Line 80: Line 80:
 
then
 
then
 
s = mw.text.split(list[i], '~', true)
 
s = mw.text.split(list[i], '~', true)
canceled = h.inlist(list_of_canceled_issues, s[1])
+
canceled = h.in_list(list_of_canceled_issues, s[1])
 
year = s[2]
 
year = s[2]
 
sort_date = p.lua_get_sort_date(year, s[3])
 
sort_date = p.lua_get_sort_date(year, s[3])
Line 117: Line 117:
 
if first_year ~= '' and last_year ~= ''
 
if first_year ~= '' and last_year ~= ''
 
then
 
then
first_year = h.lua_link_to_category(first_year)
+
first_year = h.LinkToCategory(first_year)
last_year = h.lua_link_to_category(last_year)
+
last_year = h.LinkToCategory(last_year)
 
if volume_status == 'Canceled'
 
if volume_status == 'Canceled'
 
then output = volume_status
 
then output = volume_status

Revision as of 02:32, 21 November 2020

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

-- module to form Template:Cvd - Comic Volume Description
local p = {}
local h = require("Module:HF")
local standart = require("Module:StandardizedName")
local m_date = require("Module:Date")
local getArgs = require('Dev:Arguments').getArgs


function p.main(frame)
    local args = getArgs(frame)
    local pagename = args[1]
    local list_of_canceled_issues
    local list = {}
    local issues
    local running_years
    local link
    local output = pagename or ''
    
    if not h.isempty(pagename)
        then
            list_of_canceled_issues = mw.text.split( frame:preprocess('{{#dpl:|category = Canceled Comics|namespace = 0|mode = userformat|listseparators = ,%PAGE%,@@@}}'), '@@@' )
            
            dpl_string = frame:preprocess( p.lua_dpl_list(pagename, _)..p.lua_dpl_list(pagename, 500) )
            
            if dpl_string ~= 'no_resultsno_results'
                then
                    dpl_string = string.gsub(dpl_string, 'no_results', '')
                    list = p.lua_transform_dpl_string_into_list(dpl_string, list_of_canceled_issues)
                    running_years = p.lua_get_running_years(list)
                    link = p.lua_get_link(pagename)
                    issues = p.lua_get_number_of_issues(#list)
                    output = link..'<br />'..running_years..'<br />'..issues
                else 
                    output = p.lua_get_link(pagename)
            end
            output = '<center>'..output..'</center>'
    end

    return output
end


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

    table.insert(output, '{{#dpl:')
    table.insert(output, '|categorymatch = '..pagename)
    table.insert(output, '|namespace = 0')
    table.insert(output, '|include = '..year..month)
    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_transform_dpl_string_into_list(dpl_string, list_of_canceled_issues)
    local list = {}
    local canceled = false
    local sort_date = ''
    local year = ''
    local s = ''
    local output = {}
    
    if not h.isempty(dpl_string)
        then 
            list = mw.text.split(dpl_string, '@@@', true)
            for i = 1, #list do
                if not h.isempty(list[i])
                    then
                        s = mw.text.split(list[i], '~', true)
                        canceled = h.in_list(list_of_canceled_issues, s[1])
                        year = s[2]
                        sort_date = p.lua_get_sort_date(year, s[3])
                        s = {
                             ['year'] = year,
                             ['canceled'] = canceled,
                             ['sort_date'] = sort_date,
                            }
                        table.insert(output, s)
                end
            end
    end
    
    return output
end


function p.lua_get_running_years(list)
    local first_by_date, last_by_date = p.lua_get_first_and_last_by_date(list)
    local first_year = first_by_date.year
    local last_year = last_by_date.year
    local last_sort_date = last_by_date.sort_date
    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 volume_status = ''
    local output = ''
    
    if last_sort_date < 999901 and last_sort_date >= date_of_last_solicit
        then volume_status = 'Active'
        elseif first_by_date.canceled
            then volume_status = 'Canceled'
    end
    
    if first_year ~= '' and last_year ~= ''
        then
            first_year = h.LinkToCategory(first_year)
            last_year = h.LinkToCategory(last_year)
            if volume_status == 'Canceled'
                then output = volume_status
                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 = '('..output..')'
    end
    
    return output
end


function p.lua_get_first_and_last_by_date(list)
    local first_by_date
    local last_by_date = ''

    if not h.isempty(list)
        then
            table.sort(list, p.lua_sort_by_date)
            first_by_date = list[1]
            if #list == 1
                then last_by_date = list[1]
                else
                    for i = #list,1,-1 do
                        if list[i].sort_date < 999901 and not list[i].canceled
                            then 
                                last_by_date = list[i]
                                break
                        end
                    end
                    
                    if last_by_date == ''
                        then last_by_date = list[1]
                    end
            end
    end
    
    return first_by_date, last_by_date
end


function p.lua_get_link(pagename)
    local link = standart.lua_standardized_link(pagename, 'Comics')
    local tag = mw.html.create( 'span' )
                :css( 'font-weight', 'bold' )
                :wikitext(link)
                :done()
    return tostring(tag)
end


function p.lua_get_number_of_issues(value)
    local tag = mw.html.create('span'):css('color', 'grey')
    local output = ''

    value = tonumber(value)
    if value ~= nil
        then
            if value == 1
                then output = '1 issue'
                else output = value..' issues'
            end
            output = tostring( tag:wikitext(output):done() )
    end

    return output
end


function p.lua_sort_by_date(a, b)
    return a.sort_date < b.sort_date
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


return p