summaryrefslogtreecommitdiff
path: root/biblio/citation-style-language/citeproc-cli.lua
blob: 83e6d35e9b171153cd4061f7959871817a617430 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331

--
-- Copyright (c) 2021-2024 Zeping Lee
-- Released under the MIT license.
-- Repository: https://github.com/zepinglee/citeproc-lua
--

local cli = {}


local lpeg = require("lpeg")

require("lualibs")
local citeproc = require("citeproc")
local bibtex2csl  -- = require("citeproc-bibtex-parser")  -- load on demand
local util = require("citeproc-util")
local core = require("citeproc-latex-core")
local latex_parser = require("citeproc-latex-parser")


-- http://lua-users.org/wiki/AlternativeGetOpt
local function getopt( arg, options )
  local tab = {}
  for k, v in ipairs(arg) do
    if string.sub( v, 1, 2) == "--" then
      local x = string.find( v, "=", 1, true )
      if x then tab[ string.sub( v, 3, x-1 ) ] = string.sub( v, x+1 )
      else      tab[ string.sub( v, 3 ) ] = true
      end
    elseif string.sub( v, 1, 1 ) == "-" then
      local y = 2
      local l = string.len(v)
      local jopt
      while ( y <= l ) do
        jopt = string.sub( v, y, y )
        if string.find( options, jopt, 1, true ) then
          if y < l then
            tab[ jopt ] = string.sub( v, y+1 )
            y = l
          else
            tab[ jopt ] = arg[ k + 1 ]
          end
        else
          tab[ jopt ] = true
        end
        y = y + 1
      end
    else
      if tab.file then
        error(string.format('Invalid argument "%s"', v))
      end
      tab.file = v
    end

  end
  return tab
end


local function print_version()
  io.write(string.format("citeproc-lua %s\n", citeproc.__VERSION__))
end


local function print_help()
  io.write("Usage: citeproc-lua [options] auxname[.aux]\n")
  io.write("Options:\n")
  io.write("  -h, --help          Print this message and exit.\n")
  io.write("  -q, --quiet         Quiet mode: suppress the banner and progress reports.\n")
  io.write("  -V, --version       Print the version number and exit.\n")
end


local function convert_bib(path, output_path)
  local contents = util.read_file(path)
  bibtex2csl = bibtex2csl or require("citeproc-bibtex2csl")
  local csl_data = bibtex2csl.parse_bibtex_to_csl(contents, true, true, true, true)
  if not output_path then
    output_path = string.gsub(path, "%.bib$", ".json")
  end
  util.write_file(utilities.json.tojson(csl_data) .. "\n", output_path)
end


local balanced = lpeg.P{ "{" * lpeg.V(1)^0 * "}" + (1 - lpeg.S"{}") }


---@param text string
---@return string?
local function get_command_argument(text, command)
  if string.match(text, command) then
    local grammar = (lpeg.P(command) * lpeg.S(" \t\r\n")^0 * lpeg.C(balanced) + 1)^0
    local argument = grammar:match(text)
    if not argument then
      return nil
    end
    argument = string.sub(argument, 2, -2)
    return argument
  end
  return nil
end



---comment
---@param aux_file any
---@return string
---@return string[]
---@return Citation[]
---@return table<string, string>
---@return string[]
local function read_aux_file(aux_file)
  local csl_style = nil
  local csl_data_files = {}
  local csl_citations = {}
  local csl_options = {}
  local csl_bibliographies = {}

  local file = io.open(aux_file)
  if not file then
    util.error(string.format("Couldn't open file %s", aux_file))
    return csl_style, csl_data_files, csl_citations, csl_options, csl_bibliographies
  end
  for line in file:lines() do
    -- TODO: Use lpeg-based method and detect multiple lines
    local style = get_command_argument(line, "\\csl@aux@style")
    if style then
      csl_style = style
    else
      local data = get_command_argument(line, "\\csl@aux@data")
      if data then
        for _, bib_file in ipairs(latex_parser.parse_seq(data)) do
          table.insert(csl_data_files, bib_file)
        end
      else
        local cite = get_command_argument(line, "\\csl@aux@cite")
        if cite then
          local citation = core.make_citation(cite)
          table.insert(csl_citations, citation)
        else
          local options = get_command_argument(line, "\\csl@aux@options")
          if options then
            options = latex_parser.parse_prop(options)
            for key, value in pairs(options) do
              csl_options[key] = value
            end
          else
            local bib = get_command_argument(line, "\\csl@aux@bibliography")
            if bib then
              table.insert(csl_bibliographies, bib)
            else
              local sub_aux_file = get_command_argument(line, "\\@input")
              if sub_aux_file and util.endswith(sub_aux_file, ".aux") then
                local style_name, data_files, citations, opts, bibs = read_aux_file(sub_aux_file)
                if style_name then
                  csl_style = style_name
                end
                util.extend(csl_data_files, data_files)
                util.extend(csl_citations, citations)
                util.extend(csl_citations, citations)
                for key, value in pairs(opts) do
                  csl_options[key] = value
                end
                util.extend(csl_bibliographies, bibs)
              end
            end
          end
        end
      end
    end
  end
  file:close()

  return csl_style, csl_data_files, csl_citations, csl_options, csl_bibliographies
