mbrao 1.5.0 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -4
  3. data/.rubocop.yml +51 -3
  4. data/.travis-gemfile +5 -5
  5. data/.travis.yml +5 -3
  6. data/CHANGELOG.md +5 -0
  7. data/Gemfile +10 -9
  8. data/README.md +1 -2
  9. data/Rakefile +22 -6
  10. data/doc/ActionView.html +3 -3
  11. data/doc/ActionView/Template.html +3 -3
  12. data/doc/ActionView/Template/Handlers.html +3 -3
  13. data/doc/ActionView/Template/Handlers/MbraoTemplate.html +3 -3
  14. data/doc/HTML.html +3 -3
  15. data/doc/HTML/Pipeline.html +3 -3
  16. data/doc/HTML/Pipeline/KramdownFilter.html +3 -3
  17. data/doc/Mbrao.html +5 -5
  18. data/doc/Mbrao/Author.html +10 -10
  19. data/doc/Mbrao/Content.html +825 -844
  20. data/doc/Mbrao/ContentInterface.html +5 -5
  21. data/doc/Mbrao/ContentInterface/ClassMethods.html +4 -4
  22. data/doc/Mbrao/Exceptions.html +3 -3
  23. data/doc/Mbrao/Exceptions/InvalidDate.html +3 -3
  24. data/doc/Mbrao/Exceptions/InvalidMetadata.html +3 -3
  25. data/doc/Mbrao/Exceptions/Parsing.html +3 -3
  26. data/doc/Mbrao/Exceptions/Rendering.html +3 -3
  27. data/doc/Mbrao/Exceptions/UnavailableLocalization.html +3 -3
  28. data/doc/Mbrao/Exceptions/Unimplemented.html +3 -3
  29. data/doc/Mbrao/Exceptions/UnknownEngine.html +3 -3
  30. data/doc/Mbrao/Parser.html +3 -3
  31. data/doc/Mbrao/ParserInterface.html +3 -3
  32. data/doc/Mbrao/ParserInterface/ClassMethods.html +35 -95
  33. data/doc/Mbrao/ParserValidations.html +3 -3
  34. data/doc/Mbrao/ParserValidations/ClassMethods.html +3 -3
  35. data/doc/Mbrao/ParsingEngines.html +3 -3
  36. data/doc/Mbrao/ParsingEngines/Base.html +3 -3
  37. data/doc/Mbrao/ParsingEngines/PlainText.html +11 -17
  38. data/doc/Mbrao/RenderingEngines.html +3 -3
  39. data/doc/Mbrao/RenderingEngines/Base.html +3 -3
  40. data/doc/Mbrao/RenderingEngines/HtmlPipeline.html +24 -92
  41. data/doc/Mbrao/Version.html +4 -4
  42. data/doc/_index.html +8 -8
  43. data/doc/class_list.html +5 -1
  44. data/doc/file.README.html +5 -6
  45. data/doc/file_list.html +5 -1
  46. data/doc/frames.html +1 -1
  47. data/doc/index.html +5 -6
  48. data/doc/js/full_list.js +4 -1
  49. data/doc/method_list.html +75 -53
  50. data/doc/top-level-namespace.html +3 -3
  51. data/lib/mbrao/author.rb +1 -1
  52. data/lib/mbrao/content.rb +47 -65
  53. data/lib/mbrao/content_interface.rb +5 -17
  54. data/lib/mbrao/parser.rb +4 -10
  55. data/lib/mbrao/parser_interface.rb +9 -19
  56. data/lib/mbrao/parsing_engines/plain_text.rb +22 -58
  57. data/lib/mbrao/rendering_engines/html_pipeline.rb +9 -26
  58. data/lib/mbrao/version.rb +1 -1
  59. data/mbrao.gemspec +9 -9
  60. data/spec/mbrao/content_spec.rb +4 -4
  61. data/spec/mbrao/integrations/rails_spec.rb +4 -4
  62. data/spec/mbrao/parser_spec.rb +15 -15
  63. data/spec/spec_helper.rb +19 -5
  64. metadata +17 -19
  65. data/spec/coverage_helper.rb +0 -20
