char-fio.lua /size: 3314 b    last modification: 2021-10-28 13:50
1if not modules then modules = { } end modules ['char-fio'] = {
2    version   = 1.001,
3    comment   = "companion to char-ini.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
9-- % directives="filters.utf.reorder=false"
10
11
12local next = next
13
14-- --
15
16local sequencers      = utilities.sequencers
17local appendaction    = sequencers.appendaction
18local enableaction    = sequencers.enableaction
19local disableaction   = sequencers.disableaction
20
21local utffilters      = characters.filters.utf
22
23local textfileactions = resolvers.openers.helpers.textfileactions
24local textlineactions = resolvers.openers.helpers.textlineactions
25
26appendaction (textfileactions,"system","characters.filters.utf.reorder")
27disableaction(textfileactions,         "characters.filters.utf.reorder")
28
29appendaction (textlineactions,"system","characters.filters.utf.reorder")
30disableaction(textlineactions,         "characters.filters.utf.reorder")
31
32appendaction (textfileactions,"system","characters.filters.utf.collapse")   -- not per line
33disableaction(textfileactions,         "characters.filters.utf.collapse")
34
35appendaction (textfileactions,"system","characters.filters.utf.decompose")  -- not per line
36disableaction(textfileactions,         "characters.filters.utf.decompose")
37
38local report    = logs.reporter("unicode filter")
39local reporting = "no"
40
41-- This is messy as for performance reasons i don't want this to happen per line by
42-- default.
43
44local enforced = {
45    ["characters.filters.utf.collapse"]  = true,
46    ["characters.filters.utf.decompose"] = true,
47    ["characters.filters.utf.reorder"]   = false,
48}
49
50function utffilters.enable()
51    -- only used one time (normally)
52    for k, v in next, enforced do
53        if v then
54            if reporting == "yes" then
55                report("%a enabled",k)
56            end
57            enableaction(textfileactions,k)
58        else
59            if reporting == "yes" then
60                report("%a not enabled",k)
61            end
62        end
63    end
64    reporting = "never"
65end
66
67local function configure(what,v)
68    if v == "" then
69        report("%a unset",what)
70    elseif v == "line" then
71        disableaction(textfileactions,what)
72        enableaction (textlineactions,what)
73    elseif not toboolean(v) then
74        if reporting ~= "never" then
75            report("%a disabled",what)
76            reporting = "yes"
77        end
78        enforced[what] = false
79        disableaction(textfileactions,what)
80        disableaction(textlineactions,what)
81    else -- true or text
82        enableaction (textfileactions,what)
83        disableaction(textlineactions,what)
84    end
85end
86
87-- first line:
88--
89-- % directives="filters.utf.collapse=true"
90
91directives.register("filters.utf.reorder",   function(v) configure("characters.filters.utf.reorder",  v) end)
92directives.register("filters.utf.collapse",  function(v) configure("characters.filters.utf.collapse", v) end)
93directives.register("filters.utf.decompose", function(v) configure("characters.filters.utf.decompose",v) end)
94
95utffilters.setskippable {
96    "mkiv", "mkvi",
97    "mkix", "mkxi",
98    "mkxl", "mklx",
99}
100
101interfaces.implement {
102    name     = "enableutf",
103    onlyonce = true,
104    actions  = utffilters.enable
105}
106