Skip to content

Commit

Permalink
Fix empty locator (resolve #70)
Browse files Browse the repository at this point in the history
  • Loading branch information
zepinglee committed Jul 28, 2024
1 parent 22645fb commit a5072ea
Show file tree
Hide file tree
Showing 14 changed files with 282 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add support for multiple bibliographies (`refsection` environment).
- Add global `ref-section` option.

### Fixed

- Fix an error of empty locator in citation (([#70](https://github.com/zepinglee/citeproc-lua/discussions/70)))

## [0.5.1] - 2024-07-10

### Fixed
Expand Down
27 changes: 22 additions & 5 deletions citeproc/citeproc-element.lua
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,20 @@ function Element:apply_strip_periods(str)
end


---@param number string Non-empty string
---@param variable string
---@param form string
---@param context Context
---@return string
function Element:format_number(number, variable, form, context)
number = util.strip(number)
if variable == "locator" then
variable = context:get_variable("label")
local locator_variable = context:get_variable("label")
if not locator_variable or type(locator_variable) ~= "string" then
util.error("Invalid locator label")
locator_variable = "page"
end
variable = locator_variable
end
form = form or "numeric"
local number_part_list = self:split_number_parts_lpeg(number, context)
Expand Down Expand Up @@ -431,9 +441,11 @@ function Element:format_number(number, variable, form, context)
return res
end

---comment
---@param number any
---@param context any
---@alias NumberToken {type: string, value: string, delimiter_type: string}

---@param number string
---@param context Context
---@return NumberToken[]
function Element:parse_number_tokens(number, context)
local and_text = "and"
local and_symbol = "&"
Expand Down Expand Up @@ -465,10 +477,15 @@ function Element:parse_number_tokens(number, context)
value = token,
}
end
local grammer = l.Ct(token_patt * (delimiter * token_patt)^0)
local grammer = l.Ct((token_patt * (delimiter * token_patt)^0)^-1)
local tokens = grammer:match(number)
-- util.debug(tokens)

if not tokens then
return {}
end
---@cast tokens NumberToken[]

for i, token in ipairs(tokens) do
if token.type == "string" then
token.value = string.gsub(token.value, "\\%-", "-")
Expand Down
4 changes: 4 additions & 0 deletions citeproc/citeproc-node-choose.lua
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ function If:evaluate_condition(condition, state, context)
return self:is_uncertain_date(value)

elseif condition.condition == "locator" then
local locator = context:get_variable("locator")
if not locator or locator == "" then
return false
end
local locator_label = context:get_variable("label")
if locator_label == "sub verbo" then
locator_label = "sub-verbo"
Expand Down
3 changes: 2 additions & 1 deletion citeproc/citeproc-node-number.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ function Number:build_ir(engine, state, context)
local number
if not state.suppressed[self.variable] then
number = context:get_variable(self.variable, self.form)
---@cast number string | number?
end
if not number then
if not number or number == "" then
local ir = Rendered:new({}, self)
ir.group_var = GroupVar.Missing
return ir
Expand Down
4 changes: 3 additions & 1 deletion citeproc/citeproc-node-text.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ function Text:build_ir(engine, state, context)
if self.variable then
ir = self:build_variable_ir(engine, state, context)
elseif self.macro then
-- util.debug(self.macro)
ir = self:build_macro_ir(engine, state, context)
elseif self.term then
ir = self:build_term_ir(engine, state, context)
Expand All @@ -108,9 +109,10 @@ function Text:build_variable_ir(engine, state, context)

if not state.suppressed[variable] then
text = context:get_variable(variable, self.form)
---@case text string | number?
end

if not text then
if not text or text == "" then
local ir = Rendered:new({}, self)
ir.group_var = GroupVar.Missing
return ir
Expand Down
86 changes: 86 additions & 0 deletions tests/fixtures/local/locator_EmptyLocator.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
>>===== MODE =====>>
citation
<<===== MODE =====<<



>>===== DESCRIPTION =====>>
<https://github.com/zepinglee/citeproc-lua/discussions/70>
<<===== DESCRIPTION =====<<



>>===== RESULT =====>>
(Doe, condition false: Title Page)
<<===== RESULT =====<<


>>==== CITATION-ITEMS ====>>
[
[
{
"id": "ITEM-1",
"label": "page",
"locator": ""
}
]
]
<<==== CITATION-ITEMS ====<<


>>===== CSL =====>>
<?xml version="1.0" encoding="utf-8"?>
<style xmlns="http://purl.org/net/xbiblio/csl" class="in-text" version="1.0">
<info>
<id />
<title />
<updated>2024-07-28T11:35:35+08:00</updated>
</info>
<citation>
<layout prefix="(" suffix=")" delimiter="; ">
<group delimiter=", ">
<names variable="author">
<name form="short" and="symbol" delimiter=", " initialize-with=". "/>
</names>
<group delimiter=" ">
<choose>
<if locator="page paragraph" match="any">
<text value="condition true:"/>
<text variable="title"/>
<label variable="locator" form="short"/>
</if>
<else>
<text value="condition false:"/>
<text variable="title"/>
<label variable="locator" text-case="capitalize-first"/>
</else>
</choose>
<text variable="locator"/>
</group>
</group>
</layout>
</citation>
</style>
<<===== CSL =====<<


>>===== INPUT =====>>
[
{
"id": "ITEM-1",
"type": "book",
"author": [
{
"family": "Doe",
"given": "John"
}
],
"title": "Title"
}
]
<<===== INPUT =====<<


>>===== VERSION =====>>
1.0
<<===== VERSION =====<<
20 changes: 20 additions & 0 deletions tests/latex/luatex-1/issue-70.lvt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
% <https://github.com/zepinglee/citeproc-lua/discussions/70>

\input{regression-test}
\documentclass{article}
\input{csl-test}

\usepackage[style=chicago-fullnote-bibliography]{citation-style-language}
\cslsetup{regression-test = true}
\addbibresource{test.json}


\begin{document}
\START

\TEST{Citation with empty locator}{
\cite[page = ]{ITEM-1}
}

\OMIT
\end{document}
20 changes: 20 additions & 0 deletions tests/latex/luatex-1/issue-70.tlg
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
This is a generated file for the l3build validation system.
Don't change this file in any respect.
============================================================
TEST 1: Citation with empty locator
============================================================
> \l__csl_citation_info_tl=citationID={ITEM-1@1},citationItems={{id={ITEM-1},label={page},locator={}}},properties={noteIndex={1}}.
<recently read> }
l. ...}
> \l__csl_citation_tl=Bruce D’Arcus, \textit {Boundaries of Dissent: Protest and State Power in the Media Age} (Routledge, 2005)..
<recently read> }
l. ...}
LaTeX Font Info: External font `cmex10' loaded for size
(Font) <7> on input line ....
LaTeX Font Info: External font `cmex10' loaded for size
(Font) <5> on input line ....
LaTeX Font Info: External font `cmex10' loaded for size
(Font) <8> on input line ....
LaTeX Font Info: External font `cmex10' loaded for size
(Font) <6> on input line ....
============================================================
20 changes: 20 additions & 0 deletions tests/latex/luatex-2/issue-70.lvt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
% <https://github.com/zepinglee/citeproc-lua/discussions/70>

