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

local p = {}
local m = require("Module:Main")


function p.lua_link_part(s_cat, s_text, s_needed, s_type)
    local i
    local output = ''

    s_type = s_type or 'pages'
    i = m.pages_in_category(s_cat, s_type)
    if i > 0
        then
            output = '<li> '..m.category_link(s_cat, i..' '..s_text)
            if not m.empty(s_needed) and not m.exists({'Category:'..s_cat})
                then output = output..m.add_category(s_needed)
            end
    end
    return output
end


function p.lua_link_to_subpage(s_category, s_text, s_needed_category, s_type)
    local i
    local category = ''
    local output = ''

    s_type = s_type or 'pages'
    i = m.pages_in_category(category, s_type)
    if i > 0
        then
            output = '<li> '..m.category_link(category, i..' '..s_text)
            if not m.empty(s_needed_category) and not m.exists({'Category:'..category})
                then category = s_needed_category
            end
    end
    
    return output, category
end


--------------------------------------------------------------------------------------------------
function p.main(pagename, pagetype)
    local appearances = m.pages_in_category(pagename..'/Appearances', 'pages')
    local mentions = m.pages_in_category(pagename..'/Mentions', 'pages')
    local items = 'Item(s) used by '..pagename
    local techniques = 'Technique(s) used by '..pagename
    local events = 'Event(s) in which '..pagename..' participated'
    local value = ''
    local category = ''
    local output_categories = {}
    local output = {}

    if appearances > 0
        then
            table.insert(output, '<li> '..m.category_link(pagename..'/Appearances', appearances + mentions..' Appearance(s) of '..pagename) )

            if not m.exists({'Category:'..pagename..'/Appearances'})
                then table.insert(output_categories, 'Appearances Category Needed')
            end
            if mentions > 0 and not m.exists({'Category:'..pagename..'/Mentions'})
                then table.insert(output_categories, 'Mentions Category Needed')
            end
        elseif mentions > 0
            then 
                table.insert(output, '<li> '..m.category_link(pagename..'/Mentions', mentions..' Mention(s) of '..pagename) )

                if not m.exists({'Category:'..pagename..'/Mentions'})
                    then table.insert(output_categories, 'Mentions Category Needed')
                end
    end
    
    value, category = p.lua_link_to_subpage(pagename..'/Images', 'Image(s) of '..pagename, 'Images Category Needed', 'files')
    table.insert(output, value)
    table.insert(output_categories, category)
    
    value, category = p.lua_link_to_subpage(pagename..'/Quotes', 'Quote(s) of '..pagename, 'Quotes Category Needed')
    table.insert(output, value)
    table.insert(output_categories, category)

    return output, output_categories
end


--------------------------------------------------------------------------------------------------
function p.lua_references(notes)
    local output = {}
    
    table.insert(output, m.header('References', 2))
    table.insert(output, '<references />')
    
    if not m.empty(notes)
        then
            table.insert(output, m.header('Notes', 3))
            table.insert(output, '<references group="note"/>')
    end
    
    return table.concat(output)
end
    
    

--------------------------------------------------------------------------------------------------
function p.lua_links_lineage(pagename)
    local s = '(s) from '..pagename
    local value = ''
    local category = ''
    local output_categories = {}
    local output = {}
    
    value, category = p.lua_link_to_subpage(pagename..'/Characters', 'Character'..s, 'Characters Category Needed')
    table.insert(output, value)
    table.insert(output_categories, category)
    
    value, category = p.lua_link_to_subpage(pagename..'/Nations', 'Nation'..s, 'Lineages Category Needed')
    table.insert(output, value)
    table.insert(output_categories, category)
    
    value, category = p.lua_link_to_subpage(pagename..'/Sects', 'Sect'..s, 'Lineages Category Needed')
    table.insert(output, value)
    table.insert(output_categories, category)
    
    value, category = p.lua_link_to_subpage(pagename..'/Places', 'Place'..s, 'Lineages Category Needed')
    table.insert(output, value)
    table.insert(output_categories, category)
    
    value, category = p.lua_link_to_subpage(pagename..'/Races', 'Race'..s, 'Race Category Needed')
    table.insert(output, value)
    table.insert(output_categories, category)
    
    value, category = p.lua_link_to_subpage(pagename..'/Tribes', 'Tribe'..s, 'Tribe Category Needed')
    table.insert(output, value)
    table.insert(output_categories, category)
    
    return output, output_categories
end


