summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/lang-lab.lua
blob: e90bee017cf0aac43d251d0eb0dfb2501eaee104 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
if not modules then modules = { } end modules ['lang-lab'] = {
    version   = 1.001,
    comment   = "companion to lang-lab.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local format, find = string.format, string.find
local next, rawget, type = next, rawget, type
local lpegmatch = lpeg.match
local formatters = string.formatters

local prtcatcodes = catcodes.numbers.prtcatcodes -- todo: use different method

local trace_labels  = false  trackers.register("languages.labels", function(v) trace_labels = v end)
local report_labels = logs.reporter("languages","labels")

languages.labels        = languages.labels or { }
local labels            = languages.labels

local context           = context
local implement         = interfaces.implement

local variables         = interfaces.variables
local settings_to_array = utilities.parsers.settings_to_array

local splitter = lpeg.splitat(":")

local function split(tag)
    return lpegmatch(splitter,tag)
end

labels.split = split

local contextsprint      = context.sprint

local f_setlabeltextpair = formatters["\\setlabeltextpair{%s}{%s}{%s}{%s}{%s}"]
local f_key_key          = formatters["\\v!%s:\\v!%s"]
local f_key_raw          = formatters["\\v!%s:%s"]
local f_raw_key          = formatters["%s:\\v!%s"]
local f_raw_raw          = formatters["%s:%s"]
local f_key              = formatters["\\v!%s"]
local f_raw              = formatters["%s"]

local function definelanguagelabels(data,class,tag,rawtag)
    for language, text in next, data.labels do
        if text == "" then
            -- skip
        elseif type(text) == "table" then
            contextsprint(prtcatcodes,f_setlabeltextpair(class,language,tag,text[1],text[2]))
            if trace_labels then
                report_labels("language %a, defining label %a as %a and %a",language,rawtag,text[1],text[2])
            end
        else
            contextsprint(prtcatcodes,f_setlabeltextpair(class,language,tag,text,""))
            if trace_labels then
                report_labels("language %a, defining label %a as %a",language,rawtag,text)
            end
        end
    end
end

function labels.define(class,name,prefixed)
    local list = languages.data.labels[name]
    if list then
        report_labels("defining label set %a",name)
        for tag, data in next, list do
            tag = variables[tag] or tag
            if data.hidden then
                -- skip
            elseif prefixed then
                local first, second = lpegmatch(splitter,tag)
                if second then
                    if rawget(variables,first) then
                        if rawget(variables,second) then
                            definelanguagelabels(data,class,f_key_key(first,second),tag)
                        else
                            definelanguagelabels(data,class,f_key_raw(first,second),tag)
                        end
                     elseif rawget(variables,second) then
                        definelanguagelabels(data,class,f_raw_key(first,second),tag)
                    else
                        definelanguagelabels(data,class,f_raw_raw(first,second),tag)
                    end
                elseif rawget(variables,rawtag) then
                    definelanguagelabels(data,class,f_key(tag),tag)
                else
                    definelanguagelabels(data,class,tag,tag)
                end
            else
                definelanguagelabels(data,class,tag,tag)
            end
        end
    else
        report_labels("unknown label set %a",name)
    end
end

-- function labels.check()
--     for category, list in next, languages.data.labels do
--         for tag, specification in next, list do
--             for language, text in next, specification.labels do
--                 if type(text) == "string" and find(text,",") then
--                     report_labels("warning: label with comma found, category %a, language %a, tag %a, text %a",
--                         category, language, tag, text)
--                 end
--             end
--         end
--     end
-- end
--
-- labels.check()

-- interface

interfaces.implement {
    name      = "definelabels",
    actions   = labels.define,
    arguments = { "string",  "string", "boolean" }
}

-- function commands.setstrippedtextprefix(str)
--     context(string.strip(str))
-- end

-- list       : { "a", "b", "c" }
-- separator  : ", "
-- last       : " and "

-- text       : "a,b,c"
-- separators : "{, },{ and }"

local function concatcommalist(settings) -- it's too easy to forget that this one is there
    local list = settings.list or settings_to_array(settings.text or "")
    local size = #list
    local command = settings.command and context[settings.command] or context
    if size > 1 then
        local separator, last = " ", " "
        if settings.separators then
            local set = settings_to_array(settings.separators)
            separator = set[1] or settings.separator or separator
            last      = set[2] or settings.last      or last
        else
            separator = settings.separator or separator
            last      = settings.last      or last
        end
        command(list[1])
        for i=2,size-1 do
            context(separator)
            command(list[i])
        end
        context(last)
    end
    if size > 0 then
        command(list[size])
    end
end

implement {
    name      = "concatcommalist",
    actions   = concatcommalist,
    arguments = {
        {
            { "text" },
            { "separators" },
            { "first" },
            { "second" },
        }
    }
}