/ [texdoc] / trunk / alias.tlu
To checkout: svn checkout http://svn.gnu.org.ua/sources/texdoc/trunk/alias.tlu
Puszcza

Contents of /trunk/alias.tlu

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (show annotations)
Thu Mar 2 10:41:29 2017 UTC (4 years, 8 months ago) by cereda
File size: 3085 byte(s)
First commit
1 -- alias.tlu: configuration handling for texdoc
2 --
3 -- Manuel Pégourié-Gonnard, GPLv3, see texdoclib.tlu for details
4
5 --[[ structure of the alias table
6
7 alias = {
8 name1 = { <true or nill> stop, <aliasentry> aliasentry1, ... },
9 ...
10 }
11 stop == true means further alias directives should be ignored
12
13 aliasentry = {
14 name = <string> pattern to be matched,
15 score = <number or nil> associated score,
16 original = <true or nil> is this the original keyword?,
17 locale = <true or nil> is this entry found via config.lang?
18 }
19 score == nil means to use the default score (defined in score.tlu)
20
21 --]]
22
23 -- alias is local to this file
24 local alias = {}
25
26 -- turn a name into a suitable alias entry
27 -- if score is 'false', this is the original name
28 function make_alias(pat, score)
29 local al = {}
30 al.name = pat
31 if score == false then
32 al.original = true
33 else
34 al.score = score -- may be nil
35 end
36 return al
37 end
38
39 -- add an alias value for a key
40 function add_alias(key, value, score)
41 local k = string.lower(key)
42 alias[k] = alias[k] or { make_alias(key, false) }
43 if alias[k].stop then return end
44 table.insert(alias[k], make_alias(value, score))
45 end
46
47 -- prevent a key from being further aliased
48 function stop_alias(key)
49 local k = string.lower(key)
50 alias[k] = alias[k] or {}
51 alias[k].stop = true
52 end
53
54 -- get patterns for a name
55 function get_patterns(name, no_alias)
56 local n = string.lower(name)
57 -- get normal aliases
58 local res
59 if alias[n] and not no_alias then
60 res = alias[n]
61 else
62 res = { make_alias(name, false) }
63 end
64 -- check for language-specific aliases
65 local lang = config.lang and alias[n .. '-' .. config.lang]
66 if lang then
67 for _, entry in ipairs(lang) do
68 if not entry.original then
69 table.insert(res, {
70 name = entry.name,
71 score = entry.score,
72 locale = true,
73 })
74 end
75 end
76 end
77 return res
78 end
79
80 -- interpret a confline as an alias setting or return false
81 function confline_to_alias(line, file, pos)
82 -- alias directive without score
83 local key, val = string.match(line, '^alias%s+([%w%p]+)%s*=%s*(.+)')
84 if key and val then
85 add_alias(key, val)
86 return true
87 end
88 -- alias directive with score
89 local score, key, val = string.match(line,
90 '^alias%(([%d+-.]+)%)%s+([%w%p]+)%s*=%s*(.+)')
91 if score then score = tonumber(score) end
92 if key and val and score then
93 add_alias(key, val, score)
94 return true
95 end
96 -- stopalias directive
97 local key = string.match(line, '^stopalias%s+(.+)')
98 if key then
99 stop_alias(key)
100 return true
101 end
102 return false
103 end
104
105 -- iterator over the list of keys in the alias table
106 function aliased_names()
107 return function(_, cur)
108 return (next(alias, cur))
109 end
110 end
111
112 return {
113 confline_to_alias = confline_to_alias,
114 get_patterns = get_patterns,
115 aliased_names = aliased_names,
116 }

Send suggestions and bug reports to Sergey Poznyakoff
ViewVC Help
Powered by ViewVC 1.1.20