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

sile-typesetter / sile / 6713098919

31 Oct 2023 10:21PM UTC coverage: 52.831% (-21.8%) from 74.636%
6713098919

push

github

web-flow
Merge d0a2a1ee9 into b185d4972

45 of 45 new or added lines in 3 files covered. (100.0%)

8173 of 15470 relevant lines covered (52.83%)

6562.28 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 lpeg = require("lpeg")
×
7
local epnf = require("epnf")
×
8

9
local Bibliography
10

11
local identifier = (SILE.parserBits.identifier + lpeg.S":-")^1
×
12

13
local balanced = lpeg.C{ "{" * lpeg.P(" ")^0 * lpeg.C(((1 - lpeg.S"{}") + lpeg.V(1))^0) * "}" } / function (...) local t={...}; return t[2] end
×
14
local doubleq = lpeg.C( lpeg.P '"' * lpeg.C(((1 - lpeg.S '"\r\n\f\\') + (lpeg.P '\\' * 1)) ^ 0) * '"' )
×
15

16
-- luacheck: push ignore
17
---@diagnostic disable: undefined-global, unused-local, lowercase-global
18
local bibtexparser = epnf.define(function (_ENV)
×
19
  local _ = WS^0
×
20
  local sep = lpeg.S(",;") * _
×
21
  local myID = C(identifier + lpeg.P(1)) / function (t) return t end
×
22
  local myTag = C(identifier + lpeg.P(1)) / function (t) return t:lower() end
×
23
  local value = balanced + doubleq + myID
×
24
  local pair = lpeg.Cg(myTag * _ * "=" * _ * C(value)) * _ * sep^-1   / function (...) local t= {...}; return t[1], t[#t] end
×
25
  local list = lpeg.Cf(lpeg.Ct("") * pair^0, rawset)
×
26
  local commentKey = lpeg.Cmt(R("az", "AZ")^1, function(_, _, a)
×
27
    return a:lower() == "comment"
×
28
  end)
29

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

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

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

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

75
function package:registerCommands ()
×
76

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 (_, content)
×
83
    SU.deprecated("\\bibstyle", '\\set[parameter=bibtex.style]', "0.13.2", "0.14.0")
×
84
    if type(content) == "table" then
×
85
      content = content[1]
×
86
    end
87
    if type(content) == "string" then
×
88
      SILE.settings:set("bibtex.style", content)
×
89
    end
90
  end)
91

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

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

120
end
121

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

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

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

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

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

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

142
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