1if 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
10
11
12local tonumber , next = tonumber , next
13local utfbyte , utfchar , utfsplit = utf . byte , utf . char , utf . split
14local match , gsub = string . match , string . gsub
15local nspaces = string . nspaces
16local concat = table . concat
17local xmltext = xml . text
18local undent = buffers . undent
19
20local f_entity = string . formatters [ " &x%X; " ]
21local f_blob = string . formatters [ ' <?xml version="2.0"?>\n\n<!-- formula %i -->\n\n%s ' ]
22
23local all = nil
24local back = nil
25
26local 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
38end
39
40local 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
53end
54
55local 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 " " )
79end
80
81interfaces . implement {
82 name = " xmlformulatobuffer " ,
83 arguments = { " integer " , " string " } ,
84 actions = function ( n , target )
85 buffers . assign ( target , getblob ( n ) )
86 end
87}
88 |