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
....
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
....
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
|
self:fail('template instantiation requires at least %u arguments (in ID “%s”)',n,id)
end
return arg
else return sp end
end)
end
if not id:find'%.' then
local rid = self.sec.refs[id]
if rid then
return rid, id, self.sec
end
--nothing in the current section, but this ID could be looked up in the context of a macro expansion. if so, check section of the site of invocation as well
if self.invocation then
rid = self.invocation.origin:ref(id)
if rid then
return rid, id, self.invocation.origin.sec
end
end
self:fail("no such ref %s in current section", id or '')
else
local sec, ref = string.match(id, "(.-)%.(.+)")
local s
if sec == '' then
if self.invocation == nil then
self:fail('site-of-invocation IDs can only be dereferenced in a macro expansion (offending ID: “%s”)', id)
end
s = self.invocation.origin.sec
end
s = s or self.doc.sections[sec]
if not s then -- fall back on inheritance tree
for i, p in ipairs(self.doc.parents) do
if p.sections[sec] then
s = p.sections[sec]
break
end
end
end
if s then
if s.refs[ref] then
return s.refs[ref], ref, sec
else self:fail("no such ref %s in section %s", ref, sec) end
else self:fail("no such section %s", sec) end
end
end
};
}
ct.sec = declare {
ident = 'section';
mk = function() return {
................................................................................
origin = c:clone();
}
table.insert(c.sec.blocks, tbl)
j:hook('block_table_insert', c, tbl, l)
j:hook('block_table_row_insert', c, tbl, tbl.rows[1], l)
end
end
ct.ctlseqs = {
{seq = '.', fn = insert_paragraph};
{seq = '¶', fn = insert_paragraph};
{seq = '❡', fn = insert_paragraph};
{seq = '#', fn = insert_section};
{seq = '§', fn = insert_section};
................................................................................
})[c] ~= true then return false end
end
return true
end
end; fn = blockwrap(function()
return { kind = 'horiz-rule' }
end)};
{seq='@', fn=function(s,c,j,d)
local function mirror(b)
local ch = {}
local rev = {
['['] = ']'; [']'] = '[';
['{'] = '}'; ['}'] = '{';
['('] = ')'; [')'] = '(';
|
>
>
|
|
|
|
|
<
<
|
|
<
<
<
<
|
|
|
>
>
>
>
|
|
<
|
<
|
<
<
>
>
|
>
>
|
<
<
|
>
>
>
>
|
|
<
>
>
>
>
>
>
>
>
>
>
>
|
|
<
<
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
....
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
....
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
|
self:fail('template instantiation requires at least %u arguments (in ID “%s”)',n,id)
end
return arg
else return sp end
end)
end
local function checkFromSec(sec,doc)
if sec and not id:find'%.' then
local rid = sec.refs[id]
if rid then
return rid, id, sec
end
if doc.sections[rid] then
return nil, id, doc.sections[rid]
end
else
local secid, ref = string.match(id, "(.-)%.(.+)")
local s
s = s or doc.sections[secid]
if s then
if s.refs[ref] then
return s.refs[ref], ref, s
end
end
end
end
local function scanParents(doc)
for i, p in ipairs(doc.parents) do
-- TODO figure out a way to ref the embedding section
local o,i,s = checkFromSec(nil, p)
if o or s then return o,i,s end
end
-- breadth-first search
for i, p in ipairs(doc.parents) do
local o,i,s = scanParents(p)
if o or s then return o,i,s end
end
end
local o,i,s = checkFromSec(self.sec, self.doc)
if o or s then return o,i,s end
--nothing in the current section, but this ID could be looked up in the context of a macro expansion. if so, check section of the site of invocation as well
if self.invocation then
local dp = id:find'%.'
if dp == 1 then
local s = self.invocation.origin.sec
local ref = id:sub(2)
if s and s.refs[ref] then
return s.refs[ref], ref, s
end
elseif not dp then
rid = self.invocation.origin:ref(id)
if rid then
return rid, id, self.invocation.origin.sec
end
end
end
o,i,s = scanParents(doc)
if o or s then return o,i,s end
self:fail("ID “%s” does not name an object or section", id)
end
};
}
ct.sec = declare {
ident = 'section';
mk = function() return {
................................................................................
origin = c:clone();
}
table.insert(c.sec.blocks, tbl)
j:hook('block_table_insert', c, tbl, l)
j:hook('block_table_row_insert', c, tbl, tbl.rows[1], l)
end
end
local function insert_link_block(seq)
return blockwrap(function(s,c)
local r = s:sub(#seq):gsub('^%s+','') -- chomp
local uri, txt = r:match('^([^%s]*)%s*(.*)$')
return {
uri = ss.uri(uri);
label = ct.parse_span(txt, c);
}
end)
end
ct.ctlseqs = {
{seq = '.', fn = insert_paragraph};
{seq = '¶', fn = insert_paragraph};
{seq = '❡', fn = insert_paragraph};
{seq = '#', fn = insert_section};
{seq = '§', fn = insert_section};
................................................................................
})[c] ~= true then return false end
end
return true
end
end; fn = blockwrap(function()
return { kind = 'horiz-rule' }
end)};
{seq='=>', fn = insert_link_block '=>'};
{seq='⇒', fn = insert_link_block '⇒'};
{seq='@', fn=function(s,c,j,d)
local function mirror(b)
local ch = {}
local rev = {
['['] = ']'; [']'] = '[';
['{'] = '}'; ['}'] = '{';
['('] = ')'; [')'] = '(';
|