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
CriticalResist (talk | contribs)
attempting to fix link to library page. It will also hide source link, can always be changed.
CriticalResist (talk | contribs)
test fix 2
Line 11: Line 11:
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


local function removeApostrophes(str)
local function removeApostrophes(str)
Line 61: Line 61:
     end
     end
     str = str:gsub("^Library:", "")
     str = str:gsub("^Library:", "")
    str = str:gsub(" ", "_")  -- Ensure underscores for page names
      
      
     return getDataFromTitle(str), str
    -- Extract chapter from fragment identifier
    local base, chapter = str:match("^([^#]*)#?(.*)$")
    base = base:gsub("_", " ")  -- Convert underscores to spaces for display
   
     return getDataFromTitle(str), base, chapter
end
end


local function wikilinkFromAuthor(author)
local function wikilinkFromAuthor(author)
     if mw.getCurrentFrame():callParserFunction('#ifexist',author,true) == "1" then
     if mw.getCurrentFrame():callParserFunction('#ifexist', author, true) == "1" then
         return "[[" .. author .. "]]"
         return "[[" .. author .. "]]"
     end
     end
Line 77: Line 80:
     local link = checkset(parentFrame.args.link)
     local link = checkset(parentFrame.args.link)
     local author = nil
     local author = nil
    local title = nil
    local year = nil
    local source = nil
    local pageName = nil
    local chapter = nil
     local quote = checkset(parentFrame.args.quote)
     local quote = checkset(parentFrame.args.quote)
    local pageName = nil
    local source = nil
      
      
     if is_set(link) then
     if is_set(link) then
         title, author, year, source, pageName = getDataFromLink(link)
         title, author, year, source, base, chapter = getDataFromLink(link)
     end
     end
      
      
     local render = ""
     local render = ""
      
      
    -- Format author
     if is_set(author) then
     if is_set(author) then
         render = render .. wikilinkFromAuthor(author)
         render = render .. wikilinkFromAuthor(author)
     end
     end
   
    -- Format year in parentheses if available
     if is_set(year) then
     if is_set(year) then
         render = render .. " (" .. year .. ")."
         render = render .. " (" .. year .. ")"
     end
     end
   
    -- Format title with chapter
     if is_set(title) and is_set(pageName) then
     if is_set(title) and is_set(pageName) then
         render = render .. " ''[[Library:" .. pageName .. "|" .. title .. "]]''."
         -- Add period after author/year section
        if render ~= "" then
            render = render .. ". "
        end
       
        -- Create Library link
        local displayTitle = pageName
        local linkTarget = "Library:" .. pageName:gsub(" ", "_")
       
        -- Add chapter if present
        if is_set(chapter) then
            -- Replace underscores with spaces in chapter name
            chapter = chapter:gsub("_", " ")
            displayTitle = displayTitle .. ", " .. chapter
            linkTarget = linkTarget .. "#" .. chapter:gsub(" ", "_")
        end
       
        render = render .. "[[" .. linkTarget .. "|" .. displayTitle .. "]]"
     end
     end
     if is_set (quote) then
   
         quote = string.gsub(quote,"\n","<br>")
    -- Handle quotes
     if is_set(quote) then
         quote = string.gsub(quote, "\n", "<br>")
         quote = "“" .. quote .. "”"
         quote = "“" .. quote .. "”"
         local div = mw.html.create ('div')
         local div = mw.html.create('div')
         div
         div:attr("style", "width:80%; margin-left:10%;")
        :attr("style", "width:80%; margin-left:10%;")
            :wikitext(quote)
        :wikitext(quote)
         render = italics(tostring(div)) .. "<br>" .. render
        local br = mw.html.create ('br', selfClosing)
         render = italics(tostring(div)) .. tostring(br) .. render
     end
     end
      
      
     -- Store source in hidden metadata
     -- Preserve source in hidden element
     if is_set(source) then
     if is_set(source) then
         render = render .. '<span style="display:none">Source: ' .. source .. '</span>'
         render = render .. '<span style="display:none">Source: ' .. source .. '</span>'

Revision as of 15:14, 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:", "")
    
    -- Extract chapter from fragment identifier
    local base, chapter = str:match("^([^#]*)#?(.*)$")
    base = base:gsub("_", " ")  -- Convert underscores to spaces for display
    
    return getDataFromTitle(str), base, chapter
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 title = nil
    local year = nil
    local source = nil
    local pageName = nil
    local chapter = nil
    local quote = checkset(parentFrame.args.quote)
    
    if is_set(link) then
        title, author, year, source, base, chapter = getDataFromLink(link)
    end
    
    local render = ""
    
    -- Format author
    if is_set(author) then
        render = render .. wikilinkFromAuthor(author)
    end
    
    -- Format year in parentheses if available
    if is_set(year) then
        render = render .. " (" .. year .. ")"
    end
    
    -- Format title with chapter
    if is_set(title) and is_set(pageName) then
        -- Add period after author/year section
        if render ~= "" then
            render = render .. ". "
        end
        
        -- Create Library link
        local displayTitle = pageName
        local linkTarget = "Library:" .. pageName:gsub(" ", "_")
        
        -- Add chapter if present
        if is_set(chapter) then
            -- Replace underscores with spaces in chapter name
            chapter = chapter:gsub("_", " ")
            displayTitle = displayTitle .. ", " .. chapter
            linkTarget = linkTarget .. "#" .. chapter:gsub(" ", "_")
        end
        
        render = render .. "[[" .. linkTarget .. "|" .. displayTitle .. "]]"
    end
    
    -- Handle quotes
    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)
        render = italics(tostring(div)) .. "<br>" .. render
    end
    
    -- Preserve source in hidden element
    if is_set(source) then
        render = render .. '<span style="display:none">Source: ' .. source .. '</span>'
    end
    
    return render
end

return p