Module:Citation/BCB: Difference between revisions
NeonWabbit (talk | contribs) No edit summary |
NeonWabbit (talk | contribs) No edit summary |
||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
local mw = require('mw') | local mw = require('mw') | ||
-- Utility function to convert a value to a string safely | |||
local function safeToString(value) | |||
if type(value) == 'table' then | |||
return 'table: ' .. mw.dumpObject(value) -- Dump the table content for inspection | |||
end | |||
return tostring(value) | |||
end | |||
-- Function to format the BCB reference | -- Function to format the BCB reference | ||
function p.formatBCBReference(url) | function p.formatBCBReference(input) | ||
mw.log(' | local url = input | ||
-- Handle if input is a table (e.g., a frame object) | |||
if type(input) == 'table' then | |||
mw.log('Input is a table, dumping contents: ' .. mw.dumpObject(input)) | |||
-- Attempt to extract the `url` if it's provided as a positional argument | |||
url = input.args and input.args[1] or nil | |||
end | |||
-- Safely log the `url` value | |||
mw.log('Received URL: ' .. safeToString(url)) | |||
if not url or type(url) ~= 'string' then | if not url or type(url) ~= 'string' then | ||
mw.log('Invalid URL provided') | mw.log('Invalid URL provided or URL is not a string') | ||
return nil -- Return nil if `url` is not valid | return nil -- Return nil if `url` is not valid | ||
end | end | ||
Line 16: | Line 35: | ||
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') | ||
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: ' .. | mw.log('Matched Chapter: ' .. chapter .. ', Page: ' .. page) | ||
-- Retrieve the chapter data | -- Retrieve the chapter data | ||
local chapters = p.getChaptersList() | local chapters = p.getChaptersList() | ||
local chapter_title = chapters[chapter] | local chapter_title = chapters[chapter] | ||
if chapter_title then | if chapter_title then | ||
local result = string.format("Chapter %s: %s, page %s", | local result = string.format("Chapter %s: %s, page %s", chapter, chapter_title, page) | ||
mw.log('Generated Result: ' .. result) | mw.log('Generated Result: ' .. result) | ||
return result | return result | ||
end | end | ||
mw.log('No matching chapter found for Chapter: ' .. | mw.log('No matching chapter found for Chapter: ' .. 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:15, 12 November 2024
Documentation for this module may be created at Module:Citation/BCB/doc
local p = {}
local mw = require('mw')
-- Utility function to convert a value to a string safely
local function safeToString(value)
if type(value) == 'table' then
return 'table: ' .. mw.dumpObject(value) -- Dump the table content for inspection
end
return tostring(value)
end
-- Function to format the BCB reference
function p.formatBCBReference(input)
local url = input
-- Handle if input is a table (e.g., a frame object)
if type(input) == 'table' then
mw.log('Input is a table, dumping contents: ' .. mw.dumpObject(input))
-- Attempt to extract the `url` if it's provided as a positional argument
url = input.args and input.args[1] or nil
end
-- Safely log the `url` value
mw.log('Received URL: ' .. safeToString(url))
if not url or type(url) ~= 'string' then
mw.log('Invalid URL provided or URL is not a string')
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')
return nil -- Return nil if the URL does not match
end
mw.log('Matched Chapter: ' .. chapter .. ', Page: ' .. page)
-- Retrieve the 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 nil -- Return nil if no matching chapter is found
end
return p