Module:Citation/BCB: Difference between revisions
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..." |
NeonWabbit (talk | contribs) No edit summary |
||
Line 4: | Line 4: | ||
-- Function to fetch and parse chapter data from the `Template:ChapterList` | -- Function to fetch and parse chapter data from the `Template:ChapterList` | ||
function p.formatBCBReference(url) | function p.formatBCBReference(url) | ||
mw.log('Received URL: ' .. (url or 'nil')) -- Log the received URL | |||
if not url or type(url) ~= 'string' then | if not url or type(url) ~= 'string' then | ||
return nil -- Return nil if `url` is not valid | return nil -- Return nil if `url` is not valid | ||
Line 14: | Line 15: | ||
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 | -- Retrieve the chapter data | ||
local title = mw.title.new('Template:ChapterList') | local title = mw.title.new('Template:ChapterList') | ||
if not title or not title.exists then | if not title or not title.exists then | ||
mw.log('Template:ChapterList does not exist') -- Log if template does not exist | |||
return nil -- Return nil if the template does not exist | return nil -- Return nil if the template does not exist | ||
end | end | ||
local content = title:getContent() | local content = title:getContent() | ||
if not content then | |||
mw.log('Failed to retrieve content from Template:ChapterList') -- Log if content retrieval fails | |||
return nil | |||
end | |||
local chapter_data = {} | local chapter_data = {} | ||
for number, chapterTitle in content:gmatch('{{Chapter|number=([^|]+)|title=([^|]+)|date=[^|]+|pagecount=%d+}}') do | for number, chapterTitle in content:gmatch('{{Chapter|number=([^|]+)|title=([^|]+)|date=[^|]+|pagecount=%d+}}') do | ||
Line 31: | Line 41: | ||
local chapter_title = chapter_data[chapter] | local chapter_title = chapter_data[chapter] | ||
if chapter_title then | 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 | 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:04, 12 November 2024
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)
mw.log('Received URL: ' .. (url or 'nil')) -- Log the received 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
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
local title = mw.title.new('Template:ChapterList')
if not title or not title.exists then
mw.log('Template:ChapterList does not exist') -- Log if template does not exist
return nil -- Return nil if the template does not exist
end
local content = title:getContent()
if not content then
mw.log('Failed to retrieve content from Template:ChapterList') -- Log if content retrieval fails
return nil
end
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
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