summaryrefslogtreecommitdiff
path: root/tex/context/base/meta-tex.lua
blob: 1008e45c05edb094afb27d09ab58d60e9743ed54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
if not modules then modules = { } end modules ['meta-tex'] = {
    version   = 1.001,
    comment   = "companion to meta-tex.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local tostring = tostring
local format, gsub, find, match = string.format, string.gsub, string.find, string.match
local formatters = string.formatters
local P, S, R, C, Cs, lpegmatch = lpeg.P, lpeg.S, lpeg.R, lpeg.C, lpeg.Cs, lpeg.match

metapost = metapost or { }

local implement = interfaces.implement

-- local left     = P("[")
-- local right    = P("]")
-- local space    = P(" ")
-- local argument = left * C((1-right)^1) * right
-- local pattern  = (argument + space)^0

-- function metapost.sometxt(optional,str)
--     if optional == "" then
--         context.sometxta(str)
--     else
--         local one, two = lpegmatch(pattern,optional)
--         if two then
--             context.sometxtc(one,two,str)
--         elseif one then
--             context.sometxtb(one,str)
--         else
--             context.sometxta(str)
--         end
--     end
-- end

local pattern = Cs((P([[\"]]) + P([["]])/"\\quotedbl{}" + P(1))^0) -- or \char

function metapost.escaped(str)
    context(lpegmatch(pattern,str))
end

implement {
    name      = "metapostescaped",
    actions   = metapost.escaped,
    arguments = "string"
}

local simplify = true

-- local function strip(n,e)
--     -- get rid of e(0)
--     -- get rid of e(+*)
--     e = gsub(e,"^+","")
--     -- remove leading zeros
--     e = gsub(e,"^([+-]*)0+(%d)","%1%2")
--     if not simplify then
--         -- take it as it is
--     elseif n == "1" then
--         return format("10^{%s}",e)
--     end
--     return format("%s\\times10^{%s}",n,e)
-- end
--
-- function metapost.format_n(fmt,...)
--     fmt = gsub(fmt,"@","%%")
--     local initial, hasformat, final = match(fmt,"^(.-)(%%.-[%a])(.-)$")
--     if hasformat then
--         str = format(fmt,...)
--         str = gsub(str,"(.-)e(.-)$",strip)
--         str = format("%s\\mathematics{%s}%s",initial,str,final)
--     elseif not find(fmt,"%%") then
--         str = format("%"..fmt,...)
--         str = gsub(str,"(.-)e(.-)$",strip)
--         str = format("\\mathematics{%s}",str)
--     end
--     context(str)
-- end

-- todo: proper lpeg

-- local function strip(n,e)
--     -- get rid of e(0)
--     -- get rid of e(+*)
--     e = gsub(e,"^+","")
--     -- remove leading zeros
--     e = gsub(e,"^([+-]*)0+(%d)","%1%2")
--     if not simplify then
--         -- take it as it is
--     elseif n == "1" then
--         return format("\\mathematics{10^{%s}}",e)
--     end
--     return format("\\mathematics{%s\\times10^{%s}}",n,e)
-- end
--
-- function metapost.format_n(fmt,...)
--     fmt = gsub(fmt,"@","%%")
--     if find(fmt,"%%") then
--         str = format(fmt,...)
--     else -- yes or no
--         str = format("%"..fmt,...)
--     end
--     str = gsub(str,"([%-%+]-[%.%d]+)e([%-%+]-[%.%d]+)",strip)
--     context(str)
-- end
--
-- function metapost.format_v(fmt,str)
--     metapost.format_n(fmt,metapost.untagvariable(str,false))
-- end

-- -- --

local number  = C((S("+-")^0 * R("09","..")^1))
local enumber = number * S("eE") * number

local cleaner = Cs((P("@@")/"@" + P("@")/"%%" + P(1))^0)

context = context or { exponent = function(...) print(...) end }

function metapost.format_string(fmt,...)
    context(lpegmatch(cleaner,fmt),...)
end

function metapost.format_number(fmt,num)
    local number = tonumber(num)
    if number then
        local base, exponent = lpegmatch(enumber,formatters[lpegmatch(cleaner,fmt)](number))
        if base and exponent then
            context.MPexponent(base,exponent)
        else
            context(number)
        end
    else
        context(tostring(num))
    end
end

-- This is experimental and will change!

function metapost.svformat(fmt,str)
    metapost.format_string(fmt,metapost.untagvariable(str,false))
end

function metapost.nvformat(fmt,str)
    metapost.format_number(fmt,metapost.untagvariable(str,false))
end

implement { name =  "metapostformatted",   actions = metapost.svformat, arguments = { "string", "string" } }
implement { name =  "metapostgraphformat", actions = metapost.nvformat, arguments = { "string", "string" } }

-- local function test(fmt,n)
--     logs.report("mp format test","fmt: %s, n: %s, result: %s, \\exponent{%s}{%s}",fmt,n,
--         formatters[lpegmatch(cleaner,fmt)](n),
--         lpegmatch(enumber,formatters[lpegmatch(cleaner,fmt)](n))
--     )
-- end
--
-- test("@j","1e-8")
-- test("@j",1e-8)
-- test("@j","1e+8")
-- test("@j","1e-10")
-- test("@j",1e-10)
-- test("@j","1e+10")
-- test("@j","1e-12")
-- test("@j","1e+12")
-- test("@j","1e-0")
-- test("@j","1e+0")
-- test("@j","1")
-- test("@j test","1")
-- test("@j","-1")
-- test("@j","1e-102")
-- test("@1.4j","1e+102")
-- test("@j","1.2e+102")
-- test("@j","1.23e+102")
-- test("@j","1.234e+102")

local f_textext = formatters[ [[textext("%s")]] ]
local f_mthtext = formatters[ [[textext("\mathematics{%s}")]] ]
local f_exptext = formatters[ [[textext("\mathematics{%s\times10^{%s}}")]] ]

local mpprint   = mp.print

function mp.format(fmt,str)
    fmt = lpegmatch(cleaner,fmt)
    mpprint(f_textext(formatters[fmt](metapost.untagvariable(str,false))))
end

function mp.formatted(fmt,num) -- svformat
    fmt = lpegmatch(cleaner,fmt)
    mpprint(f_textext(formatters[fmt](tonumber(num) or num)))
end

function mp.graphformat(fmt,num) -- nvformat
    fmt = lpegmatch(cleaner,fmt)
    local number = tonumber(num)
    if number then
        local base, exponent = lpegmatch(enumber,number)
        if base and exponent then
            mpprint(f_exptext(base,exponent))
        else
            mpprint(f_mthtext(num))
        end
    else
        mpprint(f_textext(tostring(num)))
    end
end