meta-ini.lua /size: 4458 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['meta-ini'] = {
2    version   = 1.001,
3    comment   = "companion to meta-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
9local tonumber = tonumber
10local format = string.format
11local concat = table.concat
12local lpegmatch, lpegpatterns = lpeg.match, lpeg.patterns
13local P, Cs, R, S, C, Cc = lpeg.P, lpeg.Cs, lpeg.R, lpeg.S, lpeg.C, lpeg.Cc
14
15metapost       = metapost or { }
16local metapost = metapost
17local context  = context
18
19local colorhash = attributes.list[attributes.private('color')]
20local textype   = tex.type
21local MPcolor   = context.MPcolor
22
23do
24
25    local dimenorname  =
26        lpegpatterns.validdimen / function(s)
27            context("\\the\\dimexpr %s",s)
28        end
29      + (C(lpegpatterns.float) + Cc(1)) * lpegpatterns.space^0 * P("\\") * C(lpegpatterns.letter^1) / function(f,s)
30            local t = textype(s)
31            if t == "dimen" then
32                context("\\the\\dimexpr %s\\%s\\relax",f,s)
33            elseif t == "count" then
34                context("\\the\\numexpr \\%s * %s\\relax",s,f) -- <n>\scratchcounter is not permitted
35            end
36        end
37
38    local splitter = lpeg.splitat("::",true)
39
40    interfaces.implement {
41        name      = "prepareMPvariable",
42        arguments = "string",
43        actions   = function(v)
44            if v == "" then
45             -- MPcolor("black")
46                context("black")
47            else
48                local typ, var = lpegmatch(splitter,v)
49                if not var then
50                    -- parse
51                    if colorhash[v] then
52                     -- MPcolor(v)
53                        context("%q",var)
54                    elseif tonumber(v) then
55                        context(v)
56                    elseif not lpegmatch(dimenorname,v) then
57                        context("\\number %s",v) -- 0.4 ...
58                    end
59                elseif typ == "d" then -- to be documented
60                    -- dimension
61                    context("\\the\\dimexpr %s\\relax",var)
62                elseif typ == "n" then -- to be documented
63                    -- number
64                    context("\\the\\numexpr %s\\relax",var)
65                elseif typ == "s" then -- to be documented
66                    -- string
67                 -- context(var)
68                    context("%q",var)
69                elseif typ == "c" then -- to be documented
70                    -- color
71                 -- MPcolor(var)
72                    context("%q",var)
73                else
74                    context(var)
75                end
76            end
77        end
78    }
79
80end
81
82do
83
84    local ctx_mathematics = context.mathematics
85
86    -- function metapost.formatnumber(f,n) -- just lua format
87    --     f = gsub(f,"@(%d)","%%.%1")
88    --     f = gsub(f,"@","%%")
89    --     f = format(f,tonumber(n) or 0)
90    --     f = gsub(f,"e([%+%-%d]+)",function(s)
91    --         return format("\\times10^{%s}",tonumber(s) or s) -- strips leading zeros
92    --     end)
93    --     context.mathematics(f)
94    -- end
95
96    -- formatters["\\times10^{%N}"](s) -- strips leading zeros too
97
98    local one = Cs((P("@")/"%%." * (R("09")^1) + P("@")/"%%" + 1)^0)
99    local two = Cs((P("e")/"" * ((S("+-")^0 * R("09")^1) / function(s)
100     -- return format("\\times10^{%s}",tonumber(s) or s)
101        return "\\times10^{" .. (tonumber(s) or s) .."}"
102    end) + 1)^1)
103
104    -- local two = Cs((P("e")/"" * ((S("+-")^0 * R("09")^1) / formatters["\\times10^{%N}"]) + 1)^1)
105
106    function metapost.formatnumber(fmt,n) -- just lua format
107        ctx_mathematics(lpegmatch(two,format(lpegmatch(one,fmt),n)))
108    end
109
110end
111
112do
113
114    -- this is an old pass-data-to-tex mechanism
115
116    local ctx_printtable = context.printtable
117
118    local data = false
119
120    function mp.mf_start_saving_data(n)
121        data = { }
122    end
123
124    function mp.mf_stop_saving_data()
125        if data then
126            -- nothing
127        end
128    end
129
130    function mp.mf_finish_saving_data()
131        if data then
132            -- nothing
133        end
134    end
135
136    function mp.mf_save_data(str)
137        if data then
138            data[#data+1] = str
139        end
140    end
141
142    interfaces.implement {
143        name    = "getMPdata",
144        actions = function()
145            if data then
146                ctx_printtable(data,"\r")
147            end
148        end
149    }
150
151end
152