Module:T-LibraryInterview: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
 
(28 intermediate revisions by the same user not shown)
Line 1: Line 1:
--[[
--Module:T-LibraryInterview
* T-LibraryInterview.lua
-- Makes use of ICANNWiki's "Template Blueprint Framework" to render the "Internet & Digital Governance Library" template
* Module for rendering the Library Interview template using the Blueprint framework
*
* This module implements the Library Interview template functionality using the
* standardized Blueprint architecture, providing improved maintainability and
* integration with other ICANNWiki modules.
*
]]


local p = {}
local p = {}
Line 14: Line 7:
local Blueprint = require('Module:LuaTemplateBlueprint')
local Blueprint = require('Module:LuaTemplateBlueprint')
local ErrorHandling = require('Module:ErrorHandling')
local ErrorHandling = require('Module:ErrorHandling')
local ConfigRepository = require('Module:ConfigRepository')
-- ========== Module-level caches ==========
-- Cache for wiki link extraction (key: input string, value: extracted name)
local extractCache = {}
-- Cache for current page ID (single value cache)
local currentPageIdCache = nil


-- ========== Helper Functions ==========
-- ========== Helper Functions ==========
 
-- Blueprint default: Create error context for the module
-- Extract page name from wiki link [[Name]] or [[Name|Text]]
-- Optimized with anchored patterns and caching
local function extractFromWikiLink(value)
    -- Early return for nil or empty values
    if not value or value == "" then
        return value
    end
   
    -- Check cache first
    if extractCache[value] then
        return extractCache[value]
    end
   
    -- Optimized pattern matching with anchored patterns where possible
    -- First try exact [[Name]] pattern
    local name = value:match("^%[%[([^%|%]]+)%]%]$")
   
    -- If not found, try [[Name|Text]] pattern
    if not name then
        name = value:match("%[%[([^%|%]]+)%|.-%]%]")
    end
   
    -- If still not found, try unanchored [[Name]] pattern as fallback
    if not name then
        name = value:match("%[%[([^%|%]]+)%]%]")
    end
   
    -- Store result in cache
    local result = name or value
    extractCache[value] = result
   
    return result
end
 
-- Get current page ID with caching (using centralized TemplateHelpers function)
local function getCurrentPageId()
    -- Use the centralized function from TemplateHelpers
    local TemplateHelpers = require('Module:TemplateHelpers')
    return TemplateHelpers.getCurrentPageId()
end
 
-- Create error context for the module
local errorContext = ErrorHandling.createContext("T-LibraryInterview")
local errorContext = ErrorHandling.createContext("T-LibraryInterview")


-- ========== Template Registration ==========
-- ================================================================================


-- Register the template with the Blueprint
-- IMPORTANT! TEMPLATE BLUEPRINT FRAMEWORK INSTRUCTIONS
local template = Blueprint.registerTemplate('LibraryInterview')
-- CONTROL OF TEMPLATE FEATURES: THIS LIST SPECIFIES IN AN EXPLICIT MANNER WHAT FEATURES ARE TO BE CALLED/RENDERED BY THE TEMPLATE. EXTENSIVE TESTING WAS PERFORMED SO THAT THIS CAN BE TOGGLED AT ANY TIME WITH A TRUE/FALSE BOOLEAN CHOICE FROM THIS MODULE, AS IT CAN BE DONE IN ANY OTHER MODULE USING ICANNWIKI'S THE TEMPLATE BLUEPRINT FRAMEWORK
local template = Blueprint.registerTemplate('LibraryInterview', {
    features = {
        title = true,
        fields = true,
        semanticProperties = true,
        categories = true,
        errorReporting = true
    }
})


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


-- Set the table class to "library-box"
-- ================================================================================
template.config.constants = template.config.constants or {}
template.config.constants.tableClass = "library-box"


-- ========== Custom Field Processors ==========
-- TEMPLATE-SPECIFIC CALLS AND CODE
 
-- Add custom field processors
template.config.processors = {
    -- ID processor - always use current page ID, ignoring any user input
    ID = function(value, args, template)
        local TemplateHelpers = require('Module:TemplateHelpers')
        local pageId = TemplateHelpers.getCurrentPageId()
        -- Add the ID to args so it's always available
        args.ID = tostring(pageId or "")
        return tostring(pageId or "")
    end,
   
    -- Date processor - uses date format from central configuration
    Date = function(value, args, template)
        local NormalizationDate = require('Module:NormalizationDate')
        return NormalizationDate.formatDate(value)
    end,
   
    -- Debug processor - shows the current state of the ID field
    Debug = function(value, args, template)
        return "ID field value: " .. tostring(args.ID or "nil")
    end
}
 
-- Add a debug field to help troubleshoot
table.insert(template.config.fields, {key="Debug", label="Debug Info"})


-- ========== Preprocessors ==========
-- ========== Preprocessors ==========
 
-- Basic preprocessors
-- Add preprocessor to ensure ID field is always set
Blueprint.addPreprocessor(template, 'setPageIdField')
Blueprint.addPreprocessor(template, function(template, args)
    -- Set the ID field to the current page ID
    local TemplateHelpers = require('Module:TemplateHelpers')
    local pageId = TemplateHelpers.getCurrentPageId()
    args.ID = tostring(pageId or "")
    return args
end)
 
-- Add preprocessor for semantic property extraction
Blueprint.addPreprocessor(template, function(template, args)
    -- Extract names from wiki links for semantic properties
    if args.Interviewee and args.Interviewee ~= "" then
        -- If the value already has wiki links, extract the name
        if args.Interviewee:match("%[%[.-%]%]") then
            args._semanticInterviewee = ErrorHandling.protect(
                errorContext,
                "extractFromWikiLink_Interviewee",
                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",
                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
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 ==========
 
-- Blueprint default: Render
-- Main render function that delegates to the template's render method
function p.render(frame)
function p.render(frame)
     return ErrorHandling.protect(
     return ErrorHandling.protect(
Line 174: Line 46:
             return template.render(frame)
             return template.render(frame)
         end,
         end,
         "<!-- Error rendering LibraryInterview template -->",
         ErrorHandling.getMessage("TEMPLATE_RENDER_ERROR"),
         frame
         frame
     )
     )