s-languages-sorting.lua /size: 3431 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['s-languages-system'] = {
2    version   = 1.001,
3    comment   = "companion to s-languages-system.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
9moduledata.languages         = moduledata.languages         or { }
10moduledata.languages.sorting = moduledata.languages.sorting or { }
11
12local formatters = string.formatters
13local utfbyte, utfcharacters = utf.byte, utf.characters
14local sortedpairs = table.sortedpairs
15
16local definitions       = sorters.definitions
17local constants         = sorters.constants
18local replacementoffset = constants.replacementoffset
19
20local currentfont       = font.current
21local fontchars         = fonts.hashes.characters
22
23local c_darkblue        = { "darkblue" }
24local c_darkred         = { "darkred" }
25local f_chr             = formatters["\\tttf%H"]
26
27local function chr(str,done)
28    if done then
29        context.space()
30    end
31    local c = fontchars[currentfont()]
32    for s in utfcharacters(str) do
33        local u = utfbyte(s)
34        if c[u] then
35            context(s)
36        elseif u > replacementoffset then
37            context.color(c_darkblue, f_chr(u))
38        else
39            context.color(c_darkred, f_chr(u))
40        end
41    end
42    return true
43end
44
45local function map(a,b,done)
46    if done then
47        context.space()
48    end
49 -- context.tttf()
50    chr(a)
51    context("=")
52    chr(b)
53    return true
54end
55
56local function nop()
57 -- context.tttf()
58    context("none")
59end
60
61local function key(data,field)
62    context.NC()
63        context(field)
64        context.NC()
65        context(data[field])
66        context.NC()
67    context.NR()
68end
69
70function moduledata.languages.sorting.showinstalled(tag)
71    if not tag or tag == "" or tag == interfaces.variables.all then
72        for tag, data in sortedpairs(definitions) do
73            moduledata.languages.sorting.showinstalled (tag)
74        end
75    else
76        sorters.update() -- syncs data
77        local data = definitions[tag]
78        if data then
79            context.starttabulate { "|lB|pl|" }
80            key(data,"language")
81            key(data,"parent")
82            key(data,"method")
83            context.NC()
84                context("replacements")
85                context.NC()
86                    local replacements = data.replacements
87                    if #replacements == 0 then
88                        nop()
89                    else
90                        for i=1,#replacements do
91                            local r = replacements[i]
92                            map(r[1],r[2],i > 1)
93                        end
94                   end
95               context.NC()
96            context.NR()
97            context.NC()
98                context("order")
99                context.NC()
100                    local orders = data.orders
101                    for i=1,#orders do
102                        chr(orders[i],i > 1)
103                    end
104                context.NC()
105            context.NR()
106            context.NC()
107                context("entries")
108                context.NC()
109                    local done = false
110                    for k, e in sortedpairs(data.entries) do
111                        done = map(k,e,done)
112                    end
113                context.NC()
114            context.NR()
115            context.stoptabulate()
116        end
117    end
118end
119