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

sile-typesetter / sile / 9428435077

08 Jun 2024 11:35AM UTC coverage: 64.56% (-9.9%) from 74.46%
9428435077

push

github

web-flow
Merge pull request #2047 from alerque/end-pars

23 of 46 new or added lines in 5 files covered. (50.0%)

1684 existing lines in 60 files now uncovered.

11145 of 17263 relevant lines covered (64.56%)

4562.45 hits per line

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

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

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

UNCOV
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
UNCOV
13
local bibtexparser = epnf.define(function (_ENV)
×
UNCOV
14
   local identifier = (SILE.parserBits.identifier + S":-")^1
×
UNCOV
15
   local balanced = C{ "{" * P" "^0 * C(((1 - S"{}") + V(1))^0) * "}" } / function (...) local t={...}; return t[2] end
×
UNCOV
16
   local doubleq = C( P'"' * C(((1 - S'"\r\n\f\\') + (P'\\' * 1)) ^ 0) * '"' )
×
UNCOV
17
   local _ = WS^0
×
UNCOV
18
   local sep = S",;" * _
×
UNCOV
19
   local myID = C(identifier + P(1)) / function (t) return t end
×
UNCOV
20
   local myTag = C(identifier + P(1)) / function (t) return t:lower() end
×
UNCOV
21
   local value = balanced + doubleq + myID
×
UNCOV
22
   local pair = Cg(myTag * _ * "=" * _ * C(value)) * _ * sep^-1   / function (...) local t= {...}; return t[1], t[#t] end
×
UNCOV
23
   local list = Cf(Ct("") * pair^0, rawset)
×
UNCOV
24
   local commentKey = Cmt(R("az", "AZ")^1, function(_, _, a)
×
UNCOV
25
      return a:lower() == "comment"
×
26
   end)
27

28
   START "document"
UNCOV
29
   document = (V"comment" + V"entry")^1 -- order important: @comment must have precedence over @other
×
UNCOV
30
      * (-1 + E("Unexpected character at end of input"))
×
UNCOV
31
   comment  = WS +
×
UNCOV
32
      ( V"blockcomment" + (P"%" * (1-S"\r\n")^0 * S"\r\n") / function () return "" end) -- Don't bother telling me about comments
×
UNCOV
33
   blockcomment = (P("@") * commentKey) + balanced / function () return "" end -- Don't bother telling me about comments
×
UNCOV
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)
UNCOV
41
   fn = SILE.resolveFile(fn) or SU.error("Unable to resolve Bibtex file " .. fn)
×
UNCOV
42
   local fh, e = io.open(fn)
×
UNCOV
43
   if e then
×
44
      SU.error("Error reading bibliography file: " .. e)
×
45
   end
UNCOV
46
   local doc = fh:read("*all")
×
UNCOV
47
   local t = epnf.parsestring(bibtexparser, doc)
×
UNCOV
48
   if not t or not t[1] or t.id ~= "document" then
×
49
      SU.error("Error parsing bibtex")
×
50
   end
UNCOV
51
   local entries = {}
×
UNCOV
52
   for i = 1, #t do
×
UNCOV
53
      if t[i].id == "entry" then
×
UNCOV
54
         local ent = t[i][1]
×
UNCOV
55
         entries[ent.label] = { type = ent.type, attributes = ent[1] }
×
56
      end
57
   end
UNCOV
58
   return entries
×
59
end
60

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

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

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

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

UNCOV
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

UNCOV
100
   self:registerCommand("reference", function (options, content)
×
UNCOV
101
      if not options.key then
×
UNCOV
102
         options.key = SU.ast.contentToString(content)
×
103
      end
UNCOV
104
      local style = SILE.settings:get("bibtex.style")
×
UNCOV
105
      local bibstyle = require("packages.bibtex.styles." .. style)
×
UNCOV
106
      local cite, err = Bibliography.produceReference(options, SILE.scratch.bibtex.bib, bibstyle)
×
UNCOV
107
      if cite == Bibliography.Errors.UNKNOWN_REFERENCE then
×
108
         SU.warn("Unknown reference in citation " .. tostring(options.key))
×
109
         return
×
110
      end
UNCOV
111
      if cite == Bibliography.Errors.UNKNOWN_TYPE then
×
112
         SU.warn("Unknown type @" .. err .. " in citation for reference " .. options.key)
×
113
         return
×
114
      end
UNCOV
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}
UNCOV
137
]]
×
138

UNCOV
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

© 2026 Coveralls, Inc