crudley implements implied properties. needs more specs
This commit is contained in:
parent
1d8e46ce95
commit
3f2a627816
8 changed files with 189 additions and 2 deletions
|
@ -12,6 +12,10 @@ require "microformats2/property/url"
|
|||
require "microformats2/property/date_time"
|
||||
require "microformats2/property/embedded"
|
||||
require "microformats2/property"
|
||||
require "microformats2/implied_property/foundation"
|
||||
require "microformats2/implied_property/name"
|
||||
require "microformats2/implied_property/photo"
|
||||
require "microformats2/implied_property/url"
|
||||
|
||||
module Microformats2
|
||||
class << self
|
||||
|
|
|
@ -23,13 +23,33 @@ module Microformats2
|
|||
end
|
||||
|
||||
def properties
|
||||
@properties ||= PropertyParser.parse(@element.children).each do |property|
|
||||
@properties ||= parse_properties.concat parse_implied_properties
|
||||
end
|
||||
|
||||
def parse_properties
|
||||
PropertyParser.parse(@element.children).each do |property|
|
||||
save_property_name(property.method_name)
|
||||
define_method(property.method_name)
|
||||
set_value(property.method_name, property)
|
||||
end
|
||||
end
|
||||
|
||||
def parse_implied_properties
|
||||
ip = []
|
||||
ip << ImpliedProperty::Name.new(@element).parse unless property_present?(:name)
|
||||
ip << ImpliedProperty::Photo.new(@element).parse unless property_present?(:photo)
|
||||
ip << ImpliedProperty::Url.new(@element).parse unless property_present?(:url)
|
||||
ip.compact.each do |property|
|
||||
save_property_name(property.method_name)
|
||||
define_method(property.method_name)
|
||||
set_value(property.method_name, property)
|
||||
end
|
||||
end
|
||||
|
||||
def property_present?(property)
|
||||
!! respond_to?(property) && send(property)
|
||||
end
|
||||
|
||||
def to_hash
|
||||
hash = { type: format_types, properties: {} }
|
||||
@property_names.each do |method_name|
|
||||
|
|
50
lib/microformats2/implied_property/foundation.rb
Normal file
50
lib/microformats2/implied_property/foundation.rb
Normal file
|
@ -0,0 +1,50 @@
|
|||
module Microformats2
|
||||
module ImpliedProperty
|
||||
class Foundation
|
||||
def initialize(element)
|
||||
@element = element
|
||||
end
|
||||
|
||||
def parse
|
||||
self if value
|
||||
end
|
||||
|
||||
def method_name
|
||||
"foundation"
|
||||
end
|
||||
|
||||
def value
|
||||
@value ||= element_value
|
||||
end
|
||||
|
||||
def to_hash
|
||||
value.to_s
|
||||
end
|
||||
|
||||
def to_json
|
||||
to_hash.to_json
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def element_value
|
||||
ev = nil
|
||||
attr_map.each_pair do |k, v|
|
||||
selected_elements = @element.css(k)
|
||||
if selected_elements.first
|
||||
ev ||= selected_elements.first.attribute(v).to_s
|
||||
end
|
||||
end
|
||||
ev
|
||||
end
|
||||
|
||||
def attribute
|
||||
attr_map[@element.name]
|
||||
end
|
||||
|
||||
def attr_map
|
||||
{}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
31
lib/microformats2/implied_property/name.rb
Normal file
31
lib/microformats2/implied_property/name.rb
Normal file
|
@ -0,0 +1,31 @@
|
|||
module Microformats2
|
||||
module ImpliedProperty
|
||||
class Name < Foundation
|
||||
|
||||
def method_name
|
||||
"name"
|
||||
end
|
||||
|
||||
def value
|
||||
@value ||= element_value || text_value
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def attr_map
|
||||
{ "img[alt]" => "alt",
|
||||
"abbr[title]" => "title",
|
||||
">img[alt]:only-of-type" => "alt",
|
||||
">abbr[title]:only-of-type" => "title",
|
||||
">:only-of-type>img[alt]:only-of-type" => "alt",
|
||||
">:only-of-type>abbr[title]:only-of-type" => "title" }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def text_value
|
||||
@element.inner_text.gsub(/\n+/, " ").gsub(/\s+/, " ").strip
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
21
lib/microformats2/implied_property/photo.rb
Normal file
21
lib/microformats2/implied_property/photo.rb
Normal file
|
@ -0,0 +1,21 @@
|
|||
module Microformats2
|
||||
module ImpliedProperty
|
||||
class Photo < Foundation
|
||||
|
||||
def method_name
|
||||
"photo"
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def attr_map
|
||||
{ "img[src]" => "src",
|
||||
"object[data]" => "data",
|
||||
">img[src]:only-of-type" => "src",
|
||||
">object[data]:only-of-type" => "data",
|
||||
">:only-child>img[src]:only-of-type" => "src",
|
||||
">:only-child>object[data]:only-of-type" => "data" }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
17
lib/microformats2/implied_property/url.rb
Normal file
17
lib/microformats2/implied_property/url.rb
Normal file
|
@ -0,0 +1,17 @@
|
|||
module Microformats2
|
||||
module ImpliedProperty
|
||||
class Url < Foundation
|
||||
|
||||
def method_name
|
||||
"url"
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def attr_map
|
||||
{ "a[href]" => "href",
|
||||
">a[href]:only-of-type" => "href" }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue