uc3-dmp-citation 0.0.1 → 0.0.2
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 +68 -66
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f294fbee4a95b3148dab60d1c48b56a67ee71dfdb3d103b11814c68802c1d522
|
4
|
+
data.tar.gz: f61d50ddf7597ba1300b373450d306d7ff982b133197e40e87dddb5c3e1404bf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b395c33034a154b1052aefaf7f02d5fd6fa50e35bdc0dfe143ae1a33c7d5cddd3fdcc5e7964f930441c1354341cf72fb75e92a18db602419ff2e4bea0a7fa431
|
7
|
+
data.tar.gz: 755a385577fd2ef925df4a4671d26e6925751462ec24995726cefe483adfe512f5c048c48f8419af4d6c9af595664f46a60ae54ac15704fe5d950345e36d761f
|
data/lib/uc3-dmp-citation.rb
CHANGED
@@ -20,84 +20,86 @@ module Uc3DmpCitation
|
|
20
20
|
MSG_BIBTEX_FAILURE = 'Unable to fetch Bibtex for the specified DOI.'
|
21
21
|
MSG_UNABLE_TO_UPDATE = 'Unable to update the citations on the DMP ID.'
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
23
|
+
class << self
|
24
|
+
def fetch_citation(doi:, logger: nil)
|
25
|
+
uri = _doi_to_uri(doi: doi)
|
26
|
+
return nil if uri.nil? || uri.blank?
|
27
|
+
|
28
|
+
logger.debug(message: "Fetching BibTeX from: #{uri}") if logger.respond_to?(:debug)
|
29
|
+
resp = Uc3DmpExternalApi::Client.call(url: uri, method: :get, additional_headers: headers, logger: logger)
|
30
|
+
return nil if resp.nil? || resp.to_s.strip.empty?
|
31
|
+
|
32
|
+
bibtex = BibTeX.parse(_cleanse_bibtex(text: resp))
|
33
|
+
work_type = identifier['work_type'].nil? ? determine_work_type(bibtex: bibtex) : identifier['work_type']
|
34
|
+
_bibtex_to_citation(uri: uri, work_type: work_type, bibtex: bibtex)
|
35
|
+
end
|
35
36
|
|
36
|
-
|
37
|
+
private
|
37
38
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
39
|
+
# Will convert 'doi:10.1234/abcdefg' to 'http://doi.org/10.1234/abcdefg'
|
40
|
+
def _doi_to_uri(doi:)
|
41
|
+
val = doi.match(DOI_REGEX).to_s
|
42
|
+
return nil if val.nil? || val.strip == ''
|
42
43
|
|
43
|
-
|
44
|
-
|
44
|
+
doi.start_with?('http') ? doi : "#{DEFAULT_DOI_URL}/#{doi.gsub('doi:', '')}"
|
45
|
+
end
|
45
46
|
|
46
|
-
|
47
|
-
|
48
|
-
|
47
|
+
# If no :work_type was specified we can try to derive it from the BibTeX metadata
|
48
|
+
def _determine_work_type(bibtex:)
|
49
|
+
return '' if bibtex.nil? || bibtex.data.nil? || bibtex.data.first.nil?
|
49
50
|
|
50
|
-
|
51
|
+
return 'article' unless bibtex.data.first.journal.nil?
|
51
52
|
|
52
|
-
|
53
|
-
|
53
|
+
''
|
54
|
+
end
|
54
55
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
56
|
+
def _cleanse_bibtex(text:)
|
57
|
+
return nil if text.nil? || text.to_s.strip == ''
|
58
|
+
|
59
|
+
# Make sure we're working with UTF8
|
60
|
+
utf8 = text.force_encoding('UTF-8')
|
61
|
+
|
62
|
+
# Remove any encoded HTML (e.g. "Regular text $\\lt$strong$\\gt$Bold text$\\lt$/strong$\\gt$")
|
63
|
+
utf8 = utf8.gsub(%r{\$?\\\$?(less|lt|Lt)\$/?[a-zA-Z]+\$?\\\$?(greater|gt|Gt)\$}, '')
|
64
|
+
# Replace any special dash, semicolon and quote characters with a minus sign or single/double quote
|
65
|
+
utf8 = utf8.gsub(%r{\$?\\(T|t)ext[a-zA-Z]+dash\$?}, '-').gsub(%r{\{(T|t)ext[a-zA-Z]+dash\}}, '-')
|
66
|
+
.gsub(%r{\$?\\(M|m)athsemicolon\$?}, ':').gsub(%r{\{(M|m)semicolon\}}, ':')
|
67
|
+
.gsub(%r{\$?\\(T|t)extquotesingle\$?}, "'").gsub(%r{\{(T|t)extquotesingle\}}, "'")
|
68
|
+
.gsub(%r{\$?\\(T|t)extquotedouble\$?}, '"').gsub(%r{\{(T|t)extquotedouble\}}, '"')
|
69
|
+
# Remove any remaining `\v` entries which attempt to construct an accented character
|
70
|
+
utf8.gsub(%r{\\v}, '')
|
71
|
+
end
|
71
72
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
73
|
+
# Convert the BibTeX item to a citation
|
74
|
+
def _bibtex_to_citation(uri:, work_type: DEFAULT_WORK_TYPE, bibtex:, style: DEFAULT_CITATION_STYLE)
|
75
|
+
return nil unless uri.is_a?(String) && uri.strip != ''
|
76
|
+
return nil if bibtex.nil? || bibtex.data.nil? || bibtex.data.first.nil?
|
76
77
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
78
|
+
cp = CiteProc::Processor.new(style: style, format: 'html')
|
79
|
+
cp.import(bibtex.to_citeproc)
|
80
|
+
citation = cp.render(:bibliography, id: bibtex.data.first.id)
|
81
|
+
return nil unless citation.is_a?(Array) && citation.any?
|
81
82
|
|
82
|
-
|
83
|
-
|
84
|
-
|
83
|
+
# The CiteProc renderer has trouble with some things so fix them here
|
84
|
+
# - For some reason words in all caps in the title get wrapped in curl brackets
|
85
|
+
citation = citation.first.gsub('{', '').gsub('}', '')
|
85
86
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
87
|
+
unless work_type.nil? || work_type.strip == ''
|
88
|
+
# This supports the :apa and :chicago-author-date styles
|
89
|
+
citation = citation.gsub(/\.”\s+/, "\.” [#{work_type.gsub('_', ' ').capitalize}]. ")
|
90
|
+
.gsub(/<\/i>\.\s+/, "<\/i>\. [#{work_type.gsub('_', ' ').capitalize}]. ")
|
91
|
+
end
|
91
92
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
93
|
+
# Convert the URL into a link. Ensure that the trailing period is not a part of
|
94
|
+
# the link!
|
95
|
+
citation.gsub(URI.regexp) do |url|
|
96
|
+
if url.start_with?('http')
|
97
|
+
'<a href="%{url}" target="_blank">%{url}</a>.' % {
|
98
|
+
url: url.end_with?('.') ? uri : "#{uri}."
|
99
|
+
}
|
100
|
+
else
|
101
|
+
url
|
102
|
+
end
|
101
103
|
end
|
102
104
|
end
|
103
105
|
end
|