Skip to content

Commit 63083ad

Browse files
Omikhleiaalerque
authored andcommitted
feat(packages): Support @string syntax in bibTeX bibliography
Support `@string` blocks in a bibtex file, and the use of the thus defined strings in other entry fields. Supports concatenation with the "#" operator. As part of the syntax clean-up, all content between entries is now skipped (it doesn't need to start with a percent sign, everything that is not an entry is a comment. See #2051
1 parent 742a0c4 commit 63083ad

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

packages/bibtex/init.lua

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ local Bibliography
1111
-- stylua: ignore start
1212
---@diagnostic disable: undefined-global, unused-local, lowercase-global
1313
local bibtexparser = epnf.define(function (_ENV)
14+
local strings = {} -- Local store for @string entries
15+
1416
local identifier = (SILE.parserBits.identifier + S":-")^1
1517
local balanced = C{ "{" * P" "^0 * C(((1 - S"{}") + V(1))^0) * "}" } / function (...) local t={...}; return t[2] end
16-
local doubleq = C( P'"' * C(((1 - S'"\r\n\f\\') + (P'\\' * 1)) ^ 0) * '"' )
18+
local quoted = C( P'"' * C(((1 - S'"\r\n\f\\') + (P'\\' * 1)) ^ 0) * '"' ) / function (...) local t={...}; return t[2] end
1719
local _ = WS^0
1820
local sep = S",;" * _
19-
local myID = C(identifier + P(1)) / function (t) return t end
21+
local myID = C(identifier + P(1)) / function (t) return strings[t] or t end
2022
local myTag = C(identifier + P(1)) / function (t) return t:lower() end
21-
local value = balanced + doubleq + myID
23+
local pieces = balanced + quoted + myID
24+
local value = Ct(pieces * (WS * P("#") * WS * pieces)^0) / function (t) return table.concat(t) end
2225
local pair = Cg(myTag * _ * "=" * _ * C(value)) * _ * sep^-1 / function (...) local t= {...}; return t[1], t[#t] end
2326
local list = Cf(Ct("") * pair^0, rawset)
2427
local skippedType = Cmt(R("az", "AZ")^1, function(_, _, tag)
@@ -28,11 +31,16 @@ local bibtexparser = epnf.define(function (_ENV)
2831
end)
2932

3033
START "document"
31-
document = (V"skipped" + V"entry")^1 -- order important: skipped (@comment, @preamble) must be first
34+
document = (V"skipped" -- order important: skipped (@comment, @preamble) must be first
35+
+ V"stringblock" -- order important: @string must be before @entry
36+
+ V"entry")^1
3237
* (-1 + E("Unexpected character at end of input"))
33-
skipped = WS +
34-
( V"blockskipped" + (P"%" * (1-S"\r\n")^0 * S"\r\n") / "")
38+
skipped = WS + (V"blockskipped" + (1 - P"@")^1 ) / ""
3539
blockskipped = (P("@") * skippedType) + balanced / ""
40+
stringblock = Ct( P("@string") * _ * P("{") * pair * _ * P("}") * _ )
41+
/ function (t)
42+
strings[t[1]] = t[2]
43+
return t end
3644
entry = Ct( P("@") * Cg(myTag, "type") * _ * P("{") * _ * Cg(myID, "label") * _ * sep * list * P("}") * _ )
3745
end)
3846
-- luacheck: pop

0 commit comments

Comments
 (0)