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 | -- 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() | |||
local listPage = mw.title.new('Template:ChapterList') | |||
if not listPage then | |||
return {} | |||
end | |||
local content = listPage:getContent() | |||
if not content then | |||
return {} | |||
end | |||
-- Normalize apostrophes in content | |||
content = p.normalizeApostrophes(content) | |||
local chapters = {} | |||
for number, title, date, pagecount in string.gmatch(content, "{{Chapter|number=([%d%.]+)|title=([^|]+)|date=([^|]+)|pagecount=(%d+)}}") do | |||
chapters[number] = title -- Create a mapping for quick lookup | |||
end | |||
return chapters | |||
end | |||
-- Function to format the BCB reference | |||
function p.formatBCBReference(url) | function p.formatBCBReference(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 15: | Line 45: | ||
end | end | ||
if not chapter or not page then | if not chapter or not page then | ||
return nil -- Return nil if the URL does not match | return nil -- Return nil if the URL does not match | ||
end | end | ||
-- Retrieve the chapter data using the shared approach | |||
local chapters = p.getChaptersList() | |||
-- Retrieve the chapter data | local chapter_title = chapters[chapter] | ||
local | |||
local | |||
if chapter_title then | if chapter_title then | ||
return string.format("Chapter %s: %s, page %s", chapter, chapter_title, page) | |||
end | end | ||
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:08, 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()
local listPage = mw.title.new('Template:ChapterList')
if not listPage then
return {}
end
local content = listPage:getContent()
if not content then
return {}
end
-- Normalize apostrophes in content
content = p.normalizeApostrophes(content)
local chapters = {}
for number, title, date, pagecount in string.gmatch(content, "{{Chapter|number=([%d%.]+)|title=([^|]+)|date=([^|]+)|pagecount=(%d+)}}") do
chapters[number] = title -- Create a mapping for quick lookup
end
return chapters
end
-- Function to format the BCB reference
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 using the shared approach
local chapters = p.getChaptersList()
local chapter_title = chapters[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