csl 1.0.0.pre21 → 1.0.0.pre22

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,24 +2,24 @@ module CSL
2
2
  class Locale
3
3
 
4
4
  # A localized Date comprises a set of formatting rules for dates.
5
- class Date < Node
5
+ class Date < Node
6
6
 
7
- attr_struct :form, *Schema.attr(:formatting, :delimiter)
8
- attr_children :'date-part'
7
+ attr_struct :form, *Schema.attr(:formatting, :delimiter)
8
+ attr_children :'date-part'
9
9
 
10
- alias parts date_part
11
- alias locale parent
10
+ alias parts date_part
11
+ alias locale parent
12
12
 
13
- def initialize(attributes = {})
14
- super(attributes)
15
- children[:'date-part'] = []
13
+ def initialize(attributes = {})
14
+ super(attributes)
15
+ children[:'date-part'] = []
16
16
 
17
- yield self if block_given?
18
- end
17
+ yield self if block_given?
18
+ end
19
19
 
20
- def added_to(node)
21
- raise ValidationError, "parent must be locale node: was #{node.inspect}" unless node.is_a?(Locale)
22
- end
20
+ def added_to(node)
21
+ raise ValidationError, "parent must be locale node: was #{node.inspect}" unless node.is_a?(Locale)
22
+ end
23
23
 
24
24
  def delimiter
25
25
  attributes.fetch(:delimiter, '')
@@ -46,18 +46,18 @@ module CSL
46
46
  end
47
47
  alias has_parts? has_date_parts?
48
48
 
49
- end
49
+ end
50
50
 
51
- # DatePart represent the localized formatting options for an individual
52
- # date part (day, month, or year).
53
- class DatePart < Node
51
+ # DatePart represent the localized formatting options for an individual
52
+ # date part (day, month, or year).
53
+ class DatePart < Node
54
54
  has_no_children
55
55
 
56
56
  attr_struct :name, :form, :'range-delimiter',
57
57
  *Schema.attr(:formatting, :periods)
58
58
 
59
59
  include CSL::DatePart
60
- end
60
+ end
61
61
 
62
62
 
63
63
  end
@@ -1,11 +1,11 @@
1
1
  module CSL
2
- class Locale
2
+ class Locale
3
3
 
4
- class StyleOptions < Node
5
- has_no_children
6
- attr_defaults :'punctuation-in-quote' => false,
7
- :'limit-day-ordinals-to-day-1' => false
8
- end
4
+ class StyleOptions < Node
5
+ has_no_children
6
+ attr_defaults :'punctuation-in-quote' => false,
7
+ :'limit-day-ordinals-to-day-1' => false
8
+ end
9
9
 
10
- end
10
+ end
11
11
  end
@@ -213,15 +213,22 @@ module CSL
213
213
  attr_reader :form_fallbacks
214
214
 
215
215
  def specialize(options)
216
- options = options.select do |k,v|
217
- !v.nil? && Term::Attributes.keys.include?(k.to_sym)
216
+ specialized = {}
217
+
218
+ options.each do |key, value|
219
+ key = key.to_sym
220
+
221
+ if !value.nil? && Term::Attributes.keys.include?(key)
222
+ specialized[key] = value
223
+ end
218
224
  end
219
225
 
220
- options.delete :'gender-form' unless
221
- options[:'gender-form'].to_s =~ /^masculine|feminine$/
226
+ specialized.delete :'gender-form' unless
227
+ specialized[:'gender-form'].to_s =~ /^masculine|feminine$/
222
228
 
223
- options
229
+ specialized
224
230
  end
231
+
225
232
  end
226
233
 
227
234
  # This method returns whether or not the ordinal term matchs the
data/lib/csl/node.rb CHANGED
@@ -28,17 +28,17 @@ module CSL
28
28
  @default_attributes ||= {}
29
29
  end
30
30
 
31
- def hide_default_attributes?
32
- @hide_default_attributes ||= true
33
- end
31
+ def hide_default_attributes?
32
+ !@show_default_attributes
33
+ end
34
34
 
