summaryrefslogtreecommitdiff
path: root/macros/luatex/latex/lua-placeholders/scripts/lua-placeholders-namespace.lua
blob: bca680769f8e50a0c255f26ffbac333ed48499d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
-- lua-placeholders-namespace.lua
-- Copyright 2024 E. Nijenhuis
--
-- This work may be distributed and/or modified under the
-- conditions of the LaTeX Project Public License, either version 1.3c
-- of this license or (at your option) any later version.
-- The latest version of this license is in
-- http://www.latex-project.org/lppl.txt
-- and version 1.3c or later is part of all distributions of LaTeX
-- version 2005/12/01 or later.
--
-- This work has the LPPL maintenance status ‘maintained’.
--
-- The Current Maintainer of this work is E. Nijenhuis.
--
-- This work consists of the files lua-placeholders.sty
-- lua-placeholders-manual.pdf lua-placeholders.lua
-- lua-placeholders-common.lua lua-placeholders-namespace.lua
-- lua-placeholders-parser.lua and lua-placeholders-types.lua

require('lua-placeholders-types')

local namespace = {
    strict = false,
    recipe_file = nil,
    recipe_loaded = false,
    payload_file = nil,
    payload_loaded = false
}

function namespace.parse_filename(path)
    local abs_path = kpse.find_file(path)
    local _, _, name = abs_path:find('/?%w*/*(%w+)%.%w+')
    return name, abs_path
end

function namespace:new(_o)
    local o = {
        recipe_file = _o.recipe_file,
        payload_file = _o.payload_file,
        strict = _o.strict,
        values = {}
    }
    setmetatable(o, self)
    self.__index = self
    return o
end

function namespace:load_recipe(params)
    for key, opts in pairs(params) do
        local param = base_param.define(key, opts)
        if param then
            self.values[key] = param
        end
    end
    self.recipe_loaded = true
end

function namespace:load_payload(values)
    if self.recipe_loaded then
        if values then
            for key, value in pairs(values) do
                if self.values[key] then
                    local param = self.values[key]
                    param:load(key, value)
                else
                    texio.write_nl('Warning: passed an unknown key ' .. key)
                end
                texio.write_nl('Info: loaded key ' .. key)
            end
        else
            texio.write_nl('Warning: Payload file was empty')
        end
        self.payload_loaded = true
    end
end

function namespace:param(key)
    if not self.recipe_loaded then
        tex.error('Error: Recipe was not loaded yet...')
        return nil
    end
    if not self.payload_loaded then
        if self.strict then
            tex.error('Error: Payload was not loaded yet...')
            return nil
        else
            texio.write_nl('Warning: Payload was not loaded yet...')
        end
    end
    return self.values[key]
end

return namespace