From 95ecae91b2a9a567d45a4e1226f0abea647c9992 Mon Sep 17 00:00:00 2001 From: Omikhleia Date: Fri, 13 Sep 2024 22:55:42 +0200 Subject: [PATCH] refactor(packages): Some CSL-related code clean-up and simplification --- csl/core/locale.lua | 21 +++++++++------------ csl/core/style.lua | 20 +++++++++----------- csl/core/utils/xmlparser.lua | 13 +------------ 3 files changed, 19 insertions(+), 35 deletions(-) diff --git a/csl/core/locale.lua b/csl/core/locale.lua index a80a94417..162f055b7 100644 --- a/csl/core/locale.lua +++ b/csl/core/locale.lua @@ -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 @@ -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). diff --git a/csl/core/style.lua b/csl/core/style.lua index e6ad261df..f3c0b681d 100644 --- a/csl/core/style.lua +++ b/csl/core/style.lua @@ -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 @@ -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). diff --git a/csl/core/utils/xmlparser.lua b/csl/core/utils/xmlparser.lua index 51b7ab734..4ba510522 100644 --- a/csl/core/utils/xmlparser.lua +++ b/csl/core/utils/xmlparser.lua @@ -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