diff options
Diffstat (limited to 'biblio/citation-style-language/citeproc-latex-core.lua')
-rw-r--r-- | biblio/citation-style-language/citeproc-latex-core.lua | 96 |
1 files changed, 77 insertions, 19 deletions
diff --git a/biblio/citation-style-language/citeproc-latex-core.lua b/biblio/citation-style-language/citeproc-latex-core.lua index 0fa0b40ca3..c8fcf475a3 100644 --- a/biblio/citation-style-language/citeproc-latex-core.lua +++ b/biblio/citation-style-language/citeproc-latex-core.lua @@ -1,5 +1,5 @@ -- --- Copyright (c) 2021-2022 Zeping Lee +-- Copyright (c) 2021-2023 Zeping Lee -- Released under the MIT license. -- Repository: https://github.com/zepinglee/citeproc-lua -- @@ -7,7 +7,7 @@ local core = {} local citeproc = require("citeproc") -local bibtex -- = require("citeproc-bibtex") -- load on demand +local bibtex2csl -- = require("citeproc-bibtex-parser") -- load on demand local util = citeproc.util require("lualibs") local latex_parser = require("citeproc-latex-parser") @@ -17,6 +17,8 @@ core.locale_file_format = "csl-locales-%s.xml" core.uncited_ids = {} core.uncite_all_items = false +core.item_list = {} +core.item_dict = {} function core.read_file(file_name, ftype, file_info) if file_info then @@ -78,13 +80,20 @@ local function read_data_file(data_file) local status, res = pcall(utilities.json.tolua, contents) if status and res then csl_items = res + for _, item in ipairs(csl_items) do + -- Jounal abbreviations + if item.type == "article-journal" or item.type == "article-magazine" + or item.type == "article-newspaper" then + util.check_journal_abbreviations(item) + end + end else util.error(string.format('JSON decoding error in file "%s"', data_file)) csl_items = {} end elseif extension == ".bib" then - bibtex = bibtex or require("citeproc-bibtex") - csl_items = bibtex.parse(contents) + bibtex2csl = bibtex2csl or require("citeproc-bibtex2csl") + csl_items = bibtex2csl.parse_bibtex_to_csl(contents, true, true, true, true) end return file_name, csl_items @@ -92,27 +101,29 @@ end local function read_data_files(data_files) - local bib = {} + local item_list = {} + local item_dict = {} for _, data_file in ipairs(data_files) do local file_name, csl_items = read_data_file(data_file) - -- TODO: parse bib entries on demand + -- TODO: parse item_dict entries on demand for _, item in ipairs(csl_items) do local id = item.id - if bib[id] then + if item_dict[id] then util.warning(string.format('Duplicate entry key "%s" in "%s".', id, file_name)) else - bib[id] = item + item_dict[id] = item + table.insert(item_list, item) end end end - return bib + return item_list, item_dict end function core.make_citeproc_sys(data_files) - core.bib = read_data_files(data_files) + core.item_list, core.item_dict = read_data_files(data_files) local citeproc_sys = { retrieveLocale = function (lang) local locale_file_format = core.locale_file_format or "locales-%s.xml" @@ -120,7 +131,7 @@ function core.make_citeproc_sys(data_files) return core.read_file(filename) end, retrieveItem = function (id) - local res = core.bib[id] + local res = core.item_dict[id] return res end } @@ -185,10 +196,12 @@ function core.make_citation(citation_info) for i, item in ipairs(citation.citationItems) do local citation_item = parse_latex_prop(item) if citation_item.prefix then - citation_item.prefix = latex_parser.convert_latex_to_rich_text(citation_item.prefix) + -- util.debug(citation_item.prefix) + citation_item.prefix = latex_parser.latex_to_pseudo_html(citation_item.prefix, true, false) + -- util.debug(citation_item.prefix) end if citation_item.suffix then - citation_item.suffix = latex_parser.convert_latex_to_rich_text(citation_item.suffix) + citation_item.suffix = latex_parser.latex_to_pseudo_html(citation_item.suffix, true, false) end citation.citationItems[i] = citation_item end @@ -245,10 +258,10 @@ function core.update_cited_and_uncited_ids(engine, citations) for _, cite_item in ipairs(citation.citationItems) do if cite_item.id == "*" then if not core.uncite_all_items then - for id, _ in pairs(core.bib) do - if not uncited_id_map[id] then - table.insert(uncited_id_list, id) - uncited_id_map[id] = true + for _, item in ipairs(core.item_list) do + if not uncited_id_map[item.id] then + table.insert(uncited_id_list, item.id) + uncited_id_map[item.id] = true end end core.uncite_all_items = true @@ -275,8 +288,25 @@ function core.update_cited_and_uncited_ids(engine, citations) end -function core.make_bibliography(engine) - local result = engine:makeBibliography() +function core.parser_filter(filter_str) + -- util.debug(filter_str) + local filter = latex_parser.parse_prop(filter_str) + for filter_type, conditions in pairs(filter) do + conditions = latex_parser.parse_seq(conditions) + filter[filter_type] = conditions + for i, condition in ipairs(conditions) do + conditions[i] = latex_parser.parse_prop(condition) + end + end + return filter +end + +function core.make_bibliography(engine, filter_str) + local filter + if filter_str then + filter = core.parser_filter(filter_str) + end + local result = engine:makeBibliography(filter) local params = result[1] local bib_items = result[2] @@ -331,4 +361,32 @@ function core.make_bibliography(engine) end +function core.set_categories(engine, categories_str) + -- util.debug(categories_str) + local category_dict = latex_parser.parse_prop(categories_str) + for category, keys in pairs(category_dict) do + category_dict[category] = latex_parser.parse_seq(keys) + end + for category, keys in pairs(category_dict) do + for _, key in ipairs(keys) do + local item = engine.registry.registry[key] + if item then + if not item.categories then + item.categories = {} + end + if not util.in_list(category, item.categories) then + table.insert(item.categories, category) + end + else + util.error(string.format("Invalid citation key '%s'.", key)) + end + end + end + -- for id, item in pairs(csl.engine.registry.registry) do + -- util.debug(id) + -- util.debug(item.categories) + -- end +end + + return core |