html2bbcode 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ Y2I1MWM0YmZiMTE1NDdjNTljNWFmMGM5MWE4NGIzYzAyNmNlZTE0Nw==
5
+ data.tar.gz: !binary |-
6
+ OTUyYzk5Mjg1OWU1ZWQ1ZGI3NTFkYjg3YTA5YTIxZjZlNGUwYmM5ZA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ NDM4ODFhZDMxZTkwNzIwYTUyNGJlNDE3MmY3NmJhOWEyMjllMGRmNDg2ODBh
10
+ ODIyMjQwZjY1YzlmYWMyOTg4NzE1ODg0NzYzYTkzYTBhZmE4YzFhMDZkOTRi
11
+ ZjI4NTdmYTQwMDJjMmJhYThmMmNmNTg2YjIzN2QyMzgxODQyMWU=
12
+ data.tar.gz: !binary |-
13
+ MjhjNGYwOTQxNzY2ZTJmOTUwNGIyMmRlMTkxNWZkODQzOWIzMzIxY2ZiMWM4
14
+ MDVkZThmZDJhZWY2MmVjM2JlNzIxNDBjZjQxODIwMmMwMWU3YzM1ZDRlNWU3
15
+ NjA2ODY0M2I4M2RiMDhmYWI4Y2I2NzlmMTJiNDY4NzVlNTdkYWY=
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'nokogiri-styles'
4
+ gem 'ruby-units'
5
+
6
+ group :development do
7
+ gem "rdoc", "~> 3.12"
8
+ gem "bundler", "~> 1.0"
9
+ gem "jeweler", "~> 1.8.7"
10
+ gem 'rspec'
11
+ end
12
+
13
+ group :development, :test do
14
+ gem 'pry'
15
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,76 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ addressable (2.3.5)
5
+ builder (3.2.2)
6
+ coderay (1.0.9)
7
+ diff-lcs (1.2.4)
8
+ faraday (0.8.8)
9
+ multipart-post (~> 1.2.0)
10
+ git (1.2.6)
11
+ github_api (0.10.1)
12
+ addressable
13
+ faraday (~> 0.8.1)
14
+ hashie (>= 1.2)
15
+ multi_json (~> 1.4)
16
+ nokogiri (~> 1.5.2)
17
+ oauth2
18
+ hashie (2.0.5)
19
+ highline (1.6.20)
20
+ httpauth (0.2.0)
21
+ jeweler (1.8.8)
22
+ builder
23
+ bundler (~> 1.0)
24
+ git (>= 1.2.5)
25
+ github_api (= 0.10.1)
26
+ highline (>= 1.6.15)
27
+ nokogiri (= 1.5.10)
28
+ rake
29
+ rdoc
30
+ json (1.8.1)
31
+ jwt (0.1.8)
32
+ multi_json (>= 1.5)
33
+ method_source (0.8.2)
34
+ multi_json (1.8.2)
35
+ multi_xml (0.5.5)
36
+ multipart-post (1.2.0)
37
+ nokogiri (1.5.10)
38
+ nokogiri-styles (0.1.2)
39
+ nokogiri
40
+ oauth2 (0.9.2)
41
+ faraday (~> 0.8)
42
+ httpauth (~> 0.2)
43
+ jwt (~> 0.1.4)
44
+ multi_json (~> 1.0)
45
+ multi_xml (~> 0.5)
46
+ rack (~> 1.2)
47
+ pry (0.9.12.2)
48
+ coderay (~> 1.0.5)
49
+ method_source (~> 0.8)
50
+ slop (~> 3.4)
51
+ rack (1.5.2)
52
+ rake (10.1.0)
53
+ rdoc (3.12.2)
54
+ json (~> 1.4)
55
+ rspec (2.14.1)
56
+ rspec-core (~> 2.14.0)
57
+ rspec-expectations (~> 2.14.0)
58
+ rspec-mocks (~> 2.14.0)
59
+ rspec-core (2.14.7)
60
+ rspec-expectations (2.14.3)
61
+ diff-lcs (>= 1.1.3, < 2.0)
62
+ rspec-mocks (2.14.4)
63
+ ruby-units (1.4.4)
64
+ slop (3.4.6)
65
+
66
+ PLATFORMS
67
+ ruby
68
+
69
+ DEPENDENCIES
70
+ bundler (~> 1.0)
71
+ jeweler (~> 1.8.7)
72
+ nokogiri-styles
73
+ pry
74
+ rdoc (~> 3.12)
75
+ rspec
76
+ ruby-units
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Szymon Kieloch
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = html2bbcode
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to html2bbcode
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2013 Szymon Kieloch. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "html2bbcode"
18
+ gem.homepage = "http://github.com/simon2k/html2bbcode"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{A gem for converting HTML to BBCode}
21
+ gem.description = %Q{A gem which allows you converting the HTML tags to the BBCode ones}
22
+ gem.email = "s.kieloch@selleo.com"
23
+ gem.authors = ["Szymon Kieloch"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ task :default => :test
36
+
37
+ require 'rdoc/task'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "html2bbcode #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,85 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+ # stub: html2bbcode 1.0.0 ruby lib
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "html2bbcode"
9
+ s.version = "1.0.0"
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.authors = ["Szymon Kieloch"]
13
+ s.date = "2013-11-02"
14
+ s.description = "A gem which allows you converting the HTML tags to the BBCode ones"
15
+ s.email = "s.kieloch@selleo.com"
16
+ s.extra_rdoc_files = [
17
+ "LICENSE.txt",
18
+ "README.rdoc"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "html2bbcode.gemspec",
29
+ "lib/bbcode/closing_tags.rb",
30
+ "lib/bbcode/element.rb",
31
+ "lib/bbcode/opening_tags.rb",
32
+ "lib/bbcode/rules.rb",
33
+ "lib/html/attributes.rb",
34
+ "lib/html/color.rb",
35
+ "lib/html/element.rb",
36
+ "lib/html2bbcode.rb",
37
+ "lib/ruby_units/definitions.rb",
38
+ "spec/lib/bbcode/closing_tags_spec.rb",
39
+ "spec/lib/bbcode/element_spec.rb",
40
+ "spec/lib/bbcode/opening_tags_spec.rb",
41
+ "spec/lib/bbcode/rules_spec.rb",
42
+ "spec/lib/html/attributes_spec.rb",
43
+ "spec/lib/html/color_spec.rb",
44
+ "spec/lib/html/element_spec.rb",
45
+ "spec/lib/html2bbcode_spec.rb",
46
+ "spec/lib/ruby_units/definitions_spec.rb",
47
+ "spec/spec_helper.rb"
48
+ ]
49
+ s.homepage = "http://github.com/simon2k/html2bbcode"
50
+ s.licenses = ["MIT"]
51
+ s.require_paths = ["lib"]
52
+ s.rubygems_version = "2.1.9"
53
+ s.summary = "A gem for converting HTML to BBCode"
54
+
55
+ if s.respond_to? :specification_version then
56
+ s.specification_version = 4
57
+
58
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
59
+ s.add_runtime_dependency(%q<nokogiri-styles>, [">= 0"])
60
+ s.add_runtime_dependency(%q<ruby-units>, [">= 0"])
61
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
62
+ s.add_development_dependency(%q<bundler>, ["~> 1.0"])
63
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.7"])
64
+ s.add_development_dependency(%q<rspec>, [">= 0"])
65
+ s.add_development_dependency(%q<pry>, [">= 0"])
66
+ else
67
+ s.add_dependency(%q<nokogiri-styles>, [">= 0"])
68
+ s.add_dependency(%q<ruby-units>, [">= 0"])
69
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
70
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
71
+ s.add_dependency(%q<jeweler>, ["~> 1.8.7"])
72
+ s.add_dependency(%q<rspec>, [">= 0"])
73
+ s.add_dependency(%q<pry>, [">= 0"])
74
+ end
75
+ else
76
+ s.add_dependency(%q<nokogiri-styles>, [">= 0"])
77
+ s.add_dependency(%q<ruby-units>, [">= 0"])
78
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
79
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
80
+ s.add_dependency(%q<jeweler>, ["~> 1.8.7"])
81
+ s.add_dependency(%q<rspec>, [">= 0"])
82
+ s.add_dependency(%q<pry>, [">= 0"])
83
+ end
84
+ end
85
+
@@ -0,0 +1,29 @@
1
+ require 'forwardable'
2
+
3
+ class Html2Bbcode
4
+ class Bbcode
5
+ class ClosingTags
6
+ extend Forwardable
7
+
8
+ def initialize bbcode_element
9
+ @bbcode_element = bbcode_element
10
+ end
11
+
12
+ def_delegators :@bbcode_element, :names
13
+
14
+ def tags
15
+ collect_tags.join
16
+ end
17
+
18
+ private
19
+
20
+ def collect_tags
21
+ bbcode_names.collect { |tag| tag ? "[/#{tag}]" : '' }
22
+ end
23
+
24
+ def bbcode_names
25
+ names.reverse.map { |name| name == '*' ? nil : name }
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,41 @@
1
+ class Html2Bbcode
2
+ class Bbcode
3
+ class Element
4
+ def initialize html_element
5
+ @html_element = html_element
6
+ end
7
+
8
+ attr_reader :html_element
9
+
10
+ def opening_tags
11
+ get_opening_tags.tags
12
+ end
13
+
14
+ def closing_tags
15
+ get_closing_tags.tags
16
+ end
17
+
18
+ def names
19
+ Array name
20
+ end
21
+
22
+ def name
23
+ rules.bbcode_name
24
+ end
25
+
26
+ private
27
+
28
+ def get_opening_tags
29
+ OpeningTags.new(self)
30
+ end
31
+
32
+ def get_closing_tags
33
+ ClosingTags.new self
34
+ end
35
+
36
+ def rules
37
+ Rules.new(self)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,42 @@
1
+ require 'forwardable'
2
+
3
+ class OpeningTags
4
+ extend Forwardable
5
+
6
+ def initialize bbcode_element
7
+ @bbcode_element = bbcode_element
8
+ end
9
+
10
+ def_delegators :@bbcode_element, :names
11
+
12
+ def tags
13
+ bbcode_names.collect { |tag| "[#{tag}]" }.join
14
+ end
15
+
16
+ private
17
+
18
+ attr_reader :bbcode_element
19
+
20
+ def_delegators :bbcode_element, :html_element
21
+ def_delegators :html_element, :text, :href, :font_size, :font_color
22
+
23
+ def bbcode_names
24
+ names.collect do |name|
25
+ if name == 'url' && href != text
26
+ "#{name}=#{href}"
27
+ elsif name == 'size'
28
+ "#{name}=#{font_size}"
29
+ elsif name == 'color'
30
+ "#{name}=#{font_color}"
31
+ elsif name == 'list' && html_name == 'ol'
32
+ "#{name}=1"
33
+ else
34
+ name
35
+ end
36
+ end
37
+ end
38
+
39
+ def html_name
40
+ html_element.name
41
+ end
42
+ end
@@ -0,0 +1,85 @@
1
+ class Rules
2
+ def initialize bbcode_element
3
+ @bbcode_element = bbcode_element
4
+ end
5
+
6
+ def bbcode_name
7
+ case html_name
8
+ when 'a'
9
+ get_name_for_a
10
+ when 'blockquote'
11
+ 'quote'
12
+ when 'pre'
13
+ 'code'
14
+ when 'strong'
15
+ 'b'
16
+ when 'em'
17
+ 'i'
18
+ when 'ins'
19
+ 'u'
20
+ when 'del'
21
+ 's'
22
+ when 'ul', 'ol'
23
+ 'list'
24
+ when 'li'
25
+ '*'
26
+ when 'font'
27
+ get_name_for_font
28
+ when 'b', 'i', 'u', 's', 'img', 'table', 'td', 'tr'
29
+ html_name
30
+ when 'span'
31
+ get_names_for_span
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ attr_reader :bbcode_element
38
+
39
+ def get_name_for_a
40
+ 'url' if html_href
41
+ end
42
+
43
+ def get_name_for_font
44
+ if html_font_size
45
+ 'size'
46
+ elsif html_color
47
+ 'color'
48
+ end
49
+ end
50
+
51
+ def get_names_for_span
52
+ [].tap do |tag_names|
53
+ tag_names << 'b' if html_styles['font-weight'] == 'bold'
54
+ tag_names << 'u' if html_styles['text-decoration'] == 'underline'
55
+ tag_names << 's' if html_styles['text-decoration'] == 'line-through'
56
+ tag_names << 'i' if html_styles['font-style'] == 'italic'
57
+ tag_names << 'size' if html_styles['font-size']
58
+ tag_names << 'color' if html_styles['color']
59
+ end
60
+ end
61
+
62
+ def html_element
63
+ bbcode_element.html_element
64
+ end
65
+
66
+ def html_color
67
+ html_element.font_color
68
+ end
69
+
70
+ def html_name
71
+ html_element.name
72
+ end
73
+
74
+ def html_styles
75
+ html_element.styles
76
+ end
77
+
78
+ def html_href
79
+ html_element.href
80
+ end
81
+
82
+ def html_font_size
83
+ html_element.font_size
84
+ end
85
+ end
@@ -0,0 +1,13 @@
1
+ class Html2Bbcode
2
+ class Html
3
+ module Attributes
4
+ def has_html_attributes *attrs
5
+ attrs.each do |attr|
6
+ define_method attr do
7
+ element[attr.to_s]
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
data/lib/html/color.rb ADDED
@@ -0,0 +1,43 @@
1
+ class Html2Bbcode
2
+ class Html
3
+ class Color
4
+ def initialize color
5
+ @color = color
6
+ end
7
+
8
+ def get
9
+ parse_color
10
+ end
11
+
12
+ private
13
+
14
+ attr_reader :color
15
+
16
+ def parse_color
17
+ is_color_rgb? ? "##{get_hex_color}" : color
18
+ end
19
+
20
+ def get_hex_color
21
+ rgb_decimals_to_hexs.join
22
+ end
23
+
24
+ def rgb_decimals_to_hexs
25
+ get_rgb_decimals.collect do |decimal|
26
+ decimal.to_i.to_s(16).rjust(2, '0')
27
+ end
28
+ end
29
+
30
+ def get_rgb_decimals
31
+ get_rgb_decimal_arguments.split(',')
32
+ end
33
+
34
+ def get_rgb_decimal_arguments
35
+ color[/(\d+\,(\s)+){2}\d+/]
36
+ end
37
+
38
+ def is_color_rgb?
39
+ color =~ /rgb\((\d+\,(\s)+){2}\d+\)/
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,107 @@
1
+ require 'forwardable'
2
+
3
+ class Html2Bbcode
4
+ class Html
5
+ class Element
6
+ extend Forwardable
7
+ extend Attributes
8
+
9
+ def initialize element, options = {}
10
+ @element = element
11
+ @options = options
12
+
13
+ add_src_to_text if name.eql? 'img'
14
+ end
15
+
16
+ attr_reader :element, :options
17
+
18
+ def_delegators :element, :attributes, :element?, :name, :text, :text?, :add_child, :styles, :inner_html
19
+
20
+ has_html_attributes :src, :href, :size, :color
21
+
22
+ def process
23
+ children.map(&:to_bbcode_element).join
24
+ end
25
+
26
+ def to_bbcode_element
27
+ if name == 'br'
28
+ "\n"
29
+ elsif element?
30
+ build_bbcode_element
31
+ elsif text?
32
+ text
33
+ end
34
+ end
35
+
36
+ def build_bbcode_element
37
+ bbcode_opening_tags + bbcode_content + bbcode_closing_tags
38
+ end
39
+
40
+ def font_color
41
+ element_color.get
42
+ end
43
+
44
+ def font_size
45
+ get_size.to_i if get_font_size
46
+ end
47
+
48
+ private
49
+
50
+ def get_size
51
+ if size_unit
52
+ Unit.new(get_font_size_value, get_font_size_unit).to(size_unit).scalar
53
+ else
54
+ get_font_size_value
55
+ end
56
+ end
57
+
58
+ def get_font_size_value
59
+ get_font_size[/\d+/]
60
+ end
61
+
62
+ def get_font_size_unit
63
+ get_font_size[/\w+/]
64
+ end
65
+
66
+ def size_unit
67
+ options[:size_unit]
68
+ end
69
+
70
+ def bbcode_content
71
+ name == 'pre' ? inner_html : process
72
+ end
73
+
74
+ def get_font_color
75
+ color || styles['color']
76
+ end
77
+
78
+ def get_font_size
79
+ size || styles['font-size']
80
+ end
81
+
82
+ def add_src_to_text
83
+ add_child Nokogiri::XML::Text.new src, element.document
84
+ end
85
+
86
+ def element_color
87
+ Html::Color.new get_font_color
88
+ end
89
+
90
+ def children
91
+ element.children.collect { |child| self.class.new child, options }
92
+ end
93
+
94
+ def bbcode_opening_tags
95
+ bbcode_element.opening_tags
96
+ end
97
+
98
+ def bbcode_closing_tags
99
+ bbcode_element.closing_tags
100
+ end
101
+
102
+ def bbcode_element
103
+ Bbcode::Element.new self
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,32 @@
1
+ require 'nokogiri'
2
+ require 'nokogiri-styles'
3
+ require 'html/attributes'
4
+ require 'html/element'
5
+ require 'html/color'
6
+ require 'bbcode/element'
7
+ require 'bbcode/rules'
8
+ require 'bbcode/opening_tags'
9
+ require 'bbcode/closing_tags'
10
+ require 'ruby-units'
11
+ require 'ruby_units/definitions'
12
+
13
+ class Html2Bbcode
14
+ def initialize input_html, options = {}
15
+ @parsed_html = Nokogiri::HTML::Document.parse input_html
16
+ @options = options
17
+ end
18
+
19
+ attr_reader :parsed_html
20
+
21
+ def convert
22
+ html_element.process
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :options
28
+
29
+ def html_element
30
+ Html::Element.new parsed_html, options
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ RubyUnits::Unit.redefine! 'pixel' do |pixel|
2
+ pixel.definition = RubyUnits::Unit.new '1/72 inch'
3
+ pixel.aliases = %w{px pixel pixels}
4
+ pixel.kind = :length
5
+ end