summaryrefslogtreecommitdiff
path: root/mod/tex/context/third/rst/rst_directives.lua
blob: 374f942c0b1b2c5e02def0fec88488298c27c6e3 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/usr/bin/env texlua
--------------------------------------------------------------------------------
--         FILE:  rst_directives.lua
--        USAGE:  called by rst_parser.lua
--  DESCRIPTION:  Complement to the reStructuredText parser
--       AUTHOR:  Philipp Gesang (Phg), <phg42.2a@gmail.com>
--      CHANGED:  2013-03-26 22:45:45+0100
--------------------------------------------------------------------------------
--

local helpers = helpers or thirddata and thirddata.rst_helpers

--------------------------------------------------------------------------------
-- Directives for use with |substitutions|
--------------------------------------------------------------------------------

local rst_directives     = { }
thirddata.rst_directives = rst_directives
local rst_context        = thirddata.rst

local lpegmatch      = lpeg.match
local stringformat   = string.format
local stringstrip    = string.strip
local tableconcat    = table.concat
local tableflattened = table.flattened
local type           = type

--rst_directives.anonymous     = 0
rst_directives.images        = {}
rst_directives.images.done   = {}
rst_directives.images.values = {}


rst_directives.images.keys = {
    ["width"]   = "width",
    ["size"]    = "width",
    ["caption"] = "caption",
    ["alt"]     = "caption",
    ["scale"]   = "scale",
}

rst_directives.images.values.scale = function (orig)
    -- http://wiki.contextgarden.net/Reference/en/useexternalfigure
    -- scale=1000 is original size; to get 72%, use scale=720.
    return tonumber(orig) * 1000
end

rst_directives.images.values.width = {
    ["fit"]    = "\\hsize",
    ["hsize"]  = "\\hsize",
    ["broad"]  = "\\hsize",
    ["normal"] = "local",
    ["normal"] = "local",
}

-- we won't allow passing arbitrary setups to context
local permitted_setups = {
    "width",
    "scale"
}

local function img_setup (properties)
    local result = ""
    for _, prop in next, permitted_setups do
        if properties[prop] then
            result = result .. prop .. "=" .. properties[prop] .. ","
        end
    end
    if result ~= "" then
        result = "[" .. result .. "]"
    end
    return result
end

rst_directives.image = function(data)
    local inline_parser = rst_context.inline_parser
    local properties    = {}
    local anon          = false
    local rdi           = rst_directives.images
    local hp            = helpers.patterns

    local name = stringstrip(data.name)

    --rd.anonymous = rd.anonymous + 1
    --anon = true -- indicates a nameless picture
    --name = "anonymous" .. rd.anonymous

    properties.caption = name
    data               = tableflattened(data)

    for i=1, #data do
        local str = data[i]
        local key, val = lpegmatch(hp.colon_keyval, str)
        if key and val then
            key = rdi.keys[key] -- sanitize key expression
            local valtype = type(rdi.values[key])
            if valtype == "table" then
                val = rdi.values[key][val]
            elseif valtype == "function" then
                val = rdi.values[key](val)
            end
            properties[key] = val
        end
    end
    properties.setup = img_setup(properties) or ""
    local img = ""
--    local images_done = rdi.done
--    if not anon then -- TODO: implement?
--        if not images_done[name] then
--            img = img .. stringformat([[
--
--\useexternalfigure[%s][%s][]%%
--]], name, data)
--        images_done[name] = true
--        end
--        img = img .. stringformat([[
--\def\RSTsubstitution%s{%%
--  \placefigure[here]{%s}{\externalfigure[%s]%s}%%
--}
--]], name, rst_context.escape(lpegmatch(inline_parser, properties.caption)), name, properties.setup)
--    else -- image won't be referenced but used instantly
    img = stringformat([[

\placefigure[here]{%s}{\externalfigure[%s]%s}
]],     rst_context.escape(lpegmatch(inline_parser, properties.caption)),
        name,
        properties.setup)
--    end
    return img
end

rst_directives.caution = function(data)
    local inline_parser = rst_context.inline_parser
    rst_context.addsetups("dbend")
    rst_context.addsetups("caution")
    local text = { }
    for i=1, #data do -- paragraphs
        local current = tableconcat(data[i], "\n")
        current = lpegmatch(inline_parser, current)
        current = rst_context.escape(current)
        text[i] = current
    end
    return stringformat([[
\startRSTcaution
%s
\stopRSTcaution
]], tableconcat(text, "\n\n"))
end