@@ -6,7 +6,7 @@
6
6
  <title>
7
7
  Top Level Namespace
8
8
 
9
- &mdash; Documentation by YARD 0.8.7.4
9
+ &mdash; Documentation by YARD 0.8.7.6
10
10
 
11
11
  </title>
12
12
 
@@ -103,9 +103,9 @@
103
103
  </div>
104
104
 
105
105
  <div id="footer">
106
- Generated on Sun May 11 18:17:16 2014 by
106
+ Generated on Tue Mar 29 12:47:22 2016 by
107
107
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
108
- 0.8.7.4 (ruby-2.1.0).
108
+ 0.8.7.6 (ruby-2.3.0).
109
109
  </div>
110
110
 
111
111
  </body>
@@ -39,7 +39,7 @@ module Mbrao
39
39
  @email = Mbrao::Parser.email?(email) ? email : nil
40
40
  @website = Mbrao::Parser.url?(website) ? website : nil
41
41
  @image = Mbrao::Parser.url?(image) ? image : nil
42
- @metadata = metadata.ensure_hash(:indifferent)
42
+ @metadata = metadata.ensure_hash(accesses: :indifferent)
43
43
  end
44
44
 
45
45
  # Returns the author as an Hash.
@@ -56,7 +56,7 @@ module Mbrao
56
56
  #
57
57
  # @param value [Array] The new value for the attribute. A empty or "*" will be the default value.
58
58
  def locales=(value)
59
- @locales = value.ensure_array(nil, true, true, true) { |l| l.ensure_string.strip }
59
+ @locales = value.ensure_array(no_duplicates: true, compact: true, flatten: true) { |l| l.ensure_string.strip }
60
60
  end
61
61
 
62
62
  # Sets the `title` attribute.
@@ -64,7 +64,7 @@ module Mbrao
64
64
  # @param new_title [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas.
65
65
  # A empty or "*" will be the default value.
66
66
  def title=(new_title)
67
- @title = hash?(new_title) ? new_title.ensure_hash(:indifferent, nil, :ensure_string) : new_title.ensure_string
67
+ @title = hash?(new_title) ? new_title.ensure_hash(accesses: :indifferent, default: nil, sanitizer: :ensure_string) : new_title.ensure_string
68
68
  end
69
69
 
70
70
  # Sets the `summary` attribute.
@@ -72,7 +72,7 @@ module Mbrao
72
72
  # @param new_summary [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas.
73
73
  # A empty or "*" will be the default value.
74
74
  def summary=(new_summary)
75
- @summary = hash?(new_summary) ? new_summary.ensure_hash(:indifferent, nil, :ensure_string) : new_summary.ensure_string
75
+ @summary = hash?(new_summary) ? new_summary.ensure_hash(accesses: :indifferent, default: nil, sanitizer: :ensure_string) : new_summary.ensure_string
76
76
  end
77
77
 
78
78
  # Sets the `body` attribute.
@@ -87,7 +87,7 @@ module Mbrao
87
87
  # @param new_tags [Array|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas.
88
88
  # A empty or "*" will be the default value. Tags can also be comma-separated.
89
89
  def tags=(new_tags)
90
- @tags = hash?(new_tags) ? new_tags.ensure_hash(:indifferent) { |v| parse_tags(v) } : parse_tags(new_tags)
90
+ @tags = hash?(new_tags) ? new_tags.ensure_hash(accesses: :indifferent) { |v| parse_tags(v) } : parse_tags(new_tags)
91
91
  end
92
92
 
93
93
  # Sets the `more` attribute.
@@ -95,7 +95,7 @@ module Mbrao
95
95
  # @param new_more [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas.
96
96
  # A empty or "*" will be the default value.
97
97
  def more=(new_more)
98
- @more = hash?(new_more) ? new_more.ensure_hash(:indifferent, nil, :ensure_string) : new_more.ensure_string
98
+ @more = hash?(new_more) ? new_more.ensure_hash(accesses: :indifferent, default: nil, sanitizer: :ensure_string) : new_more.ensure_string
99
99
  end
100
100
 
101
101
  # Sets the `author` attribute.
@@ -105,8 +105,8 @@ module Mbrao
105
105
  @author =
