Jump to content

Module:T-LibraryInterview: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
Line 8: Line 8:
local ErrorHandling = require('Module:ErrorHandling')
local ErrorHandling = require('Module:ErrorHandling')
local ConfigRepository = require('Module:ConfigRepository')
local ConfigRepository = require('Module:ConfigRepository')
local LinkParser = require('Module:LinkParser')
-- Module-level cache for lazy-loaded modules
local moduleCache = {}
-- Lazy module loader
local function lazyRequire(moduleName)
    return function()
        if not moduleCache[moduleName] then
            moduleCache[moduleName] = require(moduleName)
        end
        return moduleCache[moduleName]
    end
end
-- We can add lazily loaded modules here when needed


-- ========== Helper Functions ==========
-- ========== Helper Functions ==========
Line 28: Line 44:
-- Add preprocessor to ensure ID field is always set
-- Add preprocessor to ensure ID field is always set
Blueprint.addPreprocessor(template, 'setPageIdField')
Blueprint.addPreprocessor(template, 'setPageIdField')
-- Helper function to extract semantic value from field
local function extractSemanticValue(fieldValue, fieldName)
    if not fieldValue or fieldValue == "" then
        return nil
    end
   
    -- If the value already has wiki links, extract the name using LinkParser
    if LinkParser.processWikiLink(fieldValue, "check") then
        return ErrorHandling.protect(
            errorContext,
            "extractFromWikiLink_" .. fieldName,
            LinkParser.extractFromWikiLink,
            fieldValue,  -- fallback to original value on error
            fieldValue
        )
    else
        -- Otherwise, use the plain text value
        return fieldValue
    end
end


-- Add preprocessor for semantic property extraction
-- Add preprocessor for semantic property extraction
Blueprint.addPreprocessor(template, function(template, args)
Blueprint.addPreprocessor(template, function(template, args)
    -- Load TemplateHelpers module
    local TemplateHelpers = require('Module:TemplateHelpers')
   
     -- Extract names from wiki links for semantic properties
     -- Extract names from wiki links for semantic properties
     if args.Interviewee and args.Interviewee ~= "" then
     args._semanticInterviewee = extractSemanticValue(args.Interviewee, "Interviewee")
        -- If the value already has wiki links, extract the name
     args._semanticInterviewer = extractSemanticValue(args.Interviewer, "Interviewer")
        if args.Interviewee:match("%[%[.-%]%]") then
            args._semanticInterviewee = ErrorHandling.protect(
                errorContext,
                "extractFromWikiLink_Interviewee",
                TemplateHelpers.extractFromWikiLink,
                args.Interviewee, -- fallback to original value on error
                args.Interviewee
            )
        else
            -- Otherwise, use the plain text value
            args._semanticInterviewee = args.Interviewee
        end
    end
   
     if args.Interviewer and args.Interviewer ~= "" then
        -- If the value already has wiki links, extract the name
        if args.Interviewer:match("%[%[.-%]%]") then
            args._semanticInterviewer = ErrorHandling.protect(
                errorContext,
                "extractFromWikiLink_Interviewer",
                TemplateHelpers.extractFromWikiLink,
                args.Interviewer, -- fallback to original value on error
                args.Interviewer
            )
        else
            -- Otherwise, use the plain text value
            args._semanticInterviewer = args.Interviewer
        end
    end
      
      
     return args
     return args

Revision as of 04:45, 23 April 2025

Documentation for this module may be created at Module:T-LibraryInterview/doc

--Module:T-LibraryInterview
-- Module for rendering the I&DG Library Interview template using the Blueprint framework

local p = {}

-- ========== Required modules ==========
local Blueprint = require('Module:LuaTemplateBlueprint')
local ErrorHandling = require('Module:ErrorHandling')
local ConfigRepository = require('Module:ConfigRepository')
local LinkParser = require('Module:LinkParser')

-- Module-level cache for lazy-loaded modules
local moduleCache = {}

-- Lazy module loader
local function lazyRequire(moduleName)
    return function()
        if not moduleCache[moduleName] then
            moduleCache[moduleName] = require(moduleName)
        end
        return moduleCache[moduleName]
    end
end

-- We can add lazily loaded modules here when needed

-- ========== Helper Functions ==========
-- Create error context for the module
local errorContext = ErrorHandling.createContext("T-LibraryInterview")

-- ========== Template Registration ==========
-- Register the template with the Blueprint
local template = Blueprint.registerTemplate('LibraryInterview')

-- Initialize the standard configuration
Blueprint.initializeConfig(template)

-- Get property mappings from ConfigRepository for tooltips
local propertyMappings = ConfigRepository.templates.LibraryInterview.semantics.properties

-- The ID field is handled by the 'setPageIdField' preprocessor

-- ========== Preprocessors ==========
-- Add preprocessor to ensure ID field is always set
Blueprint.addPreprocessor(template, 'setPageIdField')

-- Helper function to extract semantic value from field
local function extractSemanticValue(fieldValue, fieldName)
    if not fieldValue or fieldValue == "" then
        return nil
    end
    
    -- If the value already has wiki links, extract the name using LinkParser
    if LinkParser.processWikiLink(fieldValue, "check") then
        return ErrorHandling.protect(
            errorContext,
            "extractFromWikiLink_" .. fieldName,
            LinkParser.extractFromWikiLink,
            fieldValue,  -- fallback to original value on error
            fieldValue
        )
    else
        -- Otherwise, use the plain text value
        return fieldValue
    end
end

-- Add preprocessor for semantic property extraction
Blueprint.addPreprocessor(template, function(template, args)
    -- Extract names from wiki links for semantic properties
    args._semanticInterviewee = extractSemanticValue(args.Interviewee, "Interviewee")
    args._semanticInterviewer = extractSemanticValue(args.Interviewer, "Interviewer")
    
    return args
end)

-- Configure semantic properties to use extracted values
template.config.semantics = template.config.semantics or {}
template.config.semantics.properties = template.config.semantics.properties or {}
template.config.semantics.properties["Has interviewee"] = "_semanticInterviewee"
template.config.semantics.properties["Has interviewer"] = "_semanticInterviewer"

-- ========== Main Render Function ==========
-- Main render function that delegates to the template's render method
function p.render(frame)
    return ErrorHandling.protect(
        errorContext,
        "render",
        function()
            return template.render(frame)
        end,
        ErrorHandling.getMessage("TEMPLATE_RENDER_ERROR", "<!-- Error rendering template -->"),
        frame
    )
end

return p