rst_directives.danger = function(data)
    local inline_parser = rst_context.inline_parser
    rst_context.addsetups("dbend")
    rst_context.addsetups("danger")
    local text = { }
    for i=1, #data do -- paragraphs
        local current = tableconcat(data[i], "\n")
        current = lpegmatch(inline_parser, current)
        current = rst_context.escape(current)
        text[i] = current
    end
    return stringformat([[
\startRSTdanger
%s
\stopRSTdanger
]], tableconcat(text, "\n\n"))
end

-- http://docutils.sourceforge.net/docs/ref/rst/directives.html
rst_directives.DANGER = function(data)
    local inline_parser = rst_context.inline_parser
    local text = { }
    for i=1, #data do -- paragraphs
        local current = tableconcat(data[i], "\n")
        current = lpegmatch(inline_parser, current)
        current = rst_context.escape(current)
        text[i] = current
    end
    return stringformat([[

%% The Rabbit of Caerbannog
\startlinecorrection
\blank[force,big]
\framed[frame=on,
        corner=round,
        rulethickness=5pt,
        align=middle,
        width=\hsize,
        frameoffset=.5em,
        backgroundoffset=1em,
        background=color,
        backgroundcolor=red,
        foreground=color,
        foregroundcolor=black]{%%
  \language[en-gb]\tfb\bf
  Follow only if ye be men of valour, for the entrance to this cave is guarded
  by a creature so foul, so cruel that no man yet has fought with it and lived.
  Bones of full fifty men lie strewn about its lair. So, brave knights, if you
  do doubt your courage or your strength, come no further, for death awaits you
  all with nasty, big, pointy teeth.%%
  \blank[force,big]
  %s%%
}
\blank[force,big]
\stoplinecorrection
]], tableconcat(text, "\n\n"))
end

rst_directives.mp = function(name, data)
    local mpcode = stringformat([[
\startreusableMPgraphic{%s}
%s
\stopreusableMPgraphic
]], name, data)
    mpcode = mpcode .. stringformat([[
\def\RSTsubstitution%s{%%
  \reuseMPgraphic{%s}%%
}
]], name, name)
    return mpcode
end

--- There’s an issue with buffers leaving trailing spaces due to their
--- implementation.
--- http://archive.contextgarden.net/message/20111108.175913.1d994624.en.html
rst_directives.ctx = function(name, data)
    local ctx = stringformat([[

\startbuffer[%s]
%s\stopbuffer
\def\RSTsubstitution%s{%%
  \getbuffer[%s]\removeunwantedspaces%%
}
]], name, data, name, name)
    return ctx
end

rst_directives.lua = function(name, data)
    local luacode = stringformat([[

\startbuffer[%s]
\startluacode
%s
\stopluacode
\stopbuffer
\def\RSTsubstitution%s{%%
  \getbuffer[%s]\removeunwantedspaces%%
}
]], name, data, name, name)
    return luacode
end

--------------------------------------------------------------------------------
--- Experimental math directive
--------------------------------------------------------------------------------

rst_directives.math = function (name, data)
    data = data or name
    local formula
    if type(data) == "table" then
        local last, i = #data, 1
        while i <= last do
            local line = stringstrip(data[i])
            if line and line ~= "" then
                formula = formula and formula .. " " .. line or line
            end
            i = i + 1
        end
    end
    return stringformat([[
\startformula
%s
\stopformula
]], formula)
end

--------------------------------------------------------------------------------
--- End math directive
--------------------------------------------------------------------------------

rst_directives.replace = function(name, data)
    return stringformat([[

\def\RSTsubstitution%s{%s}
]], name, data)
end

--------------------------------------------------------------------------------
--- Containers.
--------------------------------------------------------------------------------

--- *data*:
---     { [1]  -> directive name,
---       [>1] -> paragraphs }

rst_directives.container = function(data)
    local inline_parser = rst_context.inline_parser
    local tmp = { }
    for i=1, #data do -- paragraphs
        local current = tableconcat(data[i], "\n")
        current = lpegmatch(inline_parser, current)
        current = rst_context.escape(current)
        tmp[i] = current
    end
    local content = tableconcat(tmp, "\n\n")
    local name = data.name
    if name and name ~= "" then
        name = stringstrip(data.name)
        return stringformat([[
\start[%s]%%
%s%%
\stop
]], name, content)
    else
        return stringformat([[
\begingroup%%
%s%%
\endgroup
]], content)
    end
end

-- vim:ft=lua:sw=4:ts=4:expandtab