citeproc 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/.travis.yml +13 -0
- data/.yardopts +2 -0
- data/AGPL +662 -0
- data/{LICENSE → BSDL} +2 -1
- data/Gemfile +13 -2
- data/README.md +32 -1
- data/Rakefile +39 -0
- data/citeproc.gemspec +18 -11
- data/lib/citeproc.rb +13 -11
- data/lib/citeproc/abbreviate.rb +1 -4
- data/lib/citeproc/assets.rb +11 -4
- data/lib/citeproc/attributes.rb +102 -95
- data/lib/citeproc/bibliography.rb +66 -42
- data/lib/citeproc/citation_data.rb +57 -46
- data/lib/citeproc/compatibility.rb +161 -4
- data/lib/citeproc/date.rb +517 -225
- data/lib/citeproc/engine.rb +0 -2
- data/lib/citeproc/errors.rb +4 -4
- data/lib/citeproc/extensions.rb +6 -5
- data/lib/citeproc/item.rb +85 -22
- data/lib/citeproc/names.rb +642 -543
- data/lib/citeproc/variable.rb +149 -70
- data/lib/citeproc/version.rb +1 -1
- data/spec/citeproc/assets_spec.rb +10 -4
- data/spec/citeproc/date_spec.rb +274 -8
- data/spec/citeproc/item_spec.rb +23 -4
- data/spec/citeproc/names_spec.rb +601 -486
- data/spec/citeproc/variable_spec.rb +4 -12
- data/spec/spec_helper.rb +13 -0
- metadata +64 -31
@@ -1,49 +1,14 @@
|
|
1
|
-
|
2
1
|
module CiteProc
|
3
2
|
|
4
|
-
#
|
5
|
-
#
|
6
|
-
# Typically a Bibliography is returned by the Processor. It contains a list
|
7
|
-
# of references and formatting options.
|
3
|
+
# Typically a {Bibliography} is returned by a {Processor} instance. It
|
4
|
+
# contains a list of references and formatting options.
|
8
5
|
#
|
9
6
|
# A Bibliography can be created from (and exported to) CiteProc JSON
|
10
|
-
# bibliographies; these consist of a
|
7
|
+
# bibliographies; these consist of a two-element list, composed of a
|
11
8
|
# JavaScript array containing certain formatting parameters, and a list of
|
12
9
|
# strings representing bibliography entries.
|
13
10
|
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
# :offset => 0
|
17
|
-
# Some citation styles apply a label (either a number or an alphanumeric
|
18
|
-
# code) to each bibliography entry, and use this label to cite
|
19
|
-
# bibliography items in the main text. In the bibliography, the labels
|
20
|
-
# may either be hung in the margin, or they may be set flush to the
|
21
|
-
# margin, with the citations indented by a uniform amount to the right.
|
22
|
-
# In the latter case, the amount of indentation needed depends on the
|
23
|
-
# maximum width of any label. The :offset option gives the maximum
|
24
|
-
# number of characters that appear in any label used in the
|
25
|
-
# bibliography. The client that controls the final rendering of the
|
26
|
-
# bibliography string should use this value to calculate and apply a
|
27
|
-
# suitable indentation length.
|
28
|
-
#
|
29
|
-
# :entry_spacing => 0
|
30
|
-
# An integer representing the spacing between entries in the
|
31
|
-
# bibliography.
|
32
|
-
#
|
33
|
-
# :line_spacing => 0
|
34
|
-
# An integer representing the spacing between the lines within each
|
35
|
-
# bibliography entry.
|
36
|
-
#
|
37
|
-
# :indent => 0
|
38
|
-
# The number of em-spaces to apply in hanging indents within the
|
39
|
-
# bibliography.
|
40
|
-
#
|
41
|
-
# :align => false
|
42
|
-
# When the second-field-align CSL option is set, this returns either
|
43
|
-
# "flush" or "margin". The calling application should align text in
|
44
|
-
# bibliography output as described in the CSL specification. Where
|
45
|
-
# second-field-align is not set, this return value is set to false.
|
46
|
-
#
|
11
|
+
# See {.defaults} for the default formatting options.
|
47
12
|
class Bibliography
|
48
13
|
|
49
14
|
@defaults = {
|
@@ -72,7 +37,46 @@ module CiteProc
|
|
72
37
|
|
73
38
|
class << self
|
74
39
|
|
75
|
-
|
40
|
+
# @!attribute [r] defaults
|
41
|
+
# @example Default Formatting Options
|
42
|
+
# {
|
43
|
+
# :offset => 0,
|
44
|
+
# # Some citation styles apply a label (either a number or an
|
45
|
+
# # alphanumeric code) to each bibliography entry, and use this
|
46
|
+
# # label to cite bibliography items in the main text. In the
|
47
|
+
# # bibliography, the labels may either be hung in the margin,
|
48
|
+
# # or they may be set flush to the margin, with the citations
|
49
|
+
# # indented by a uniform amount to the right. In the latter case,
|
50
|
+
# # the amount of indentation needed depends on the maximum width
|
51
|
+
# # of any label. The :offset option gives the maximum number of
|
52
|
+
# # characters that appear in any label used in the bibliography.
|
53
|
+
# # The client that controls the final rendering of the bibliography
|
54
|
+
# # string should use this value to calculate and apply a suitable
|
55
|
+
# # indentation length.
|
56
|
+
#
|
57
|
+
# :entry_spacing => 0,
|
58
|
+
# # An integer representing the spacing between entries in the
|
59
|
+
# # bibliography.
|
60
|
+
#
|
61
|
+
# :line_spacing => 0,
|
62
|
+
# # An integer representing the spacing between the lines within
|
63
|
+
# # each bibliography entry.
|
64
|
+
#
|
65
|
+
# :indent => 0,
|
66
|
+
# # The number of em-spaces to apply in hanging indents within the
|
67
|
+
# # bibliography.
|
68
|
+
#
|
69
|
+
# :align => false
|
70
|
+
# # When the second-field-align CSL option is set, this returns
|
71
|
+
# # either "flush" or "margin". The calling application should
|
72
|
+
# # align text in the bibliography output as described by the CSL
|
73
|
+
# # specification. Where second-field-align is not set, this
|
74
|
+
# # return value is set to false.
|
75
|
+
# }
|
76
|
+
# @return [Hash] default formatting options
|
77
|
+
attr_reader :defaults
|
78
|
+
|
79
|
+
attr_reader :cp2rb, :rb2cp
|
76
80
|
|
77
81
|
# Create a new Bibliography from the passed-in string or array, or nil
|
78
82
|
# if the input cannot be parsed.
|
@@ -115,10 +119,29 @@ module CiteProc
|
|
115
119
|
|
116
120
|
extend Forwardable
|
117
121
|
|
118
|
-
|
122
|
+
# @!attribute [r] refrences
|
123
|
+
# @return [Array<String>] the list of references
|
124
|
+
attr_reader :references
|
125
|
+
|
126
|
+
# @!attribute [r] options
|
127
|
+
# @see .defaults
|
128
|
+
# @return [Hash] the current formatting options
|
129
|
+
attr_reader :options
|
130
|
+
|
131
|
+
# @!attribute [r] errors
|
132
|
+
# @todo not implemented yet
|
133
|
+
# @return [Array<String>] a list of errors
|
134
|
+
attr_reader :errors
|
119
135
|
|
136
|
+
# @!attribute prefix
|
137
|
+
# @return [String] content included before the reference list
|
120
138
|
attr_accessor :prefix, :suffix
|
121
139
|
|
140
|
+
# @!attribute suffix
|
141
|
+
# @return [String] content included after the reference list
|
142
|
+
attr_accessor :suffix
|
143
|
+
|
144
|
+
|
122
145
|
# Bibliographies quack sorta like an Array
|
123
146
|
def_delegators :@references, :length, :empty?, :[], :include?, :index
|
124
147
|
|
@@ -150,7 +173,7 @@ module CiteProc
|
|
150
173
|
alias errors? has_errors?
|
151
174
|
|
152
175
|
def join(connector = "\n")
|
153
|
-
[prefix, references.join(connector), suffix].join
|
176
|
+
[prefix, references.join(connector), suffix].compact.join
|
154
177
|
end
|
155
178
|
|
156
179
|
def each
|
@@ -190,6 +213,7 @@ module CiteProc
|
|
190
213
|
MultiJson.encode(to_citeproc)
|
191
214
|
end
|
192
215
|
|
216
|
+
# @return [String] a human-readable representation of the bibliography
|
193
217
|
def inspect
|
194
218
|
"#<CiteProc::Bibliography @references=[#{references.length}], @errors=[#{errors.length}]>"
|
195
219
|
end
|
@@ -1,29 +1,9 @@
|
|
1
1
|
module CiteProc
|
2
2
|
|
3
|
-
#
|
4
|
-
# methods. In order to be processed correctly, an item must
|
5
|
-
# id attribute used to retrieve the
|
6
|
-
#
|
7
|
-
#
|
8
|
-
# * locator: a string identifying a page number or other pinpoint location
|
9
|
-
# or range within the resource;
|
10
|
-
#
|
11
|
-
# * label: a label type, indicating whether the locator is to a page, a
|
12
|
-
# chapter, or other subdivision of the target resource. Valid labels are
|
13
|
-
# defined in CitationItem.labels
|
14
|
-
#
|
15
|
-
# * suppress-author: if true, author names will not be included in the
|
16
|
-
# citation output for this cite;
|
17
|
-
#
|
18
|
-
# * author-only: if true, only the author name will be included in the
|
19
|
-
# citation output for this cite -- this optional parameter provides a
|
20
|
-
# means for certain demanding styles that require the processor output
|
21
|
-
# to be divided between the main text and a footnote.
|
22
|
-
#
|
23
|
-
# * prefix: a string to print before this cite item;
|
24
|
-
#
|
25
|
-
# * suffix: a string to print after this cite item.
|
26
|
-
#
|
3
|
+
# A {CitationItem} consititues the main input elements to CiteProc's
|
4
|
+
# processing methods. In order to be processed correctly, an item must
|
5
|
+
# have a valid {#id} attribute used to retrieve the correpsonding {Item}
|
6
|
+
# containing the actual bibliographic data.
|
27
7
|
class CitationItem
|
28
8
|
|
29
9
|
include Attributes
|
@@ -37,27 +17,58 @@ module CiteProc
|
|
37
17
|
attr_reader :labels
|
38
18
|
end
|
39
19
|
|
20
|
+
# @!attribute id
|
21
|
+
# @return [Symbol,String] the id of the corresponding resource
|
22
|
+
|
23
|
+
# @!attribute locator
|
24
|
+
# @return [String] a string identifying a page number or similar to mark
|
25
|
+
# a location or range within the resource
|
26
|
+
|
27
|
+
# @!attribute label
|
28
|
+
# Labels indicate whether the current locator is to a page, a chapter,
|
29
|
+
# or other subdivision of the target resource. Valid labels are defined
|
30
|
+
# by {.labels}.
|
31
|
+
# @return [Symbol,String] the label type
|
32
|
+
|
33
|
+
# @!attribute suppress_author
|
34
|
+
# @return [Boolean] whether or not author names will not be included
|
35
|
+
# in the citation output for this cite
|
36
|
+
|
37
|
+
# @!attribute author_only
|
38
|
+
# This optional parameter provides a means for certain demanding styles
|
39
|
+
# that require the processor output to be divided between the main text
|
40
|
+
# and a footnote.
|
41
|
+
# @return [Boolean] whether or not only the author name will be included
|
42
|
+
# in the citation output for this cite
|
43
|
+
|
44
|
+
# @!attribute prefix
|
45
|
+
# @return [String] a string to print before cites produced for this item
|
46
|
+
|
47
|
+
# @!attribute suffix
|
48
|
+
# @return [String] a string to print after cites produced for this item
|
49
|
+
|
40
50
|
attr_predicates :id, :locator, :label, :'suppress-author',
|
41
51
|
:'author-only', :prefix, :suffix
|
42
|
-
|
43
|
-
#
|
52
|
+
|
53
|
+
# Attributes added by processor
|
44
54
|
attr_predicates :sortkeys, :postion, :'first-reference-note-number',
|
45
55
|
:'near-note', :unsorted
|
46
|
-
|
56
|
+
|
47
57
|
attr_accessor :data
|
48
|
-
|
58
|
+
|
49
59
|
def initialize(attributes = nil)
|
50
60
|
merge(attributes)
|
51
61
|
end
|
52
|
-
|
62
|
+
|
53
63
|
def initialize_copy(other)
|
54
64
|
@attributes = other.attributes.deep_copy
|
55
65
|
end
|
56
|
-
|
66
|
+
|
67
|
+
# @return [String] a human-readable representation of the citation item
|
57
68
|
def inspect
|
58
|
-
"#<CiteProc::CitationItem #{id.to_s}, #{locator.to_s}
|
69
|
+
"#<CiteProc::CitationItem #{id.to_s.inspect}, #{locator.to_s.inspect}>"
|
59
70
|
end
|
60
|
-
|
71
|
+
|
61
72
|
end
|
62
73
|
|
63
74
|
|
@@ -102,17 +113,7 @@ module CiteProc
|
|
102
113
|
self
|
103
114
|
end
|
104
115
|
end
|
105
|
-
|
106
|
-
def each
|
107
|
-
if block_given?
|
108
|
-
items.each(&Proc.new)
|
109
|
-
self
|
110
|
-
else
|
111
|
-
to_enum
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
|
116
|
+
|
116
117
|
def initialize(attributes = nil, options = {})
|
117
118
|
@options = CitationData.defaults.merge(options)
|
118
119
|
@items, @sorted_items = [], []
|
@@ -125,7 +126,7 @@ module CiteProc
|
|
125
126
|
@sorted_items = other.items.map(&:dup)
|
126
127
|
@id = other.id.dup if other.processed?
|
127
128
|
end
|
128
|
-
|
129
|
+
|
129
130
|
def merge(other)
|
130
131
|
return self if other.nil?
|
131
132
|
|
@@ -151,16 +152,25 @@ module CiteProc
|
|
151
152
|
options.merge!(convert_from_citeproc(Hash[properties])) unless properties.nil?
|
152
153
|
|
153
154
|
@id = other[:id] if other.has_key?(:id)
|
154
|
-
|
155
|
+
|
155
156
|
self
|
156
157
|
end
|
157
158
|
|
158
159
|
alias update merge
|
159
160
|
|
161
|
+
def each
|
162
|
+
if block_given?
|
163
|
+
items.each(&Proc.new)
|
164
|
+
self
|
165
|
+
else
|
166
|
+
to_enum
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
160
170
|
def processed?
|
161
171
|
!!id
|
162
172
|
end
|
163
|
-
|
173
|
+
|
164
174
|
def index
|
165
175
|
options[:footnote]
|
166
176
|
end
|
@@ -186,6 +196,7 @@ module CiteProc
|
|
186
196
|
|
187
197
|
alias to_s to_json
|
188
198
|
|
199
|
+
# @return [String] a human-readable representation of the citation data
|
189
200
|
def inspect
|
190
201
|
"#<CiteProc::CitationData items=[#{length}]>"
|
191
202
|
end
|
@@ -1,10 +1,12 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
1
3
|
unless Symbol.is_a?(Comparable)
|
2
4
|
class Symbol
|
3
5
|
include Comparable
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
6
|
+
|
7
|
+
def =~(pattern)
|
8
|
+
to_s =~ pattern
|
9
|
+
end
|
8
10
|
|
9
11
|
def <=>(other)
|
10
12
|
return nil unless other.is_a?(Symbol)
|
@@ -12,3 +14,158 @@ unless Symbol.is_a?(Comparable)
|
|
12
14
|
end
|
13
15
|
end
|
14
16
|
end
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
if RUBY_VERSION < '1.9'
|
21
|
+
require 'enumerator'
|
22
|
+
|
23
|
+
$KCODE = 'u'
|
24
|
+
require 'jcode'
|
25
|
+
|
26
|
+
module CiteProc
|
27
|
+
|
28
|
+
def to_unicode(string)
|
29
|
+
string.gsub(/\\?u([\da-f]{4})/i) { |m| [$1.to_i(16)].pack('U') }
|
30
|
+
end
|
31
|
+
|
32
|
+
def ruby_18
|
33
|
+
yield
|
34
|
+
end
|
35
|
+
|
36
|
+
def ruby_19
|
37
|
+
false
|
38
|
+
end
|
39
|
+
|
40
|
+
begin
|
41
|
+
require 'oniguruma'
|
42
|
+
|
43
|
+
def oniguruma
|
44
|
+
Oniguruma::ORegexp.new(yield, :syntax => Oniguruma::SYNTAX_JAVA, :encoding => Oniguruma::ENCODING_UTF8)
|
45
|
+
end
|
46
|
+
|
47
|
+
def oniguruma?
|
48
|
+
true
|
49
|
+
end
|
50
|
+
rescue LoadError
|
51
|
+
def oniguruma
|
52
|
+
false
|
53
|
+
end
|
54
|
+
alias oniguruma? oniguruma
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# Remove the 'id' and 'type' methods in Ruby 1.8
|
59
|
+
class Object
|
60
|
+
undef_method :id
|
61
|
+
undef_method :type
|
62
|
+
end
|
63
|
+
|
64
|
+
else
|
65
|
+
|
66
|
+
module CiteProc
|
67
|
+
def to_unicode(string)
|
68
|
+
string
|
69
|
+
end
|
70
|
+
|
71
|
+
def ruby_18
|
72
|
+
false
|
73
|
+
end
|
74
|
+
|
75
|
+
def ruby_19
|
76
|
+
yield
|
77
|
+
end
|
78
|
+
|
79
|
+
def oniguruma
|
80
|
+
Regexp.new(yield)
|
81
|
+
end
|
82
|
+
|
83
|
+
def oniguruma?
|
84
|
+
true
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
#
|
90
|
+
# Robust unicode upcase/downcase
|
91
|
+
#
|
92
|
+
|
93
|
+
if RUBY_PLATFORM == 'java'
|
94
|
+
require 'java'
|
95
|
+
|
96
|
+
puts java.lang.System.getProperty('file.encoding')
|
97
|
+
|
98
|
+
module CiteProc
|
99
|
+
def upcase(string)
|
100
|
+
java.lang.String.new(string).to_upper_case(java.util.Locale::ENGLISH).to_s
|
101
|
+
end
|
102
|
+
|
103
|
+
def downcase(string)
|
104
|
+
java.lang.String.new(string).to_lower_case(java.util.Locale::ENGLISH).to_s
|
105
|
+
end
|
106
|
+
|
107
|
+
# def oniguruma
|
108
|
+
# Regexp.new(yield)
|
109
|
+
# end
|
110
|
+
#
|
111
|
+
# def oniguruma?
|
112
|
+
# true
|
113
|
+
# end
|
114
|
+
end
|
115
|
+
|
116
|
+
else
|
117
|
+
|
118
|
+
module CiteProc
|
119
|
+
private
|
120
|
+
|
121
|
+
begin
|
122
|
+
require 'unicode'
|
123
|
+
|
124
|
+
def upcase(string)
|
125
|
+
Unicode.upcase(string)
|
126
|
+
end
|
127
|
+
|
128
|
+
def downcase(string)
|
129
|
+
Unicode.downcase(string)
|
130
|
+
end
|
131
|
+
rescue LoadError
|
132
|
+
begin
|
133
|
+
require 'unicode_utils'
|
134
|
+
|
135
|
+
def upcase(string)
|
136
|
+
UnicodeUtils.upcase(string)
|
137
|
+
end
|
138
|
+
|
139
|
+
def downcase(string)
|
140
|
+
UnicodeUtils.downcase(string)
|
141
|
+
end
|
142
|
+
rescue LoadError
|
143
|
+
begin
|
144
|
+
require 'active_support/multibyte/chars'
|
145
|
+
|
146
|
+
def upcase(string)
|
147
|
+
ActiveSupport::Multibyte::Chars.new(string).upcase.to_s
|
148
|
+
end
|
149
|
+
|
150
|
+
def downcase(string)
|
151
|
+
ActiveSupport::Multibyte::Chars.new(string).downcase.to_s
|
152
|
+
end
|
153
|
+
rescue LoadError
|
154
|
+
|
155
|
+
def upcase(string)
|
156
|
+
string.upcase
|
157
|
+
end
|
158
|
+
|
159
|
+
def downcase(string)
|
160
|
+
string.downcase
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
module CiteProc
|
169
|
+
module_function :ruby_18, :ruby_19, :oniguruma, :oniguruma?,
|
170
|
+
:to_unicode, :upcase, :downcase
|
171
|
+
end
|