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)
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
46 context("black")
47 else
48 local typ, var = lpegmatch(splitter,v)
49 if not var then
50
51 if colorhash[v] then
52
53 context("%q",var)
54 elseif tonumber(v) then
55 context(v)
56 elseif not lpegmatch(dimenorname,v) then
57 context("\\number %s",v)
58 end
59 elseif typ == "d" then
60
61 context("\\the\\dimexpr %s\\relax",var)
62 elseif typ == "n" then
63
64 context("\\the\\numexpr %s\\relax",var)
65 elseif typ == "s" then
66
67
68 context("%q",var)
69 elseif typ == "c" then
70
71
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
87
88
89
90
91
92
93
94
95
96
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
101 return "\\times10^{" .. (tonumber(s) or s) .."}"
102 end) + 1)^1)
103
104
105
106 function metapost.formatnumber(fmt,n)
107 ctx_mathematics(lpegmatch(two,format(lpegmatch(one,fmt),n)))
108 end
109
110end
111
112do
113
114
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
127 end
128 end
129
130 function mp.mf_finish_saving_data()
131 if data then
132
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 |