35
- def hide_default_attributes!
36
- @hide_default_attributes = true
37
- end
35
+ def hide_default_attributes!
36
+ @show_default_attributes = false
37
+ end
38
38
 
39
- def show_default_attributes!
40
- @hide_default_attributes = false
41
- end
39
+ def show_default_attributes!
40
+ @show_default_attributes = true
41
+ end
42
42
 
43
43
  def constantize(name)
44
44
  pattern = /:#{name.to_s.tr('-', '')}$/i
@@ -246,11 +246,11 @@ module CSL
246
246
  copy
247
247
  end
248
248
 
249
- # @return [Boolean] whether or not the node has default attributes
250
- def has_default_attributes?
251
- !default_attributes.empty?
252
- end
253
- alias has_defaults? has_default_attributes?
249
+ # @return [Boolean] whether or not the node has default attributes
250
+ def has_default_attributes?
251
+ !default_attributes.empty?
252
+ end
253
+ alias has_defaults? has_default_attributes?
254
254
 
255
255
  # Iterates through the Node's attributes
256
256
  def each
@@ -263,29 +263,29 @@ module CSL
263
263
  end
264
264
  alias each_pair each
265
265
 
266
- # @param name [#to_sym] the name of the attribute
267
- # @return [Boolean] whether or not key is set to the default value
268
- def default_attribute?(name)
269
- defaults = self.class.default_attributes
270
- name, value = name.to_sym, attributes.fetch(name)
271
-
272
- return false unless !value.nil? || defaults.key?(name)
273
- defaults[name] == value
274
- end
275
-
276
- # @return [Hash] the attributes currently set to their default values
277
- def default_attributes
278
- attributes.to_hash.select do |name, _|
279
- default_attribute?(name)
280
- end
281
- end
282
-
283
- # @return [Hash] the attributes currently not set to their default values
284
- def custom_attributes
285
- attributes.to_hash.reject do |name, _|
286
- default_attribute?(name)
287
- end
288
- end
266
+ # @param name [#to_sym] the name of the attribute
267
+ # @return [Boolean] whether or not key is set to the default value
268
+ def default_attribute?(name)
269
+ defaults = self.class.default_attributes
270
+ name, value = name.to_sym, attributes.fetch(name)
271
+
272
+ return false unless !value.nil? || defaults.key?(name)
273
+ defaults[name] == value
274
+ end
275
+
276
+ # @return [Hash] the attributes currently set to their default values
277
+ def default_attributes
278
+ attributes.to_hash.select do |name, _|
279
+ default_attribute?(name)
280
+ end
281
+ end
282
+
283
+ # @return [Hash] the attributes currently not set to their default values
284
+ def custom_attributes
285
+ attributes.to_hash.reject do |name, _|
286
+ default_attribute?(name)
287
+ end
288
+ end
289
289
 
290
290
  # Returns true if the node contains an attribute with the passed-in name;
291
291
  # false otherwise.
@@ -452,9 +452,11 @@ module CSL
452
452
  private
453
453
 
454
454
  def attribute_assignments
455
- a = self.class.hide_default_attributes? ? custom_attributes : attributes
456
- a.map { |name, value|
457
- value.nil? ? nil : [name, value.to_s.inspect].join('=')
455
+ attrs = self.class.hide_default_attributes? ?
456
+ custom_attributes : attributes.to_hash
457
+
458
+ attrs.map { |name, value|
459
+ value.nil? ? nil : [name, CSL.encode_xml_attr(value.to_s)].join('=')
458
460
  }.compact
459
461
  end
460
462
 
@@ -494,7 +496,7 @@ module CSL
494
496
  attr_accessor :text
495
497
 
496
498
  def to_s
497
- text.to_s.strip
499
+ CSL.encode_xml_text text.to_s.strip
498
500
  end
499
501
 
500
502
  # TextNodes quack like a string.
@@ -530,7 +532,7 @@ module CSL
530
532
  end
531
533
 
532
534
  def tags
533
- ["<#{attribute_assignments.unshift(nodename).join(' ')}>#{text}</#{nodename}>"]
535
+ ["<#{attribute_assignments.unshift(nodename).join(' ')}>#{to_s}</#{nodename}>"]
534
536
  end
535
537
 
536
538
  def inspect
@@ -15,14 +15,14 @@ module CSL
15
15
 
16
16
  private
17
17
 
18
- def tabwidth
19
- 2
20
- end
21
-
22
- def preamble
23
- ''
24
- end
25
-
18
+ def tabwidth
19
+ 2
20
+ end
21
+
22
+ def preamble
23
+ ''
24
+ end
25
+
26
26
  def pp(tag, level = 0)
27
27
  if tag.is_a?(Array)
28
28
  tag.map { |t| pp t, level + 1 }.join("\n")
data/lib/csl/schema.rb CHANGED
@@ -2,10 +2,17 @@ module CSL
2
2
 
3
3
  class Schema
4
4
 
5
- @version = '1.0.1'.freeze
5
+ @version = '1.0.1'.freeze
6
+ @major_version = '1.0'.freeze
7
+
6
8
  @namespace = 'http://purl.org/net/xbiblio/csl'.freeze
7
9
  @preamble = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".freeze
8
10
 
11
+ @default_license = 'http://creativecommons.org/licenses/by-sa/3.0/'
12
+ @default_rights_string =
13
+ "This work is licensed under a Creative Commons Attribution-Share Alike 3.0 License: #{@default_license}"
14
+
15
+
9
16
  @types = %w{ article article-journal article-magazine article-newspaper
10
17
  bill book broadcast chapter entry entry-dictionary entry-encyclopedia
11
18
  figure graphic interview legal_case legislation manuscript map
@@ -84,15 +91,20 @@ module CSL
84
91
  names-delimiter
85
92
  },
86
93
  :conditionals => %w{
87
- disambiguate is-numeric is-uncertain-date locator
88
- position type variable
94
+ disambiguate position
95
+ is-numeric is-numeric-any is-numeric-all is-numeric-none
96
+ is-uncertain-date is-uncertain-date-any is-uncertain-date-all
97
+ is-uncertain-date-none
98
+ locator locator-any locator-all locator-none
99
+ type type-any type-all type-none
100
+ variable variable-any variable-all variable-none
89
101
  }
90
102
  })
91
103
 
92
104
  @attributes.each_value { |v| v.map!(&:to_sym).freeze }
93
105
 
94
- @attributes[:formatting] = [:'text-case'].concat(
95
- @attributes.values_at(:affixes, :font).flatten)
106
+ @attributes[:formatting] = [:'text-case'].concat(
107
+ @attributes.values_at(:affixes, :font).flatten)
96
108
 
97
109
  @attributes.freeze
98
110
 
@@ -131,8 +143,9 @@ module CSL
131
143
 
132
144
  class << self
133
145
 
134
- attr_accessor :version, :namespace, :types, :variables, :categories,
135
- :attributes, :preamble, :values
146
+ attr_accessor :version, :major_version, :namespace, :types,
147
+ :variables, :categories, :attributes, :preamble, :values,
148
+ :default_rights_string, :default_license
136
149
 
137
150
  private :new
138
151
 
data/lib/csl/style.rb CHANGED
@@ -20,9 +20,10 @@ module CSL
20
20
  end
21
21
  end
22
22
 
23
- attr_defaults :version => Schema.version, :xmlns => Schema.namespace
23
+ attr_defaults :version => Schema.major_version,
24
+ :xmlns => Schema.namespace
24
25
 
25
- show_default_attributes!
26
+ show_default_attributes!
26
27
 
27
28
  attr_struct :xmlns, :version, :class, :'default-locale',
28
29
  :'initialize-with-hyphen', :'page-range-format',
@@ -39,11 +40,13 @@ module CSL
39
40
  alias has_macros? has_macro?
40
41
 
41
42
  def_delegators :info, :self_link, :self_link=, :has_self_link?,
42
- :template_link, :template_link=, :has_template_link?,
43
+ :self_link!, :template_link, :template_link=, :has_template_link?,
43
44
  :documentation_link, :documentation_link=, :has_documentation_link?,
44
45
  :independent_parent_link, :independent_parent_link=,
45
46
  :has_independent_parent_link?, :title=, :id=, :has_title?, :has_id?,
46
- :published_at, :updated_at, :citation_format, :citation_format=
47
+ :published_at, :updated_at, :citation_format, :citation_format=,
48
+ :updated_at, :update!, :license, :license=, :default_license?,
49
+ :default_license!
47
50
 
48
51
  def initialize(attributes = {})
49
52
  super(attributes, &nil)
@@ -81,16 +84,16 @@ module CSL
81
84
  alias_child :metadata, :info
82
85
 
83
86
  # @return [String] the style's id
84
- def id
85
- return unless info.has_id?
86
- info.id.to_s
87
- end
87
+ def id
88
+ return unless info.has_id?
89
+ info.id.to_s
90
+ end
88
91
 
89
92
  # @return [String] the style's title
90
- def title
91
- return unless info.has_title?
92
- info.title.to_s
93
- end
93
+ def title
94
+ return unless info.has_title?
95
+ info.title.to_s
96
+ end
94
97
 
95
98
  alias has_template? has_template_link?
96
99
 
@@ -15,13 +15,15 @@ module CSL
15
15
  end
16
16
 
17
17
  def conditions
18
- attributes_for(*Schema.attr(:conditionals)).map { |type, values|
19
- values.to_s.split(/\s+/).map { |value| [type, value] }
20
- }.flatten(1)
18
+ attributes_for(*Schema.attr(:conditionals)).map do |name, values|
19
+ extract_type_and_matcher_from(name) << values.to_s.split(/\s+/)
20
+ end
21
21
  end
22
22
 
23
- def matcher
24
- case attributes[:match]
23
+ def matcher(match = nil)
24
+ match ||= attributes[:match]
25
+
26
+ case match
25
27
  when 'any'
26
28
  :any?
27
29
  when 'none'
@@ -29,7 +31,14 @@ module CSL
29
31
  else
30
32
  :all?
31
33
  end
32
- end
34
+ end
35
+
36
+ private
37
+
38
+ def extract_type_and_matcher_from(attribute)
39
+ type, match = attribute.to_s.split(/-(any|all|none)$/, 2)
40
+ [type.to_sym, matcher(match)]
41
+ end
33
42
  end
34
43
  end
35
44
 
@@ -36,7 +36,6 @@ module CSL
36
36
  end
37
37
 
38
38
  def has_variable?
39
- return parent.has_variable? if names_label?
40
39
  attribute?(:variable)
41
40
  end
42
41
 
@@ -44,9 +43,8 @@ module CSL
44
43
  # is the child of a {Names} node, returns the parent's variable
45
44
  # attribute instead.
46
45
  #
47
- # @return [String] the value of the node's variable attribute
46
+ # @return [String, nil] the value of the node's variable attribute
48
47
  def variable
49
- return parent.variable if name_label?
50
48
  attributes[:variable]
51
49
  end
52
50
 
@@ -2,11 +2,11 @@ module CSL
2
2
  class Style
3
3
 
4
4
  class Layout < Node
5
- attr_struct *Schema.attr(:affixes, :font, :delimiter)
5
+ attr_struct(*Schema.attr(:affixes, :font, :delimiter))
6
6
 
7
- def delimiter
8
- attributes.fetch(:delimiter, '')
9
- end
7
+ def delimiter
8
+ attributes.fetch(:delimiter, '')
9
+ end
10
10
  end
11
11
 
12
12
  end
@@ -16,12 +16,16 @@ module CSL
16
16
  yield self if block_given?
17
17
  end
18
18
 
19
+ def delimiter
20
+ attributes.fetch(:delimiter, '')
21
+ end
22
+
19
23
  def has_variable?
20
24
  attribute?(:variable)
21
25
  end
22
26
 
23
27
  def variable
24
- attributes[:variable]
28
+ attributes[:variable].to_s
25
29
  end
26
30
 
27
31
  end