summaryrefslogtreecommitdiff
path: root/mod/tex/context/third/rst/rst_setups.lua
blob: 7502621eb489392fdc41e973fb5d2eec6fea5bae (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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/usr/bin/env texlua
--------------------------------------------------------------------------------
--         FILE:  rst_setups.lua
--        USAGE:  called by rst_parser.lua
--  DESCRIPTION:  Complement to the reStructuredText parser
--       AUTHOR:  Philipp Gesang (Phg), <megas.kapaneus@gmail.com>
--      CHANGED:  2011-08-28 13:47:24+0200
--------------------------------------------------------------------------------
--

--local rst_directives  = context and thirddata.rst_directives or require "rst_directives"
local optional_setups = { }
thirddata.rst_setups  = optional_setups
local rst_directives  = thirddata.rst_directives
local rst_context     = thirddata.rst
    --rst_directives = require "rst_directives"

function optional_setups.footnote_symbol ()
    local setup = [[
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Footnotes with symbol conversion                              %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\definenote[symbolnote][footnote]
\setupnote [symbolnote][way=bypage,numberconversion=set 2]
]]
    return setup
end

function optional_setups.footnotes ()
    local tf = state.footnotes
    local fn = [[

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Footnotes                                                     %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
]]
    local buffer = [[

%% %s
\startbuffer[%s]
%s\stopbuffer
]]
    
    for nf, note in next, tf.numbered do
        fn = fn .. string.format(buffer, "Autonumbered footnote", "__footnote_number_"..nf, note)
    end
    for nf, note in next, tf.autolabel do
        fn = fn .. string.format(buffer, "Labeled footnote", "__footnote_label_"..nf, note)
    end
    for nf, note in next, tf.symbol do
        fn = fn .. string.format(buffer, "Symbol footnote", "__footnote_symbol_"..nf, note)
    end
    return fn
end

function optional_setups.references ()
    local refs  = rst_context.collected_references
    local crefs = rst_context.context_references
    local arefs = rst_context.anonymous_set
    
    local function urlescape (str)
        return str:gsub("#", "\\#")
    end

    local function resolve_indirect (r)
        if r and r:match(".*_$") then -- pointing elsewhere
            local look_me_up = r:match("^`?([^`]*)`?_$")
            local result = resolve_indirect (refs[look_me_up])
            if result then
                return result
            else
                if rst_context.structure_references[look_me_up] then
                    -- Internal link, no useURL etc.
                    return false
                end
            end
        end
        return r
    end

    local refsection = [[

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% References                                                    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

]]
    local references = {}
    local ref_keys   = {}
    for ref, target in next, refs do
        ref_keys[#ref_keys+1] = [[__target_]] .. rst_context.whitespace_to_underscore(ref)
        target = resolve_indirect(target)
        if target ~= false then
            ref_text = ref
            if arefs[ref_text] then
                ref_text = rst_context.anonymous_links[tonumber(arefs[ref_text])]
            end
            references[#references+1] = string.format([[
\useURL[__target_%s] [%s] []   [%s] ]], rst_context.whitespace_to_underscore(ref), urlescape(target), ref_text)
        end
    end
    refsection = refsection .. table.concat(references, "\n")
    -- this is needed in order to select the right reference command later
    refsection = refsection .. "\n\n" .. [[\def \RSTexternalreferences{]] .. table.concat(ref_keys, ",") .. [[}

% #1 target name, #2 link text
\def\RSTchoosegoto#1#2{%
  \rawdoifinsetelse{#1}{\RSTexternalreferences}%
    {\from[#1]}%
    {\goto{#2}[#1]}%
}
]]

    return refsection
end

function optional_setups.substitutions ()
    local directives = rst_directives
    local substitutions = [[

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Substitutions                                                 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
]]
    local rs = rst_context.substitutions
    for name, content in next, rs do
        local directive, data = content.directive, content.data
        name, data = name:gsub("%s", ""), string.strip(data)
        if directives[directive] then
            substitutions = substitutions .. directives[directive](name, data)
        else
            err(directive .. " does not exist.")
        end
    end
    return substitutions
end

function optional_setups.directive ()
    --local dirstr = [[

--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
--% Directives                                                    %
--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
--]]
    --return dirstr
    return ""
end

function optional_setups.blockquote ()
    return [[

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Blockquotes                                                   %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\setupdelimitedtext  [blockquote][style={\tfx}] % awful placeholder
\definedelimitedtext[attribution][blockquote]
\setupdelimitedtext [attribution][style={\tfx\it}]
]]
end

function optional_setups.deflist ()
    return [[

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Definitionlist                                                %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\startRSTdefinitionlist{
  \bgroup
  \def      \RSTdeflistterm##1{{\bf ##1}}
  \def\RSTdeflistclassifier##1{\hbox to 1em{\it ##1}}
  \def\RSTdeflistdefinition##1{%
    \startnarrower[left]
    ##1%
    \stopnarrower}
  \def\RSTdeflistparagraph ##1{%
    \startparagraph{%
      \noindentation ##1
    \stopparagraph}
  }
}

\let\stopRSTdefinitionlist\egroup
]]
end

function optional_setups.lines ()
    return [[

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Lines environment (line blocks)                               %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\setuplines[%
  space=on,%
  before={\startlinecorrection\blank[small]},%
  after={\blank[small]\stoplinecorrection},%
]
]]
end

function optional_setups.breaks ()
    return [[

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Fancy transitions                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Get Wolfgang’s module at <https://bitbucket.org/wolfs/fancybreak>.
\usemodule[fancybreak]
\setupfancybreak[symbol=star]
]]
end

function optional_setups.fieldlist ()
    return [[

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Fieldlists                                                    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def\startRSTfieldlist{%
  \bgroup%
  \unexpanded\def\RSTfieldname##1{\bTR\bTC ##1\eTC}
  \unexpanded\def\RSTfieldbody##1{\bTC ##1\eTC\eTR}
%
  \setupTABLE[c][first] [background=color, backgroundcolor=grey, style=\bf]
  \setupTABLE[c][2]     [align=right]
  \setupTABLE[c][each]  [frame=off]
  \setupTABLE[r][each]  [frame=off]
  \bTABLE[split=yes,option=stretch]
  \bTABLEhead
  \bTR
   \bTH  Field       \eTH
   \bTH  Body        \eTH
  \eTR
  \eTABLEhead
  \bTABLEbody
}

\def\stopRSTfieldlist{%
  %\eTABLEbody % doesn't work, temporarily moved to rst_context.field_list()
  \eTABLE
  \egroup%
}
]]
end

function optional_setups.dbend ()
    -- There's just no reason for not providing this.
    optional_setups.dbend_done = true
    return [[
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Dangerous bend                                                %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\loadmapfile [manfnt.map]
\definefontsynonym [bends] [manfnt]

\def\GetSym#1{\getglyph{bends}{\char#1}}

\startsymbolset [Dangerous Bends]
    \definesymbol [dbend]       [\GetSym{127}]
    \definesymbol [lhdbend]     [\GetSym{126}]
    \definesymbol [lhdbend]     [\GetSym{0}]
\stopsymbolset

\setupsymbolset [Dangerous Bends]

]]
end

function optional_setups.caution ()
    local result = ""
    --if not optional_setups.dbend_done then
        --result = result .. optional_setups.dbend()
    --end
    return result .. [[
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Caution directive                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usemodule[lettrine]

\setbox0=\hbox{\symbol[dbend]}
\newskip\RSTbendskip
\RSTbendskip=\wd0
\advance\RSTbendskip by 1em % These two lines should add
\advance\RSTbendskip by 1pt % 13.4pt in mkiv and 13.14983pt in mkii
                            % to make the indent equal to the indent
                            % of the “danger” directive.
                            % (2*(width)dbend + (kern)1pt + 1em

\def\startRSTcaution{%
\startparagraph
\dontleavehmode\lettrine[Lines=2,Raise=.6,Findent=\RSTbendskip,Nindent=0pt]{\symbol[dbend]}{}%
}

\let\stopRSTcaution\stopparagraph

]]

end

function optional_setups.danger ()
    local result = ""
    --if not optional_setups.dbend_done then
        --result = result .. optional_setups.dbend()
    --end
    return result .. [[
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Danger directive                                              %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usemodule[lettrine]

\def\startRSTdanger{%
\startparagraph
\lettrine[Lines=2,Raise=.6,Findent=1em,Nindent=0pt]{\symbol[dbend]\kern 1pt\symbol[dbend]}{}%
}

\let\stopRSTdanger\stopparagraph

]]

end

function optional_setups.citations ()
    local cit = [[
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Citations                                                     %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\setupbibtex[database=\jobname]
]]
    

    return cit
end

function optional_setups.citator ()
    local cit = [[
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Citator Options                                               %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usemodule[citator]
\loadbibdb{\jobname.bib}
\setupcitator[sortmode=authoryear]
\setupcite[mainmode=authoryear]

\startbuffer[bibliography]
\chapter{References}
\setupbodyfont[small]
\bibbykey{shorthand}{all}{author}
\stopbuffer

\prependtoks \getbuffer[bibliography] \to \everystoptext
]]

    return cit
end

return optional_setups