Skip to content

Commit

Permalink
Resolve pdftex-1
Browse files Browse the repository at this point in the history
  • Loading branch information
zepinglee committed Jul 19, 2024
1 parent 9581144 commit 4964c4c
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 296 deletions.
214 changes: 45 additions & 169 deletions citeproc/citeproc-cli.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ local cli = {}
local lpeg = require("lpeg")

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


Expand Down Expand Up @@ -83,9 +83,27 @@ local function convert_bib(path, output_path)
end


local balanced = lpeg.P{ "{" * ((1 - lpeg.S"{}") + lpeg.V(1))^0 * "}" }
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[]
Expand All @@ -104,53 +122,35 @@ local function read_aux_file(aux_file)
util.error(string.format("Couldn't open file %s", aux_file))
return csl_style, csl_data_files, csl_citations, csl_options, csl_bibliographies
end

local command_and_arguments = {
{"\\csl@aux@style", 2},
{"\\csl@aux@data", 2},
{"\\csl@aux@cite", 2},
{"\\csl@aux@options", 2},
{"\\csl@aux@bibliography", 2},
{"\\@input", 1},
}
for line in file:lines() do
-- TODO: Use lpeg-based method and detect multiple lines

for _, command_argment in ipairs(command_and_arguments) do
local command, num_args = table.unpack(command_argment)
if util.startswith(line, command) then
local arguments = get_command_arguments(line, command, num_args)
end
end
local style_args = get_command_arguments(line, "\\csl@aux@style", 2)
if style_args then
local ref_section_index = style_args[1]
csl_style = style_args[2]
local ref_section
local style = get_command_argument(line, "\\csl@aux@style")
if style then
csl_style = style
else
local data = get_command_arguments(line, "\\csl@aux@data")
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_arguments(line, "\\csl@aux@cite")
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_arguments(line, "\\csl@aux@options")
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_arguments(line, "\\csl@aux@bibliography")
local bib = get_command_argument(line, "\\csl@aux@bibliography")
if bib then
table.insert(csl_bibliographies, bib)
else
local sub_aux_file = get_command_arguments(line, "\\@input")
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
Expand Down Expand Up @@ -205,158 +205,34 @@ local function process_aux_file(aux_file)
util.info(banner)
util.info(string.format("The top-level auxiliary file: %s", aux_file))

local csl_citation_manager = csl.CslCitationManager:new()
csl_citation_manager:read_aux_file(aux_file)
local csl_citation_manager = csl_manager.CslCitationManager:new()

local global_ref_section = csl_citation_manager.ref_sections[0]
if global_ref_section and #csl_citation_manager.ref_sections == 0 then
util.error(string.format("No citation commands in file %s", aux_file))
local aux_content = util.read_file(aux_file)
if not aux_content then
return
end

global_ref_section.style_name = global_ref_section.style_name or "apa"
global_ref_section.lang = global_ref_section.lang or "en-US"
local bbl_str = csl_citation_manager:read_aux_file(aux_content)

local ref_section_index_max = 0
for ref_section_index, ref_section in pairs(csl_citation_manager.ref_sections) do
if ref_section_index > ref_section_index_max then
ref_section_index_max = ref_section_index
end
end
local output_path = string.gsub(aux_file, "%.aux$", ".bbl")
util.write_file(bbl_str, output_path)

for i = 0, ref_section_index_max do
local ref_section = csl_citation_manager.ref_sections[i]
if ref_section then
util.debug(ref_section)
ref_section.style_id = ref_section.style_id or global_ref_section.style_name
if #ref_section.bib_resources == 0 then
ref_section.bib_resources = global_ref_section.bib_resources
end
ref_section.lang = ref_section.style_id or global_ref_section.style_name

cli.process_ref_section(ref_section)
end
end
end

---@param ref_section RefSection
function cli.process_ref_section(ref_section)
if #ref_section.citations == 0 and #ref_section.uncited_ids == 0 then
return
end

if ref_section.style_id and ref_section.style_id ~= "" then
util.info(string.format("The style file: %s.csl", ref_section.style_id))
else
util.error("citeproc-lua: missing style name")
return
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 #ref_section.bib_resources == 0 then
-- TODO: Perhaps "Empty bibtexliography resources"?
util.warning("empty bibliography data files")
return
else
for i, bib_file in ipairs(ref_section.bib_resources) do
-- TODO: append extension name ".json" or ".bib"
util.info(string.format("Database file #%d: %s", i, bib_file))
end
end

local engine = core.init(ref_section.style_id, ref_section.bib_resources, ref_section.lang)
if not engine then
error("citeproc-lua: fails in initialize engine")
return
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
ref_section.engine = engine
if engine and engine.style.citation then
ref_section.initialized = true


end
util.close_logging_file()
end

-- if style_name and style_name ~= "" then
-- util.info(string.format("The style file: %s.csl", style_name))
-- else
-- util.error("citeproc-lua: missing style name")
-- 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, "")
Expand Down
Loading

0 comments on commit 4964c4c

Please sign in to comment.