Module:Citation/BCB: Difference between revisions

From Candypedia
No edit summary
No edit summary
 
(7 intermediate revisions by the same user not shown)
Line 2: Line 2:
local mw = require('mw')
local mw = require('mw')


-- Function to fetch and parse chapter data from the `Template:ChapterList`
-- Function to retrieve chapter list from `Template:ChapterList`
function p.formatBCBReference(url)
function p.getChaptersList()
     mw.log('Received URL: ' .. (url or 'nil')) -- Log the received URL
     local listPage = mw.title.new('Template:ChapterList')
     if not url or type(url) ~= 'string' then
     if not listPage then
         return nil -- Return nil if `url` is not valid
         return {}
     end
     end


    -- Check if the URL matches the expected pattern
     local content = listPage:getContent()
     local chapter, page = url:match("^https://www%.bittersweetcandybowl%.com/c(%d+)/p(%d+)")
     if not content then
     if not chapter then
         return {}
         chapter, page = url:match("^https://bcb%.cat/c(%d+)/p(%d+)")
     end
     end
     if not chapter or not page then
 
        mw.log('URL did not match expected pattern') -- Log if no match
    local chapters = {}
         return nil -- Return nil if the URL does not match
     -- Match raw `{{Chapter|number=...|title=...|date=...|pagecount=...}}` calls
    for number, title, date, pagecount in string.gmatch(content, "{{%s*Chapter%s*|%s*number%s*=%s*([%d%.]+)%s*|%s*title%s*=%s*([^|]+)%s*|%s*date%s*=%s*([^|]+)%s*|%s*pagecount%s*=%s*(%d+)%s*}}") do
         chapters[number] = title
     end
     end


     mw.log('Matched Chapter: ' .. chapter .. ', Page: ' .. page) -- Log matched values
     return chapters
end


    -- Retrieve the chapter data
-- Function to format the BCB reference
     local title = mw.title.new('Template:ChapterList')
function p.formatBCBReference(input)
     if not title or not title.exists then
     local url = input
        mw.log('Template:ChapterList does not exist') -- Log if template does not exist
 
         return nil -- Return nil if the template does not exist
    -- Handle if input is a table (e.g., frame object from a template call)
     if type(input) == 'table' then
         url = input.args and input.args[1] or nil
     end
     end


    local content = title:getContent()
     if not url or type(url) ~= 'string' then
     if not content then
        mw.log('Failed to retrieve content from Template:ChapterList') -- Log if content retrieval fails
         return nil
         return nil
     end
     end


     local chapter_data = {}
     -- Match the URL pattern to extract chapter and page
     for number, chapterTitle in content:gmatch('{{Chapter|number=([^|]+)|title=([^|]+)|date=[^|]+|pagecount=%d+}}') do
     local chapter, page = url:match("^https://www%.bittersweetcandybowl%.com/c([%d%.]+)/p(%d+)")
         chapter_data[number] = chapterTitle
    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
     end
     end


     local chapter_title = chapter_data[chapter]
    -- Retrieve chapter data
    local chapters = p.getChaptersList()
     local chapter_title = chapters[chapter]
 
     if chapter_title then
     if chapter_title then
         local result = string.format("Chapter %s: %s, page %s", chapter, chapter_title, page)
         -- Check if the chapter number contains a decimal (indicating an intermission)
         mw.log('Generated Result: ' .. result) -- Log the generated result
        if string.match(chapter, "%d+%.%d+") then
         return result
            -- Format without "Chapter [number]:"
            return string.format("[https://www.bittersweetcandybowl.com/c%s/p%s %s, page %s]", chapter, page, chapter_title, page)
         else
            -- Format with "Chapter [number]:"
            return string.format("[https://www.bittersweetcandybowl.com/c%s/p%s Chapter %s: %s, page %s]", chapter, page, chapter, chapter_title, page)
         end
     end
     end


     mw.log('No matching chapter found for Chapter: ' .. chapter) -- Log if no matching chapter
     -- Fallback if no matching chapter is found
     return nil -- Return nil if no matching chapter is found
     return string.format("[https://www.bittersweetcandybowl.com/c%s/p%s Chapter %s: [Unknown Title], page %s]", chapter, page, chapter, page)
end
end


return p
return p

Latest revision as of 06:18, 14 November 2024

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

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

-- Function to retrieve chapter list from `Template:ChapterList`
function p.getChaptersList()
    local listPage = mw.title.new('Template:ChapterList')
    if not listPage then
        return {}
    end

    local content = listPage:getContent()
    if not content then
        return {}
    end

    local chapters = {}
    -- Match raw `{{Chapter|number=...|title=...|date=...|pagecount=...}}` calls
    for number, title, date, pagecount in string.gmatch(content, "{{%s*Chapter%s*|%s*number%s*=%s*([%d%.]+)%s*|%s*title%s*=%s*([^|]+)%s*|%s*date%s*=%s*([^|]+)%s*|%s*pagecount%s*=%s*(%d+)%s*}}") do
        chapters[number] = title
    end

    return chapters
end

-- Function to format the BCB reference
function p.formatBCBReference(input)
    local url = input

    -- Handle if input is a table (e.g., frame object from a template call)
    if type(input) == 'table' then
        url = input.args and input.args[1] or nil
    end

    if not url or type(url) ~= 'string' then
        return nil
    end

    -- Match the URL pattern to extract chapter and page
    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
    end

    -- Retrieve chapter data
    local chapters = p.getChaptersList()
    local chapter_title = chapters[chapter]

    if chapter_title then
        -- Check if the chapter number contains a decimal (indicating an intermission)
        if string.match(chapter, "%d+%.%d+") then
            -- Format without "Chapter [number]:"
            return string.format("[https://www.bittersweetcandybowl.com/c%s/p%s %s, page %s]", chapter, page, chapter_title, page)
        else
            -- Format with "Chapter [number]:"
            return string.format("[https://www.bittersweetcandybowl.com/c%s/p%s Chapter %s: %s, page %s]", chapter, page, chapter, chapter_title, page)
        end
    end

    -- Fallback if no matching chapter is found
    return string.format("[https://www.bittersweetcandybowl.com/c%s/p%s Chapter %s: [Unknown Title], page %s]", chapter, page, chapter, page)
end

return p