Module:Citation/BCB

From Candypedia
Revision as of 15:01, 12 November 2024 by NeonWabbit (talk | contribs) (Created page with "local p = {} local mw = require('mw') -- Function to fetch and parse chapter data from the `Template:ChapterList` function p.formatBCBReference(url) if not url or type(url) ~= 'string' then return nil -- Return nil if `url` is not valid end -- Check if the URL matches the expected pattern local chapter, page = url:match("^https://www%.bittersweetcandybowl%.com/c(%d+)/p(%d+)") if not chapter then chapter, page = url:match("^https://bc...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local p = {}
local mw = require('mw')

-- Function to fetch and parse chapter data from the `Template:ChapterList`
function p.formatBCBReference(url)
    if not url or type(url) ~= 'string' then
        return nil -- Return nil if `url` is not valid
    end

    -- Check if the URL matches the expected pattern
    local chapter, page = url:match("^https://www%.bittersweetcandybowl%.com/c(%d+)/p(%d+)")
    if not chapter then
        chapter, page = url:match("^https://bcb%.cat/c(%d+)/p(%d+)")
    end
    if not chapter or not page then
        return nil -- Return nil if the URL does not match
    end

    -- Retrieve the chapter data
    local title = mw.title.new('Template:ChapterList')
    if not title or not title.exists then
        return nil -- Return nil if the template does not exist
    end

    local content = title:getContent()
    local chapter_data = {}
    for number, chapterTitle in content:gmatch('{{Chapter|number=([^|]+)|title=([^|]+)|date=[^|]+|pagecount=%d+}}') do
        chapter_data[number] = chapterTitle
    end

    local chapter_title = chapter_data[chapter]
    if chapter_title then
        return string.format("Chapter %s: %s, page %s", chapter, chapter_title, page)
    end

    return nil -- Return nil if no matching chapter is found
end

return p