Module:Citation/BCB: Difference between revisions

From Candypedia
No edit summary
No edit summary
Line 12: Line 12:
-- Function to retrieve chapter list (reuse from the working module)
-- Function to retrieve chapter list (reuse from the working module)
function p.getChaptersList()
function p.getChaptersList()
    mw.log('Entering getChaptersList') -- Log entry
     local listPage = mw.title.new('Template:ChapterList')
     local listPage = mw.title.new('Template:ChapterList')
     if not listPage then
     if not listPage then
        mw.log('Template:ChapterList not found') -- Log missing template
         return {}
         return {}
     end
     end
Line 19: Line 21:
     local content = listPage:getContent()
     local content = listPage:getContent()
     if not content then
     if not content then
        mw.log('Failed to get content from Template:ChapterList') -- Log content retrieval failure
         return {}
         return {}
     end
     end
Line 24: Line 27:
     -- Normalize apostrophes in content
     -- Normalize apostrophes in content
     content = p.normalizeApostrophes(content)
     content = p.normalizeApostrophes(content)
    mw.log('Retrieved and normalized content from Template:ChapterList') -- Log content retrieval


     local chapters = {}
     local chapters = {}
     for number, title, date, pagecount in string.gmatch(content, "{{Chapter|number=([%d%.]+)|title=([^|]+)|date=([^|]+)|pagecount=(%d+)}}") do
     for number, title, date, pagecount in string.gmatch(content, "{{Chapter|number=([%d%.]+)|title=([^|]+)|date=([^|]+)|pagecount=(%d+)}}") do
        mw.log('Parsed chapter - Number: ' .. number .. ', Title: ' .. title) -- Log each parsed chapter
         chapters[number] = title -- Create a mapping for quick lookup
         chapters[number] = title -- Create a mapping for quick lookup
     end
     end
Line 35: Line 40:
-- Function to format the BCB reference
-- Function to format the BCB reference
function p.formatBCBReference(url)
function p.formatBCBReference(url)
    mw.log('Entering formatBCBReference with URL: ' .. (url or 'nil')) -- Log entry and URL
     if not url or type(url) ~= 'string' then
     if not url or type(url) ~= 'string' then
        mw.log('Invalid URL provided') -- Log invalid URL
         return nil -- Return nil if `url` is not valid
         return nil -- Return nil if `url` is not valid
     end
     end
Line 45: Line 52:
     end
     end
     if not chapter or not page then
     if not chapter or not page then
        mw.log('URL did not match expected pattern') -- Log if no match
         return nil -- Return nil if the URL does not match
         return nil -- Return nil if the URL does not match
     end
     end
    mw.log('Matched Chapter: ' .. chapter .. ', Page: ' .. page) -- Log matched values


     -- Retrieve the chapter data using the shared approach
     -- Retrieve the chapter data using the shared approach
Line 53: Line 63:


     if chapter_title then
     if chapter_title then
         return string.format("Chapter %s: %s, page %s", chapter, chapter_title, page)
         local result = string.format("Chapter %s: %s, page %s", chapter, chapter_title, page)
        mw.log('Generated Result: ' .. result) -- Log the generated result
        return result
     end
     end


    mw.log('No matching chapter found for Chapter: ' .. chapter) -- Log if no matching chapter
     return nil -- Return nil if no matching chapter is found
     return nil -- Return nil if no matching chapter is found
end
end


return p
return p

Revision as of 15:10, 12 November 2024

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

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

-- Function to normalize apostrophes (reuse from the working module)
function p.normalizeApostrophes(str)
    str = string.gsub(str, "’", "'")
    str = string.gsub(str, "'", "'")
    str = string.gsub(str, "%%27", "'")
    return str
end

-- Function to retrieve chapter list (reuse from the working module)
function p.getChaptersList()
    mw.log('Entering getChaptersList') -- Log entry
    local listPage = mw.title.new('Template:ChapterList')
    if not listPage then
        mw.log('Template:ChapterList not found') -- Log missing template
        return {}
    end

    local content = listPage:getContent()
    if not content then
        mw.log('Failed to get content from Template:ChapterList') -- Log content retrieval failure
        return {}
    end

    -- Normalize apostrophes in content
    content = p.normalizeApostrophes(content)
    mw.log('Retrieved and normalized content from Template:ChapterList') -- Log content retrieval

    local chapters = {}
    for number, title, date, pagecount in string.gmatch(content, "{{Chapter|number=([%d%.]+)|title=([^|]+)|date=([^|]+)|pagecount=(%d+)}}") do
        mw.log('Parsed chapter - Number: ' .. number .. ', Title: ' .. title) -- Log each parsed chapter
        chapters[number] = title -- Create a mapping for quick lookup
    end

    return chapters
end

-- Function to format the BCB reference
function p.formatBCBReference(url)
    mw.log('Entering formatBCBReference with URL: ' .. (url or 'nil')) -- Log entry and URL
    if not url or type(url) ~= 'string' then
        mw.log('Invalid URL provided') -- Log invalid URL
        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
        mw.log('URL did not match expected pattern') -- Log if no match
        return nil -- Return nil if the URL does not match
    end

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

    -- Retrieve the chapter data using the shared approach
    local chapters = p.getChaptersList()
    local chapter_title = chapters[chapter]

    if chapter_title then
        local result = string.format("Chapter %s: %s, page %s", chapter, chapter_title, page)
        mw.log('Generated Result: ' .. result) -- Log the generated result
        return result
    end

    mw.log('No matching chapter found for Chapter: ' .. chapter) -- Log if no matching chapter
    return nil -- Return nil if no matching chapter is found
end

return p