netshade-oembed_links 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/oembed_links/formatters/hpricot_xml.rb +37 -0
- data/lib/oembed_links/formatters/lib_xml.rb +36 -0
- data/lib/oembed_links/formatters/ruby_xml.rb +34 -0
- data/lib/oembed_links/template_resolver.rb +133 -0
- data/oembed_links.gemspec +2 -2
- data/spec/templates/test.haml +1 -0
- data/spec/templates/test.html.erb +1 -0
- data/spec/templates/test.rhtml +1 -0
- metadata +9 -2
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'hpricot'
|
2
|
+
|
3
|
+
class OEmbed
|
4
|
+
module Formatters
|
5
|
+
class HpricotXML
|
6
|
+
|
7
|
+
def name
|
8
|
+
"xml"
|
9
|
+
end
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
# This is an extremely naive XML doc to hash
|
14
|
+
# formatter. Cases like arrays represented in
|
15
|
+
# XML will not work; only strings, ints and
|
16
|
+
# floats will be converted.
|
17
|
+
def format(txt)
|
18
|
+
doc = ::Hpricot.XML(txt)
|
19
|
+
h = { }
|
20
|
+
(doc/"/oembed/*").each do |elem|
|
21
|
+
if elem.is_a? Hpricot::Elem
|
22
|
+
c = elem.innerHTML
|
23
|
+
if c =~ /^[0-9]+$/
|
24
|
+
c = c.to_i
|
25
|
+
elsif c=~ /^[0-9]+\.[0-9]+$/
|
26
|
+
c = c.to_f
|
27
|
+
end
|
28
|
+
h[elem.name.strip] = c
|
29
|
+
end
|
30
|
+
end
|
31
|
+
return h
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'libxml'
|
2
|
+
|
3
|
+
class OEmbed
|
4
|
+
module Formatters
|
5
|
+
class LibXML
|
6
|
+
|
7
|
+
def name
|
8
|
+
"xml"
|
9
|
+
end
|
10
|
+
|
11
|
+
# This is an extremely naive XML doc to hash
|
12
|
+
# formatter. Cases like arrays represented in
|
13
|
+
# XML will not work; only strings, ints and
|
14
|
+
# floats will be converted.
|
15
|
+
def format(txt)
|
16
|
+
parser = ::LibXML::XML::Parser.string(txt)
|
17
|
+
doc = parser.parse
|
18
|
+
h = { }
|
19
|
+
doc.root.children.each do |node|
|
20
|
+
unless node.name.strip.empty?
|
21
|
+
c = node.content
|
22
|
+
if c =~ /^[0-9]+$/
|
23
|
+
c = c.to_i
|
24
|
+
elsif c=~ /^[0-9]+\.[0-9]+$/
|
25
|
+
c = c.to_f
|
26
|
+
end
|
27
|
+
h[node.name.strip] = c
|
28
|
+
end
|
29
|
+
end
|
30
|
+
return h
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'rexml/document'
|
2
|
+
|
3
|
+
|
4
|
+
class OEmbed
|
5
|
+
module Formatters
|
6
|
+
class RubyXML
|
7
|
+
|
8
|
+
def name
|
9
|
+
"xml"
|
10
|
+
end
|
11
|
+
|
12
|
+
# This is an extremely naive XML doc to hash
|
13
|
+
# formatter. Cases like arrays represented in
|
14
|
+
# XML will not work; only strings, ints and
|
15
|
+
# floats will be converted.
|
16
|
+
def format(txt)
|
17
|
+
doc = ::REXML::Document.new(txt)
|
18
|
+
h = { }
|
19
|
+
doc.elements.each("/oembed/*") do |elem|
|
20
|
+
c = elem.text
|
21
|
+
if c =~ /^[0-9]+$/
|
22
|
+
c = c.to_i
|
23
|
+
elsif c=~ /^[0-9]+\.[0-9]+$/
|
24
|
+
c = c.to_f
|
25
|
+
end
|
26
|
+
h[elem.name.strip] = c
|
27
|
+
end
|
28
|
+
return h
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
@@ -0,0 +1,133 @@
|
|
1
|
+
class OEmbed
|
2
|
+
# The TemplateResolver class acts as a filesystem path resolver for finding
|
3
|
+
# templates, as well as a template renderer. Currently support is enabled for
|
4
|
+
# Haml, Erubis, and ERB templates; if you wish to force a particular type of processor
|
5
|
+
# for any of the templates used, you may set it by specifing it with template_processor=
|
6
|
+
# method. You can specify a base path for resolving template names with the template_root=
|
7
|
+
# method.
|
8
|
+
class TemplateResolver
|
9
|
+
|
10
|
+
# Specify the base filesystem path for resolving templates.
|
11
|
+
def self.template_root=(r)
|
12
|
+
@template_root = r
|
13
|
+
end
|
14
|
+
|
15
|
+
# Get the current base filesystem path, or nil
|
16
|
+
def self.template_root
|
17
|
+
@template_root
|
18
|
+
end
|
19
|
+
|
20
|
+
# Specify the template processor to use for rendering templates;
|
21
|
+
# this will be used regardless of file extension
|
22
|
+
def self.template_processor=(p)
|
23
|
+
p = p.to_s if p.is_a? Symbol
|
24
|
+
raise "Unsupported processor type" unless ["erb", "haml", "erubis", nil].include?(p)
|
25
|
+
@template_processor = p
|
26
|
+
end
|
27
|
+
|
28
|
+
# Return the current forced template processor, or nil
|
29
|
+
def self.template_processor
|
30
|
+
@template_processor
|
31
|
+
end
|
32
|
+
|
33
|
+
# Resolves the template path for the given (potentially relative) path
|
34
|
+
# If the given path is found to exist immediately, whether by itself
|
35
|
+
# or when combined with the optionally present template_root (OEmbed::TemplateResolver.template_root),
|
36
|
+
# it is returned immediately. Otherwise, a check is made to see if the
|
37
|
+
# usual Rails classes (ActionView and ApplicationController) have been defined,
|
38
|
+
# and if so, detect the Rails template path via the .view_paths class
|
39
|
+
# method of ApplicationController. Furthermore, Haml support is detected
|
40
|
+
# by presence of a Haml template handler in the ActionView::Template
|
41
|
+
# class, and if it's found, given default precedence over the other template forms.
|
42
|
+
#
|
43
|
+
# If no path is found for the supplied template path, an exception
|
44
|
+
# is raised.
|
45
|
+
def self.resolve_template_path(path)
|
46
|
+
tmp_path = (@template_root) ? File.join(@template_root, path) : path
|
47
|
+
found_path = nil
|
48
|
+
if File.exists?(tmp_path)
|
49
|
+
found_path = tmp_path
|
50
|
+
else
|
51
|
+
# Rails like templates
|
52
|
+
if defined?(ApplicationController) && defined?(ActionView)
|
53
|
+
exts = ["html.erb", "erb", "rhtml"]
|
54
|
+
exts = (["haml"] + exts) if ActionView::Template.class_eval("@@template_handlers").keys.include?("haml")
|
55
|
+
unless exts.any? { |e| path =~ /\.#{e}/ }
|
56
|
+
for ext in exts
|
57
|
+
if found_path = resolve_path_in_view_paths("#{path}.#{ext}", ApplicationController.view_paths)
|
58
|
+
break
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
found_path ||= resolve_path_in_view_paths(path, ApplicationController.view_paths)
|
63
|
+
|
64
|
+
end
|
65
|
+
end
|
66
|
+
unless found_path
|
67
|
+
raise StandardError.new("File not found: #{path}")
|
68
|
+
else
|
69
|
+
return found_path
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# Evaluate the template at the given (possibly relative) path,
|
74
|
+
# assigning the template the local variables url, data and response.
|
75
|
+
# If you specify a template processor (via OEmbed::TemplateResolver.template_processor=)
|
76
|
+
# then it will be used to process the template at the given path. Otherwise
|
77
|
+
# a processor will be selected on the following criterion:
|
78
|
+
#
|
79
|
+
# If the file extension is haml, then the Haml library will be required and used
|
80
|
+
# If the Erubis library has been loaded or the file extension is erubis, the Erubis library will be used
|
81
|
+
# In all other cases, the ERB library will be used
|
82
|
+
#
|
83
|
+
# The evaluated result of the template will be returned.
|
84
|
+
def self.eval_template_for_path(path, url, data, response)
|
85
|
+
if actual_path = resolve_template_path(path)
|
86
|
+
contents = File.read(actual_path)
|
87
|
+
processor = (@template_processor || File.extname(actual_path)[1..4]).to_s
|
88
|
+
has_erubis = defined?(Erubis)
|
89
|
+
if processor == "haml"
|
90
|
+
require 'haml' unless defined?(Haml)
|
91
|
+
Haml::Engine.new(contents).render({ }, :data => data, :url => url, :response => response)
|
92
|
+
elsif processor == "erubis" || has_erubis
|
93
|
+
require 'erubis' unless has_erubis
|
94
|
+
Erubis::Eruby.new(contents).result(:data => data, :url => url, :response => response)
|
95
|
+
else
|
96
|
+
require 'erb' unless defined?(ERB)
|
97
|
+
ERBTemplate.new(url, data, response).evaluate(contents)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
private
|
103
|
+
|
104
|
+
# Resolve a relative path among an array of potential base
|
105
|
+
# paths, returning the found path if it exists or nil if
|
106
|
+
# none were found.
|
107
|
+
def self.resolve_path_in_view_paths(desired, view_paths)
|
108
|
+
view_paths.each do |p|
|
109
|
+
prop = File.join(p, desired)
|
110
|
+
if File.exists?(prop)
|
111
|
+
return prop
|
112
|
+
end
|
113
|
+
end
|
114
|
+
return nil
|
115
|
+
end
|
116
|
+
|
117
|
+
class ERBTemplate
|
118
|
+
attr_reader :url, :data, :response
|
119
|
+
|
120
|
+
def initialize(u, d, r)
|
121
|
+
@url = u
|
122
|
+
@data = d
|
123
|
+
@response = r
|
124
|
+
end
|
125
|
+
|
126
|
+
|
127
|
+
def evaluate(contents)
|
128
|
+
ERB.new(contents).result(binding)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
133
|
+
end
|
data/oembed_links.gemspec
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "oembed_links"
|
3
|
-
s.version = "0.0.
|
3
|
+
s.version = "0.0.8"
|
4
4
|
s.author = "Indianapolis Star"
|
5
5
|
s.email = "bugs at indystar dot com"
|
6
6
|
s.homepage = "http://www.indystar.com"
|
7
7
|
s.platform = Gem::Platform::RUBY
|
8
8
|
s.summary = "a library for using the OEmbed format (http://oembed.com/) to acquire embedding information for freetext"
|
9
|
-
s.files = ["Rakefile", "README", "oembed_links_example.yml", "oembed_links.gemspec", "lib", "lib/oembed_links.rb", "lib/oembed_links", "lib/oembed_links/formatters", "lib/oembed_links/fetchers", "lib/oembed_links/response.rb", "lib/oembed_links/formatters/json.rb", "lib/oembed_links/formatters/
|
9
|
+
s.files = ["Rakefile", "README", "oembed_links_example.yml", "oembed_links.gemspec", "lib", "lib/oembed_links.rb", "lib/oembed_links", "lib/oembed_links/formatters", "lib/oembed_links/fetchers", "lib/oembed_links/response.rb", "lib/oembed_links/template_resolver.rb", "lib/oembed_links/formatters/json.rb", "lib/oembed_links/formatters/lib_xml.rb", "lib/oembed_links/formatters/hpricot_xml.rb", "lib/oembed_links/formatters/ruby_xml.rb", "lib/oembed_links/fetchers/net_http.rb", "rails", "rails/init.rb", "spec", "spec/spec_helper.rb", "spec/oembed_links_spec.rb", "spec/oembed_links_test.yml", "spec/templates", "spec/templates/test.haml", "spec/templates/test.rhtml", "spec/templates/test.html.erb" ]
|
10
10
|
s.require_path = "lib"
|
11
11
|
s.has_rdoc = true
|
12
12
|
s.extra_rdoc_files = ['README']
|
@@ -0,0 +1 @@
|
|
1
|
+
= data['url'] + ' haml'
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= data['url'] %> erb
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= data['url'] %> rhtml
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: netshade-oembed_links
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Indianapolis Star
|
@@ -40,8 +40,11 @@ files:
|
|
40
40
|
- lib/oembed_links/formatters
|
41
41
|
- lib/oembed_links/fetchers
|
42
42
|
- lib/oembed_links/response.rb
|
43
|
+
- lib/oembed_links/template_resolver.rb
|
43
44
|
- lib/oembed_links/formatters/json.rb
|
44
|
-
- lib/oembed_links/formatters/
|
45
|
+
- lib/oembed_links/formatters/lib_xml.rb
|
46
|
+
- lib/oembed_links/formatters/hpricot_xml.rb
|
47
|
+
- lib/oembed_links/formatters/ruby_xml.rb
|
45
48
|
- lib/oembed_links/fetchers/net_http.rb
|
46
49
|
- rails
|
47
50
|
- rails/init.rb
|
@@ -49,6 +52,10 @@ files:
|
|
49
52
|
- spec/spec_helper.rb
|
50
53
|
- spec/oembed_links_spec.rb
|
51
54
|
- spec/oembed_links_test.yml
|
55
|
+
- spec/templates
|
56
|
+
- spec/templates/test.haml
|
57
|
+
- spec/templates/test.rhtml
|
58
|
+
- spec/templates/test.html.erb
|
52
59
|
has_rdoc: true
|
53
60
|
homepage: http://www.indystar.com
|
54
61
|
post_install_message:
|