Instiki 0.16.5
Update to Rails 2.3.2 (the stable Rails 2.3 release). Add audio/speex support Update CHANGELOG Bump version number
This commit is contained in:
parent
801d307405
commit
e2ccdfd812
264 changed files with 4850 additions and 1906 deletions
|
@ -1,7 +1,7 @@
|
|||
module ActiveSupport #:nodoc:
|
||||
module CoreExtensions #:nodoc:
|
||||
module Date #:nodoc:
|
||||
# Enables the use of time calculations within Time itself
|
||||
# Enables the use of time calculations within Date itself
|
||||
module Calculations
|
||||
def self.included(base) #:nodoc:
|
||||
base.extend ClassMethods
|
||||
|
|
|
@ -24,11 +24,12 @@ module ActiveSupport #:nodoc:
|
|||
"Bignum" => "integer",
|
||||
"BigDecimal" => "decimal",
|
||||
"Float" => "float",
|
||||
"TrueClass" => "boolean",
|
||||
"FalseClass" => "boolean",
|
||||
"Date" => "date",
|
||||
"DateTime" => "datetime",
|
||||
"Time" => "datetime",
|
||||
"TrueClass" => "boolean",
|
||||
"FalseClass" => "boolean"
|
||||
"ActiveSupport::TimeWithZone" => "datetime"
|
||||
} unless defined?(XML_TYPE_NAMES)
|
||||
|
||||
XML_FORMATTING = {
|
||||
|
|
|
@ -91,6 +91,12 @@ class HashWithIndifferentAccess < Hash
|
|||
self.dup.update(hash)
|
||||
end
|
||||
|
||||
# Performs the opposite of merge, with the keys and values from the first hash taking precedence over the second.
|
||||
# This overloaded definition prevents returning a regular hash, if reverse_merge is called on a HashWithDifferentAccess.
|
||||
def reverse_merge(other_hash)
|
||||
super other_hash.with_indifferent_access
|
||||
end
|
||||
|
||||
# Removes a specified key from the hash.
|
||||
def delete(key)
|
||||
super(convert_key(key))
|
||||
|
|
|
@ -102,8 +102,8 @@ module ActiveSupport #:nodoc:
|
|||
#
|
||||
# <%= link_to(@person.name, person_path %>
|
||||
# # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>
|
||||
def parameterize
|
||||
Inflector.parameterize(self)
|
||||
def parameterize(sep = '-')
|
||||
Inflector.parameterize(self, sep)
|
||||
end
|
||||
|
||||
# Creates the name of a table like Rails does for models to table names. This method
|
||||
|
|
|
@ -257,15 +257,17 @@ module ActiveSupport
|
|||
# <%= link_to(@person.name, person_path(@person)) %>
|
||||
# # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>
|
||||
def parameterize(string, sep = '-')
|
||||
re_sep = Regexp.escape(sep)
|
||||
# replace accented chars with ther ascii equivalents
|
||||
parameterized_string = transliterate(string)
|
||||
# Turn unwanted chars into the seperator
|
||||
parameterized_string.gsub!(/[^a-z0-9\-_\+]+/i, sep)
|
||||
# No more than one of the separator in a row.
|
||||
parameterized_string.squeeze!(sep)
|
||||
# Remove leading/trailing separator.
|
||||
parameterized_string.gsub!(/^#{re_sep}|#{re_sep}$/i, '')
|
||||
unless sep.blank?
|
||||
re_sep = Regexp.escape(sep)
|
||||
# No more than one of the separator in a row.
|
||||
parameterized_string.gsub!(/#{re_sep}{2,}/, sep)
|
||||
# Remove leading/trailing separator.
|
||||
parameterized_string.gsub!(/^#{re_sep}|#{re_sep}$/i, '')
|
||||
end
|
||||
parameterized_string.downcase
|
||||
end
|
||||
|
||||
|
|
|
@ -43,14 +43,32 @@ module ActiveSupport
|
|||
end
|
||||
|
||||
if marks.empty?
|
||||
json.gsub(/\\\//, '/')
|
||||
json.gsub(/\\([\\\/]|u[[:xdigit:]]{4})/) do
|
||||
ustr = $1
|
||||
if ustr.starts_with?('u')
|
||||
[ustr[1..-1].to_i(16)].pack("U")
|
||||
elsif ustr == '\\'
|
||||
'\\\\'
|
||||
else
|
||||
ustr
|
||||
end
|
||||
end
|
||||
else
|
||||
left_pos = [-1].push(*marks)
|
||||
right_pos = marks << scanner.pos + scanner.rest_size
|
||||
output = []
|
||||
left_pos.each_with_index do |left, i|
|
||||
scanner.pos = left.succ
|
||||
output << scanner.peek(right_pos[i] - scanner.pos + 1)
|
||||
output << scanner.peek(right_pos[i] - scanner.pos + 1).gsub(/\\([\\\/]|u[[:xdigit:]]{4})/) do
|
||||
ustr = $1
|
||||
if ustr.starts_with?('u')
|
||||
[ustr[1..-1].to_i(16)].pack("U")
|
||||
elsif ustr == '\\'
|
||||
'\\\\'
|
||||
else
|
||||
ustr
|
||||
end
|
||||
end
|
||||
end
|
||||
output = output * " "
|
||||
|
||||
|
|
|
@ -88,6 +88,10 @@ module ActiveSupport
|
|||
#{original_method}(*args) # _unmemoized_mime_type(*args)
|
||||
end # end
|
||||
end # end
|
||||
end # end
|
||||
#
|
||||
if private_method_defined?(#{original_method.inspect}) # if private_method_defined?(:_unmemoized_mime_type)
|
||||
private #{symbol.inspect} # private :mime_type
|
||||
end # end
|
||||
EOS
|
||||
end
|
||||
|
|
|
@ -344,7 +344,19 @@ module ActiveSupport #:nodoc:
|
|||
end
|
||||
alias_method :[], :slice
|
||||
|
||||
# Converts first character in the string to Unicode value
|
||||
# Like <tt>String#slice!</tt>, except instead of byte offsets you specify character offsets.
|
||||
#
|
||||
# Example:
|
||||
# s = 'こんにちは'
|
||||
# s.mb_chars.slice!(2..3).to_s #=> "にち"
|
||||
# s #=> "こんは"
|
||||
def slice!(*args)
|
||||
slice = self[*args]
|
||||
self[*args] = ''
|
||||
slice
|
||||
end
|
||||
|
||||
# Returns the codepoint of the first character in the string.
|
||||
#
|
||||
# Example:
|
||||
# 'こんにちは'.mb_chars.ord #=> 12371
|
||||
|
@ -432,7 +444,7 @@ module ActiveSupport #:nodoc:
|
|||
chars(self.class.tidy_bytes(@wrapped_string))
|
||||
end
|
||||
|
||||
%w(lstrip rstrip strip reverse upcase downcase slice tidy_bytes capitalize).each do |method|
|
||||
%w(lstrip rstrip strip reverse upcase downcase tidy_bytes capitalize).each do |method|
|
||||
define_method("#{method}!") do |*args|
|
||||
unless args.nil?
|
||||
@wrapped_string = send(method, *args).to_s
|
||||
|
|
|
@ -45,7 +45,12 @@ module ActiveSupport
|
|||
return if @method_name.to_s == "default_test"
|
||||
|
||||
if using_mocha = respond_to?(:mocha_verify)
|
||||
assertion_counter = Mocha::TestCaseAdapter::AssertionCounter.new(result)
|
||||
assertion_counter_klass = if defined?(Mocha::TestCaseAdapter::AssertionCounter)
|
||||
Mocha::TestCaseAdapter::AssertionCounter
|
||||
else
|
||||
Mocha::Integration::TestUnit::AssertionCounter
|
||||
end
|
||||
assertion_counter = assertion_counter_klass.new(result)
|
||||
end
|
||||
|
||||
yield(Test::Unit::TestCase::STARTED, name)
|
||||
|
|
|
@ -116,10 +116,10 @@ class I18nTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def test_localize_nil_raises_argument_error
|
||||
assert_raises(I18n::ArgumentError) { I18n.l nil }
|
||||
assert_raise(I18n::ArgumentError) { I18n.l nil }
|
||||
end
|
||||
|
||||
def test_localize_object_raises_argument_error
|
||||
assert_raises(I18n::ArgumentError) { I18n.l Object.new }
|
||||
assert_raise(I18n::ArgumentError) { I18n.l Object.new }
|
||||
end
|
||||
end
|
||||
|
|
|
@ -155,7 +155,7 @@ class I18nSimpleBackendTranslateTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def test_translate_given_an_array_of_inexistent_keys_it_raises_missing_translation_data
|
||||
assert_raises I18n::MissingTranslationData do
|
||||
assert_raise I18n::MissingTranslationData do
|
||||
@backend.translate('en', :does_not_exist, :scope => [:foo], :default => [:does_not_exist_2, :does_not_exist_3])
|
||||
end
|
||||
end
|
||||
|
@ -180,11 +180,11 @@ class I18nSimpleBackendTranslateTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def test_translate_given_nil_as_a_locale_raises_an_argument_error
|
||||
assert_raises(I18n::InvalidLocale){ @backend.translate nil, :bar }
|
||||
assert_raise(I18n::InvalidLocale){ @backend.translate nil, :bar }
|
||||
end
|
||||
|
||||
def test_translate_with_a_bogus_key_and_no_default_raises_missing_translation_data
|
||||
assert_raises(I18n::MissingTranslationData){ @backend.translate 'de', :bogus }
|
||||
assert_raise(I18n::MissingTranslationData){ @backend.translate 'de', :bogus }
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -230,15 +230,15 @@ class I18nSimpleBackendPluralizeTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def test_interpolate_given_incomplete_pluralization_data_raises_invalid_pluralization_data
|
||||
assert_raises(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, {:one => 'bar'}, 2) }
|
||||
assert_raise(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, {:one => 'bar'}, 2) }
|
||||
end
|
||||
|
||||
# def test_interpolate_given_a_string_raises_invalid_pluralization_data
|
||||
# assert_raises(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, 'bar', 2) }
|
||||
# assert_raise(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, 'bar', 2) }
|
||||
# end
|
||||
#
|
||||
# def test_interpolate_given_an_array_raises_invalid_pluralization_data
|
||||
# assert_raises(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, ['bar'], 2) }
|
||||
# assert_raise(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, ['bar'], 2) }
|
||||
# end
|
||||
end
|
||||
|
||||
|
@ -267,13 +267,13 @@ class I18nSimpleBackendInterpolateTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def test_interpolate_given_an_unicode_value_hash_into_a_non_unicode_multibyte_string_raises_encoding_compatibility_error
|
||||
assert_raises(Encoding::CompatibilityError) do
|
||||
assert_raise(Encoding::CompatibilityError) do
|
||||
@backend.send(:interpolate, nil, euc_jp('こんにちは、{{name}}さん!'), :name => 'ゆきひろ')
|
||||
end
|
||||
end
|
||||
|
||||
def test_interpolate_given_a_non_unicode_multibyte_value_hash_into_an_unicode_string_raises_encoding_compatibility_error
|
||||
assert_raises(Encoding::CompatibilityError) do
|
||||
assert_raise(Encoding::CompatibilityError) do
|
||||
@backend.send(:interpolate, nil, 'こんにちは、{{name}}さん!', :name => euc_jp('ゆきひろ'))
|
||||
end
|
||||
end
|
||||
|
@ -292,11 +292,11 @@ class I18nSimpleBackendInterpolateTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def test_interpolate_given_an_empty_values_hash_raises_missing_interpolation_argument
|
||||
assert_raises(I18n::MissingInterpolationArgument) { @backend.send(:interpolate, nil, 'Hi {{name}}!', {}) }
|
||||
assert_raise(I18n::MissingInterpolationArgument) { @backend.send(:interpolate, nil, 'Hi {{name}}!', {}) }
|
||||
end
|
||||
|
||||
def test_interpolate_given_a_string_containing_a_reserved_key_raises_reserved_interpolation_key
|
||||
assert_raises(I18n::ReservedInterpolationKey) { @backend.send(:interpolate, nil, '{{default}}', {:default => nil}) }
|
||||
assert_raise(I18n::ReservedInterpolationKey) { @backend.send(:interpolate, nil, '{{default}}', {:default => nil}) }
|
||||
end
|
||||
|
||||
private
|
||||
|
@ -352,11 +352,11 @@ class I18nSimpleBackendLocalizeDateTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def test_localize_nil_raises_argument_error
|
||||
assert_raises(I18n::ArgumentError) { @backend.localize 'de', nil }
|
||||
assert_raise(I18n::ArgumentError) { @backend.localize 'de', nil }
|
||||
end
|
||||
|
||||
def test_localize_object_raises_argument_error
|
||||
assert_raises(I18n::ArgumentError) { @backend.localize 'de', Object.new }
|
||||
assert_raise(I18n::ArgumentError) { @backend.localize 'de', Object.new }
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -486,7 +486,7 @@ class I18nSimpleBackendLoadTranslationsTest < Test::Unit::TestCase
|
|||
include I18nSimpleBackendTestSetup
|
||||
|
||||
def test_load_translations_with_unknown_file_type_raises_exception
|
||||
assert_raises(I18n::UnknownFileType) { @backend.load_translations "#{@locale_dir}/en.xml" }
|
||||
assert_raise(I18n::UnknownFileType) { @backend.load_translations "#{@locale_dir}/en.xml" }
|
||||
end
|
||||
|
||||
def test_load_translations_with_ruby_file_type_does_not_raise_exception
|
||||
|
|
|
@ -2,7 +2,7 @@ module ActiveSupport
|
|||
module VERSION #:nodoc:
|
||||
MAJOR = 2
|
||||
MINOR = 3
|
||||
TINY = 1
|
||||
TINY = 2
|
||||
|
||||
STRING = [MAJOR, MINOR, TINY].join('.')
|
||||
end
|
||||
|
|
|
@ -1,113 +1,31 @@
|
|||
# = XmlMini
|
||||
# This is a derivitive work of XmlSimple 1.0.11
|
||||
# Author:: Joseph Holsten <joseph@josephholsten.com>
|
||||
# Copyright:: Copyright (c) 2008 Joseph Holsten
|
||||
# Copyright:: Copyright (c) 2003-2006 Maik Schmidt <contact@maik-schmidt.de>
|
||||
# License:: Distributes under the same terms as Ruby.
|
||||
module ActiveSupport
|
||||
# = XmlMini
|
||||
#
|
||||
# To use the much faster libxml parser:
|
||||
# gem 'libxml-ruby', '=0.9.7'
|
||||
# XmlMini.backend = 'LibXML'
|
||||
module XmlMini
|
||||
extend self
|
||||
|
||||
CONTENT_KEY = '__content__'.freeze
|
||||
attr_reader :backend
|
||||
delegate :parse, :to => :backend
|
||||
|
||||
# Parse an XML Document string into a simple hash
|
||||
#
|
||||
# Same as XmlSimple::xml_in but doesn't shoot itself in the foot,
|
||||
# and uses the defaults from ActiveSupport
|
||||
#
|
||||
# string::
|
||||
# XML Document string to parse
|
||||
def parse(string)
|
||||
require 'rexml/document' unless defined?(REXML::Document)
|
||||
doc = REXML::Document.new(string)
|
||||
merge_element!({}, doc.root)
|
||||
def backend=(name)
|
||||
if name.is_a?(Module)
|
||||
@backend = name
|
||||
else
|
||||
require "active_support/xml_mini/#{name.to_s.downcase}.rb"
|
||||
@backend = ActiveSupport.const_get("XmlMini_#{name}")
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
# Convert an XML element and merge into the hash
|
||||
#
|
||||
# hash::
|
||||
# Hash to merge the converted element into.
|
||||
# element::
|
||||
# XML element to merge into hash
|
||||
def merge_element!(hash, element)
|
||||
merge!(hash, element.name, collapse(element))
|
||||
end
|
||||
|
||||
# Actually converts an XML document element into a data structure.
|
||||
#
|
||||
# element::
|
||||
# The document element to be collapsed.
|
||||
def collapse(element)
|
||||
hash = get_attributes(element)
|
||||
|
||||
if element.has_elements?
|
||||
element.each_element {|child| merge_element!(hash, child) }
|
||||
merge_texts!(hash, element) unless empty_content?(element)
|
||||
hash
|
||||
else
|
||||
merge_texts!(hash, element)
|
||||
end
|
||||
end
|
||||
|
||||
# Merge all the texts of an element into the hash
|
||||
#
|
||||
# hash::
|
||||
# Hash to add the converted emement to.
|
||||
# element::
|
||||
# XML element whose texts are to me merged into the hash
|
||||
def merge_texts!(hash, element)
|
||||
unless element.has_text?
|
||||
hash
|
||||
else
|
||||
# must use value to prevent double-escaping
|
||||
merge!(hash, CONTENT_KEY, element.texts.sum(&:value))
|
||||
end
|
||||
end
|
||||
|
||||
# Adds a new key/value pair to an existing Hash. If the key to be added
|
||||
# already exists and the existing value associated with key is not
|
||||
# an Array, it will be wrapped in an Array. Then the new value is
|
||||
# appended to that Array.
|
||||
#
|
||||
# hash::
|
||||
# Hash to add key/value pair to.
|
||||
# key::
|
||||
# Key to be added.
|
||||
# value::
|
||||
# Value to be associated with key.
|
||||
def merge!(hash, key, value)
|
||||
if hash.has_key?(key)
|
||||
if hash[key].instance_of?(Array)
|
||||
hash[key] << value
|
||||
else
|
||||
hash[key] = [hash[key], value]
|
||||
end
|
||||
elsif value.instance_of?(Array)
|
||||
hash[key] = [value]
|
||||
else
|
||||
hash[key] = value
|
||||
end
|
||||
hash
|
||||
end
|
||||
|
||||
# Converts the attributes array of an XML element into a hash.
|
||||
# Returns an empty Hash if node has no attributes.
|
||||
#
|
||||
# element::
|
||||
# XML element to extract attributes from.
|
||||
def get_attributes(element)
|
||||
attributes = {}
|
||||
element.attributes.each { |n,v| attributes[n] = v }
|
||||
attributes
|
||||
end
|
||||
|
||||
# Determines if a document element has text content
|
||||
#
|
||||
# element::
|
||||
# XML element to be checked.
|
||||
def empty_content?(element)
|
||||
element.texts.join.blank?
|
||||
end
|
||||
def with_backend(name)
|
||||
old_backend, self.backend = backend, name
|
||||
yield
|
||||
ensure
|
||||
self.backend = old_backend
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
XmlMini.backend = 'REXML'
|
||||
end
|
||||
|
|
133
vendor/rails/activesupport/lib/active_support/xml_mini/libxml.rb
vendored
Normal file
133
vendor/rails/activesupport/lib/active_support/xml_mini/libxml.rb
vendored
Normal file
|
@ -0,0 +1,133 @@
|
|||
require 'libxml'
|
||||
|
||||
# = XmlMini LibXML implementation
|
||||
module ActiveSupport
|
||||
module XmlMini_LibXML #:nodoc:
|
||||
extend self
|
||||
|
||||
# Parse an XML Document string into a simple hash using libxml.
|
||||
# string::
|
||||
# XML Document string to parse
|
||||
def parse(string)
|
||||
LibXML::XML.default_keep_blanks = false
|
||||
|
||||
if string.blank?
|
||||
{}
|
||||
else
|
||||
LibXML::XML::Parser.string(string.strip).parse.to_hash
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
module LibXML
|
||||
module Conversions
|
||||
module Document
|
||||
def to_hash
|
||||
root.to_hash
|
||||
end
|
||||
end
|
||||
|
||||
module Node
|
||||
CONTENT_ROOT = '__content__'
|
||||
LIB_XML_LIMIT = 30000000 # Hardcoded LibXML limit
|
||||
|
||||
# Convert XML document to hash
|
||||
#
|
||||
# hash::
|
||||
# Hash to merge the converted element into.
|
||||
def to_hash(hash={})
|
||||
if text?
|
||||
raise LibXML::XML::Error if content.length >= LIB_XML_LIMIT
|
||||
hash[CONTENT_ROOT] = content
|
||||
else
|
||||
sub_hash = insert_name_into_hash(hash, name)
|
||||
attributes_to_hash(sub_hash)
|
||||
if array?
|
||||
children_array_to_hash(sub_hash)
|
||||
elsif yaml?
|
||||
children_yaml_to_hash(sub_hash)
|
||||
else
|
||||
children_to_hash(sub_hash)
|
||||
end
|
||||
end
|
||||
hash
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
# Insert name into hash
|
||||
#
|
||||
# hash::
|
||||
# Hash to merge the converted element into.
|
||||
# name::
|
||||
# name to to merge into hash
|
||||
def insert_name_into_hash(hash, name)
|
||||
sub_hash = {}
|
||||
if hash[name]
|
||||
if !hash[name].kind_of? Array
|
||||
hash[name] = [hash[name]]
|
||||
end
|
||||
hash[name] << sub_hash
|
||||
else
|
||||
hash[name] = sub_hash
|
||||
end
|
||||
sub_hash
|
||||
end
|
||||
|
||||
# Insert children into hash
|
||||
#
|
||||
# hash::
|
||||
# Hash to merge the children into.
|
||||
def children_to_hash(hash={})
|
||||
each { |child| child.to_hash(hash) }
|
||||
attributes_to_hash(hash)
|
||||
hash
|
||||
end
|
||||
|
||||
# Convert xml attributes to hash
|
||||
#
|
||||
# hash::
|
||||
# Hash to merge the attributes into
|
||||
def attributes_to_hash(hash={})
|
||||
each_attr { |attr| hash[attr.name] = attr.value }
|
||||
hash
|
||||
end
|
||||
|
||||
# Convert array into hash
|
||||
#
|
||||
# hash::
|
||||
# Hash to merge the array into
|
||||
def children_array_to_hash(hash={})
|
||||
hash[child.name] = map do |child|
|
||||
returning({}) { |sub_hash| child.children_to_hash(sub_hash) }
|
||||
end
|
||||
hash
|
||||
end
|
||||
|
||||
# Convert yaml into hash
|
||||
#
|
||||
# hash::
|
||||
# Hash to merge the yaml into
|
||||
def children_yaml_to_hash(hash = {})
|
||||
hash[CONTENT_ROOT] = content unless content.blank?
|
||||
hash
|
||||
end
|
||||
|
||||
# Check if child is of type array
|
||||
def array?
|
||||
child? && child.next? && child.name == child.next.name
|
||||
end
|
||||
|
||||
# Check if child is of type yaml
|
||||
def yaml?
|
||||
attributes.collect{|x| x.value}.include?('yaml')
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
LibXML::XML::Document.send(:include, LibXML::Conversions::Document)
|
||||
LibXML::XML::Node.send(:include, LibXML::Conversions::Node)
|
77
vendor/rails/activesupport/lib/active_support/xml_mini/nokogiri.rb
vendored
Normal file
77
vendor/rails/activesupport/lib/active_support/xml_mini/nokogiri.rb
vendored
Normal file
|
@ -0,0 +1,77 @@
|
|||
require 'nokogiri'
|
||||
|
||||
# = XmlMini Nokogiri implementation
|
||||
module ActiveSupport
|
||||
module XmlMini_Nokogiri #:nodoc:
|
||||
extend self
|
||||
|
||||
# Parse an XML Document string into a simple hash using libxml / nokogiri.
|
||||
# string::
|
||||
# XML Document string to parse
|
||||
def parse(string)
|
||||
if string.blank?
|
||||
{}
|
||||
else
|
||||
doc = Nokogiri::XML(string)
|
||||
raise doc.errors.first if doc.errors.length > 0
|
||||
doc.to_hash
|
||||
end
|
||||
end
|
||||
|
||||
module Conversions
|
||||
module Document
|
||||
def to_hash
|
||||
root.to_hash
|
||||
end
|
||||
end
|
||||
|
||||
module Node
|
||||
CONTENT_ROOT = '__content__'
|
||||
|
||||
# Convert XML document to hash
|
||||
#
|
||||
# hash::
|
||||
# Hash to merge the converted element into.
|
||||
def to_hash(hash = {})
|
||||
hash[name] ||= attributes_as_hash
|
||||
|
||||
walker = lambda { |memo, parent, child, callback|
|
||||
next if child.blank? && 'file' != parent['type']
|
||||
|
||||
if child.text?
|
||||
(memo[CONTENT_ROOT] ||= '') << child.content
|
||||
next
|
||||
end
|
||||
|
||||
name = child.name
|
||||
|
||||
child_hash = child.attributes_as_hash
|
||||
if memo[name]
|
||||
memo[name] = [memo[name]].flatten
|
||||
memo[name] << child_hash
|
||||
else
|
||||
memo[name] = child_hash
|
||||
end
|
||||
|
||||
# Recusively walk children
|
||||
child.children.each { |c|
|
||||
callback.call(child_hash, child, c, callback)
|
||||
}
|
||||
}
|
||||
|
||||
children.each { |c| walker.call(hash[name], self, c, walker) }
|
||||
hash
|
||||
end
|
||||
|
||||
def attributes_as_hash
|
||||
Hash[*(attribute_nodes.map { |node|
|
||||
[node.node_name, node.value]
|
||||
}.flatten)]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Nokogiri::XML::Document.send(:include, Conversions::Document)
|
||||
Nokogiri::XML::Node.send(:include, Conversions::Node)
|
||||
end
|
||||
end
|
108
vendor/rails/activesupport/lib/active_support/xml_mini/rexml.rb
vendored
Normal file
108
vendor/rails/activesupport/lib/active_support/xml_mini/rexml.rb
vendored
Normal file
|
@ -0,0 +1,108 @@
|
|||
# = XmlMini ReXML implementation
|
||||
module ActiveSupport
|
||||
module XmlMini_REXML #:nodoc:
|
||||
extend self
|
||||
|
||||
CONTENT_KEY = '__content__'.freeze
|
||||
|
||||
# Parse an XML Document string into a simple hash
|
||||
#
|
||||
# Same as XmlSimple::xml_in but doesn't shoot itself in the foot,
|
||||
# and uses the defaults from ActiveSupport
|
||||
#
|
||||
# string::
|
||||
# XML Document string to parse
|
||||
def parse(string)
|
||||
require 'rexml/document' unless defined?(REXML::Document)
|
||||
doc = REXML::Document.new(string)
|
||||
merge_element!({}, doc.root)
|
||||
end
|
||||
|
||||
private
|
||||
# Convert an XML element and merge into the hash
|
||||
#
|
||||
# hash::
|
||||
# Hash to merge the converted element into.
|
||||
# element::
|
||||
# XML element to merge into hash
|
||||
def merge_element!(hash, element)
|
||||
merge!(hash, element.name, collapse(element))
|
||||
end
|
||||
|
||||
# Actually converts an XML document element into a data structure.
|
||||
#
|
||||
# element::
|
||||
# The document element to be collapsed.
|
||||
def collapse(element)
|
||||
hash = get_attributes(element)
|
||||
|
||||
if element.has_elements?
|
||||
element.each_element {|child| merge_element!(hash, child) }
|
||||
merge_texts!(hash, element) unless empty_content?(element)
|
||||
hash
|
||||
else
|
||||
merge_texts!(hash, element)
|
||||
end
|
||||
end
|
||||
|
||||
# Merge all the texts of an element into the hash
|
||||
#
|
||||
# hash::
|
||||
# Hash to add the converted emement to.
|
||||
# element::
|
||||
# XML element whose texts are to me merged into the hash
|
||||
def merge_texts!(hash, element)
|
||||
unless element.has_text?
|
||||
hash
|
||||
else
|
||||
# must use value to prevent double-escaping
|
||||
merge!(hash, CONTENT_KEY, element.texts.sum(&:value))
|
||||
end
|
||||
end
|
||||
|
||||
# Adds a new key/value pair to an existing Hash. If the key to be added
|
||||
# already exists and the existing value associated with key is not
|
||||
# an Array, it will be wrapped in an Array. Then the new value is
|
||||
# appended to that Array.
|
||||
#
|
||||
# hash::
|
||||
# Hash to add key/value pair to.
|
||||
# key::
|
||||
# Key to be added.
|
||||
# value::
|
||||
# Value to be associated with key.
|
||||
def merge!(hash, key, value)
|
||||
if hash.has_key?(key)
|
||||
if hash[key].instance_of?(Array)
|
||||
hash[key] << value
|
||||
else
|
||||
hash[key] = [hash[key], value]
|
||||
end
|
||||
elsif value.instance_of?(Array)
|
||||
hash[key] = [value]
|
||||
else
|
||||
hash[key] = value
|
||||
end
|
||||
hash
|
||||
end
|
||||
|
||||
# Converts the attributes array of an XML element into a hash.
|
||||
# Returns an empty Hash if node has no attributes.
|
||||
#
|
||||
# element::
|
||||
# XML element to extract attributes from.
|
||||
def get_attributes(element)
|
||||
attributes = {}
|
||||
element.attributes.each { |n,v| attributes[n] = v }
|
||||
attributes
|
||||
end
|
||||
|
||||
# Determines if a document element has text content
|
||||
#
|
||||
# element::
|
||||
# XML element to be checked.
|
||||
def empty_content?(element)
|
||||
element.texts.join.blank?
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue