math-inc.lua /size: 2528 b    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
back-inc
'
]
=
{
2
version
=
1
.
001
,
3
comment
=
"
companion to back-exp.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
-- This is an experiment. If it's really useful then I'll make a more efficient
10
-- local export facility.
11 12
local
tonumber
,
next
=
tonumber
,
next
13
local
utfbyte
,
utfchar
,
utfsplit
=
utf
.
byte
,
utf
.
char
,
utf
.
split
14
local
match
,
gsub
=
string
.
match
,
string
.
gsub
15
local
nspaces
=
string
.
nspaces
16
local
concat
=
table
.
concat
17
local
xmltext
=
xml
.
text
18
local
undent
=
buffers
.
undent
19 20
local
f_entity
=
string
.
formatters
[
"
&x%X;
"
]
21
local
f_blob
=
string
.
formatters
[
'
<?xml version="2.0"?>\n\n<!-- formula %i -->\n\n%s
'
]
22 23
local
all
=
nil
24
local
back
=
nil
25 26
local
function
unmath
(
s
)
27
local
t
=
utfsplit
(
s
)
28
for
i
=
1
,
#
t
do
29
local
ti
=
t
[
i
]
30
local
bi
=
utfbyte
(
ti
)
31
if
bi
>
0xFFFF
then
32
local
ch
=
back
[
bi
]
33
t
[
i
]
=
ch
and
utfchar
(
ch
)
or
f_entity
(
bi
)
34
end
35
end
36
s
=
concat
(
t
)
37
return
s
38
end
39 40
local
function
beautify
(
s
)
41
local
b
=
match
(
s
,
"
^( *)<m:math
"
)
42
local
e
=
match
(
s
,
"
( *)</m:math>%s*$
"
)
43
if
b
and
e
then
44
b
=
#
b
45
e
=
#
e
46
if
e
>
b
then
47
s
=
undent
(
nspaces
[
e
-
b
]
.
.
s
)
48
elseif
e
<
b
then
49
s
=
undent
(
(
gsub
(
s
,
"
^( *)
"
,
nspaces
[
b
-
e
]
)
)
)
50
end
51
end
52
return
s
53
end
54 55
local
function
getblob
(
n
)
56
if
all
=
=
nil
then
57
local
name
=
file
.
nameonly
(
tex
.
jobname
)
58
local
full
=
name
.
.
"
-export/
"
.
.
name
.
.
"
-raw.xml
"
59
if
lfs
.
isfile
(
full
)
then
60
all
=
{
}
61
back
=
{
}
62
local
root
=
xml
.
load
(
full
)
63
for
c
in
xml
.
collected
(
root
,
"
formulacontent
"
)
do
64
local
index
=
tonumber
(
c
.
at
.
n
)
65
all
[
index
]
=
f_blob
(
index
,
beautify
(
xmltext
(
c
,
"
math
"
)
or
"
"
)
)
66
end
67
local
it
=
mathematics
.
alphabets
.
regular
.
it
68
for
k
,
v
in
next
,
it
.
digits
do
back
[
v
]
=
k
end
69
for
k
,
v
in
next
,
it
.
ucletters
do
back
[
v
]
=
k
end
70
for
k
,
v
in
next
,
it
.
lcletters
do
back
[
v
]
=
k
end
71
else
72
all
=
false
73
end
74
end
75
if
all
=
=
false
then
76
return
"
"
77
end
78
return
unmath
(
all
[
n
]
or
"
"
)
79
end
80 81
interfaces
.
implement
{
82
name
=
"
xmlformulatobuffer
"
,
83
arguments
=
{
"
integer
"
,
"
string
"
}
,
84
actions
=
function
(
n
,
target
)
85
buffers
.
assign
(
target
,
getblob
(
n
)
)
86
end
87
}
88