lang-lab.lua /size: 5734 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['lang-lab'] = {
2    version   = 1.001,
3    comment   = "companion to lang-lab.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9local format, find = string.format, string.find
10local next, rawget, type = next, rawget, type
11local lpegmatch = lpeg.match
12local formatters = string.formatters
13
14local prtcatcodes = catcodes.numbers.prtcatcodes -- todo: use different method
15
16local trace_labels  = false  trackers.register("languages.labels", function(v) trace_labels = v end)
17local report_labels = logs.reporter("languages","labels")
18
19languages.labels        = languages.labels or { }
20local labels            = languages.labels
21
22local context           = context
23local implement         = interfaces.implement
24
25local variables         = interfaces.variables
26local settings_to_array = utilities.parsers.settings_to_array
27
28local splitter = lpeg.splitat(":")
29
30local function split(tag)
31    return lpegmatch(splitter,tag)
32end
33
34labels.split = split
35
36local contextsprint      = context.sprint
37
38local f_setlabeltextpair = formatters["\\setlabeltextpair{%s}{%s}{%s}{%s}{%s}"]
39local f_key_key          = formatters["\\v!%s:\\v!%s"]
40local f_key_raw          = formatters["\\v!%s:%s"]
41local f_raw_key          = formatters["%s:\\v!%s"]
42local f_raw_raw          = formatters["%s:%s"]
43local f_key              = formatters["\\v!%s"]
44local f_raw              = formatters["%s"]
45
46local function definelanguagelabels(data,class,tag,rawtag)
47    for language, text in next, data.labels do
48        if text == "" then
49            -- skip
50        elseif type(text) == "table" then
51            contextsprint(prtcatcodes,f_setlabeltextpair(class,language,tag,text[1],text[2]))
52            if trace_labels then
53                report_labels("language %a, defining label %a as %a and %a",language,rawtag,text[1],text[2])
54            end
55        else
56            contextsprint(prtcatcodes,f_setlabeltextpair(class,language,tag,text,""))
57            if trace_labels then
58                report_labels("language %a, defining label %a as %a",language,rawtag,text)
59            end
60        end
61    end
62end
63
64function labels.define(class,name,prefixed)
65    local list = languages.data.labels[name]
66    if list then
67        report_labels("defining label set %a",name)
68        for tag, data in next, list do
69            tag = variables[tag] or tag
70            if data.hidden then
71                -- skip
72            elseif prefixed then
73                local first, second = lpegmatch(splitter,tag)
74                if second then
75                    if rawget(variables,first) then
76                        if rawget(variables,second) then
77                            definelanguagelabels(data,class,f_key_key(first,second),tag)
78                        else
79                            definelanguagelabels(data,class,f_key_raw(first,second),tag)
80                        end
81                     elseif rawget(variables,second) then
82                        definelanguagelabels(data,class,f_raw_key(first,second),tag)
83                    else
84                        definelanguagelabels(data,class,f_raw_raw(first,second),tag)
85                    end
86                elseif rawget(variables,rawtag) then
87                    definelanguagelabels(data,class,f_key(tag),tag)
88                else
89                    definelanguagelabels(data,class,tag,tag)
90                end
91            else
92                definelanguagelabels(data,class,tag,tag)
93            end
94        end
95    else
96        report_labels("unknown label set %a",name)
97    end
98end
99
100-- function labels.check()
101--     for category, list in next, languages.data.labels do
102--         for tag, specification in next, list do
103--             for language, text in next, specification.labels do
104--                 if type(text) == "string" and find(text,",") then
105--                     report_labels("warning: label with comma found, category %a, language %a, tag %a, text %a",
106--                         category, language, tag, text)
107--                 end
108--             end
109--         end
110--     end
111-- end
112--
113-- labels.check()
114
115-- interface
116
117interfaces.implement {
118    name      = "definelabels",
119    actions   = labels.define,
120    arguments = { "string",  "string", "boolean" }
121}
122
123-- function commands.setstrippedtextprefix(str)
124--     context(string.strip(str))
125-- end
126
127-- list       : { "a", "b", "c" }
128-- separator  : ", "
129-- last       : " and "
130
131-- text       : "a,b,c"
132-- separators : "{, },{ and }"
133
134local function concatcommalist(settings) -- it's too easy to forget that this one is there
135    local list = settings.list or settings_to_array(settings.text or "")
136    local size = #list
137    local command = settings.command and context[settings.command] or context
138    if size > 1 then
139        local separator, last = " ", " "
140        if settings.separators then
141            local set = settings_to_array(settings.separators)
142            separator = set[1] or settings.separator or separator
143            last      = set[2] or settings.last      or last
144        else
145            separator = settings.separator or separator
146            last      = settings.last      or last
147        end
148        command(list[1])
149        for i=2,size-1 do
150            context(separator)
151            command(list[i])
152        end
153        context(last)
154    end
155    if size > 0 then
156        command(list[size])
157    end
158end
159
160implement {
161    name      = "concatcommalist",
162    actions   = concatcommalist,
163    arguments = {
164        {
165            { "text" },
166            { "separators" },
167            { "separator" },
168            { "last" },
169        }
170    }
171}
172