Skip to content

Commit

Permalink
Fix \ref with underscore in citation affixes (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
zepinglee committed Aug 22, 2024
1 parent 7ded92f commit 7b3c8cc
Show file tree
Hide file tree
Showing 7 changed files with 1,123 additions and 102 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Fix `\ref` with underscore in citation affixes ([#74](https://github.com/zepinglee/citeproc-lua/issues/74)).

## [0.6.2] - 2024-08-21

### Added
Expand All @@ -15,14 +19,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- Fix note position in multiple chapters ([#72](https://github.com/zepinglee/citeproc-lua/discussions/72)).
- Fix note position in multiple chapters ([#72](https://github.com/zepinglee/citeproc-lua/issues/72)).
- Fix incorrect locale map of UKenglish.

## [0.6.1] - 2024-08-15

### Fixed

- Fix parsing quotation marks ([#71](https://github.com/zepinglee/citeproc-lua/discussions/71)).
- Fix parsing quotation marks ([#71](https://github.com/zepinglee/citeproc-lua/issues/71)).

## [0.6.0] - 2024-07-31

Expand Down
41 changes: 38 additions & 3 deletions citeproc/citeproc-latex-parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,11 @@ function latex_parser.get_latex_grammar()
local latex_grammar = P{
"latex_text";
latex_text = Ct((specials + control_sequence + math + ligatures + specials + V"group" + plain_text)^0),
group = P"{" * V"latex_text" * P"}" / function (group_contents)
group = C(P"{" * V"latex_text" * P"}") / function (raw, group_contents)
return {
type = "group",
contents = group_contents,
raw = raw,
}
end,
}
Expand Down Expand Up @@ -348,9 +349,18 @@ function latex_parser.convert_cs_to_inlines(tokens, i, strict, case_protection)
else
-- Unrecognized command
if strict then
table.insert(inlines, markup.Code:new(token.raw))
local raw = token.raw
for j = i + 1, #tokens do
if type(tokens[j]) == "table" and tokens[j].type == "group" then
i = j
raw = raw .. latex_parser.convert_token_to_raw(tokens[j])
else
break
end
end
table.insert(inlines, markup.Code:new(raw))
else
-- Gobble following groupsas pandoc does:
-- Gobble following groups as pandoc does:
-- "Foo \unrecognized{bar}{baz} quz" -> "Foo quz"
for j = i + 1, #tokens do
if type(tokens[j]) == "table" and tokens[j].type == "group" then
Expand All @@ -365,6 +375,31 @@ function latex_parser.convert_cs_to_inlines(tokens, i, strict, case_protection)
return inlines, i
end

---@param token any
---@return string
function latex_parser.convert_token_to_raw(token)
if type(token) == "string" then
return token
elseif type(token) == "table" then
if token.raw then
return token.raw
elseif token.type == "group" then
local raw = "{"
for _, token_ in ipairs(token.contents) do
raw = raw .. latex_parser.convert_token_to_raw(token_)
end
raw = raw .. "}"
return raw
else
error(token.type)
return ""
end
else
error(type(token))
return ""
end
end

-- TODO: raise a warning for unrecognized LaTeX command like `\switchargs`
function latex_parser.convert_group_to_inlines(token, strict, case_protection, force_add_braces)
if case_protection then
Expand Down
184 changes: 92 additions & 92 deletions tests/bibtex/biblatex-examples.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions tests/bibtex/xampl.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"given": "L[eslie] A."
}
],
"container-title": "<code>\\mbox{</code><span class=\"nocase\">G-Animal’s</span><code>}</code> Journal",
"container-title": "<code>\\mbox{G-Animal's}</code> Journal",
"issued": {
"date-parts": [
[
Expand All @@ -27,7 +27,7 @@
"given": "L[eslie] A."
}
],
"container-title": "<code>\\mbox{</code><span class=\"nocase\">G-Animal’s</span><code>}</code> Journal",
"container-title": "<code>\\mbox{G-Animal's}</code> Journal",
"issue": "7",
"issued": {
"date-parts": [
Expand Down Expand Up @@ -58,7 +58,7 @@
{
"id": "whole-journal",
"type": "article-journal",
"container-title": "<code>\\mbox{</code><span class=\"nocase\">G-Animal’s</span><code>}</code> Journal",
"container-title": "<code>\\mbox{G-Animal's}</code> Journal",
"issue": "7",
"issued": {
"date-parts": [
Expand Down Expand Up @@ -786,6 +786,6 @@
{
"id": "random-note-crossref",
"type": "document",
"note": "Volume 2 is listed under Knuth <code>\\cite{</code><span class=\"nocase\">book-full</span><code>}</code>"
"note": "Volume 2 is listed under Knuth <code>\\cite{book-full}</code>"
}
]
2 changes: 1 addition & 1 deletion tests/latex-parser-test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe("LaTeX parser", function ()
end)

it("command with arguments", function ()
assert.same("<code>\\mbox{</code><span class=\"nocase\">G-Animal’s</span><code>}</code> Journal",
assert.same("<code>\\mbox{G-Animal's}</code> Journal",
latex_parser.latex_to_pseudo_html("\\mbox{G-Animal's} Journal", true, true)
)
end)
Expand Down
Loading

0 comments on commit 7b3c8cc

Please sign in to comment.