end


local function get_undefined_info(core, citation)
  local res = ""
  for _, cite_item in ipairs(citation.citationItems) do
    if not core.item_dict[cite_item.id] then
      if res ~= "" then
        res = res .. ","
      end
      res = res .. cite_item.id
    end
  end
  if res ~= "" then
    res = string.format("\\cslsetup{undefined-cites={%s}}", res)
  end
  return res
end


---@param aux_file string
local function process_aux_file(aux_file)
  if not util.endswith(aux_file, ".aux") then
    aux_file = aux_file .. ".aux"
  end
  local blg_file = string.gsub(aux_file, "%.aux$", ".blg")
  util.set_logging_file(blg_file)

  local banner = string.format("This is citeproc-lua, Version %s", citeproc.__VERSION__)
  util.info(banner)
  util.info(string.format("The top-level auxiliary file: %s", aux_file))

  local style_name, bib_files, citations, csl_options, bibliographies = read_aux_file(aux_file)

  if style_name and style_name ~= "" then
    util.info(string.format("The style file: %s.csl", style_name))
  else
    util.warning("Missing style name. Will use default APA style.")
    style_name = "apa"
  end

  if #citations == 0 then
    util.error(string.format("No citation commands in file %s", aux_file))
  end

  if #bib_files == 0 then
    util.warning("empty bibliography data files")
  else
    for i, bib_file in ipairs(bib_files) do
      util.info(string.format("Database file #%d: %s", i, bib_file))
    end
  end

  local lang = csl_options.locale

  local engine = core.init(style_name, bib_files, lang)
  if not engine then
    error("citeproc-lua: fails in initialize engine")
  end
  if csl_options.linking then
    engine:enable_linking()
  end
  local style_class = engine:get_style_class()

  if style_class == "in-text" then
    for _, citation in ipairs(citations) do
      citation.properties.noteIndex = 0
    end
  end

  local citation_strings = core.process_citations(engine, citations)

  -- util.debug(citation_strings)

  local output_string = string.format("\\cslsetup{class = %s}\n\n", style_class)

  for _, citation in ipairs(citations) do
    local citation_id = citation.citationID
    if citation_id ~= "@nocite" then
      local citation_str = citation_strings[citation_id]
      local undefined_entry_info = get_undefined_info(core, citation)
      output_string = output_string .. string.format("\\cslcitation{%s}{%s%s}\n",
        citation_id, undefined_entry_info, citation_str)
    end
  end

  output_string = output_string .. "\n"
  local categories_str = csl_options["categories"]
  if categories_str then
    core.set_categories(engine, categories_str)
  end

  for _, bib_filter_str in ipairs(bibliographies) do
    local result = core.make_bibliography(engine, bib_filter_str)
    output_string = output_string .. "\n\n\n" .. result
  end

  local output_path = string.gsub(aux_file, "%.aux$", ".bbl")
  util.write_file(output_string, output_path)

  util.quiet_mode = false;
  if util.num_errors > 1 then
    util.info(string.format("(There were %d error messages)", util.num_errors))
  elseif util.num_errors == 1 then
    util.info("(There was 1 error message)")
  end

  if util.num_warnings > 1 then
    util.info(string.format("(There were %d warning messages)", util.num_warnings))
  elseif util.num_warnings == 1 then
    util.info("(There was 1 warning message)")
  end

  util.close_logging_file()
end


function cli.main()
  local args = getopt(arg, "")

  -- for k, v in pairs(args) do
  --   print( k, v )
  -- end

  if args.V or args.version then
    print_version()
    return
  elseif args.h or args.help then
    print_help()
    return
  elseif args.q or args.quiet then
    util.quiet_mode = true
  end

  if not args.file then
    error("citeproc-lua: Need exactly one file argument.\n")
  end

  local path = args.file

  local output_path = args.o or args.output
  if util.endswith(path, ".bib") then
    convert_bib(path, output_path)
  else
    process_aux_file(path)
  end

  if util.num_errors > 0 then
    return 1
  end

  return 0

end


return cli