shortcodes 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE.txt +22 -0
- data/README.md +58 -0
- data/Rakefile +1 -0
- data/lib/shortcodes/all.rb +4 -0
- data/lib/shortcodes/handler.rb +22 -0
- data/lib/shortcodes/parser.rb +47 -0
- data/lib/shortcodes/version.rb +3 -0
- data/lib/shortcodes/wufoo.rb +50 -0
- data/lib/shortcodes/youtube.rb +36 -0
- data/lib/shortcodes.rb +17 -0
- metadata +144 -0
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Amiel Martin
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# Shortcodes
|
2
|
+
|
3
|
+
Wordpress style shortcodes for your Ruby CMS.
|
4
|
+
|
5
|
+
A simple ruby gem that will parse your cms content for wordpress style shortcodes.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'shortcodes'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install shortcodes
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
Shortcodes.shortcode(content)
|
25
|
+
```
|
26
|
+
|
27
|
+
## Built-in Shortcodes
|
28
|
+
|
29
|
+
### Youtube
|
30
|
+
|
31
|
+
Example:
|
32
|
+
|
33
|
+
```
|
34
|
+
[youtube url="https://www.youtube.com/watch?v=Gzj723LkRJY#t=3m21s"]
|
35
|
+
```
|
36
|
+
|
37
|
+
Attributes:
|
38
|
+
|
39
|
+
* `url` - required
|
40
|
+
* `width`, `height` - optional. defaults to 560x315
|
41
|
+
|
42
|
+
### Wufoo
|
43
|
+
|
44
|
+
Copying and pasting the Wufoo Wordpress shortcode should work.
|
45
|
+
|
46
|
+
Example:
|
47
|
+
|
48
|
+
```
|
49
|
+
[wufoo username="awesome_user" formhash="a04909c" autoresize="true" height="961" header="show" ssl="true"]
|
50
|
+
```
|
51
|
+
|
52
|
+
## Contributing
|
53
|
+
|
54
|
+
1. Fork it
|
55
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
56
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
57
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
58
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Shortcodes
|
2
|
+
# Abstract Handler
|
3
|
+
class Handler
|
4
|
+
|
5
|
+
def self.call(shortcode)
|
6
|
+
new(shortcode).render
|
7
|
+
end
|
8
|
+
|
9
|
+
attr_reader :shortcode
|
10
|
+
def initialize(shortcode)
|
11
|
+
@shortcode = shortcode
|
12
|
+
end
|
13
|
+
|
14
|
+
def attributes
|
15
|
+
shortcode.attributes
|
16
|
+
end
|
17
|
+
|
18
|
+
def render
|
19
|
+
raise NotImplementedError
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'active_support/core_ext/class/attribute'
|
2
|
+
|
3
|
+
module Shortcodes
|
4
|
+
class Parser
|
5
|
+
autoload 'Nokogiri', 'nokogiri'
|
6
|
+
autoload 'Sanitize', 'sanitize'
|
7
|
+
|
8
|
+
class_attribute :handlers
|
9
|
+
class_attribute :default_handler
|
10
|
+
self.handlers ||= {}
|
11
|
+
|
12
|
+
attr_reader :content
|
13
|
+
|
14
|
+
def initialize(content)
|
15
|
+
@content = content
|
16
|
+
end
|
17
|
+
|
18
|
+
Shortcode = Struct.new(:code, :attributes)
|
19
|
+
|
20
|
+
def to_html
|
21
|
+
content.gsub(/\[[^\]]+\]/) do |code|
|
22
|
+
shortcode = parse_shortcode(code)
|
23
|
+
|
24
|
+
get_handler(shortcode).call shortcode
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def parse_attributes(node)
|
31
|
+
Hash[node.to_a]
|
32
|
+
end
|
33
|
+
|
34
|
+
def get_handler(shortcode)
|
35
|
+
handlers.fetch(shortcode.code) { default_handler }
|
36
|
+
end
|
37
|
+
|
38
|
+
def parse_shortcode(code)
|
39
|
+
code = Sanitize.clean(code).gsub(/^\[/, '<').gsub(/\]$/, '>')
|
40
|
+
doc = Nokogiri::XML.parse(code)
|
41
|
+
node = doc.root
|
42
|
+
|
43
|
+
Shortcode.new(node.name, parse_attributes(node))
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'erb'
|
2
|
+
require 'shortcodes/handler'
|
3
|
+
|
4
|
+
module Shortcodes
|
5
|
+
class Wufoo < Handler
|
6
|
+
|
7
|
+
View = Struct.new(:username, :formhash, :autoresize, :height, :header, :ssl) do
|
8
|
+
def self.new_with_attributes(attributes)
|
9
|
+
new(*attributes.values_at(*members.map(&:to_s)))
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def view
|
14
|
+
erb = ERB.new(template)
|
15
|
+
klass = erb.def_class(View)
|
16
|
+
|
17
|
+
klass.new_with_attributes(attributes)
|
18
|
+
end
|
19
|
+
|
20
|
+
def render
|
21
|
+
view.result
|
22
|
+
end
|
23
|
+
|
24
|
+
def template
|
25
|
+
<<-HTML
|
26
|
+
<div id="wufoo-<%= formhash %>">
|
27
|
+
Fill out my <a href="http://<%= username %>.wufoo.com/forms/<%= formhash %>">online form</a>.
|
28
|
+
</div>
|
29
|
+
<script type="text/javascript">var <%= formhash %>;(function(d, t) {
|
30
|
+
var s = d.createElement(t), options = {
|
31
|
+
'userName':'<%= username %>',
|
32
|
+
'formHash':'<%= formhash %>',
|
33
|
+
'autoResize':<%= autoresize %>,
|
34
|
+
'height':'<%= height %>',
|
35
|
+
'async':true,
|
36
|
+
'header':'<%= header %>',
|
37
|
+
'ssl':<%= ssl %>};
|
38
|
+
s.src = ('https:' == d.location.protocol ? 'https://' : 'http://') + 'wufoo.com/scripts/embed/form.js';
|
39
|
+
s.onload = s.onreadystatechange = function() {
|
40
|
+
var rs = this.readyState; if (rs) if (rs != 'complete') if (rs != 'loaded') return;
|
41
|
+
try { <%= formhash %> = new WufooForm();<%= formhash %>.initialize(options);<%= formhash %>.display(); } catch (e) {}};
|
42
|
+
var scr = d.getElementsByTagName(t)[0], par = scr.parentNode; par.insertBefore(s, scr);
|
43
|
+
})(document, 'script');</script>
|
44
|
+
HTML
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
Shortcodes.register_shortcode('wufoo', self)
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'shortcodes/handler'
|
2
|
+
|
3
|
+
module Shortcodes
|
4
|
+
class Youtube < Handler
|
5
|
+
|
6
|
+
def url
|
7
|
+
# TODO: Provide a better interface for missing url
|
8
|
+
url = shortcode.attributes.fetch('url')
|
9
|
+
end
|
10
|
+
|
11
|
+
def youtube_id
|
12
|
+
match = url.match(/(v=|youtu.be\/)(\w+)/)
|
13
|
+
|
14
|
+
match ? match[2] : nil
|
15
|
+
end
|
16
|
+
|
17
|
+
def time
|
18
|
+
t = url[/(t=[0-9msh]+)/]
|
19
|
+
t ? "##{t}" : nil
|
20
|
+
end
|
21
|
+
|
22
|
+
def height
|
23
|
+
attributes.fetch('height', 315)
|
24
|
+
end
|
25
|
+
|
26
|
+
def width
|
27
|
+
attributes.fetch('width', 560)
|
28
|
+
end
|
29
|
+
|
30
|
+
def render
|
31
|
+
%Q{<iframe width="#{width}" height="#{height}" src="http://www.youtube.com/embed/#{youtube_id}#{time}" frameborder="0" allowfullscreen></iframe>}
|
32
|
+
end
|
33
|
+
|
34
|
+
Shortcodes.register_shortcode('youtube', self)
|
35
|
+
end
|
36
|
+
end
|
data/lib/shortcodes.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require "shortcodes/version"
|
2
|
+
|
3
|
+
require 'shortcodes/parser'
|
4
|
+
|
5
|
+
module Shortcodes
|
6
|
+
|
7
|
+
def self.shortcode(content)
|
8
|
+
Parser.new(content).to_html
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.register_shortcode(tag, klass)
|
12
|
+
Parser.handlers[tag] = klass
|
13
|
+
end
|
14
|
+
|
15
|
+
Parser.default_handler = ->(shortcode) { '' }
|
16
|
+
end
|
17
|
+
|
metadata
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: shortcodes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Amiel Martin
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-14 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: nokogiri
|
16
|
+
requirement: !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: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: sanitize
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: activesupport
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: bundler
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '1.3'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '1.3'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rake
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
description: Wordpress style shortcodes for your Ruby CMS
|
95
|
+
email:
|
96
|
+
- amiel@carnesmedia.com
|
97
|
+
executables: []
|
98
|
+
extensions: []
|
99
|
+
extra_rdoc_files: []
|
100
|
+
files:
|
101
|
+
- lib/shortcodes/all.rb
|
102
|
+
- lib/shortcodes/handler.rb
|
103
|
+
- lib/shortcodes/parser.rb
|
104
|
+
- lib/shortcodes/version.rb
|
105
|
+
- lib/shortcodes/wufoo.rb
|
106
|
+
- lib/shortcodes/youtube.rb
|
107
|
+
- lib/shortcodes.rb
|
108
|
+
- LICENSE.txt
|
109
|
+
- Rakefile
|
110
|
+
- README.md
|
111
|
+
homepage: https://github.com/carnesmedia/shortcodes
|
112
|
+
licenses:
|
113
|
+
- MIT
|
114
|
+
post_install_message:
|
115
|
+
rdoc_options: []
|
116
|
+
require_paths:
|
117
|
+
- lib
|
118
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
119
|
+
none: false
|
120
|
+
requirements:
|
121
|
+
- - ! '>='
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
segments:
|
125
|
+
- 0
|
126
|
+
hash: -1083873180484645285
|
127
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
128
|
+
none: false
|
129
|
+
requirements:
|
130
|
+
- - ! '>='
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
segments:
|
134
|
+
- 0
|
135
|
+
hash: -1083873180484645285
|
136
|
+
requirements: []
|
137
|
+
rubyforge_project:
|
138
|
+
rubygems_version: 1.8.25
|
139
|
+
signing_key:
|
140
|
+
specification_version: 3
|
141
|
+
summary: Shortcodes is a simple ruby gem that will parse your cms content for wordpress
|
142
|
+
style shortcodes.
|
143
|
+
test_files: []
|
144
|
+
has_rdoc:
|