1if not modules then modules = { } end modules ['meta-lua'] = {
2 version = 1.001,
3 comment = "companion to meta-lua.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
12
13
14
15local P, V, Cs, lpegmatch = lpeg.P, lpeg.V, lpeg.Cs, lpeg.match
16local formatters = string.formatters
17local concat = table.concat
18local load, pcall = load, pcall
19
20local errorformatter = formatters[ [[draw textext("\tttf error in template '%s'") ;]] ]
21local concatformatter = formatters[ [[local _t = { } local _n = 0 local p = function(s) _n = _n + 1 _t[_n] = s end %s return table.concat(_t," ")]] ]
22local appendformatter = formatters[ [[_n=_n+1 _t[_n]=%q]] ]
23
24local blua = P("blua ") / " "
25local elua = P(" elua") / " "
26local bluacode = P("<?lua ") / " "
27local eluacode = P(" ?>") / " "
28
29local plua = (blua * (1 - elua)^1 * elua)
30local pluacode = (bluacode * (1 - eluacode)^1 * eluacode)
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52local methods = {
53 both = Cs ((pluacode + plua + (1 - plua - pluacode)^1 / appendformatter)^0),
54 xml = Cs ((pluacode + (1 - pluacode)^1 / appendformatter)^0),
55 metapost = Cs (( plua + (1 - plua )^1 / appendformatter)^0),
56}
57
58methods.mp = methods.metapost
59
60
61
62
63
64mplib.finders.mpstemplate = function(specification,name,mode,ftype)
65 local authority = specification.authority
66 local queries = specification.queries
67 local nameonly = file.nameonly(queries.name or "")
68 local method = file.nameonly(queries.method or "")
69 local pattern = methods[method] or methods.both
70 local data = nil
71 if nameonly == "" then
72 data = errorformatter("no name")
73 elseif authority == "file" then
74 local foundname = resolvers.findfile(nameonly)
75 if foundname ~= "" then
76 data = io.loaddata(foundname)
77 end
78 elseif authority == "buffer" then
79 data = buffers.getcontent(nameonly)
80 end
81 data = data and lpegmatch(pattern,data)
82 data = data and concatformatter(data)
83 data = data and load(data)
84 if data then
85 local okay
86 okay, data = pcall(data)
87 end
88 if not data or data == "" then
89 data = errorformatter(nameonly)
90 end
91 local name = luatex.registertempfile(nameonly,true)
92
93 io.savedata(name,data)
94 return name
95end
96
97 |