Module:BZPowerCitation

From BIONICLEsector01

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

local forumCitation = require("Module:ForumCitation")
local websiteCitation = require("Module:WebsiteCitation")
local p = {}

-- The value of the section arg indicating that the desired citation cites
-- the forums section of BZPower.
local SECTION_FORUM = "forum"

-- The value of the section arg indicating that the desired citation cites
-- the blogs section of BZPower.
local SECTION_BLOG = "blog"

-- The value of the section arg indicating that the desired citation cites
-- the news section of BZPower.
local SECTION_NEWS = "news"

-- The title of the forums section of BZPower as it should appear in the
-- desired citation.
local WEBSITE_TITLE_FORUM = "BZPower Forums"

-- The title of the blogs section of BZPower as it should appear in the
-- desired citation.
local WEBSITE_TITLE_BLOG = "BZPower Blogs"

-- The title of the news section of BZPower as it should appear in the
-- desired citation.
local WEBSITE_TITLE_NEWS = "BZPower News"

-- The text to place in a BZPowerCitation's main link to indicate a particular
-- comment in a blog post
local COMMENT_TEXT_BLOG = "comment"
--------------------------------------------------------------------------------
------------------------------------ FORUMS ------------------------------------
--------------------------------------------------------------------------------

-- Return the forum post id gleaned from the given URL, or nil if none is found.
-- Assumes that mainUrl is not nil.
local function getForumPostId(mainUrl)
	return string.match(mainUrl, "#comment%-(%d+)") or string.match(mainUrl, "#entry(%d+)" )
end

-- Build and return the desired forum citation.
-- Asserts that topicTitle is not nil
-- and that at least one of originalUrl, archiveUrl, or brokenUrl is not nil.
function p.getBZPowerForumCitation(topicTitle, originalUrl, archiveUrl, brokenUrl, postId)
	assert(topicTitle ~= nil)
	assert(originalUrl ~= nil or archiveUrl ~= nil or brokenUrl ~= nil)
	
	return forumCitation.getForumCitation(WEBSITE_TITLE_FORUM, topicTitle, originalUrl, archiveUrl, brokenUrl, postId)
end

--------------------------------------------------------------------------------
------------------------------------ BLOGS -------------------------------------
--------------------------------------------------------------------------------

-- Return the blog comment id gleaned from the given URL,
-- or nil if none is found. Assumes that mainUrl is not nil.
local function getBlogCommentId(mainUrl)
	return string.match(mainUrl, "#comment%-(%d+)") or string.match(mainUrl, "#entry(%d+)" )
end

-- Return the text to place in the main citation link for a blog citation.
-- Assumes that pageTitle is are not nil.
local function getBlogMainLinkText(pageTitle, commentId)
	if commentId ~= nil then
		return "\"" .. pageTitle .. "\", " .. COMMENT_TEXT_BLOG .. " " .. commentId
	end
	
	return "\"" .. pageTitle .. "\""
end

-- Build and return the desired blog citation.
-- Asserts that pageTitle is not nil
-- and that at least one of originalUrl, archiveUrl, or brokenUrl is not nil.
function p.getBZPowerBlogCitation(pageTitle, originalUrl, archiveUrl, brokenUrl, commentId)
	assert(pageTitle ~= nil)
	assert(originalUrl ~= nil or archiveUrl ~= nil or brokenUrl ~= nil)
	
	local mainLinkText = getBlogMainLinkText(pageTitle, commentId)
	return websiteCitation.getWebsiteCitation(WEBSITE_TITLE_BLOG, mainLinkText, originalUrl, archiveUrl, brokenUrl)
end

--------------------------------------------------------------------------------
------------------------------------- NEWS -------------------------------------
--------------------------------------------------------------------------------

-- Build and return the desired news citation.
-- Asserts that pageTitle is not nil
-- and that at least one of originalUrl, archiveUrl, or brokenUrl is not nil.
function p.getBZPowerNewsCitation(pageTitle, originalUrl, archiveUrl, brokenUrl)
	assert(pageTitle ~= nil)
	assert(originalUrl ~= nil or archiveUrl ~= nil or brokenUrl ~= nil)
	
	local mainLinkText = "\"" .. pageTitle .. "\""
	return websiteCitation.getWebsiteCitation(WEBSITE_TITLE_NEWS, mainLinkText, originalUrl, archiveUrl, brokenUrl)
end

--------------------------------------------------------------------------------
------------------------------ TEMPLATE INTERFACE ------------------------------
--------------------------------------------------------------------------------

-- Pull out the relevant args for use.
function p.bzpowerCitation(frame)
	local section = string.lower(frame.args[1])
	
	local pageTitle = frame.args["pagetitle"]
    
    local originalUrl = frame.args["originalurl"]
    if originalUrl == "" then
    	originalUrl = nil
    end
    
    local archiveUrl = frame.args["archiveurl"]
    if archiveUrl == "" then
    	archiveUrl = nil
    end
    
    local brokenUrl = frame.args["brokenurl"]
    if brokenUrl == "" then
    	brokenUrl = nil
    end
    
    assert(section == SECTION_FORUM or section == SECTION_BLOG or section == SECTION_NEWS,
    	   [[BZPowerCitation received an invalid section argument;
    	   all BZPowerCitations must have the first parameter set to
    	   either "]] .. SECTION_FORUM ..
    	   [[", "]] .. SECTION_BLOG ..
    	   [[", or "]] .. SECTION_NEWS .. [["]])
    assert(pageTitle ~= nil,
    	   [[BZPowerCitation received no page title;
    	   all BZPowerCitations must have a title specified
    	   through the `pagetitle` parameter]])
	assert(originalUrl ~= nil or archiveUrl ~= nil or brokenUrl ~= nil,
		   [[BZPowerCitation received no topic URL;
		   all BZPowerCitations must have a URL specified through
		   at least one of the following parameters:
		   `originalurl`, `archiveurl`, or `brokenurl`]])
    
    -- a mapping from the value of the section argument to the appropriate
    -- anonymous function to call to build the desired citation
    local sectionToFunctionMap = {
		[SECTION_FORUM] = function()
			local postId = getForumPostId(originalUrl or archiveUrl or brokenUrl)
			return p.getBZPowerForumCitation(pageTitle, originalUrl, archiveUrl, brokenUrl, postId)
		end,
		[SECTION_BLOG] = function()
			local commentId = getBlogCommentId(originalUrl or archiveUrl or brokenUrl)
			return p.getBZPowerBlogCitation(pageTitle, originalUrl, archiveUrl, brokenUrl, commentId)
		end,
		[SECTION_NEWS] = function()
			return p.getBZPowerNewsCitation(pageTitle, originalUrl, archiveUrl, brokenUrl)
		end
    }
    return sectionToFunctionMap[section]()
end

return p