uc3-dmp-citation 0.0.4 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/uc3-dmp-citation/version.rb +1 -1
- data/lib/uc3-dmp-citation.rb +22 -18
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7c2817afd07293e08976bd32fad38b356079ef155ae14f4ebd7fba69110de83a
|
4
|
+
data.tar.gz: 6b985ed4cdf93d42d5ef1a6304c71c3094077ad4398bab1b853ef62ba07d4d99
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa6b47581cc57faa4bf79f9e079cf57e24054a58ba324ebff96b19c06d8625b593adbfd60337ed732d162c990ceb7fe581d6953f60d01a897c30c123ee1e7097
|
7
|
+
data.tar.gz: bd438167069b0f3ce62fc983c26d6ee9207b71a662b06a12ec0c301d49c5a21de87729094d869eb09f83bc689aa7c87ec0f10e2d420268ce365b0bf510287590
|
data/lib/uc3-dmp-citation.rb
CHANGED
@@ -10,6 +10,7 @@ require 'uc3-dmp-external-api'
|
|
10
10
|
module Uc3DmpCitation
|
11
11
|
class CiterError < StandardError; end
|
12
12
|
|
13
|
+
# Clas that fetches Citations for a given DOI
|
13
14
|
class Citer
|
14
15
|
DEFAULT_CITATION_STYLE = 'chicago-author-date'
|
15
16
|
DEFAULT_DOI_URL = 'http://doi.org'
|
@@ -21,19 +22,22 @@ module Uc3DmpCitation
|
|
21
22
|
MSG_UNABLE_TO_UPDATE = 'Unable to update the citations on the DMP ID.'
|
22
23
|
|
23
24
|
class << self
|
24
|
-
|
25
|
-
|
25
|
+
# rubocop:disable Metrics/AbcSize
|
26
|
+
def fetch_citation(doi:, work_type: DEFAULT_WORK_TYPE, style: DEFAULT_CITATION_STYLE, logger: nil)
|
27
|
+
uri = _doi_to_uri(doi:)
|
26
28
|
return nil if uri.nil? || uri.blank?
|
27
29
|
|
28
30
|
headers = { Accept: 'application/x-bibtex' }
|
29
31
|
logger.debug(message: "Fetching BibTeX from: #{uri}") if logger.respond_to?(:debug)
|
30
|
-
resp = Uc3DmpExternalApi::Client.call(url: uri, method: :get, additional_headers: headers, logger:
|
32
|
+
resp = Uc3DmpExternalApi::Client.call(url: uri, method: :get, additional_headers: headers, logger:)
|
31
33
|
return nil if resp.nil? || resp.to_s.strip.empty?
|
32
34
|
|
33
35
|
bibtex = BibTeX.parse(_cleanse_bibtex(text: resp))
|
34
|
-
work_type = work_type.nil? ? determine_work_type(bibtex:
|
35
|
-
|
36
|
+
work_type = work_type.nil? ? determine_work_type(bibtex:) : work_type
|
37
|
+
style = DEFAULT_CITATION_STYLE if style.nil?
|
38
|
+
_bibtex_to_citation(uri:, work_type:, style:, bibtex:)
|
36
39
|
end
|
40
|
+
# rubocop:enable Metrics/AbcSize
|
37
41
|
|
38
42
|
private
|
39
43
|
|
@@ -63,20 +67,21 @@ module Uc3DmpCitation
|
|
63
67
|
# Remove any encoded HTML (e.g. "Regular text $\\lt$strong$\\gt$Bold text$\\lt$/strong$\\gt$")
|
64
68
|
utf8 = utf8.gsub(%r{\$?\\\$?(less|lt|Lt)\$/?[a-zA-Z]+\$?\\\$?(greater|gt|Gt)\$}, '')
|
65
69
|
# Replace any special dash, semicolon and quote characters with a minus sign or single/double quote
|
66
|
-
utf8 = utf8.gsub(
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
+
utf8 = utf8.gsub(/\$?\\(T|t)ext[a-zA-Z]+dash\$?/, '-').gsub(/\{(T|t)ext[a-zA-Z]+dash\}/, '-')
|
71
|
+
.gsub(/\$?\\(M|m)athsemicolon\$?/, ':').gsub(/\{(M|m)semicolon\}/, ':')
|
72
|
+
.gsub(/\$?\\(T|t)extquotesingle\$?/, "'").gsub(/\{(T|t)extquotesingle\}/, "'")
|
73
|
+
.gsub(/\$?\\(T|t)extquotedouble\$?/, '"').gsub(/\{(T|t)extquotedouble\}/, '"')
|
70
74
|
# Remove any remaining `\v` entries which attempt to construct an accented character
|
71
|
-
utf8.gsub(
|
75
|
+
utf8.gsub(/\\v/, '')
|
72
76
|
end
|
73
77
|
|
74
78
|
# Convert the BibTeX item to a citation
|
75
|
-
|
79
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
80
|
+
def _bibtex_to_citation(uri:, bibtex:, work_type: DEFAULT_WORK_TYPE, style: DEFAULT_CITATION_STYLE)
|
76
81
|
return nil unless uri.is_a?(String) && uri.strip != ''
|
77
82
|
return nil if bibtex.nil? || bibtex.data.nil? || bibtex.data.first.nil?
|
78
83
|
|
79
|
-
cp = CiteProc::Processor.new(style
|
84
|
+
cp = CiteProc::Processor.new(style:, format: 'html')
|
80
85
|
cp.import(bibtex.to_citeproc)
|
81
86
|
citation = cp.render(:bibliography, id: bibtex.data.first.id)
|
82
87
|
return nil unless citation.is_a?(Array) && citation.any?
|
@@ -87,22 +92,21 @@ module Uc3DmpCitation
|
|
87
92
|
|
88
93
|
unless work_type.nil? || work_type.strip == ''
|
89
94
|
# This supports the :apa and :chicago-author-date styles
|
90
|
-
citation = citation.gsub(/\.”\s+/, "
|
91
|
-
|
95
|
+
citation = citation.gsub(/\.”\s+/, ".” [#{work_type.gsub('_', ' ').capitalize}]. ")
|
96
|
+
.gsub(%r{</i>\.\s+}, "</i>. [#{work_type.gsub('_', ' ').capitalize}]. ")
|
92
97
|
end
|
93
98
|
|
94
99
|
# Convert the URL into a link. Ensure that the trailing period is not a part of
|
95
100
|
# the link!
|
96
|
-
citation.gsub(URI.
|
101
|
+
citation.gsub(URI::DEFAULT_PARSER.make_regexp) do |url|
|
97
102
|
if url.start_with?('http')
|
98
|
-
'<a href="%{url}" target="_blank">%{url}</a>.'
|
99
|
-
url: url.end_with?('.') ? uri : "#{uri}."
|
100
|
-
}
|
103
|
+
format('<a href="%{url}" target="_blank">%{url}</a>.', url: url.end_with?('.') ? uri : "#{uri}.")
|
101
104
|
else
|
102
105
|
url
|
103
106
|
end
|
104
107
|
end
|
105
108
|
end
|
109
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
106
110
|
end
|
107
111
|
end
|
108
112
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: uc3-dmp-citation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian Riley
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-10-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: byebug
|
@@ -81,7 +81,7 @@ licenses:
|
|
81
81
|
- MIT
|
82
82
|
metadata:
|
83
83
|
rubygems_mfa_required: 'false'
|
84
|
-
post_install_message:
|
84
|
+
post_install_message:
|
85
85
|
rdoc_options: []
|
86
86
|
require_paths:
|
87
87
|
- lib
|
@@ -89,15 +89,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
89
89
|
requirements:
|
90
90
|
- - ">="
|
91
91
|
- !ruby/object:Gem::Version
|
92
|
-
version: '2
|
92
|
+
version: '3.2'
|
93
93
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
94
|
requirements:
|
95
95
|
- - ">="
|
96
96
|
- !ruby/object:Gem::Version
|
97
97
|
version: '0'
|
98
98
|
requirements: []
|
99
|
-
rubygems_version: 3.
|
100
|
-
signing_key:
|
99
|
+
rubygems_version: 3.4.10
|
100
|
+
signing_key:
|
101
101
|
specification_version: 4
|
102
102
|
summary: DMPTool gem that provides support for fetching BibTex for a DOI and converting
|
103
103
|
it into a citation
|