106
106
  if new_author.is_a?(Mbrao::Author)
107
107
  new_author
108
- elsif hash?(new_author) then
109
- Mbrao::Author.create(new_author.ensure_hash(:indifferent))
108
+ elsif hash?(new_author)
109
+ Mbrao::Author.create(new_author.ensure_hash(accesses: :indifferent))
110
110
  else
111
111
  new_author ? Mbrao::Author.new(new_author.ensure_string) : nil
112
112
  end
@@ -130,15 +130,44 @@ module Mbrao
130
130
  #
131
131
  # @param new_metadata [Hash] The new value for the attribute.
132
132
  def metadata=(new_metadata)
133
- @metadata = hash?(new_metadata) ? new_metadata.ensure_hash(:indifferent) : @metadata = HashWithIndifferentAccess.new({raw: new_metadata})
133
+ @metadata = hash?(new_metadata) ? new_metadata.ensure_hash(accesses: :indifferent) : @metadata = HashWithIndifferentAccess.new({raw: new_metadata})
134
134
  end
135
135
 
136
- private
136
+ # Assigns metadata to a content
137
+ #
138
+ # @param content [Content] The content to manipulate.
139
+ # @param metadata [Hash] The metadata to assign.
140
+ # @return [Content] The content with metadata.
141
+ def self.assign_metadata(content, metadata)
142
+ [:uid, :title, :summary, :tags, :more, :created_at, :updated_at].each do |field|
143
+ content.send("#{field}=", metadata.delete(field))
144
+ end
137
145
 
138
- # Extracts a date and time from a value.
146
+ content.author = Mbrao::Author.create(metadata.delete(:author))
147
+ content.locales = extract_locales(metadata.delete(:locales))
148
+ content.metadata = metadata
149
+ end
150
+
151
+ # Extracts locales from metadata.
139
152
  #
140
- # @param value [String|DateTime|Fixnum] The value to parse.
141
- # @return [DateTime] The extracted value.
153
+ # @param locales [String] The list of locales.
154
+ # @return [Array] The locales.
155
+ def self.extract_locales(locales)
156
+ locales = locales.ensure_string.split(/\s*,\s*/) unless locales.is_a?(::Array)
157
+ normalize_locales(locales)
158
+ end
159
+
160
+ # Normalizes locales for further usage.
161
+ #
162
+ # @param locales [Array] The locales to normalize.
163
+ # @return [Array] The normalized locales.
164
+ def self.normalize_locales(locales)
165
+ locales.flatten.map(&:ensure_string).map(&:strip).uniq
166
+ end
167
+
168
+ private
169
+
170
+ # :nodoc:
142
171
  def extract_datetime(value)
143
172
  value = parse_datetime(value) if value
144
173
  value ? value.utc : nil
@@ -146,10 +175,7 @@ module Mbrao
146
175
  raise Mbrao::Exceptions::InvalidDate
147
176
  end
148
177
 
149
- # Parses a datetime.
150
- #
151
- # @param value [String|DateTime|Fixnum] The value to parse.
152
- # @return [DateTime] The extracted value.
178
+ # :nodoc:
153
179
  def parse_datetime(value)
154
180
  rv =
155
181
  case value.class.to_s
@@ -163,19 +189,13 @@ module Mbrao
163
189
  rv
164
190
  end
165
191
 
166
- # Parses a datetime from a number.
167
- #
168
- # @param value [String] The value to parse.
169
- # @return [Float|Fixnum] The extracted value.
192
+ # :nodoc:
170
193
  def parse_datetime_number(value)
171
194
  number = value.to_float
172
195
  number > 0 ? Time.at(number).to_datetime : nil
173
196
  end
174
197
 
175
- # Parses a datetime from a string.
176
- #
177
- # @param value [String] The value to parse.
178
- # @return [DateTime] The extracted value.
198
+ # :nodoc:
179
199
  def parse_datetime_string(value)
180
200
  value = value.ensure_string
181
201
 
@@ -190,52 +210,14 @@ module Mbrao
190
210
  end
191
211
  end
192
212
 
193
- # Extracts tags from an array, making sure all the comma separated strings are evaluated.
194
- #
195
- # @param value [String|Array] The string or array to parse.
196
- # @return [Array] The list of tags.
213
+ # :nodoc:
197
214
  def parse_tags(value)
198
- value.ensure_array(nil, true, true, true) { |v| v.ensure_string.split(/\s*,\s*/) }
215
+ value.ensure_array(no_duplicates: true, compact: true, flatten: true) { |v| v.ensure_string.split(/\s*,\s*/) }
199
216
  end
200
217
 
201
- # Check if value is an Hash.
202
- #
203
- # @param value [Object] The object to check.
204
- # @return [Boolean] `true` if value is an Hash, `false` otherwise
218
+ # :nodoc:
205
219
  def hash?(value)
206
220
  value.is_a?(Hash)
207
221
  end
208
-
209
- # Assigns metadata to a content
210
- #
211
- # @param content [Content] The content to manipulate.
212
- # @param metadata [Hash] The metadata to assign.
213
- # @return [Content] The content with metadata.
214
- def self.assign_metadata(content, metadata)
215
- [:uid, :title, :summary, :tags, :more, :created_at, :updated_at].each do |field|
216
- content.send("#{field}=", metadata.delete(field))
217
- end
218
-
219
- content.author = Mbrao::Author.create(metadata.delete(:author))
220
- content.locales = extract_locales(metadata.delete(:locales))
221
- content.metadata = metadata
222
- end
223
-
224
- # Extracts locales from metadata.
225
- #
226
- # @param locales [String] The list of locales.
227
- # @return [Array] The locales.
228
- def self.extract_locales(locales)
229
- locales = locales.ensure_string.split(/\s*,\s*/) unless locales.is_a?(::Array)
230
- normalize_locales(locales)
231
- end
232
-
233
- # Normalizes locales for further usage.
234
- #
235
- # @param locales [Array] The locales to normalize.
236
- # @return [Array] The normalized locales.
237
- def self.normalize_locales(locales)
238
- locales.flatten.map(&:ensure_string).map(&:strip).uniq
239
- end
240
222
  end
241
223
  end
@@ -19,7 +19,7 @@ module Mbrao
19
19
 
20
20
  "%F %T.%L", "%F %T", "%F %H:%M", "%F",
21
21
  "%d/%m/%Y %T.%L", "%d/%m/%Y %T", "%d/%m/%Y %H:%M", "%d/%m/%Y"
22
- ]
22
+ ].freeze
23
23
 
24
24
  # Class methods.
25
25
  module ClassMethods
@@ -30,7 +30,7 @@ module Mbrao
30
30
  # @param content [Content|nil] An optional content to check for availability
31
31
  # @return [Array] The validated list of locales.
32
32
  def validate_locales(locales, content = nil)
33
- locales = locales.ensure_array(nil, true, true, true) { |l| l.ensure_string.strip }
33
+ locales = locales.ensure_array(no_duplicates: true, compact: true, flatten: true) { |l| l.ensure_string.strip }
34
34
  locales = (locales.empty? ? [Mbrao::Parser.locale] : locales)
35
35
  raise Mbrao::Exceptions::UnavailableLocalization if content && !content.enabled_for_locales?(locales)
36
36
  locales
@@ -54,7 +54,7 @@ module Mbrao
54
54
  # @param locales [Array] The desired locales. Can include `*` to match all. If none are specified, the default mbrao locale will be used.
55
55
  # @return [Boolean] `true` if the content is available for at least one of the desired locales, `false` otherwise.
56
56
  def enabled_for_locales?(*locales)
57
- locales = locales.flatten.ensure_array(nil, false, false, true) { |l| l.ensure_string.strip }.reject { |l| l == "*" }
57
+ locales = locales.flatten.ensure_array(flatten: true) { |l| l.ensure_string.strip }.reject { |l| l == "*" }
58
58
  @locales.blank? || locales.blank? || (@locales & locales).present?
59
59
  end
60
60
 
@@ -110,12 +110,7 @@ module Mbrao
110
110
 
111
111
  private
112
112
 
