Jump to content

Module:AIA Guide

Permanently protected module
From Wikipedia, the free encyclopedia

local p = {}

-- Roman Numeral to nX page number mapping
local editions = {
    ['V4'] = {
        id = "aiaguidetonewyor0000unse_a0o0",
        map = {
            ['v']="n8", ['vi']="n9", ['vii']="n10", ['viii']="n11", ['ix']="n12", ['x']="n13",
            ['xi']="n14", ['xii']="n15", ['xiii']="n16", ['xiv']="n17", ['xv']="n18", ['xvi']="n19",
            ['xvii']="n20", ['xviii']="n21", ['xix']="n22", ['xx']="n23", ['xxi']="n24", ['xxii']="n25",
            ['xxiii']="n26", ['xxiv']="n27", ['xxv']="n28", ['xxvi']="n29", ['xxvii']="n30", ['xxviii']="n31"
        }
    },
    ['V5'] = {
        id = "aiaguidetonewyor0000unse_m6u4",
        map = {
            ['v']="n6", ['vi']="n7", ['vii']="n8", ['viii']="n9", ['ix']="n10", ['x']="n11",
            ['xi']="n12", ['xii']="n13", ['xiii']="n14", ['xiv']="n15", ['xv']="n16", ['xvi']="n17",
            ['xvii']="n18", ['xviii']="n19", ['xix']="n20", ['xx']="n21", ['xxi']="n22", ['xxii']="n23",
            ['1']="n27", ['2']="n28", ['3']="n29"
        }
    }
}

-- Formats the comma-separated lists into IA links
local function build_links(input, config)
    if not input or mw.text.trim(input) == '' then return nil end
    
    -- Strip accidental "p." or "pp." from the start of the string
    input = mw.ustring.gsub(input, "^p+%.%s*", "")

    local chunks = mw.text.split(input, "%s*,%s*")
    local results = {}

    for _, chunk in ipairs(chunks) do
        local target = mw.ustring.match(chunk, "(%w+)")
        
        if target then
            local target_lc = mw.ustring.lower(target)
            local mapped_val = config.map[target_lc] or target_lc
            local q_param = mw.uri.encode(target, "QUERY")
            
            local url = string.format("https://archive.org/details/%s/page/%s/mode/2up?q=%s", config.id, mapped_val, q_param)
            table.insert(results, string.format("[%s %s]", url, chunk))
        else
            table.insert(results, chunk)
        end
    end

    return table.concat(results, ", ")
end

-- Core citation builder
local function generate_citation(frame, edition_key, default_args)
    local pframe = frame:getParent()
    local args = pframe.args
    local config = editions[edition_key]
    
    local citeArgs = {}
    
    -- 1. Load the hardcoded edition data
    for k, v in pairs(default_args) do
        citeArgs[k] = v
    end
    
    -- 2. Pull in any extra arguments the user typed (like |ref= or |quote=)
    for k, v in pairs(args) do
        if mw.text.trim(v) ~= '' then
            citeArgs[k] = mw.text.trim(v)
        end
    end
    
    -- 3. Process the page and pages arguments
    local page = citeArgs['page']
    local pages = citeArgs['pages']
    
	if page then citeArgs['page'] = build_links(page, config) end
    if pages then citeArgs['pages'] = build_links(pages, config) end
    
    -- Force url and url-access to be blank to prevent conflicts with the wikilinked title
    citeArgs['url'] = nil
    citeArgs['url-access'] = nil
    
    -- 4. Send the whole thing to {{cite book}}
    return frame:expandTemplate{ title = 'cite book', args = citeArgs }
end

-- Entry point for Template:Cite AIA4
function p.V4(frame)
    local defaults = {
        ['last1'] = "White", ['first1'] = "Norval", ['author-link1'] = "Norval White",
        ['last2'] = "Willensky", ['first2'] = "Elliot",
        ['title'] = "AIA Guide to New York City", ['title-link'] = "AIA Guide to New York City",
        ['edition'] = "4th", ['location'] = "New York", ['publisher'] = "Crown Publishers",
        ['year'] = "2000", ['isbn'] = "978-0-8129-3107-5"
    }
    return generate_citation(frame, 'V4', defaults)
end

-- Entry point for Template:Cite aia5
function p.V5(frame)
    local defaults = {
        ['first1'] = "Norval", ['last1'] = "White", ['author-link1'] = "Norval White",
        ['last2'] = "Willensky", ['first2'] = "Elliot",
        ['last3'] = "Leadon", ['first3'] = "Fran",
        ['title'] = "AIA Guide to New York City", ['title-link'] = "AIA Guide to New York City",
        ['edition'] = "5th", ['location'] = "New York", ['publisher'] = "Oxford University Press",
        ['year'] = "2010", ['isbn'] = "978-0-19538-386-7"
    }
    return generate_citation(frame, 'V5', defaults)
end

return p