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
Forte (talk | contribs)
m Soft-coding "year" variable in case of trouble
CriticalResist (talk | contribs)
attempting to fix link to library page. It will also hide source link, can always be changed.
Line 4: Line 4:


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


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


Line 20: Line 20:


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


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


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


function getDataFromLink(link)
function getDataFromLink(link)
-- In case it's in URL format
    local str = link
local str = link
     local result = str:match("https?://[^/]+/wiki/(.*)")
     local result = str:match("https?://[^/]+/wiki/(.*)")
     if result then
     if result then
         str = result -- Use the extracted part after /wiki/
         str = result
     end
     end
    -- Remove "Library:" prefix, if present
     str = str:gsub("^Library:", "")
     str = str:gsub("^Library:", "")
 
     str = str:gsub(" ", "_") -- Ensure underscores for page names
    -- Replace underscores with spaces
      
     str = str:gsub("_", " ")
     return getDataFromTitle(str), str
 
     -- Return data using the other function
     return getDataFromTitle(str)
end
 
function wikilinkFromTitle(title)
local pagename = mw.getCurrentFrame():callParserFunction("#dpl","namespace=Library","titleregexp=" .. title .. "$","format=,%TITLE%")
if is_set(pagename) then
return "[[Library:" .. pagename .. "|" .. title .. "]]"
end
return title
end
end


local function wikilinkFromAuthor(author)
local function wikilinkFromAuthor(author)
-- if page exists
    if mw.getCurrentFrame():callParserFunction('#ifexist',author,true) == "1" then
if mw.getCurrentFrame():callParserFunction('#ifexist',author,true) == "1" then
        return "[[" .. author .. "]]"
return "[[" .. author .. "]]"
    end
end
    return author
return author
end
end


function p.cite(frame)
function p.cite(frame)
-- Parent frame to get parameters from template calls instead of from {{#invoke: ...
    local parentFrame = frame:getParent()
local parentFrame = frame:getParent()
    local link = checkset(parentFrame.args.link)
-- get arguments from "args" table pre-defined by MediaWiki/Scribunto
    local author = nil
local link = checkset(parentFrame.args.link)
    local quote = checkset(parentFrame.args.quote)
local author = nil
    local pageName = nil
local quote = checkset(parentFrame.args.quote)
    local source = nil
if is_set(link) then
   
title,author,year,source = getDataFromLink(link)
    if is_set(link) then
end
        title, author, year, source, pageName = getDataFromLink(link)
    end
local render = ""
   
    local render = ""
if is_set(author) then
   
render = render .. wikilinkFromAuthor(author)
    if is_set(author) then
end
        render = render .. wikilinkFromAuthor(author)
if is_set(year) then
    end
render = render .. " (" .. year .. ")."
    if is_set(year) then
end
        render = render .. " (" .. year .. ")."
if is_set(title) then
    end
render = render .. " ''" .. wikilinkFromTitle(title) .. "''."
    if is_set(title) and is_set(pageName) then
end
        render = render .. " ''[[Library:" .. pageName .. "|" .. title .. "]]''."
if is_set(source) then
    end
render = render .. " " .. source .. "."
    if is_set (quote) then
end
        quote = string.gsub(quote,"\n","<br>")
if is_set (quote) then
        quote = "“" .. quote .. "”"
quote = string.gsub(quote,"\n","<br>")
        local div = mw.html.create ('div')
quote = "“" .. quote .. "”"
        div
local div = mw.html.create ('div')
        :attr("style", "width:80%; margin-left:10%;")
div
        :wikitext(quote)
:attr("style", "width:80%; margin-left:10%;")
        local br = mw.html.create ('br', selfClosing)
:wikitext(quote)
        render = italics(tostring(div)) .. tostring(br) .. render
local br = mw.html.create ('br', selfClosing)
    end
render = italics(tostring(div)) .. tostring(br) .. render
   
end
    -- Store source in hidden metadata
-- error("error message") to send error messages
    if is_set(source) then
return render
        render = render .. '<span style="display:none">Source: ' .. source .. '</span>'
    end
   
    return render
end
end


return p
return p

Revision as of 14:55, 13 July 2025

local p = {}

require("Module:Auxiliary functions")

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 = ""
    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 = ""
    if is_set(published_date) then
        year = published_date:match("%d%d%d%d")
    end
    return title,author,year,source
end

function getDataFromLink(link)
    local str = link
    local result = str:match("https?://[^/]+/wiki/(.*)")
    if result then
        str = result
    end
    str = str:gsub("^Library:", "")
    str = str:gsub(" ", "_")  -- Ensure underscores for page names
    
    return getDataFromTitle(str), str
end

local function wikilinkFromAuthor(author)
    if mw.getCurrentFrame():callParserFunction('#ifexist',author,true) == "1" then
        return "[[" .. author .. "]]"
    end
    return author
end

function p.cite(frame)
    local parentFrame = frame:getParent()
    local link = checkset(parentFrame.args.link)
    local author = nil
    local quote = checkset(parentFrame.args.quote)
    local pageName = nil
    local source = nil
    
    if is_set(link) then
        title, author, year, source, pageName = getDataFromLink(link)
    end
    
    local render = ""
    
    if is_set(author) then
        render = render .. wikilinkFromAuthor(author)
    end
    if is_set(year) then
        render = render .. " (" .. year .. ")."
    end
    if is_set(title) and is_set(pageName) then
        render = render .. " ''[[Library:" .. pageName .. "|" .. title .. "]]''."
    end
    if is_set (quote) then
        quote = string.gsub(quote,"\n","<br>")
        quote = "“" .. quote .. "”"
        local div = mw.html.create ('div')
        div
        :attr("style", "width:80%; margin-left:10%;")
        :wikitext(quote)
        local br = mw.html.create ('br', selfClosing)
        render = italics(tostring(div)) .. tostring(br) .. render
    end
    
    -- Store source in hidden metadata
    if is_set(source) then
        render = render .. '<span style="display:none">Source: ' .. source .. '</span>'
    end
    
    return render
end

return p