--------------------------------------------------------------------------------------------------
function p.lua_links(s_page, s_type)
    local appearances = m.pages_in_category(s_page..'/Appearances', 'pages')
    local mentions = m.pages_in_category(s_page..'/Mentions', 'pages')
    local items = 'Item(s) used by '..s_page
    local techniques = 'Technique(s) used by '..s_page
    local events = 'Event(s) in which '..s_page..' participated'
    local output = {}
    
    ------ Appearances and Mentions ------
    if appearances > 0
        then
            table.insert( output, '<li> '..m.category_link(s_page..'/Appearances', appearances + mentions..' Appearance(s) of '..s_page) )

            if not m.exists({'Category:'..s_page..'/Appearances'})
                then table.insert(output, m.add_category('Appearances Category Needed') )
            end
            if mentions > 0 and not m.exists({'Category:'..s_page..'/Mentions'})
                then table.insert(output, m.add_category('Mentions Category Needed') )
            end
        elseif mentions > 0
            then 
                table.insert( output, '<li> '..m.category_link(s_page..'/Mentions', mentions..' Mention(s) of '..s_page) )

                if not m.exists({'Category:'..s_page..'/Mentions'})
                    then table.insert(output, m.add_category('Mentions Category Needed') )
                end
    end
    
    table.insert( output, p.lua_link_part(s_page..'/Images', 'Image(s) of '..s_page, 'Images Category Needed', 'files') )
    table.insert( output, p.lua_link_part(s_page..'/Quotes', 'Quote(s) of '..s_page, 'Quotes Category Needed') )
    ------ character ------
    --if s_type == 'character'
    --    then
    ------ location ------
    if s_type == 'location'
        then
            table.insert( output, p.lua_link_part(s_page..'/Characters', 'Character(s) from '..s_page, 'Characters Category Needed') )
            table.insert( output, p.lua_link_part(s_page..'/Nations', 'Nation(s) from '..s_page, 'Locations Category Needed') )
            table.insert( output, p.lua_link_part(s_page..'/Sects', 'Sect(s) from '..s_page, 'Locations Category Needed') )
            table.insert( output, p.lua_link_part(s_page..'/Places', 'Place(s) from '..s_page, 'Locations Category Needed') )
            table.insert( output, p.lua_link_part(s_page..'/Races', 'Race(s) from '..s_page, 'Race Category Needed') )
            table.insert( output, p.lua_link_part(s_page..'/Tribes', 'Tribe(s) from  '..s_page, 'Tribe Category Needed') )
    ------ tribe ------
    elseif s_type == 'tribe'
        then
            table.insert( output, p.lua_link_part(s_page, 'Representative(s) of '..s_page, 'Tribe Category Needed') )
            table.insert( output, p.lua_link_part(s_page..'/Places', 'Place(s) from '..s_page, 'Locations Category Needed') )
    ------ race ------
    elseif s_type == 'race'
        then
            table.insert( output, p.lua_link_part(s_page, 'Representative(s) of '..s_page, 'Race Category Needed') )
            table.insert( output, p.lua_link_part(s_page..'/Lineages', 'Lineage(s) of '..s_page, 'Locations Category Needed') )
            table.insert( output, p.lua_link_part(s_page..'/Tribes', 'Tribe(s) of '..s_page, 'Tribe Category Needed') )
    ------ era ------
    elseif s_type == 'era'
        then
            table.insert( output, p.lua_link_part(s_page..'/Races', 'Race(s) from '..s_page, 'Race Category Needed') )
            table.insert( output, p.lua_link_part(s_page..'/Characters', 'Character(s) from '..s_page, 'Characters Category Needed') )
            table.insert( output, p.lua_link_part(s_page..'/Locations', 'Location(s) from '..s_page, 'Locations Category Needed') )
            items = 'Item(s) from '..s_page
            techniques = 'Technique(s) from '..s_page
            events = 'Event(s) during '..s_page
    ------ cultivation type (physique, fate palace, etc.) ------
    elseif s_type == 'cultivation type'
        then
            table.insert( output, p.lua_link_part(s_page, 'Character(s) with '..s_page, 'Cultivators Category Needed') )

    ------ level (cultivation, alchemy, etc.) ------
    elseif s_type == 'level'
        then
            table.insert( output, p.lua_link_part(s_page, 'Character(s) at the '..s_page..' level', 'Cultivators Category Needed') )
    
    ------ techniques by type ------
    elseif s_type == 'techniques by type'
        then
            table.insert( output, p.lua_link_part(string.gsub(s_page,'Merit Laws','Techniques'), s_page, 'Techniques Category Needed') )
--[[    
    ------ items by type ------
    if s_type == 'items by type'
        then
            table.insert( output, p.lua_link_part(s_page, s_page, 'Items Category Needed') )
    end

    ------ techniques ------
    if s_type == 'techniques'
        then
            table.insert( output, p.lua_link_part(s_page, s_page, 'Techniques Category Needed') )
    end
--]]    
    ------ else ------
    else 
        table.insert( output, p.lua_link_part(s_page, s_page, 'Category Needed') )
    end

    table.insert( output, p.lua_link_part(s_page..'/Items', items, 'Items Category Needed') )
    table.insert( output, p.lua_link_part(s_page..'/Techniques', techniques, 'Techniques Category Needed') )
    table.insert( output, p.lua_link_part(s_page..'/Events', events, 'Events Category Needed') )
    table.insert( output, p.lua_link_part('Events in '..s_page, 'Event(s) that occurred in '..s_page, 'Events Category Needed') )
    table.insert( output, p.lua_link_part('Killed by '..s_page, 'character(s) killed by '..s_page, 'Killed by Category Needed') )
    table.insert( output, p.lua_link_part('Locations destroyed by '..s_page, 'location(s) destroyed by '..s_page, 'Destroyed by Category Needed') )
    table.insert( output, p.lua_link_part('Items destroyed by '..s_page, 'item(s) destroyed by '..s_page, 'Destroyed by Category Needed') )
    

    if not m.empty(output)
        then output = m.header('Links', 2)..table.concat(output)
        else output = ''
    end
    
    return output
end

function p.links(frame)
    local s1 = frame.args[1]
    local s2 = frame.args[2]
    if s2 ~= nil and s2 ~= ''
        then s2 = string.lower(s2)
        else s2 =''
    end
    
    return p.lua_links(s1, s2)
end

return p
Community content is available under CC-BY-SA unless otherwise noted.