Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Library citation: Difference between revisions

From ProleWiki, the proletarian encyclopedia
(Added methods for processing links and returning data)
(Adding rough presentation of formatting)
Line 2: Line 2:


local function checkset(var)
local function checkset(var)
local rtn = ""
local rtn = nil
if not (var == nil or var == '') then
if not (var == nil or var == '') then
rtn = var
rtn = var
Line 73: Line 73:
-- get arguments from "args" table pre-defined by MediaWiki/Scribunto
-- get arguments from "args" table pre-defined by MediaWiki/Scribunto
local link = checkset(parentFrame.args.link)
local link = checkset(parentFrame.args.link)
local author = checkset(parentFrame.args.author)
local author = nil
local work = checkset(parentFrame.args.work)
if is_set(link) then
title,author,year,source = getDataFromLink(link)
end
author = checkset(parentFrame.args.author) or author
local title = checkset(parentFrame.args.title) or title
-- error("error message") to send error messages
-- error("error message") to send error messages
-- mw.log(mw.getCurrentFrame():callParserFunction("#dpl","namespace=Library","titleregexp=Vladimir Lenin/[^/]+$","format=,%PAGE%\\n"))
local render = author .. " (" .. year .. "). " .. "''" .. title .. "''." .. source
-- mw.log(mw.getCurrentFrame():callParserFunction("#dpl","namespace=Library","category=Library works by Vladimir Lenin","format=,%PAGE%\\n"))
return render
return listOfWorks(author)
end
end


return p
return p

Revision as of 23:14, 7 October 2024

local p = {}

local function checkset(var)
	local rtn = nil
	if not (var == nil or var == '') then
		rtn = var
	end
	return rtn
end

local function is_set(var)
	return not (var == nil or var == '')
end

local function removeApostrophes(str)
    return str:gsub("[\"'%`]", "")
end

local function compareStrings(a,b)
	local stringA = removeApostrophes(a):lower()
    local stringB = removeApostrophes(b):lower()
    return stringA < stringB
end

function listOfWorks(author) 
	local newMethod = mw.getCurrentFrame():callParserFunction("#dpl","namespace=Library","titleregexp=" .. author .. "/[^/]+$","format=,%TITLE%\\n")
	local catMethod = mw.getCurrentFrame():callParserFunction("#dpl","namespace=Library","category=Library works by " .. author ,"format=,%TITLE%\\n")
	local list = ""
	-- Picks whatever method of recovering pages has the higher count, usually categories
	if string.len(newMethod) > string.len(catMethod) then
		list = newMethod
		else
			list = catMethod
	end
	titleTable = mw.text.split(string.gsub(mw.text.trim(list),author .. "/",""),"\n")
	linkTable=mw.text.split(mw.text.trim(list),"\n")
	for i,v in ipairs(linkTable) do
		linkTable[i] = "Library:" .. v
	end
	table.sort(linkTable,compareStrings)
	table.sort(titleTable,compareStrings)
	return {links=linkTable,titles=titleTable}
end

function getDataFromTitle(title)
	local rawString = mw.getCurrentFrame():callParserFunction("#dpl","namespace=Library","title=" .. title,"mode=userformat","include={Library work}:title:author:published_date:source")
	local title,author,published_date,source = rawString:match("^(.-)%s|(.-)%s|(.-)%s|(.-)$")
	local year = published_date:match("%d%d%d%d")
	return title,author,year,source
end

function getDataFromLink(link)
	-- In case it's in URL format
	local str = link
    local result = str:match("https?://[^/]+/wiki/(.*)")
    if result then
        str = result  -- Use the extracted part after /wiki/
    end

    -- Remove "Library:" prefix, if present
    str = str:gsub("^Library:", "")

    -- Case 3: Replace underscores with spaces
    str = str:gsub("_", " ")

    -- Return data using the other function
    return getDataFromTitle(str)
end

function p.cite(frame)
	-- Parent frame to get parameters from template calls instead of from {{#invoke: ...
	local parentFrame = frame:getParent()
	-- get arguments from "args" table pre-defined by MediaWiki/Scribunto
	local link = checkset(parentFrame.args.link)
	local author = nil
	if is_set(link) then
		title,author,year,source = getDataFromLink(link)
	end
	author = checkset(parentFrame.args.author) or author
	local title = checkset(parentFrame.args.title) or title
	-- error("error message") to send error messages
	local render = author .. " (" .. year .. "). " .. "''" .. title .. "''." .. source
	return render
end

return p