113
- # Filters an attribute basing a set of locales.
114
- #
115
- # @param attribute [Object|HashWithIndifferentAccess] The desired attribute.
116
- # @param locales [String|Array] The desired locales. Can include `*` to match all. If none are specified, the default mbrao locale will be used.
117
- # @return [String|HashWithIndifferentAccess] Return the object for desired locales. If only one locale is available, then only a object is returned,
118
- # else a `HashWithIndifferentAccess` with locales as keys.
113
+ # :nodoc:
119
114
  def filter_attribute_for_locales(attribute, locales)
120
115
  locales = ::Mbrao::Content.validate_locales(locales, self)
121
116
 
@@ -127,14 +122,7 @@ module Mbrao
127
122
  end
128
123
  end
129
124
 
130
- # Adds an value on a hash if enable for requested locales.
131
- #
132
- # @param hash [Hash] The hash to handle.
133
- # @param locale [String] The list of locale (separated by commas) for which the value is available.
134
- # @param locales [Array] The list of locale for which this value is requested. Can include `*` to match all. If none are specified, the default mbrao
135
- # locale will be used.
136
- # @param value [Object] The value to add.
137
- # @return [Hash] Return the original hash.
125
+ # :nodoc:
138
126
  def append_value_for_locale(hash, locale, locales, value)
139
127
  locale.split(/\s*,\s*/).map(&:strip).each do |l|
140
128
  hash[l] = value if locales.include?("*") || locales.include?(l)
@@ -35,12 +35,9 @@ module Mbrao
35
35
 
36
36
  private
37
37
 
38
- # Sanitizes options for parsing.
39
- #
40
- # @param options [Hash] The options to sanitize.
41
- # @return [HashWithIndifferentAccess] The sanitized options.
38
+ # :nodoc:
42
39
  def sanitize_parsing_options(options)
43
- options = options.ensure_hash(:symbols)
40
+ options = options.ensure_hash(accesses: :symbols)
44
41
 
45
42
  options[:engine] ||= Mbrao::Parser.parsing_engine
46
43
  options[:metadata] = options.fetch(:metadata, true).to_boolean
@@ -49,12 +46,9 @@ module Mbrao
49
46
  HashWithIndifferentAccess.new(options)
50
47
  end
51
48
 
52
- # Sanitizes options for rendering.
53
- #
54
- # @param options [Hash] The options to sanitize.
55
- # @return [HashWithIndifferentAccess] The sanitized options.
49
+ # :nodoc:
56
50
  def sanitize_rendering_options(options)
57
- options = options.ensure_hash(:symbols)
51
+ options = options.ensure_hash(accesses: :symbols)
58
52
 
59
53
  options[:engine] ||= Mbrao::Parser.rendering_engine
60
54
 
@@ -75,8 +75,8 @@ module Mbrao
75
75
  # @return [Hash] An hash with all attributes.
76
76
  def as_json(target, keys, options = {})
77
77
  include_empty = !options[:exclude_empty].to_boolean
78
- exclude = options[:exclude].ensure_array(nil, true, true, true, :ensure_string)
79
- keys = keys.ensure_array(nil, true, true, true, :ensure_string)
78
+ exclude = options[:exclude].ensure_array(no_duplicates: true, compact: true, flatten: true, sanitizer: :ensure_string)
79
+ keys = keys.ensure_array(no_duplicates: true, compact: true, flatten: true, sanitizer: :ensure_string)
80
80
 
81
81
  map_to_json(target, (keys - exclude), include_empty)
82
82
  end
@@ -104,35 +104,25 @@ module Mbrao
104
104
 
105
105
  private
106
106
 
107
- # Returns an attribute or a default value.
108
- #
109
- # @param attr [Object ]The attribute to return.
110
- # @param default_value [Object] The value to return if `attr` is blank.
111
- # @param sanitizer [Symbol] An optional method to sanitize the returned value.
107
+ # :nodoc:
112
108
  def attribute_or_default(attr, default_value = nil, sanitizer = :ensure_string)
113
109
  rv = attr.present? ? attr : default_value
114
110
  rv = rv.send(sanitizer) if sanitizer
115
111
  rv
116
112
  end
117
113
 
118
- # Perform the mapping to JSON.
119
- #
120
- # @param target [Object] The target to serialize.
121
- # @param keys [Array] The attributes to include in the serialization.
122
- # @param include_empty [Boolean], if to include nil values.
123
- # @return [Hash] An hash with all attributes.
114
+ # :nodoc:
124
115
  def map_to_json(target, keys, include_empty)
125
- keys.reduce({}) { |rv, key|
116
+ json = keys.reduce({}) do |rv, key|
126
117
  value = get_json_field(target, key)
127
118
  rv[key] = value if include_empty || value.present?
128
119
  rv
129
- }.deep_stringify_keys
120
+ end
121
+
122
+ json.deep_stringify_keys
130
123
  end
131
124
 
132
- # Get a field as JSON.
133
- #
134
- # @param target [Object] The object containing the value.
135
- # @param method [Symbol] The method containing the value.
125
+ # :nodoc:
136
126
  def get_json_field(target, method)
137
127
  value = target.send(method)
138
128
  value = value.as_json if value && value.respond_to?(:as_json) && !value.is_a?(Symbol)
@@ -40,11 +40,8 @@ module Mbrao
40
40
  raise ArgumentError unless rv.is_a?(Hash)
41
41
  rv
42
42
  rescue => e
43
- if options[:default]
44
- options[:default]
45
- else
46
- raise ::Mbrao::Exceptions::InvalidMetadata, e.to_s
47
- end
43
+ raise(::Mbrao::Exceptions::InvalidMetadata, e.to_s) unless options[:default]
44
+ options[:default]
48
45
  end
49
46
 
50
47
  # Filters content of a post by locale.
@@ -68,11 +65,7 @@ module Mbrao
68
65
 
69
66
  private
70
67
 
71
- # Prepare arguments for separation.
72
- #
73
- # @param content [String] The content to separate.
74
- # @param options [Hash] The options to sanitize.
75
- # @return [Array] The sanitized arguments.
68
+ # :nodoc:
76
69
  def prepare_for_separation(content, options)
77
70
  content = content.ensure_string.strip
78
71
  meta_tags = sanitize_tags(options[:meta_tags], ["{{metadata}}", "{{/metadata}}"])
@@ -80,21 +73,13 @@ module Mbrao
80
73
  [nil, content.ensure_string.strip, StringScanner.new(content), meta_tags.first, meta_tags.last]
81
74
  end
82
75
 
83
- # Sanitizes tag markers.
84
- #
85
- # @param tag [Array|String] The tag to sanitize.
86
- # @return [Array] Sanitized tags.
76
+ # :nodoc:
87
77
  def sanitize_tags(tag, default = ["---"])
88
78
  tag = tag.ensure_string.split(/\s*,\s*/).map(&:strip) if tag && !tag.is_a?(Array)
