nanoc-embed 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +37 -0
  2. data/lib/nanoc-embed.rb +56 -0
  3. metadata +58 -0
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # Embed
2
+
3
+ All to embed many type of content directly in your page.
4
+
5
+ It use [ruby-oembed](https://github.com/judofyr/ruby-oembed).
6
+
7
+ ## Install
8
+
9
+ gem install nanoc-embed
10
+
11
+ If you use bundler, add it to your Gemfile:
12
+
13
+ gem "nanoc-snippet", "~> 0.0.1"
14
+
15
+ ## Usage
16
+
17
+ Add to *lib/default.rb*:
18
+
19
+ ```ruby
20
+ require 'nanoc-embed'
21
+ ```
22
+
23
+ Add a filter at the compile time to use it:
24
+
25
+ ```ruby
26
+ compile '*' do
27
+ filter :embed
28
+ filter :erb
29
+ layout 'default'
30
+ end
31
+ ```
32
+
33
+ ## License
34
+
35
+ (c) 2012 Stormz
36
+
37
+ This code is free to use under the terms of the MIT license
@@ -0,0 +1,56 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'uri'
3
+ require 'oembed'
4
+
5
+ skitch = OEmbed::Provider.new("https://skitch.com/oembed")
6
+ skitch << "http://*.skitch.com/*"
7
+ skitch << "https://*.skitch.com/*"
8
+ OEmbed::Providers.register(OEmbed::Providers::Youtube, skitch)
9
+
10
+ class EmbedFilter < Nanoc::Filter
11
+ identifier :embed
12
+ type :text
13
+
14
+ def initialize(hash={})
15
+ super
16
+ @cache_file = ".oembed_cache"
17
+ @cache = open_cache
18
+ end
19
+
20
+ def run(content, params={})
21
+ urls = URI.extract(content, ['http', 'https'])
22
+ content2 = content.dup
23
+ urls.each do |url|
24
+ html = @cache.fetch(url, nil)
25
+ if html.nil?
26
+ html = get_oembed(url)
27
+ end
28
+ if !html.nil?
29
+ content2.gsub!(url, html)
30
+ end
31
+ @cache.store(url, html)
32
+ end
33
+ save_cache @cache
34
+ content2
35
+ end
36
+
37
+ def get_oembed(url)
38
+ resource = OEmbed::Providers.get(url)
39
+ resource.html
40
+ rescue OEmbed::NotFound => e
41
+ url
42
+ end
43
+
44
+ def open_cache
45
+ if File.exists?(@cache_file)
46
+ File.open(@cache_file) {|f| Marshal.load(f)}
47
+ else
48
+ Hash.new
49
+ end
50
+ end
51
+
52
+ def save_cache(cache)
53
+ File.open(@cache_file, 'w') {|f| Marshal.dump(cache, f)}
54
+ end
55
+
56
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nanoc-embed
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - François de Metz
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: ruby-oembed
16
+ requirement: &21911780 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *21911780
25
+ description:
26
+ email: francois@2metz.fr
27
+ executables: []
28
+ extensions: []
29
+ extra_rdoc_files:
30
+ - README.md
31
+ files:
32
+ - README.md
33
+ - lib/nanoc-embed.rb
34
+ homepage: https://github.com/stormz/nanoc-plugins
35
+ licenses: []
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 1.8.10
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: All to embed many type of content directly in your page.
58
+ test_files: []