summaryrefslogtreecommitdiff
path: root/tex/context/base/buff-vis.lua
blob: 1a97f2591c0b1ba53006661be350d32e6d09f3cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
if not modules then modules = { } end modules ['buff-vis'] = {
    version   = 1.001,
    comment   = "companion to buff-vis.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local type = type
local format = string.format
local C, P, V, patterns, lpegmatch = lpeg.C, lpeg.P, lpeg.V, lpeg.patterns, lpeg.match

visualizers = visualizers or { }

local patterns = { }  visualizers.patterns = patterns

local fallback = context.verbatim

function visualizers.pattern(visualizer,kind,pattern)
    if type(visualizer) == "table" and type(kind) == "string" then
        kind = visualizer[kind] or visualizer.default or fallback
    else
        kind = fallback
    end
    return C(pattern)/kind
end

setmetatable(patterns, {
    __index = function(t,k)
        local v = require(format("v-%s.lua",k)) or false
        context.input(format("v-%s.mkiv",k))
        t[k] = v
        return v
    end
} )

local function visualizestring(method,content)
    if content and content ~= "" then
        lpegmatch(patterns[method],content)
    end
end

visualizers.visualizestring = visualizestring

function visualizers.visualizefile(method,name)
    visualizestring(method,resolvers.loadtexfile(name))
end

function visualizers.visualizebuffer(method,name)
    lpegmatch(method,buffers.content(name))
end

local visualizer = {
    start   = function() context.startSnippet() end,
    stop    = function() context.stopSnippet() end ,
    default = context.verbatim,
}

local patterns = lpeg.patterns
local pattern = visualizers.pattern

local texvisualizer = P { "process",
    process =
        V("start") * V("content") * V("stop"),
    start =
        pattern(visualizer,"start",patterns.beginofstring),
    stop =
        pattern(visualizer,"stop",patterns.endofstring),
    content = (
        pattern(visualizer,"default",patterns.anything)
    )^1
}

return texvisualizer