summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/texdoc/alias.tlu
blob: 1ae150b5b1cf6e8791bf60c72954dc2a1c70555a (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
-- alias.tlu: configuration handling for texdoc
--
-- Manuel Pégourié-Gonnard, GPLv3, see texdoclib.tlu for details

--[[ structure of the alias table

alias = {
  name1 = { <true or nill> stop, <aliasentry> aliasentry1, ... },
  ...
}
stop == true means further alias directives should be ignored

aliasentry = {
  name      = <string> pattern to be matched,
  score     = <number or nil> associated score,
  original  = <true or nil> is this the original keyword?,
  locale    = <true or nil> is this entry found via config.lang?
}
score == nil means to use the default score (defined in score.tlu)

--]]

-- alias is local to this file
local alias = {}

-- turn a name into a suitable alias entry
-- if score is 'false', this is the original name
function make_alias(pat, score)
    local al = {}
    al.name = pat
    if score == false then
        al.original = true
    else
        al.score = score -- may be nil
    end
    return al
end

-- add an alias value for a key
function add_alias(key, value, score)
    local k = string.lower(key)
    alias[k] = alias[k] or { make_alias(key, false) }
    if alias[k].stop then return end
    table.insert(alias[k], make_alias(value, score))
end

-- prevent a key from being further aliased
function stop_alias(key)
    local k = string.lower(key)
    alias[k] = alias[k] or {}
    alias[k].stop = true
end

-- get patterns for a name
function get_patterns(name, no_alias)
    local n = string.lower(name)
    -- get normal aliases
    local res
    if alias[n] and not no_alias then
        res = alias[n]
    else
        res = { make_alias(name, false) }
    end
    -- check for language-specific aliases
    local lang = config.lang and alias[n .. '-' .. config.lang]
    if lang then
        for _, entry in ipairs(lang) do
            if not entry.original then
                table.insert(res, {
                    name    = entry.name,
                    score   = entry.score,
                    locale  = true,
                })
            end
        end
    end
    return res
end

-- interpret a confline as an alias setting or return false
function confline_to_alias(line, file, pos)
    -- alias directive without score
    local key, val = string.match(line, '^alias%s+([%w%p]+)%s*=%s*(.+)')
    if key and val then
        add_alias(key, val)
        return true
    end
    -- alias directive with score
    local score, key, val = string.match(line,
        '^alias%(([%d+-.]+)%)%s+([%w%p]+)%s*=%s*(.+)')
    if score then score = tonumber(score) end
    if key and val and score then
        add_alias(key, val, score)
        return true
    end
    -- stopalias directive
    local key = string.match(line, '^stopalias%s+(.+)')
    if key then 
        stop_alias(key)
        return true
    end
    return false
end

-- iterator over the list of keys in the alias table
function aliased_names()
    return function(_, cur)
        return (next(alias, cur))
    end
end

return {
    confline_to_alias = confline_to_alias,
    get_patterns      = get_patterns,
    aliased_names     = aliased_names,
}