cldf-ver.lua /size: 3202 b    last modification: 2023-12-21 09:44
1if not modules then modules = { } end modules ['cldf-ver'] = {
2    version   = 1.001,
3    comment   = "companion to cldf-ver.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-- We have better verbatim: context.verbatim so that needs to be looked
10-- into. We can also directly store in buffers although this variant works
11-- better when used mixed with other code (synchronization issue).
12
13local concat, tohandle = table.concat, table.tohandle
14local splitlines, strip = string.splitlines, string.strip
15local tostring, type = tostring, type
16local assignbuffer = buffers.assign
17
18local context = context
19
20context.tobuffer = assignbuffer -- (name,str,catcodes)
21
22function context.tolines(str,strip)
23    local lines = type(str) == "string" and splitlines(str) or str
24    for i=1,#lines do
25        if strip then
26            context(strip(lines[i]) .. " ")
27        else
28            context(lines[i] .. " ")
29        end
30    end
31end
32
33-- local function flush(...)
34--     context(concat { ..., "\r" }) -- was \n
35-- end
36--
37-- somehow this doesn't work any longer .. i need to figure out why
38--
39-- local function t_tocontext(t)
40--     context.starttyping { "typing" } -- else [1] is intercepted
41--     context.pushcatcodes("verbatim")
42--  -- tohandle(flush,...)
43--     context(table.serialize(t))
44--     context.stoptyping()
45--     context.popcatcodes()
46-- end
47--
48-- local function s_tocontext(first,second,...) -- we need to catch {\}
49--     context.type()
50--     context("{")
51--     context.pushcatcodes("verbatim")
52--     if second then
53--         context(concat({ first, second, ... }, " "))
54--     else
55--         context(first) -- no need to waste a { }
56--     end
57--     context.popcatcodes()
58--     context("}")
59-- end
60
61local t_buffer = { "t_o_c_o_n_t_e_x_t" }
62local t_typing = { "typing" }
63local t_type   = { "type" }
64
65local function flush(s,inline)
66    assignbuffer("t_o_c_o_n_t_e_x_t",s)
67    context[inline and "typeinlinebuffer" or "typebuffer"](t_buffer)
68    context.resetbuffer(t_buffer)
69end
70
71local function t_tocontext(t,s)
72    local s = table.serialize(t,s)
73    context(function() flush(s,false) end)
74end
75
76local function s_tocontext(first,second,...) -- we need to catch {\}
77    local s = second and concat({ first, second, ... }, " ") or first
78    context(function() flush(s,true) end)
79end
80
81local function b_tocontext(b)
82    s_tocontext(tostring(b))
83end
84
85table  .tocontext = t_tocontext
86string .tocontext = s_tocontext
87boolean.tocontext = b_tocontext
88number .tocontext = s_tocontext
89
90local tocontext = {
91    ["string"]   = s_tocontext,
92    ["table"]    = t_tocontext,
93    ["boolean"]  = b_tocontext,
94    ["number"]   = s_tocontext,
95    ["function"] = function() s_tocontext("<function>") end,
96    ["nil"]      = function() s_tocontext("<nil>") end,
97 -- ------------ = -------- can be extended elsewhere
98}
99
100table.setmetatableindex(tocontext,function(t,k)
101    local v = function(s)
102        s_tocontext("<"..tostring(s)..">")
103    end
104    t[k] = v
105    return v
106end)
107
108table.setmetatablecall(tocontext,function(t,k,...)
109    tocontext[type(k)](k)
110end)
111
112context.tocontext = tocontext
113
114