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

sile-typesetter / sile / 9304060604

30 May 2024 02:07PM UTC coverage: 74.124% (-0.6%) from 74.707%
9304060604

push

github

alerque
style: Reformat Lua with stylua

8104 of 11995 new or added lines in 184 files covered. (67.56%)

15 existing lines in 11 files now uncovered.

12444 of 16788 relevant lines covered (74.12%)

7175.1 hits per line

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

74.68
/packages/bibtex/init.lua
1
local base = require("packages.base")
1✔
2

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

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

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

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

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

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

82
   self:registerCommand("bibstyle", function (_, content)
2✔
NEW
83
      SU.deprecated("\\bibstyle", "\\set[parameter=bibtex.style]", "0.13.2", "0.14.0")
×
NEW
84
      if type(content) == "table" then
×
NEW
85
         content = content[1]
×
86
      end
NEW
87
      if type(content) == "string" then
×
NEW
88
         SILE.settings:set("bibtex.style", content)
×
89
      end
90
   end)
91

92
   self:registerCommand("cite", function (options, content)
2✔
NEW
93
      if not options.key then
×
NEW
94
         options.key = SU.contentToString(content)
×
95
      end
NEW
96
      local style = SILE.settings:get("bibtex.style")
×
NEW
97
      local bibstyle = require("packages.bibtex.styles." .. style)
×
NEW
98
      local cite = Bibliography.produceCitation(options, SILE.scratch.bibtex.bib, bibstyle)
×
NEW
99
      if cite == Bibliography.Errors.UNKNOWN_REFERENCE then
×
NEW
100
         SU.warn("Unknown reference in citation " .. options.key)
×
NEW
101
         return
×
102
      end
NEW
103
      SILE.processString(("<sile>%s</sile>"):format(cite), "xml")
×
104
   end)
105

106
   self:registerCommand("reference", function (options, content)
2✔
107
      if not options.key then
24✔
108
         options.key = SU.contentToString(content)
48✔
109
      end
110
      local style = SILE.settings:get("bibtex.style")
24✔
111
      local bibstyle = require("packages.bibtex.styles." .. style)
24✔
112
      local cite, err = Bibliography.produceReference(options, SILE.scratch.bibtex.bib, bibstyle)
24✔
113
      if cite == Bibliography.Errors.UNKNOWN_REFERENCE then
24✔
NEW
114
         SU.warn("Unknown reference in citation " .. tostring(options.key))
×
NEW
115
         return
×
116
      end
117
      if cite == Bibliography.Errors.UNKNOWN_TYPE then
24✔
NEW
118
         SU.warn("Unknown type @" .. err .. " in citation for reference " .. options.key)
×
NEW
119
         return
×
120
      end
121
      SILE.processString(("<sile>%s</sile>"):format(cite), "xml")
24✔
122
   end)
123
end
124

125
package.documentation = [[
126
\begin{document}
127
BibTeX is a citation management system.
128
It was originally designed for TeX but has since been integrated into a variety of situations.
129

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

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

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

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

140
Currently, the only supported bibliography style is Chicago referencing, but other styles should be easy to implement.
141
Adapt \code{packages/bibtex/styles/chicago.lua} as necessary.
142
\end{document}
143
]]
1✔
144

145
return package
1✔
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