89
79
  (tag.present? ? tag : default).slice(0, 2).map { |t| /#{Regexp.quote(t).gsub("%ARGS%", "\\s*(?<args>[^\\n\\}]+,?)*")}/ }
90
80
  end
91
81
 
92
- # Scans a text and content section.
93
- #
94
- # @param content [String] The string to scan
95
- # @param start_tag [Regexp] The tag to match for starting section.
96
- # @param end_tag [Regexp] The tag to match for ending section.
97
- # @return [String] The result of the scan.
82
+ # :nodoc:
98
83
  def scan_content(content, start_tag, end_tag)
99
84
  rv = []
100
85
  scanner = StringScanner.new(content)
@@ -105,12 +90,7 @@ module Mbrao
105
90
  rv
106
91
  end
107
92
 
108
- # Perform a scan on the text and content.
109
- #
110
- # @param rv [String] The string where to put the results.
111
- # @param scanner [StringScanner] The scanner to use.
112
- # @param start_tag [Regexp] The tag to match for starting section.
113
- # @param end_tag [Regexp] The tag to match for ending section.
93
+ # :nodoc:
114
94
  def perform_scan(rv, scanner, start_tag, end_tag)
115
95
  if scanner.exist?(start_tag) # It may start an embedded content
116
96
  # Scan until the start tag, remove the tag from the match and then store to results.
@@ -130,24 +110,13 @@ module Mbrao
130
110
  end
131
111
  end
132
112
 
133
- # Gets results for an embedded content.
134
- #
135
- # @param [String] starting The match starting expression.
136
- # @param [String] embedded The embedded contents.
137
- # @param start_tag [Regexp] The tag to match for starting section.
138
- # @param end_tag [Regexp] The tag to match for ending section.
139
- # @return [Array] An array which the first element is the list of valid contents and second is the list of valid locales.
113
+ # :nodoc:
140
114
  def get_embedded_content(starting, embedded, start_tag, end_tag)
141
115
  # Either we have some content or the content was not closed and therefore we ignore this tag.
142
116
  embedded.present? ? [scan_content(embedded, start_tag, end_tag), starting.match(start_tag)["args"]] : [starting, "*"]
143
117
  end
144
118
 
145
- # Parses embedded content of a tag
146
- #
147
- # @param scanner [StringScanner] The scanner to use.
148
- # @param start_tag [Regexp] The tag to match for starting section.
149
- # @param end_tag [Regexp] The tag to match for ending section.
150
- # @return [String] The embedded content or `nil`, if the content was never closed.
119
+ # :nodoc:
151
120
  def parse_embedded_content(scanner, start_tag, end_tag)
152
121
  rv = ""
153
122
  balance = 1
@@ -155,7 +124,7 @@ module Mbrao
155
124
 
156
125
  while balance > 0 && embedded_part
157
126
  balance += embedded_part.scan(start_tag).count - 1 # -1 Because there is a closure
158
- embedded_part = embedded_part.partition(end_tag).first if balance == 0 || !scanner.exist?(end_tag) # This is the last occurrence.
127
+ embedded_part = extract_embedded_part(scanner, embedded_part, end_tag, balance) # This is the last occurrence.
159
128
  rv << embedded_part
160
129
  embedded_part = scanner.scan_until(end_tag) if balance > 0
161
130
  end
@@ -163,28 +132,27 @@ module Mbrao
163
132
  rv
164
133
  end
165
134
 
166
- # Filters content by locale.
167
- #
168
- # @param content [Array] The content to filter. @see #scan_content.
169
- # @param locales [Array] The desired locales. Can include `*` to match all.
170
- # @return [String|nil] Return the filtered content or `nil` if the content must be hidden.
135
+ # :nodoc:
136
+ def extract_embedded_part(scanner, embedded_part, end_tag, balance)
137
+ embedded_part = embedded_part.partition(end_tag).first if balance == 0 || !scanner.exist?(end_tag)
138
+ embedded_part
139
+ end
140
+
141
+ # :nodoc:
171
142
  def perform_filter_content(content, locales)
172
- content.map { |part|
143
+ rv = content.map do |part|
173
144
  part_content = part[0]
174
145
  part_locales = parse_locales(part[1])
175
146
 
176
147
  if locales_valid?(locales, part_locales)
177
148
  part_content.is_a?(Array) ? perform_filter_content(part_content, locales) : part_content
178
- else
179
- nil
180
149
  end
181
- }.compact.join("")
150
+ end
151
+
152
+ rv.compact.join("")
182
153
  end
183
154
 
184
- # Parses locales of a content.
185
- #
186
- # @param locales [String] The desired locales. Can include `*` to match all. Note that `!*` is ignored.
187
- # @return [Hash] An hash with valid and invalid locales.
155
+ # :nodoc:
188
156
  def parse_locales(locales)
189
157
  types = locales.split(/\s*,\s*/).map(&:strip).group_by { |l| l !~ /^!/ ? "valid" : "invalid" }
190
158
  types["valid"] ||= []
@@ -192,11 +160,7 @@ module Mbrao
192
160
  types
193
161
  end
194
162
 
195
- # Checks if all locales in a list are valid for a part.
196
- #
197
- # @param locales [Array] The desired locales. Can include `*` to match all.
198
- # @param part_locales[Hash] An hash with valid and invalid locales.
199
- # @return [Boolean] `true` if the locales are valid, `false` otherwise.
163
+ # :nodoc:
200
164
  def locales_valid?(locales, part_locales)
201
165
  valid = part_locales["valid"]
202
166
  invalid = part_locales["invalid"]