Module:InterviewArchive

From JoJo's Bizarre Encyclopedia - JoJo Wiki
Revision as of 19:40, 12 May 2023 by Vish (talk | contribs)
Jump to navigation Jump to search

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

local utils = require('Module:InterviewUtils')
local p = {}

local function cleanIntervieweeName(interviewee)
	    local cleaned = mw.text.trim(interviewee)
	    cleaned = string.gsub(cleaned, '%[%[(.-)%]%]', '%1')  -- remove wikitext link
	    cleaned = string.gsub(cleaned, '{{W|(.-)|(.-)}}', '%2')  -- handle template with piped link
	    cleaned = string.gsub(cleaned, '{{W|(.-)}}', '%1')  -- remove template
	    cleaned = string.gsub(cleaned, '%[extlink (.-)%]', '%1')  -- remove external link
	    cleaned = string.gsub(cleaned, "'", ''')  -- replace single quotes with HTML character entity
	    return cleaned
end

local function generateInterview(interview, frame)
    local output = {}
    local year = utils.parse(interview.date)
    local year, _, _ = utils.parse(interview.date) -- get the year from the date

	local cleanInterviewees = {}
	for _, interviewee in ipairs(interview.interviewee) do
	    local cleanInterviewee = cleanIntervieweeName(interviewee)
	    table.insert(cleanInterviewees, cleanInterviewee)
	end
	
	local function valueOrDefault(value, default)
	    if value == nil or value == '' then
	        return default
	    else
	        return value
	    end
	end

	table.insert(output, '<div class="mnbox interview" id="' .. interview.title .. '" data-year="' .. year .. '" data-interviewee=\'' .. mw.text.jsonEncode(cleanInterviewees) .. '\' data-type="' .. interview.type .. '" data-tlstatus="' .. valueOrDefault(interview.translation, 'Complete')  .. '" data-trstatus="' .. valueOrDefault(interview.transcript, 'Complete') .. '" data-publication="' .. valueOrDefault(interview.publication, 'None') .. '" data-tags=\'' .. mw.text.jsonEncode(interview.tags) .. '\'>')
	
    table.insert(output, '<div class="ivimage" style="display:table-cell; vertical-align:top;">')
    if interview.cover ~= '' then
        table.insert(output, '[[File:' .. interview.cover .. '||90px]]')
    else
        table.insert(output, '[[File:NoPicAv.png|90px]]')
    end
    table.insert(output, '</div>')
    table.insert(output, '<div style="display:table-cell; width:100%;">')
    table.insert(output, '<div class="column">')
    table.insert(output, '<div class="diamond" style="background:' .. frame:preprocess('{{Color|' .. interview.part .. '}}') .. '; text-align:center; padding:0.5%;">')
    table.insert(output, '<span class="fadeinimg" style="float: left; margin-left:5px;">')
    table.insert(output, '[[File:JJLSymbol.png|20px|link=#' .. interview.title .. '|Link to this section]]</span>')
    if interview.display then
    	table.insert(output, frame:preprocess("{{Shadow|{{White2|Interview:" .. interview.title .. "|'''" .. interview.display .. "}}}}"))
	else
		table.insert(output, frame:preprocess("{{Shadow|{{White2|Interview:" .. interview.title .. "|'''" .. interview.title .. "}}}}"))
	end
    table.insert(output, '</div>')
    table.insert(output, '<div style="padding-left:1%; font-size:90%;">')
    if interview.translation then
        table.insert(output, frame:preprocess('{{Tag|' .. interview.translation .. '}}'))
    end
    if interview.transcript then
        table.insert(output, frame:preprocess('{{Tag|' .. interview.transcript .. '2}}'))
    end
    table.insert(output, frame:preprocess('{{Tag|' .. interview.media .. '}}'))
    table.insert(output, frame:preprocess('{{Tag|' .. interview.type .. '}}'))
    table.insert(output, frame:preprocess('{{Align|{{Tag|Date=' .. interview.date .. '}}|right}}'))
    table.insert(output, '<div class="interviewee" style="display:table-row;">👤 ')
	for i, person in ipairs(interview.interviewee) do
	  if i > 1 then
	    table.insert(output, ", ")
	  end
	  table.insert(output, frame:preprocess(person))
	end

	table.insert(output, '</div>')
    table.insert(output, '<div style="display:table-row;">📜 ')
    for _, tag in ipairs(interview.tags) do
        table.insert(output, frame:preprocess('{{Tag|' .. tag .. '}}'))
    end
    table.insert(output, '</div></div></div></div></div>')
    return table.concat(output)
end

-- Export the generateInterview function so it can be used from outside the module
function p.generateInterview(frame)
    -- Extract the interview title from frame.args[1]
    local interviewTitle = frame.args[1]

    -- Load the interviews from JSON data
    local interviewsJson = mw.loadJsonData('JoJo Wiki:Interviews').interviews

    -- Find the interview with the matching title
    local interview = nil
    for i, v in ipairs(interviewsJson) do
        if v.title == interviewTitle then
            interview = v
            break
        end
    end

    -- If we found the interview, generate it
    if interview ~= nil then
        return generateInterview(interview, frame)
    else
        return "Interview not found"
    end
end

function p.generateArchive(frame)
    local content = mw.loadJsonData('JoJo Wiki:Interviews').interviews
    local html = {'<div id="interview-container">'} -- Start the div with the ID 'interview-container'
    
    for _, interview in ipairs(content) do
        table.insert(html, generateInterview(interview, frame))
    end

    table.insert(html, '</div>') -- End the div with the ID 'interview-container'
    return table.concat(html)
end

function p.generatePerson(frame)
    -- Extract the interviewee
    local targetInterviewee = mw.title.getCurrentTitle().text
    targetInterviewee = string.gsub(targetInterviewee, "'", '&#39;')

    -- Load the interviews from JSON data
    local content = mw.loadJsonData('JoJo Wiki:Interviews').interviews

    -- Find the interviews belonging to the interviewee
    local interviewsForPerson = {}

	for i, interview in ipairs(content) do
	    for _, interviewee in ipairs(interview.interviewee) do
	        local cleanedInterviewee = cleanIntervieweeName(interviewee)
	        if cleanedInterviewee == targetInterviewee then
	            table.insert(interviewsForPerson, interview)
	            break
	        end
	    end
	end

    -- If we found any interviews, generate them
    if #interviewsForPerson > 0 then
        local output = ""
        for _, interview in ipairs(interviewsForPerson) do
            output = output .. generateInterview(interview, frame)
        end
        return output
    else
        return "No interviews found for " .. targetInterviewee
    end
end

return p