meta-lua.lua /size: 3676 b    last modification: 2021-10-28 13:50
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-- Don't use this code yet. I use it in some experimental rendering of graphics
10-- based on output from database queries. It's not that pretty but will be
11-- considered when the (similar) lmx code is redone. Also, dropping the print
12-- variant makes it nicer. This experiment is part of playing with several template
13-- mechanisms. (Also see trac-lmx.)
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-- local methods = {
33--     both = Cs { "start",
34--         start    = (V("bluacode") + V("blua") + V("rest"))^0,
35--         blua     = plua,
36--         bluacode = pluacode,
37--         rest     = (1 - V("blua") - V("bluacode"))^1 / appendformatter,
38--     },
39--     xml = Cs { "start",
40--         start    = (V("bluacode") + V("rest"))^0,
41--         bluacode = pluacode,
42--         rest     = (1 - V("bluacode"))^1 / appendformatter,
43--     },
44--     xml = Cs ((pluacode + (1 - pluacode)^1 / appendformatter)^0),
45--     metapost = Cs { "start",
46--         start    = (V("blua") + V("rest"))^0,
47--         blua     = plua,
48--         rest     = (1 - V("blua"))^1 / appendformatter,
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-- Unfortunately mp adds a suffix ... also weird is that successive loading
61-- of the same file gives issues. Maybe some weird buffering goes on (smells
62-- similar to older write / read issues).
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--     local data = metapost.checktexts(data)
93    io.savedata(name,data)
94    return name
95end
96
97