Skip to content

Commit

Permalink
Fix missing DOI prefix (fix #28)
Browse files Browse the repository at this point in the history
  • Loading branch information
zepinglee committed Nov 17, 2022
1 parent 90b8817 commit 83caeb9
Show file tree
Hide file tree
Showing 7 changed files with 2,076 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Fix an error in checking the plurity of `number-of-pages` ([#27](https://github.com/zepinglee/citeproc-lua/issues/27)).
- Fix an error in converting value `"2nd"` to its ordinal form ([#27](https://github.com/zepinglee/citeproc-lua/issues/27)).
- Fix missing DOI prefix when used with `hyperref` ([#28](https://github.com/zepinglee/citeproc-lua/issues/28)).

## [v0.2.2] - 2022-09-23

Expand Down
9 changes: 3 additions & 6 deletions citeproc/citeproc-engine.lua
Expand Up @@ -50,8 +50,7 @@ function CiteProc.new(sys, style, lang, force_lang)

o.opt = {
-- Similar to citeproc-js's development_extensions.wrap_url_and_doi
url_link = false,
doi_link = false, -- also applied to PMID and PMCID
wrap_url_and_doi = false,
title_link = false,
}

Expand Down Expand Up @@ -600,13 +599,11 @@ function CiteProc:set_output_format(format)
end

function CiteProc:enable_linking()
self.opt.url_link = true
self.opt.doi_link = true
self.opt.wrap_url_and_doi = true
end

function CiteProc:disable_linking()
self.opt.url_link = false
self.opt.doi_link = false
self.opt.wrap_url_and_doi = false
end

function CiteProc.create_element_tree(node)
Expand Down
21 changes: 11 additions & 10 deletions citeproc/citeproc-node-text.lua
Expand Up @@ -93,10 +93,8 @@ function Text:build_variable_ir(engine, state, context)
-- if not engine.opt then
-- print(debug.traceback())
-- end
if (variable == "URL" and engine.opt.url_link) or
(variable == "DOI" and engine.opt.doi_link) or
(variable == "PMID" and engine.opt.doi_link) or
(variable == "PMID" and engine.opt.doi_link) then
if engine.opt.wrap_url_and_doi and (variable == "URL" or variable == "DOI" or
variable == "PMID" or variable == "PMID") then
inlines = self:render_linked(engine, state, context, variable, text)
else
inlines = self:render_text_inlines(text, context)
Expand All @@ -119,21 +117,21 @@ end

function Text:render_linked(engine, state, context, variable, text)
local href
local url_prefix = false -- The prefix is used as part of the URL.
if variable == "URL" then
href = text
elseif self.affixes and self.affixes.prefix then
if string.match(self.affixes.prefix, "https?://") then
text = self.affixes.prefix .. text
self.affixes.prefix = nil
href = text
end
elseif self.affixes and self.affixes.prefix and string.match(self.affixes.prefix, "https?://") then
text = self.affixes.prefix .. text
href = text
url_prefix = true
elseif variable == "DOI" then
href = "https://doi.org/" .. text
elseif variable == "PMID" then
href = "https://www.ncbi.nlm.nih.gov/pubmed/" .. text
elseif variable == "PMCID" then
href = "https://www.ncbi.nlm.nih.gov/pmc/articles/" .. text
end

local inlines = {Linked:new(text, href)}
local output_format = context.format
local localized_quotes = nil
Expand All @@ -142,6 +140,9 @@ function Text:render_linked(engine, state, context, variable, text)
end
inlines = output_format:with_format(inlines, self.formatting)
inlines = output_format:affixed_quoted(inlines, self.affixes, localized_quotes)
if url_prefix then
table.remove(inlines, 1)
end
return output_format:with_display(inlines, self.display)
end

Expand Down
2 changes: 1 addition & 1 deletion test/citeproc-test.log
Expand Up @@ -38,4 +38,4 @@ unicode 1 0 0 0
variables 5 0 0 0
virtual 1 0 0 0
-------------- ------- ------- ------- -------
(all) 849 0 0 0
(all) 850 0 0 0

0 comments on commit 83caeb9

Please sign in to comment.