Module:Citation/BCB: Difference between revisions
NeonWabbit (talk | contribs) No edit summary |
NeonWabbit (talk | contribs) No edit summary |
||
Line 2: | Line 2: | ||
local mw = require('mw') | 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 | |||
mw.log('Template:ChapterList not found') -- Log if template is not found | |||
return {} | |||
end | end | ||
return | |||
local content = listPage:getContent() | |||
if not content then | |||
mw.log('Failed to get content from Template:ChapterList') -- Log if content retrieval fails | |||
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 -- Store chapter title with number as the key | |||
end | |||
mw.log('Chapters list parsed: ' .. mw.dumpObject(chapters)) -- Log the parsed chapters list | |||
return chapters | |||
end | end | ||
Line 14: | Line 30: | ||
local url = input | local url = input | ||
-- Handle if input is a table (e.g., | -- Handle if input is a table (e.g., frame object from a template call) | ||
if type(input) == 'table' then | if type(input) == 'table' then | ||
mw.log('Input is a table, | mw.log('Input is a table, extracting URL from args') | ||
url = input.args and input.args[1] or nil | url = input.args and input.args[1] or nil | ||
end | end | ||
-- Safely log the | -- Safely log the received URL | ||
mw.log('Received URL: ' .. | mw.log('Received URL: ' .. (url or 'nil')) | ||
if not url or type(url) ~= 'string' then | if not url or type(url) ~= 'string' then | ||
mw.log('Invalid URL | mw.log('Invalid or missing URL') -- Log if URL is not valid | ||
return nil -- Return nil if `url` is not valid | return nil -- Return nil if `url` is not valid | ||
end | end | ||
-- | -- Match the URL pattern to extract chapter and page | ||
local chapter, page = url:match("^https://www%.bittersweetcandybowl%.com/c(%d+)/p(%d+)") | local chapter, page = url:match("^https://www%.bittersweetcandybowl%.com/c(%d+)/p(%d+)") | ||
if not chapter then | if not chapter then | ||
Line 35: | Line 50: | ||
end | end | ||
if not chapter or not page then | if not chapter or not page then | ||
mw.log('URL did not match expected pattern') | mw.log('URL did not match expected pattern') -- Log if URL doesn't match | ||
return nil -- Return nil if the URL does not match | return nil -- Return nil if the URL does not match | ||
end | end | ||
Line 41: | Line 56: | ||
mw.log('Matched Chapter: ' .. chapter .. ', Page: ' .. page) | mw.log('Matched Chapter: ' .. chapter .. ', Page: ' .. page) | ||
-- Retrieve | -- Retrieve chapter data | ||
local chapters = p.getChaptersList() | local chapters = p.getChaptersList() | ||
local chapter_title = chapters[chapter] | local chapter_title = chapters[chapter] | ||
Line 52: | Line 67: | ||
mw.log('No matching chapter found for Chapter: ' .. chapter) | mw.log('No matching chapter found for Chapter: ' .. chapter) | ||
return | return string.format("Chapter %s: [Unknown Title], page %s", chapter, page) -- Fallback output | ||
end | end | ||
return p | return p |
Revision as of 15:27, 12 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
mw.log('Template:ChapterList not found') -- Log if template is not found
return {}
end
local content = listPage:getContent()
if not content then
mw.log('Failed to get content from Template:ChapterList') -- Log if content retrieval fails
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 -- Store chapter title with number as the key
end
mw.log('Chapters list parsed: ' .. mw.dumpObject(chapters)) -- Log the parsed chapters list
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
mw.log('Input is a table, extracting URL from args')
url = input.args and input.args[1] or nil
end
-- Safely log the received URL
mw.log('Received URL: ' .. (url or 'nil'))
if not url or type(url) ~= 'string' then
mw.log('Invalid or missing URL') -- Log if URL is not valid
return nil -- Return nil if `url` is not valid
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
mw.log('URL did not match expected pattern') -- Log if URL doesn't match
return nil -- Return nil if the URL does not match
end
mw.log('Matched Chapter: ' .. chapter .. ', Page: ' .. page)
-- Retrieve chapter data
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)
return result
end
mw.log('No matching chapter found for Chapter: ' .. chapter)
return string.format("Chapter %s: [Unknown Title], page %s", chapter, page) -- Fallback output
end
return p