\input{regression-test}
\documentclass{article}
\input{csl-test}

\usepackage[style=chicago-fullnote-bibliography]{citation-style-language}
\cslsetup{regression-test = true}
\addbibresource{test.json}


\begin{document}
\START

\TEST{Citation with empty locator}{
\cite[page = ]{ITEM-1}
}

\OMIT
\end{document}
20 changes: 20 additions & 0 deletions tests/latex/luatex-2/issue-70.tlg
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
This is a generated file for the l3build validation system.
Don't change this file in any respect.
============================================================
TEST 1: Citation with empty locator
============================================================
> \l__csl_citation_info_tl=citationID={ITEM-1@1},citationItems={{id={ITEM-1},label={page},locator={}}},properties={noteIndex={1}}.
<recently read> }
l. ...}
> \l__csl_citation_tl=Bruce D’Arcus, \textit {Boundaries of Dissent: Protest and State Power in the Media Age} (Routledge, 2005)..
<recently read> }
l. ...}
LaTeX Font Info: External font `cmex10' loaded for size
(Font) <7> on input line ....
LaTeX Font Info: External font `cmex10' loaded for size
(Font) <5> on input line ....
LaTeX Font Info: External font `cmex10' loaded for size
(Font) <8> on input line ....
LaTeX Font Info: External font `cmex10' loaded for size
(Font) <6> on input line ....
============================================================
20 changes: 20 additions & 0 deletions tests/latex/pdftex-1/issue-70.lvt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
% <https://github.com/zepinglee/citeproc-lua/discussions/70>

