• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sile-typesetter / sile / 9409557472

07 Jun 2024 12:09AM UTC coverage: 69.448% (-4.5%) from 73.988%
9409557472

push

github

alerque
fix(build): Distribute vendored compat-5.3.c source file

12025 of 17315 relevant lines covered (69.45%)

6023.46 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/packages/bibtex/init.lua
1
local base = require("packages.base")
×
2

3
local package = pl.class(base)
×
4
package._name = "bibtex"
×
5

6
local epnf = require("epnf")
×
7

8
local Bibliography
9

10
-- luacheck: push ignore
11
-- stylua: ignore start
12
---@diagnostic disable: undefined-global, unused-local, lowercase-global
13
local bibtexparser = epnf.define(function (_ENV)
×
14
   local identifier = (SILE.parserBits.identifier + S":-")^1
×
15
   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) * '"' )
×
17
   local _ = WS^0
×
18
   local sep = S",;" * _
×
19
   local myID = C(identifier + P(1)) / function (t) return t end
×
20
   local myTag = C(identifier + P(1)) / function (t) return t:lower() end
×
21
   local value = balanced + doubleq + myID
×
22
   local pair = Cg(myTag * _ * "=" * _ * C(value)) * _ * sep^-1   / function (...) local t= {...}; return t[1], t[#t] end
×
23
   local list = Cf(Ct("") * pair^0, rawset)
×
24
   local commentKey = Cmt(R("az", "AZ")^1, function(_, _, a)
×
25
      return a:lower() == "comment"
×
26
   end)
27

28
   START "document"
29
   document = (V"comment" + V"entry")^1 -- order important: @comment must have precedence over @other
×
30
      * (-1 + E("Unexpected character at end of input"))
×
31
   comment  = WS +
×
32
      ( V"blockcomment" + (P"%" * (1-S"\r\n")^0 * S"\r\n") / function () return "" end) -- Don't bother telling me about comments
×
33
   blockcomment = (P("@") * commentKey) + balanced / function () return "" end -- Don't bother telling me about comments
×
34
   entry = Ct( P("@") * Cg(myTag, "type") * _ * P("{") * _ * Cg(myID, "label") * _ * sep * list * P("}") * _ )
×
35
end)
36
-- luacheck: pop
37
-- stylua: ignore end
38
---@diagnostic enable: undefined-global, unused-local, lowercase-global
39

40
local parseBibtex = function (fn)
41
   fn = SILE.resolveFile(fn) or SU.error("Unable to resolve Bibtex file " .. fn)
×
42
   local fh, e = io.open(fn)
×
43
   if e then
×
44
      SU.error("Error reading bibliography file: " .. e)
×
45
   end
46
   local doc = fh:read("*all")
×
47
   local t = epnf.parsestring(bibtexparser, doc)
×
48
   if not t or not t[1] or t.id ~= "document" then
×
49
      SU.error("Error parsing bibtex")
×
50
   end
51
   local entries = {}
×
52
   for i = 1, #t do
×
53
      if t[i].id == "entry" then
×
54
         local ent = t[i][1]
×
55
         entries[ent.label] = { type = ent.type, attributes = ent[1] }
×
56
      end
57
   end
58
   return entries
×
59
end
60

61
function package:_init ()
×
62
   base._init(self)
×
63
   SILE.scratch.bibtex = { bib = {} }
×
64
   Bibliography = require("packages.bibtex.bibliography")
×
65
end
66

67
function package.declareSettings (_)
×
68
   SILE.settings:declare({
×
69
      parameter = "bibtex.style",
70
      type = "string",
71
      default = "chicago",
72
      help = "BibTeX style",
73
   })
74
end
75

76
function package:registerCommands ()
×
77
   self:registerCommand("loadbibliography", function (options, _)
×
78
      local file = SU.required(options, "file", "loadbibliography")
×
79
      SILE.scratch.bibtex.bib = parseBibtex(file) -- Later we'll do multiple bibliogs, but not now
×
80
   end)
81

82
   self:registerCommand("bibstyle", function (_, _)
×
83
      SU.deprecated("\\bibstyle", "\\set[parameter=bibtex.style]", "0.13.2", "0.14.0")
×
84
   end)
85

86
   self:registerCommand("cite", function (options, content)
×
87
      if not options.key then
×
88
         options.key = SU.ast.contentToString(content)
×
89
      end
90
      local style = SILE.settings:get("bibtex.style")
×
91
      local bibstyle = require("packages.bibtex.styles." .. style)
×
92
      local cite = Bibliography.produceCitation(options, SILE.scratch.bibtex.bib, bibstyle)
×
93
      if cite == Bibliography.Errors.UNKNOWN_REFERENCE then
×
94
         SU.warn("Unknown reference in citation " .. options.key)
×
95
         return
×
96
      end
97
      SILE.processString(("<sile>%s</sile>"):format(cite), "xml")
×
98
   end)
99

100
   self:registerCommand("reference", function (options, content)
×
101
      if not options.key then
×
102
         options.key = SU.ast.contentToString(content)
×
103
      end
104
      local style = SILE.settings:get("bibtex.style")
×
105
      local bibstyle = require("packages.bibtex.styles." .. style)
×
106
      local cite, err = Bibliography.produceReference(options, SILE.scratch.bibtex.bib, bibstyle)
×
107
      if cite == Bibliography.Errors.UNKNOWN_REFERENCE then
×
108
         SU.warn("Unknown reference in citation " .. tostring(options.key))
×
109
         return
×
110
      end
111
      if cite == Bibliography.Errors.UNKNOWN_TYPE then
×
112
         SU.warn("Unknown type @" .. err .. " in citation for reference " .. options.key)
×
113
         return
×
114
      end
115
      SILE.processString(("<sile>%s</sile>"):format(cite), "xml")
×
116
   end)
117
end
118

119
package.documentation = [[
120
\begin{document}
121
BibTeX is a citation management system.
122
It was originally designed for TeX but has since been integrated into a variety of situations.
123

124
This experimental package allows SILE to read and process BibTeX \code{.bib} files and output citations and full text references.
125
(It doesn’t currently produce full bibliography listings.)
126

127
To load a BibTeX file, issue the command \autodoc:command{\loadbibliography[file=<whatever.bib>]}
128

129
To produce an inline citation, call \autodoc:command{\cite{<key>}}, which will typeset something like “Jones 1982”.
130
If you want to cite a particular page number, use \autodoc:command{\cite[page=22]{<key>}}.
131

132
To produce a full reference, use \autodoc:command{\reference{<key>}}.
133

134
Currently, the only supported bibliography style is Chicago referencing, but other styles should be easy to implement.
135
Adapt \code{packages/bibtex/styles/chicago.lua} as necessary.
136
\end{document}
137
]]
×
138

139
return package
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc