bb_tag_closer 1.0.0
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/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +2 -0
- data/bb_tag_closer.gemspec +18 -0
- data/changelog.md +3 -0
- data/lib/bb_tag_closer/railtie.rb +9 -0
- data/lib/bb_tag_closer/version.rb +3 -0
- data/lib/bb_tag_closer.rb +83 -0
- data/spec/bb_tag_closer_spec.rb +53 -0
- data/spec/spec_helper.rb +3 -0
- metadata +92 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Chase
|
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,29 @@
|
|
1
|
+
# BBTagCloser
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'bb_tag_closer'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install bb_tag_closer
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/bb_tag_closer/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Chase Conklin"]
|
6
|
+
gem.email = [""]
|
7
|
+
gem.description = %q{Automatically closes bb tags found in forums}
|
8
|
+
gem.summary = %q{Allows the customization of tags in an initializer file, and can work with multiple attributes of a model, specified in the close_tags_for method.}
|
9
|
+
gem.homepage = ""
|
10
|
+
gem.add_development_dependency "rspec"
|
11
|
+
gem.add_development_dependency 'rails', '>= 3.2'
|
12
|
+
gem.files = `git ls-files`.split($\)
|
13
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
14
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
15
|
+
gem.name = "bb_tag_closer"
|
16
|
+
gem.require_paths = ["lib"]
|
17
|
+
gem.version = BBTagCloser::VERSION
|
18
|
+
end
|
data/changelog.md
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
require "bb_tag_closer/version"
|
2
|
+
require "bb_tag_closer/railtie" if defined? Rails
|
3
|
+
module BBTagCloser
|
4
|
+
require 'active_support/configurable'
|
5
|
+
|
6
|
+
attr_accessor :font_size
|
7
|
+
attr_accessor :font_color
|
8
|
+
|
9
|
+
|
10
|
+
def self.included(base)
|
11
|
+
base.attr_accessible :font_size, :font_color
|
12
|
+
base.extend ClassMethods
|
13
|
+
end
|
14
|
+
|
15
|
+
module ClassMethods
|
16
|
+
|
17
|
+
# Text attributes are considered taggable by default
|
18
|
+
# Using close_tags_for overrides default with custom attributes
|
19
|
+
|
20
|
+
def taggable_fields
|
21
|
+
columns.map {|c| {c.name.to_sym => c.type}}.delete_if {|i| not i.value? :text}.map(&:keys).flatten
|
22
|
+
end
|
23
|
+
|
24
|
+
# Closes tags on text attributes.
|
25
|
+
# Closes tags before save by default, but can be overridden by passing :on
|
26
|
+
# the :on paramater accepts :save, :create, :update, :validation
|
27
|
+
# If custom mass_assignment is used such as attr_accessible :foo, :as => :bar
|
28
|
+
# pass :mass_assignment_keys => :bar or :mass_assignment_keys => [:bar, :baz]
|
29
|
+
|
30
|
+
def close_tags(options = {})
|
31
|
+
target = options[:on] ? "before_#{options[:on]}" : "before_save"
|
32
|
+
send(target, :close_tags)
|
33
|
+
mass_assignment_keys = Array options[:mass_assignment_keys]
|
34
|
+
mass_assignment_keys.each do |key|
|
35
|
+
attr_accessible :font_size, :font_color, :as => key
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Specify which attributes to close tags on. Accepts the same options as close_tags
|
40
|
+
|
41
|
+
def close_tags_for(*args)
|
42
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
43
|
+
target = options[:on] ? "before_#{options[:on]}" : "before_save"
|
44
|
+
send(target, :close_tags)
|
45
|
+
self.singleton_class.class_eval do
|
46
|
+
define_method :taggable_fields do
|
47
|
+
args.delete_if {|i| not i.is_a? Symbol}
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.configure(&block)
|
55
|
+
yield BBTagCloser::Configuration
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
def close_tags
|
60
|
+
text_attributes = self.class.taggable_fields
|
61
|
+
text_attributes.each do |attribute|
|
62
|
+
tags = Configuration.bb_tags && send(attribute).scan(Regexp.new "\\[(.*?)\\]").flatten.delete_if {|c| c.include? "/"}.map {|i| i.include?("=") ? i.slice(Regexp.new "(.*)\\=").chomp("=") : i}.reverse
|
63
|
+
tags.each do |tag|
|
64
|
+
open_tags = send(attribute).scan(Regexp.new "\\[#{tag}=").length + send(attribute).scan(Regexp.new "\\[#{tag}\\]").length
|
65
|
+
closed_tags = send(attribute).scan(Regexp.new "\\[\\/#{tag}\\]").length
|
66
|
+
difference = open_tags - closed_tags
|
67
|
+
if open_tags > closed_tags
|
68
|
+
send(attribute) << "[/#{tag}]"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
class Configuration
|
75
|
+
include ActiveSupport::Configurable
|
76
|
+
config_accessor :bb_tags
|
77
|
+
end
|
78
|
+
|
79
|
+
configure do |config|
|
80
|
+
config.bb_tags = ["u", "b", "s", "i", "quote", "url", "img", "email", "youtube", "size", "color"]
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
class Post < ActiveRecord::Base
|
3
|
+
include BBTagCloser
|
4
|
+
attr_accessor :body
|
5
|
+
close_tags_for :body
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@body = ""
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe BBTagCloser do
|
13
|
+
before(:each) do
|
14
|
+
@post = Post.new
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should close forum tags" do
|
18
|
+
@post.body = "[u]test"
|
19
|
+
@post.close_tags
|
20
|
+
@post.body.should == "[u]test[/u]"
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should close multiple forum tags" do
|
24
|
+
@post.body = "[u][b]test"
|
25
|
+
@post.close_tags
|
26
|
+
@post.body.should == "[u][b]test[/b][/u]"
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should close complex forum tags" do
|
30
|
+
@post.body = "[quote=Fish]test"
|
31
|
+
@post.close_tags
|
32
|
+
@post.body.should == "[quote=Fish]test[/quote]"
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should close multiple forum tags of the same type" do
|
36
|
+
@post.body = "[u][u]test"
|
37
|
+
@post.close_tags
|
38
|
+
@post.body.should == "[u][u]test[/u][/u]"
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should leave properly closed tags alone" do
|
42
|
+
@post.body = "[u][u]test[/u][/u]"
|
43
|
+
@post.close_tags
|
44
|
+
@post.body.should == "[u][u]test[/u][/u]"
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should leave properly closed tags alone while closing others" do
|
48
|
+
@post.body = "[u][b]test[/u]"
|
49
|
+
@post.close_tags
|
50
|
+
@post.body.should == "[u][b]test[/u][/b]"
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bb_tag_closer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Chase Conklin
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-06 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
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: rails
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '3.2'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '3.2'
|
46
|
+
description: Automatically closes bb tags found in forums
|
47
|
+
email:
|
48
|
+
- ''
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- bb_tag_closer.gemspec
|
59
|
+
- changelog.md
|
60
|
+
- lib/bb_tag_closer.rb
|
61
|
+
- lib/bb_tag_closer/railtie.rb
|
62
|
+
- lib/bb_tag_closer/version.rb
|
63
|
+
- spec/bb_tag_closer_spec.rb
|
64
|
+
- spec/spec_helper.rb
|
65
|
+
homepage: ''
|
66
|
+
licenses: []
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 1.8.24
|
86
|
+
signing_key:
|
87
|
+
specification_version: 3
|
88
|
+
summary: Allows the customization of tags in an initializer file, and can work with
|
89
|
+
multiple attributes of a model, specified in the close_tags_for method.
|
90
|
+
test_files:
|
91
|
+
- spec/bb_tag_closer_spec.rb
|
92
|
+
- spec/spec_helper.rb
|