Skip to content

Commit

Permalink
refactor(packages): Some CSL-related code clean-up and simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
Omikhleia authored and Didier Willis committed Sep 13, 2024
1 parent bc84c9a commit 95ecae9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 35 deletions.
21 changes: 9 additions & 12 deletions csl/core/locale.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,18 @@ local rules = {

local CslLocale = pl.class()

function CslLocale:_init (locale)
self.locale = locale
function CslLocale:_init (tree)
self.terms = {}
self.dates = {}
self.styleOptions = {}
self.lang = "und"
self:_preprocess()
self.locale = nil -- We don't need the AST anymore
self:_preprocess(tree)
end

-- Store items in more convenient structures and maps
function CslLocale:_preprocess ()
self.lang = self.locale.options["xml:lang"]
-- Store items from the syntax tree in more convenient structures and maps
function CslLocale:_preprocess (tree)
self.lang = tree.options["xml:lang"]

for _, content in ipairs(self.locale) do
for _, content in ipairs(tree) do
if content.command == "cs:terms" then
for _, term in ipairs(content) do
if term.command == "cs:term" then
Expand Down Expand Up @@ -224,11 +221,11 @@ end
-- @tparam string doc The CSL locale file content
-- @treturn CslLocale The locale object (or nil, error message on failure)
function CslLocale.parse (doc)
local loc, err = parse(doc, rules)
if not loc then
local tree, err = parse(doc, rules)
if not tree then
return nil, err
end
return CslLocale(loc)
return CslLocale(tree)
end

--- Read a CSL locale file (static method).
Expand Down
20 changes: 9 additions & 11 deletions csl/core/style.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,22 @@ local rules = {

local CslStyle = pl.class()

function CslStyle:_init (csl)
self.csl = csl
function CslStyle:_init (tree)
self.macros = {}
self.locales = {}
self.bibliography = nil
self.citation = nil
self.globalOptions = {}
self:_preprocess()
self.csl = nil -- We don't need the AST anymore
self:_preprocess(tree)
end

-- Store items in more convenient structures and maps
function CslStyle:_preprocess ()
-- Store items from the syntax tree in more convenient structures and maps
function CslStyle:_preprocess (tree)
-- Global options and inheritable name options
self.globalOptions = self.csl.options
self.globalOptions = tree.options

-- Extract macros, locale overrides, citation and bibliography
for _, content in ipairs(self.csl) do
for _, content in ipairs(tree) do
if content.command == "cs:macro" then
local name = content.options and content.options.name
if not name then
Expand Down Expand Up @@ -77,11 +75,11 @@ end
-- @tparam string doc The CSL style document
-- @treturn Csl The parsed CSL style object (or nil, error message on failure)
function CslStyle.parse (doc)
local csl, err = parse(doc, rules)
if not csl then
local tree, err = parse(doc, rules)
if not tree then
return nil, err
end
return CslStyle(csl)
return CslStyle(tree)
end

--- Read a CSL style file (static method).
Expand Down
13 changes: 1 addition & 12 deletions csl/core/utils/xmlparser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,7 @@ local function parse (doc, rules)
return nil, err
end
else
-- FIXME DUBIOUS CODE SMELL
-- SILE's XML parser hsome code here, which seems wrong:
-- keys on pair()?
-- EDIT: Known issue:
-- https://github.com/sile-typesetter/sile/issues/980
SU.error("XML parser: only string input should be supported")
-- for element in pairs(doc) do
-- status, err = parser:parse(element)
-- if not status then
-- return nil, err
-- end
-- end
return nil, "Only string input should be supported"
end
status, err = parser:parse()
if not status then
Expand Down

0 comments on commit 95ecae9

Please sign in to comment.