Jump to content

Module:LuaTemplateLibraryInterview

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

-- Module:LuaTemplateLibraryInterview
-- Module for rendering the Library Interview template with semantics
-- Optimized version using TemplateHelpers functions

local p = {}

-- Dependencies
local TemplateStructure = require('Module:TemplateStructure')
local dateNormalization = require('Module:NormalizationDate')
local SemanticAnnotations = require('Module:SemanticAnnotations')
local TemplateHelpers = require('Module:TemplateHelpers')
local SemanticCategoryHelpers = require('Module:SemanticCategoryHelpers')
local ConfigRepository = require('Module:ConfigRepository')

--------------------------------------------------------------------------------
-- Configuration and Constants
--------------------------------------------------------------------------------
local Config = ConfigRepository.getStandardConfig('LibraryInterview')

-- The categories are now defined in ConfigRepository.lua

--------------------------------------------------------------------------------
-- Helper Functions
--------------------------------------------------------------------------------

-- Extract page name from wiki link [[Name]] or [[Name|Text]]
local function extractFromWikiLink(value)
    local name = value:match("%[%[([^%|%]]+)%]%]") or value:match("%[%[([^%|%]]+)%|.-%]%]")
    return name or value
end

-- Get current page ID
local function getCurrentPageId()
    local title = mw.title.getCurrentTitle()
    return title and title.id
end

--------------------------------------------------------------------------------
-- Field Processors
--------------------------------------------------------------------------------

-- Define field processors for renderFieldsBlock
local fieldProcessors = {
    Date = function(value)
        return TemplateHelpers.formatDateRange(value, nil, {
            outputMode = "text"
        })
    end,
    ID = function(value, args)
        if not value or value == "" then
            return tostring(getCurrentPageId() or "")
        end
        return value
    end,
    Interviewer = function(value)
        return value -- Could add special formatting if needed
    end,
    Interviewee = function(value)
        return value -- Could add special formatting if needed
    end
}

--------------------------------------------------------------------------------
-- Main Render Function
--------------------------------------------------------------------------------
function p.render(frame)
    local args = frame:getParent().args or {}
    
    -- Normalize arguments for case-insensitivity
    args = TemplateHelpers.normalizeArgumentCase(args)
    
    -- Configure block rendering
    local config = {
        tableClass = Config.constants.tableClass,
        blocks = {
            function(a) return TemplateHelpers.renderTitleBlock(a, "template-title", Config.constants.title) end,
            function(a) return TemplateHelpers.renderFieldsBlock(a, Config.fields, fieldProcessors) end
        }
    }
    
    -- Pre-allocate output components for better performance
    local outputComponents = {}
    
    -- Generate template output
    outputComponents[1] = TemplateStructure.render(args, config)
    local componentCount = 1
    
    -- Add semantic properties with optimized generation
    -- Set up transforms including wiki link extraction
    local semanticOptions = {
        transform = Config.semantics.transforms,
        specialCaseHandler = function(args, semanticOutput)
            -- Handle wiki link extraction for person properties
            semanticOutput = SemanticCategoryHelpers.addSemanticProperties(
                "person",
                args.Interviewee,
                semanticOutput,
                {processor = extractFromWikiLink}
            )
            
            return SemanticCategoryHelpers.addSemanticProperties(
                "person",
                args.Interviewer,
                semanticOutput,
                {processor = extractFromWikiLink}
            )
        end
    }
    
    -- Generate semantic properties using SemanticCategoryHelpers
    local semantics = SemanticCategoryHelpers.generateSemanticProperties(
        args, 
        Config.semantics, 
        {
            transform = Config.semantics.transforms,
            skipProperties = Config.semantics.skipProperties,
            specialCaseHandler = semanticOptions.specialCaseHandler
        }
    )
    if semantics and semantics ~= "" then
        componentCount = componentCount + 1
        outputComponents[componentCount] = semantics
    end
    
    -- Add categories from config
    local categories = ""
    if Config.categories and Config.categories.base then
        categories = SemanticCategoryHelpers.buildCategories(Config.categories.base)
    end
    if categories and categories ~= "" then
        componentCount = componentCount + 1
        outputComponents[componentCount] = categories
    end
    
    -- Combine all components efficiently
    return table.concat(outputComponents, "\n")
end

return p