\input{regression-test}
\documentclass{article}
\input{csl-test}

\usepackage[style=chicago-fullnote-bibliography]{citation-style-language}
\cslsetup{regression-test = true}
\addbibresource{test.json}


\begin{document}
\START

\TEST{Citation with empty locator}{
\cite[page = ]{ITEM-1}
}

\OMIT
\end{document}
21 changes: 21 additions & 0 deletions tests/latex/pdftex-1/issue-70.tlg
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
This is a generated file for the l3build validation system.
Don't change this file in any respect.
============================================================
TEST 1: Citation with empty locator
============================================================
> \l__csl_citation_info_tl=citationID={ITEM-1@1},citationItems={{id={ITEM-1},label={page},locator={}}},properties={noteIndex={1}}.
<recently read> }
l. ...}
LaTeX Warning: Citation `ITEM-1' on page 1 undefined on input line ....
> \l__csl_citation_tl=[\textbf {ITEM-1}].
<recently read> }
l. ...}
LaTeX Font Info: External font `cmex10' loaded for size
(Font) <7> on input line ....
LaTeX Font Info: External font `cmex10' loaded for size
(Font) <5> on input line ....
LaTeX Font Info: External font `cmex10' loaded for size
(Font) <8> on input line ....
LaTeX Font Info: External font `cmex10' loaded for size
(Font) <6> on input line ....
============================================================
20 changes: 20 additions & 0 deletions tests/latex/pdftex-2/issue-70.lvt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
% <https://github.com/zepinglee/citeproc-lua/discussions/70>

\input{regression-test}
\documentclass{article}
\input{csl-test}

\usepackage[style=chicago-fullnote-bibliography]{citation-style-language}
\cslsetup{regression-test = true}
\addbibresource{test.json}


\begin{document}
\START

\TEST{Citation with empty locator}{
\cite[page = ]{ITEM-1}
}

\OMIT
\end{document}
20 changes: 20 additions & 0 deletions tests/latex/pdftex-2/issue-70.tlg
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
This is a generated file for the l3build validation system.
Don't change this file in any respect.
============================================================
TEST 1: Citation with empty locator
============================================================
> \l__csl_citation_info_tl=citationID={ITEM-1@1},citationItems={{id={ITEM-1},label={page},locator={}}},properties={noteIndex={1}}.
<recently read> }
l. ...}
> \l__csl_citation_tl=Bruce D’Arcus, \textit {Boundaries of Dissent: Protest and State Power in the Media Age} (Routledge, 2005)..
<recently read> }
l. ...}
LaTeX Font Info: External font `cmex10' loaded for size
(Font) <7> on input line ....
LaTeX Font Info: External font `cmex10' loaded for size
(Font) <5> on input line ....
LaTeX Font Info: External font `cmex10' loaded for size
(Font) <8> on input line ....
LaTeX Font Info: External font `cmex10' loaded for size
(Font) <6> on input line ....
============================================================

0 comments on commit a5072ea

Please sign in to comment.