summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/chem-ini.lua
blob: 06049807a69f4b59024438d68ed79d4a17072b30 (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
if not modules then modules = { } end modules ['chem-ini'] = {
    version   = 1.001,
    comment   = "companion to chem-ini.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local format = string.format
local lpegmatch, patterns = lpeg.match, lpeg.patterns

local trace_molecules = false  trackers.register("chemistry.molecules",  function(v) trace_molecules = v end)

local report_chemistry = logs.reporter("chemistry")

local context   = context
local cpatterns = patterns.context

chemistry       = chemistry or { }
local chemistry = chemistry

-- The next code started out as adaptation of code from Wolfgang Schuster as posted
-- on the mailing list. The current version supports nested braces and unbraced
-- integers as scripts.

local moleculeparser     = cpatterns.scripted
chemistry.moleculeparser = moleculeparser

function chemistry.molecule(str)
    return lpegmatch(moleculeparser,str)
end

interfaces.implement {
    name      = "molecule",
    arguments = "string",
    actions   = function(str)
        if trace_molecules then
            local rep = lpegmatch(moleculeparser,str)
            report_chemistry("molecule %a becomes %a",str,rep)
            context(rep)
        else
            context(lpegmatch(moleculeparser,str))
        end
    end,
}

-- interfaces.implement {
--     name      = "molecule",
--     scope     = "private",
--     action    = function()
--         local str = scanstring()
--         if trace_molecules then
--             local rep = lpegmatch(moleculeparser,str)
--             report_chemistry("molecule %a becomes %a",str,rep)
--             context(rep)
--         else
--             context(lpegmatch(moleculeparser,str))
--         end
--     end,
-- }