x-chemml.lua /size: 1751 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['x-chemml'] = {
2    version   = 1.001,
3    comment   = "companion to x-chemml.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-- not yet acceptable cld
10
11local format, lower, upper, gsub, sub, match = string.format, string.lower, string.upper, string.gsub, string.sub, string.match
12local concat = table.concat
13
14local chemml      = { }
15local moduledata  = moduledata or { }
16moduledata.chemml = chemml
17
18function chemml.pi(id)
19    local str = xml.content(lxml.id(id))
20    local _, class, key, value = match(str,"^(%S+)%s+(%S+)%s+(%S+)%s+(%S+)%s*$")
21    if key and value then
22        context("\\setupCMLappearance[%s][%s=%s]",class, key, value)
23    end
24end
25
26function chemml.do_graphic(id)
27    local t = { }
28    for r, d, k in xml.elements(lxml.id(id),"cml:graphic") do
29        t[#t+1] = xml.tostring(d[k].dt)
30    end
31    context(concat(t,","))
32end
33
34function chemml.no_graphic(id)
35    local t = { }
36    for r, d, k in xml.elements(lxml.id(id),"cml:text|cml:oxidation|cml:annotation") do
37        local dk = d[k]
38        if dk.tg == "oxidation" then
39            t[#t+1] = format("\\chemicaloxidation{%s}{%s}{%s}",r.at.sign or "",r.at.n or 1,xml.tostring(dk.dt))
40        elseif dk.tg == "annotation" then
41            local location = r.at.location or "r"
42            local caption  = xml.content(xml.first(dk,"cml:caption"))
43            local text     = xml.content(xml.first(dk,"cml:text"))
44            t[#t+1] = format("\\doCMLannotation{%s}{%s}{%s}",location,caption,text)
45        else
46            t[#t+1] = xml.tostring(dk.dt) or ""
47        end
48    end
49    context(concat(t,","))
50end
51
52