Jump to content

Module:PrinterInstructionsLookup

From Swarthmore Knowledge Base
Revision as of 14:37, 14 May 2026 by Aruethe2 (talk | contribs) (Created page with "local p = {} function p.getInstructions(frame) local args = frame:getParent().args local model = args['Printer Model'] or '' local access = args['Access'] or '' local output = {} -- Normalize for case-insensitive matching local modelLower = mw.ustring.lower(model) -- Check if model contains each brand local hasCanon = mw.ustring.find(modelLower, 'canon', 1, true) ~= nil local hasHP = mw.ustring.find(modelLower, 'hp',...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This module handles the conditional inclusion of printing instruction templates on printer pages, based on the printer's model, access permissions, and print release station status. It is invoked by Template:Printer.

Usage

This module is not intended to be called directly. It is invoked from within the Printer template using:

{{#invoke:PrinterInstructionsLookup|getInstructions}}

The module reads its inputs from the parent template's parameters, so it must be invoked from a template that has the relevant parameters defined.

Parameters Read

The module reads the following parameters from the parent template:

Parameter Type Description
Printer Model String The full model name of the printer (e.g., "Canon imageRUNNER 2630", "HP LaserJet 4555", "Dymo LabelWriter 450"). Matched case-insensitively against known brand names.
Access Comma-separated list Who is permitted to use the printer. Recognized values: Employees, Students. Multiple values may be supplied, separated by commas.
Print Release Station Boolean Whether the printer has a print release station. Truthy values: Yes, yes, true, 1, on (case-insensitive). Any other value is treated as false.

Logic

The module evaluates each of the following conditions independently and appends the matching template to the output. A single printer may trigger multiple inclusions.

Condition Template Included
Printer Model contains "Dymo" {{Printing Instructions for Dymo LabelWriter}}
Printer Model contains "Canon" AND Access includes "Employees" {{Printing Instructions for Employees - Canon}}
Printer Model contains "HP" AND Access includes "Employees" {{Printing Instructions for Employees - HP}}
Printer Model contains "Canon" AND Access includes "Students" {{Printing Instructions for Students - Canon}}
Printer Model contains "HP" AND Access includes "Students" {{Printing Instructions for Students - HP}}
Print Release Station is truthy {{Print Release Station Instructions}}

Brand matching is case-insensitive and uses substring matching, so "canon imagerunner" and "Canon imageRUNNER" both match the Canon condition.

The Dymo condition does not depend on the Access value — Dymo instructions are included for any printer with "Dymo" in the model, regardless of who can use it.

Output

The module returns wikitext containing the matched template inclusions, joined by newlines. The output is preprocessed via frame:preprocess() so that template calls are expanded rather than appearing as literal text.

If no conditions match, the module returns an empty string.

Functions

getInstructions(frame)

The single entry point of the module. Reads parameters from the parent frame, evaluates all conditions, and returns the assembled wikitext.

Parameters
frame — The Scribunto frame object. The module calls frame:getParent().args to access the invoking template's parameters.
Returns
A string of wikitext containing zero or more template inclusions.

Adding a New Brand

To add support for an additional printer brand (for example, Brother):

  1. Create the relevant instruction template(s), such as Template:Printing Instructions for Employees - Brother.
  2. Edit Module:PrinterInstructionsLookup and add a detection line near the existing brand checks:
    local hasBrother = mw.ustring.find(modelLower, 'brother', 1, true) ~= nil
    
  3. Add the corresponding conditional blocks alongside the Canon and HP ones:
    if hasBrother and hasEmployees then
        table.insert(output, '{{Printing Instructions for Employees - Brother}}')
    end
    

No changes to the Printer template are required.

Adding a New Access Type

To add a new access type (for example, "Guests"):

  1. Update the Printer template's Access parameter to accept the new value (in the <templatedata> block and any forms).
  2. In the module, add a lookup line near the existing ones:
    local hasGuests = accessSet['Guests'] == true
    
  3. Add the conditional blocks for the brand/access combinations you want to support.
  4. If a new category is needed, add it to the #arraymap / #switch block in the Printer template.

Debugging

To inspect the values the module is receiving, temporarily add a debug return at the end of getInstructions, before the final return frame:preprocess(result):

return 'Model: "' .. model .. '" | Access: "' .. access 
    .. '" | PrintRelease: "' .. printRelease .. '"'
    .. ' | Canon: ' .. tostring(hasCanon)
    .. ' | HP: ' .. tostring(hasHP)
    .. ' | Dymo: ' .. tostring(hasDymo)
    .. ' | Employees: ' .. tostring(hasEmployees)
    .. ' | Students: ' .. tostring(hasStudents)
    .. ' | PR: ' .. tostring(hasPrintRelease)

This will replace the normal output with a diagnostic string showing what the module parsed from the parameters. Remember to remove the debug line once finished.

Dependencies

  • Extension:Scribunto — required to run Lua modules.
  • Template:Printer — the template that invokes this module.
  • The instruction templates referenced above. Missing templates will produce red links in the rendered output.

See also


local p = {}

function p.getInstructions(frame)
    local args = frame:getParent().args
    local model = args['Printer Model'] or ''
    local access = args['Access'] or ''
    
    local output = {}
    
    -- Normalize for case-insensitive matching
    local modelLower = mw.ustring.lower(model)
    
    -- Check if model contains each brand
    local hasCanon = mw.ustring.find(modelLower, 'canon', 1, true) ~= nil
    local hasHP    = mw.ustring.find(modelLower, 'hp', 1, true) ~= nil
    local hasDymo  = mw.ustring.find(modelLower, 'dymo', 1, true) ~= nil
    
    -- Parse the Access list into a set for easy lookup
    local accessSet = {}
    for value in mw.ustring.gmatch(access, '([^,]+)') do
        -- Trim whitespace
        local trimmed = mw.ustring.gsub(value, '^%s*(.-)%s*$', '%1')
        accessSet[trimmed] = true
    end
    
    local hasEmployees = accessSet['Employees'] == true
    local hasStudents  = accessSet['Students'] == true
    
    -- Dymo: include regardless of access
    if hasDymo then
        table.insert(output, '{{Printing Instructions for Dymo LabelWriter}}')
    end
    
    -- Canon + Employees
    if hasCanon and hasEmployees then
        table.insert(output, '{{Printing Instructions for Employees - Canon}}')
    end
    
    -- HP + Employees
    if hasHP and hasEmployees then
        table.insert(output, '{{Printing Instructions for Employees - HP}}')
    end
    
    -- Canon + Students
    if hasCanon and hasStudents then
        table.insert(output, '{{Printing Instructions for Students - Canon}}')
    end
    
    -- HP + Students
    if hasHP and hasStudents then
        table.insert(output, '{{Printing Instructions for Students - HP}}')
    end
    
    -- Join and preprocess so the template calls actually expand
    local result = table.concat(output, '\n')
    return frame:preprocess(result)
end

return p