s-languages-goodies.lmt /size: 3961 b    last modification: 2021-10-28 13:51
1if not modules then modules = { } end modules ['s-languages-goodies'] = {
2    version   = 1.001,
3    comment   = "companion to s-languages-goodies.mkxl",
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.goodies = moduledata.languages.goodies or { }
11
12function moduledata.languages.goodies.show(specification)
13    specification = interfaces.checkedspecification(specification)
14    local filename = specification.file
15    if filename and filename ~= "" then
16        local list = table.load(resolvers.findfile(filename))
17        if list then
18            list = list.options
19            if list then
20                for i=1,#list do
21                    local l = list[i]
22                    local w = l.words
23                    if w then
24                        local pre = l.prefixes
25                        local suf = l.suffixes
26                        context.startsubject { title = table.concat(table.sortedkeys(l.patterns)," ") }
27                            context(languages.strippedgoodiewords(w))
28                            if pre then
29                                context.blank()
30                                context.bold("prefixes: ")
31                                context(languages.strippedgoodiewords(pre))
32                            end
33                            if suf then
34                                context.blank()
35                                context.bold("suffixes: ")
36                                context(languages.strippedgoodiewords(suf))
37                            end
38                        context.stopsubject()
39                    end
40                end
41            end
42        end
43    end
44end
45
46local lpegmatch = lpeg.match
47
48moduledata.languages.goodies.ligaturehandlers = { }
49
50function moduledata.languages.goodies.ligatures(specification)
51
52    specification  = interfaces.checkedspecification(specification)
53    local language = specification.language
54    local filename = specification.file
55
56    if not language then
57    elseif moduledata.languages.goodies.ligaturehandlers[language] then
58    else
59        -- fb ff ffb fff ffh ffi ffj ffk ffl fft fi fk fl ft
60        local list    = specification.list or "ff fi fl ffi fff ffl"
61        local hash    = table.tohash(lpeg.split(" ",list)) -- also strip
62        local pattern = (1-lpeg.utfchartabletopattern(hash))^1 * lpeg.P(-1)
63        local checked = { }
64
65        moduledata.languages.goodies.ligaturehandlers[language] = function(original)
66            if not checked[original] and not lpegmatch(pattern,original) then
67                checked[original] = true
68            end
69            return original
70        end
71
72        languages.installhandler(language,"moduledata.languages.goodies.ligaturehandlers." .. language .. "")
73
74        statistics.register(string.formatters["'% t' ligatures checked for language %a"](table.sortedkeys(hash), language), function()
75            return next(checked) and table.concat(table.sortedkeys(checked)," ") or nil
76        end)
77
78        local applied = languages.appliedoptions[language]
79
80        trackers.enable("languages.applied")
81
82        if applied then
83            statistics.register(string.formatters["options applied for language %a"](language), function()
84                return next(applied) and table.concat(table.sortedkeys(applied)," ") or nil
85            end)
86            statistics.register(string.formatters["missed ligatures for language %a"](language), function()
87                for k, v in next, applied do
88                    checked[k] = nil
89                end
90                for k, v in next, hash do
91                    checked[k] = nil
92                end
93                return next(checked) and table.concat(table.sortedkeys(checked)," ") or nil
94            end)
95         end
96
97    end
98
99end
100