FANDOM


-- This module, along with its submodules, intends to ease the pain of tracking
-- publication history.
-- See [[User:CXuesong/Publications]] for more information.
 
-- This module can load its submodules, i.e., [[Module:Sandbox/CXuesong/Publication/data]],
-- and [[Module:Sandbox/CXuesong/Publication/data/...]] modules, to generate a book
-- publication status overview table as shown in [[Warriors Translation]], along with the
-- lists of publication history as shown in "Publication History" section in various book
-- articles.
 
-- Motivation: The "Book Translations" section on [[Warriors Translation]] is difficult
-- to edit, especially when it comes to adding a column or two.
-- Now I'm only going to replace the table on [[Warriors Translation]], becuase there's
-- still a lot of tables to fill were I to replace the "Publication History" sections.
-- If you have any questions or suggestions, please leave me a message on my talk page. Thank you!
-- [[User:CXuesong]]
 
local LOCALE_DATA_PREFIX = "Module:Sandbox/CXuesong/Publication/data/"
 
local p = {}
 
-- local data = mw.loadData("Module:Sandbox/CXuesong/Publication/data")
-- #data won't work
local data = require("Module:Sandbox/CXuesong/Publication/data")
 
function loadLocaleData(locales)
    local ld = {}
    for i, v in ipairs(locales) do
        --ld[v] = mw.loadData(LOCALE_DATA_PREFIX .. v)
        ld[v] = require(LOCALE_DATA_PREFIX .. v)
    end
    return ld
end
 
-- Returns ahe abbreviation of a volume, or serial number of no customized abbr. avaliable.
function getVolumeAbbreviation(volume, serial)
    return volume[2] or serial
end
 
-- Converts a date expression to timestamp. Local time zone is assumed.
-- Besides, it accepts expression such as 2016-5-0 in "loose mode",
-- where 0 indicates an unknown day/month. This feature is designed
-- to acommodate certain undecided book release dates.
function parseDate(dateExpr, looseMode)
    -- Assuming a date pattern like: yyyy-mm-dd
    local pattern = "(%d+)-(%d+)-(%d+)"
    local y, m, d = dateExpr:match(pattern)
    if (y or m or d) == nil then
        return nil
    end
    if (tonumber(m) == 0) then
        if not looseMode then return nil end
        m = 1
    end
    if (tonumber(d) == 0) then
        if not looseMode then return nil end
        d = 1
    end
    return os.time{year = y, month = m, day = d}
end
 
-- Formats the date expression, if it's valid. Otherwise
-- directly returns the given expression.
function formatDate(dateExpr)
    local parsed = parseDate(dateExpr, false)
    if parsed then
        return os.date("%d %B %Y", parsed)
    else
        local pattern = "(%d+)-(%d+)-(%d+)"
        local y, m, d = dateExpr:match(pattern)
        if (y or m or d) == nil then
            return dateExpr
        end
        if (tonumber(m) == 0) then
            return y
        end
        if (tonumber(d) == 0) then
            return os.date("%d %B", os.time{year = y, month = m, day = 1})
        end
        return dateExpr
    end
end
 
-- showEntryCount: if set to true, number of publication entries will be shown instead of ticks(√) in the cells.
-- showAutonym: show autonym of each language instead of its English name.
function p._buildBookTranslationTable(showEntryCount, showAutonym)
    local builder = mw.html.create("table")
                        :addClass("wikitable")
    local arcHeader = mw.html.create("tr")
    local volumeHeader = mw.html.create("tr")
    local volumeList = {}
    arcHeader:tag("th"):attr{rowspan = 2}
    for i, v in ipairs(data.Arcs) do
        arcHeader:tag("th"):wikitext(v[1])
                :attr{colspan = #v.Volumes}
                :done()
        for j, w in ipairs(v.Volumes) do
            table.insert(volumeList, w[1])
            volumeHeader:tag("th")
                :wikitext("[[" .. w[1] .. "|" .. getVolumeAbbreviation(w, j) .. "]]")
                :done()
        end
    end
    builder
        :node(arcHeader)
        :node(volumeHeader)
    local localeData = loadLocaleData(data.Locales)
    for i, locale in ipairs(data.Locales) do
        local row = builder:tag("tr")
        local localeVolumes = localeData[locale].Volumes
        local languageName
        if showAutonym then
            languageName = mw.language.fetchLanguageName(locale)
        else
            languageName = localeData[locale].Culture.Name
        end
        row:tag("th")
            :wikitext("[[" .. LOCALE_DATA_PREFIX .. locale .. "|" .. mw.text.nowiki(languageName) .. "]]")
        for j, v in ipairs(volumeList) do
            local localeVolume = localeVolumes[v] or {}
            local localePublication = localeVolume.Publication
            if localePublication ~= nil then
                if showEntryCount and #localePublication > 0 then
                    row:tag("td"):wikitext(#localePublication)
                else
                    row:tag("td"):wikitext("√")
                end
            else
                row:tag("td")
            end
        end
    end
    return tostring(builder)
end
 
function buildPublicationHistoryEntry(frame, fallbackTitle, entry, culture, locale)
    -- {"2013-03-05", "hardcover", "HarperCollins", "notes", "{{r|hc|dc1}}"},
    local expr = string.format(culture.TitleFormat, entry[1] or fallbackTitle)
    expr = expr .. " (<abbr title=\"" .. mw.text.nowiki(mw.language.fetchLanguageName(locale)) .. "\" style=\"border-bottom: 1px dotted\">" .. locale .. "</abbr>)"
    -- Publisher
    if entry[4] then expr = expr .. ", " .. entry[4] end
    -- Edition
    if entry[3] then expr = expr .. " (" .. entry[3] .. ")" end
    -- Date
    if entry[2] then expr = expr .. ", " .. formatDate(entry[2]) end
    -- Note
    if entry[5] then expr = expr .. ", " .. entry[5] end
    -- Citation
    if entry[6] then
        local rawCitation
        if string.lower(string.sub(entry[6], 1, 4)) == "http" then
            rawCitation = "<ref>Information from [" .. entry[6] .. "]</ref>"
        else
            rawCitation = entry[6]
        end
        if frame then
            expr = expr .. frame:preprocess(rawCitation)
        else
            -- For debugging purpose
            expr = expr .. rawCitation
        end
    end
    return expr
end
 
function p._buildPublicationHistory(frame, volumeName)
    function publicationEntryComparer(pub1, pub2)
        return pub1.TimeStamp < pub2.TimeStamp
    end
    local localeData = loadLocaleData(data.Locales)
    local builder = mw.html.create("ul")
    local publicationList = {}
    for i, locale in ipairs(data.Locales) do
        local localeCulture = localeData[locale].Culture
        local localeVolume = localeData[locale].Volumes[volumeName] or {}
        if localeVolume.Publication ~= nil then
            for j, pub in ipairs(localeVolume.Publication) do
                table.insert(publicationList, {
                    Text = buildPublicationHistoryEntry(frame, localeVolume.Title or volumeName, pub, localeCulture, locale),
                    TimeStamp = parseDate(pub[2], true) or 0,
                })
            end
        end
    end
    table.sort(publicationList, publicationEntryComparer)
    for j, pub in ipairs(publicationList) do
        builder:tag("li")
            :wikitext(pub.Text)
    end
    return tostring(builder)
end
 
 
function p.buildBookTranslationTable(frame)
    return p._buildBookTranslationTable(frame.args[1] == "yes", false)
end
 
function p.buildPublicationHistory(frame)
    return p._buildPublicationHistory(frame, frame.args[1])
end
 
return p

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.