html2bbcode 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -13
- data/.travis.yml +4 -0
- data/README.markdown +88 -0
- data/Rakefile +20 -22
- data/VERSION +1 -1
- data/html2bbcode.gemspec +6 -6
- data/lib/bbcode/closing_tags.rb +1 -11
- data/lib/bbcode/element.rb +3 -3
- data/lib/bbcode/opening_tags.rb +28 -31
- data/lib/bbcode/rules.rb +58 -76
- data/lib/html/color.rb +4 -14
- data/lib/html/element.rb +14 -18
- data/lib/html2bbcode.rb +2 -3
- data/spec/lib/bbcode/closing_tags_spec.rb +4 -5
- data/spec/lib/bbcode/opening_tags_spec.rb +24 -12
- data/spec/lib/bbcode/rules_spec.rb +19 -10
- data/spec/lib/html/attributes_spec.rb +2 -2
- data/spec/lib/html/element_spec.rb +6 -0
- metadata +18 -19
- data/README.rdoc +0 -19
checksums.yaml
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
data.tar.gz: !binary |-
|
6
|
-
OTUyYzk5Mjg1OWU1ZWQ1ZGI3NTFkYjg3YTA5YTIxZjZlNGUwYmM5ZA==
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d2bd68d74368b6ad9933b2e7ac81f576ce082050
|
4
|
+
data.tar.gz: 80489b1f3213732f4310ff766de7d889124d425e
|
7
5
|
SHA512:
|
8
|
-
metadata.gz:
|
9
|
-
|
10
|
-
ODIyMjQwZjY1YzlmYWMyOTg4NzE1ODg0NzYzYTkzYTBhZmE4YzFhMDZkOTRi
|
11
|
-
ZjI4NTdmYTQwMDJjMmJhYThmMmNmNTg2YjIzN2QyMzgxODQyMWU=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
MjhjNGYwOTQxNzY2ZTJmOTUwNGIyMmRlMTkxNWZkODQzOWIzMzIxY2ZiMWM4
|
14
|
-
MDVkZThmZDJhZWY2MmVjM2JlNzIxNDBjZjQxODIwMmMwMWU3YzM1ZDRlNWU3
|
15
|
-
NjA2ODY0M2I4M2RiMDhmYWI4Y2I2NzlmMTJiNDY4NzVlNTdkYWY=
|
6
|
+
metadata.gz: 89631994f648abd700ce0381f39af43f5821e6aa37afb08c78c318c387ac8a2894bce0e69870d8a3b29fa902d8057cec72e7973aa0f7e7238d734395713ccc45
|
7
|
+
data.tar.gz: bbfe892cfa152316941499e1041f9b10c7671ad8d14419466337dc23f1d7e8d7342d0585b967b8f7987ca8f4646e455e64cc0740047ea49c866be294ace2cc8b
|
data/.travis.yml
ADDED
data/README.markdown
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# Html2Bbcode [![Build Status](https://travis-ci.org/simon2k/html2bbcode.png?branch=master)](https://travis-ci.org/simon2k/html2bbcode)
|
2
|
+
|
3
|
+
The library for converting the HTML code into the BBCode.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Install the gem manually:
|
8
|
+
|
9
|
+
```bash
|
10
|
+
gem install html2bbcode
|
11
|
+
```
|
12
|
+
|
13
|
+
or add it into your Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'html2bbcode'
|
17
|
+
```
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
### Basic usage:
|
22
|
+
|
23
|
+
The given code:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
Html2Bbcode.new('normal <b> bolded words <i> italic and bolded </i> bolded </b> normal').convert
|
27
|
+
```
|
28
|
+
|
29
|
+
Will produce the following output:
|
30
|
+
|
31
|
+
```bbcode
|
32
|
+
'normal [b] bolded words [i] italic and bolded [/i] bolded [/b] normal'
|
33
|
+
```
|
34
|
+
|
35
|
+
### Customizing the font size unit
|
36
|
+
|
37
|
+
You can specify the font size unit, used in your BBCode, simply using the `size_unit` option:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
Html2Bbcode.new('<span style="font-size: 15cm;">text 15 cm</span>', size_unit: 'mm')
|
41
|
+
```
|
42
|
+
|
43
|
+
Will produce the following output:
|
44
|
+
|
45
|
+
```bbcode
|
46
|
+
'[size=150]text 15 cm[/size]'
|
47
|
+
```
|
48
|
+
|
49
|
+
**Notice:** When the size unit is not specified, the size value is not converted.
|
50
|
+
|
51
|
+
## Supported tags
|
52
|
+
|
53
|
+
Currently, the library supports all the necessary HTML tags:
|
54
|
+
|
55
|
+
* **a** (a link)
|
56
|
+
* **b**
|
57
|
+
* **blockquote**
|
58
|
+
* **del**
|
59
|
+
* **em**
|
60
|
+
* **font**
|
61
|
+
* **i**
|
62
|
+
* **ins**
|
63
|
+
* **img**
|
64
|
+
* **li**
|
65
|
+
* **ol**
|
66
|
+
* **pre**
|
67
|
+
* **s**
|
68
|
+
* **span** (text formatting)
|
69
|
+
* **strong**
|
70
|
+
* **table**
|
71
|
+
* **td**
|
72
|
+
* **tr**
|
73
|
+
* **u**
|
74
|
+
* **ul**
|
75
|
+
|
76
|
+
## Contributing to html2bbcode
|
77
|
+
|
78
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
79
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
80
|
+
* Fork the project.
|
81
|
+
* Start a feature/bugfix branch.
|
82
|
+
* Commit and push until you are happy with your contribution.
|
83
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
84
|
+
* 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.
|
85
|
+
|
86
|
+
## Copyright
|
87
|
+
|
88
|
+
Copyright (c) 2013 Szymon Kieloch. See LICENSE.txt for further details.
|
data/Rakefile
CHANGED
@@ -1,45 +1,43 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require 'rubygems'
|
4
1
|
require 'bundler'
|
2
|
+
require 'jeweler'
|
3
|
+
require 'rake'
|
4
|
+
require 'rdoc/task'
|
5
|
+
require 'rspec/core/rake_task'
|
6
|
+
require 'rubygems'
|
7
|
+
|
5
8
|
begin
|
6
9
|
Bundler.setup(:default, :development)
|
7
10
|
rescue Bundler::BundlerError => e
|
8
11
|
$stderr.puts e.message
|
9
|
-
$stderr.puts
|
12
|
+
$stderr.puts 'Run `bundle install` to install missing gems'
|
10
13
|
exit e.status_code
|
11
14
|
end
|
12
|
-
require 'rake'
|
13
15
|
|
14
|
-
require 'jeweler'
|
15
16
|
Jeweler::Tasks.new do |gem|
|
16
17
|
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
-
gem.name
|
18
|
-
gem.homepage
|
19
|
-
gem.license
|
20
|
-
gem.summary
|
21
|
-
gem.description =
|
22
|
-
gem.email
|
23
|
-
gem.authors
|
18
|
+
gem.name = 'html2bbcode'
|
19
|
+
gem.homepage = 'http://github.com/simon2k/html2bbcode'
|
20
|
+
gem.license = 'MIT'
|
21
|
+
gem.summary = 'A gem for converting HTML to BBCode'
|
22
|
+
gem.description = 'A gem which allows you converting the HTML tags to the BBCode ones'
|
23
|
+
gem.email = 's.kieloch@selleo.com'
|
24
|
+
gem.authors = ['Szymon Kieloch']
|
24
25
|
# dependencies defined in Gemfile
|
25
26
|
end
|
27
|
+
|
26
28
|
Jeweler::RubygemsDotOrgTasks.new
|
27
29
|
|
28
|
-
|
29
|
-
|
30
|
-
test.libs << 'lib' << 'test'
|
31
|
-
test.pattern = 'test/**/test_*.rb'
|
32
|
-
test.verbose = true
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |config|
|
31
|
+
config.spec_opts = '--format progress'
|
33
32
|
end
|
34
33
|
|
35
|
-
task :default => :
|
34
|
+
task :default => :spec
|
36
35
|
|
37
|
-
require 'rdoc/task'
|
38
36
|
Rake::RDocTask.new do |rdoc|
|
39
|
-
version = File.exist?('VERSION') ? File.read('VERSION') :
|
37
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ''
|
40
38
|
|
41
39
|
rdoc.rdoc_dir = 'rdoc'
|
42
|
-
rdoc.title
|
40
|
+
rdoc.title = "html2bbcode #{version}"
|
43
41
|
rdoc.rdoc_files.include('README*')
|
44
42
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
43
|
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.1
|
data/html2bbcode.gemspec
CHANGED
@@ -2,27 +2,27 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: html2bbcode 1.0.0 ruby lib
|
6
5
|
|
7
6
|
Gem::Specification.new do |s|
|
8
7
|
s.name = "html2bbcode"
|
9
|
-
s.version = "1.0.
|
8
|
+
s.version = "1.0.1"
|
10
9
|
|
11
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
11
|
s.authors = ["Szymon Kieloch"]
|
13
|
-
s.date = "2013-11-
|
12
|
+
s.date = "2013-11-04"
|
14
13
|
s.description = "A gem which allows you converting the HTML tags to the BBCode ones"
|
15
14
|
s.email = "s.kieloch@selleo.com"
|
16
15
|
s.extra_rdoc_files = [
|
17
16
|
"LICENSE.txt",
|
18
|
-
"README.
|
17
|
+
"README.markdown"
|
19
18
|
]
|
20
19
|
s.files = [
|
21
20
|
".document",
|
21
|
+
".travis.yml",
|
22
22
|
"Gemfile",
|
23
23
|
"Gemfile.lock",
|
24
24
|
"LICENSE.txt",
|
25
|
-
"README.
|
25
|
+
"README.markdown",
|
26
26
|
"Rakefile",
|
27
27
|
"VERSION",
|
28
28
|
"html2bbcode.gemspec",
|
@@ -49,7 +49,7 @@ Gem::Specification.new do |s|
|
|
49
49
|
s.homepage = "http://github.com/simon2k/html2bbcode"
|
50
50
|
s.licenses = ["MIT"]
|
51
51
|
s.require_paths = ["lib"]
|
52
|
-
s.rubygems_version = "2.
|
52
|
+
s.rubygems_version = "2.0.6"
|
53
53
|
s.summary = "A gem for converting HTML to BBCode"
|
54
54
|
|
55
55
|
if s.respond_to? :specification_version then
|
data/lib/bbcode/closing_tags.rb
CHANGED
@@ -1,16 +1,6 @@
|
|
1
|
-
require 'forwardable'
|
2
|
-
|
3
1
|
class Html2Bbcode
|
4
2
|
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
|
-
|
3
|
+
class ClosingTags < Struct.new(:names)
|
14
4
|
def tags
|
15
5
|
collect_tags.join
|
16
6
|
end
|
data/lib/bbcode/element.rb
CHANGED
@@ -26,15 +26,15 @@ class Html2Bbcode
|
|
26
26
|
private
|
27
27
|
|
28
28
|
def get_opening_tags
|
29
|
-
OpeningTags.new
|
29
|
+
OpeningTags.new self
|
30
30
|
end
|
31
31
|
|
32
32
|
def get_closing_tags
|
33
|
-
ClosingTags.new
|
33
|
+
ClosingTags.new names
|
34
34
|
end
|
35
35
|
|
36
36
|
def rules
|
37
|
-
Rules.new
|
37
|
+
Rules.new self
|
38
38
|
end
|
39
39
|
end
|
40
40
|
end
|
data/lib/bbcode/opening_tags.rb
CHANGED
@@ -1,42 +1,39 @@
|
|
1
|
-
|
1
|
+
class Html2Bbcode
|
2
|
+
class Bbcode
|
3
|
+
class OpeningTags < Struct.new(:bbcode_element)
|
4
|
+
extend Forwardable
|
2
5
|
|
3
|
-
|
4
|
-
extend Forwardable
|
6
|
+
def_delegators :bbcode_element, :names
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
def_delegators :@bbcode_element, :names
|
8
|
+
def tags
|
9
|
+
bbcode_names.map { |tag| "[#{tag}]" }.join
|
10
|
+
end
|
11
11
|
|
12
|
-
|
13
|
-
bbcode_names.collect { |tag| "[#{tag}]" }.join
|
14
|
-
end
|
12
|
+
private
|
15
13
|
|
16
|
-
|
14
|
+
def_delegators :bbcode_element, :html_element
|
15
|
+
def_delegators :html_element, :text, :href, :font_size, :font_color
|
16
|
+
def_delegator :html_element, :name, :html_name
|
17
17
|
|
18
|
-
|
18
|
+
def bbcode_names
|
19
|
+
names.map { |name| name_with_attribute(name) }
|
20
|
+
end
|
19
21
|
|
20
|
-
|
21
|
-
|
22
|
+
def name_with_attribute(name)
|
23
|
+
attribute_for(name) ? "#{name}=#{attribute_for(name)}" : name
|
24
|
+
end
|
22
25
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
else
|
34
|
-
name
|
26
|
+
def attribute_for(name)
|
27
|
+
if name == 'url' && href != text
|
28
|
+
href
|
29
|
+
elsif name == 'size'
|
30
|
+
font_size
|
31
|
+
elsif name == 'color'
|
32
|
+
font_color
|
33
|
+
elsif name == 'list' && html_name == 'ol'
|
34
|
+
1
|
35
|
+
end
|
35
36
|
end
|
36
37
|
end
|
37
38
|
end
|
38
|
-
|
39
|
-
def html_name
|
40
|
-
html_element.name
|
41
|
-
end
|
42
39
|
end
|
data/lib/bbcode/rules.rb
CHANGED
@@ -1,85 +1,67 @@
|
|
1
|
-
class
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
class Html2Bbcode
|
2
|
+
class Bbcode
|
3
|
+
class Rules < Struct.new(:bbcode_element)
|
4
|
+
extend Forwardable
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
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
34
|
|
35
|
-
|
35
|
+
private
|
36
36
|
|
37
|
-
|
37
|
+
delegate html_element: :bbcode_element
|
38
|
+
def_delegator :html_element, :font_color, :html_color
|
39
|
+
def_delegator :html_element, :name, :html_name
|
40
|
+
def_delegator :html_element, :styles, :html_styles
|
41
|
+
def_delegator :html_element, :href, :html_href
|
42
|
+
def_delegator :html_element, :font_size, :html_font_size
|
38
43
|
|
39
|
-
|
40
|
-
|
41
|
-
|
44
|
+
def get_name_for_a
|
45
|
+
'url' if html_href
|
46
|
+
end
|
42
47
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
end
|
48
|
+
def get_name_for_font
|
49
|
+
[].tap do |tag_names|
|
50
|
+
tag_names << 'color' if html_color
|
51
|
+
tag_names << 'size' if html_font_size
|
52
|
+
end
|
53
|
+
end
|
50
54
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
55
|
+
def get_names_for_span
|
56
|
+
[].tap do |tag_names|
|
57
|
+
tag_names << 'b' if html_styles['font-weight'] == 'bold'
|
58
|
+
tag_names << 'u' if html_styles['text-decoration'] == 'underline'
|
59
|
+
tag_names << 's' if html_styles['text-decoration'] == 'line-through'
|
60
|
+
tag_names << 'i' if html_styles['font-style'] == 'italic'
|
61
|
+
tag_names << 'size' if html_styles['font-size']
|
62
|
+
tag_names << 'color' if html_styles['color']
|
63
|
+
end
|
64
|
+
end
|
59
65
|
end
|
60
66
|
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
67
|
end
|
data/lib/html/color.rb
CHANGED
@@ -1,34 +1,24 @@
|
|
1
1
|
class Html2Bbcode
|
2
2
|
class Html
|
3
|
-
class Color
|
4
|
-
def initialize color
|
5
|
-
@color = color
|
6
|
-
end
|
7
|
-
|
3
|
+
class Color < Struct.new(:color)
|
8
4
|
def get
|
9
|
-
|
5
|
+
is_color_rgb? ? "##{get_hex_color}" : color
|
10
6
|
end
|
11
7
|
|
12
8
|
private
|
13
9
|
|
14
|
-
attr_reader :color
|
15
|
-
|
16
|
-
def parse_color
|
17
|
-
is_color_rgb? ? "##{get_hex_color}" : color
|
18
|
-
end
|
19
|
-
|
20
10
|
def get_hex_color
|
21
11
|
rgb_decimals_to_hexs.join
|
22
12
|
end
|
23
13
|
|
24
14
|
def rgb_decimals_to_hexs
|
25
15
|
get_rgb_decimals.collect do |decimal|
|
26
|
-
decimal.to_i.to_s(16).rjust
|
16
|
+
decimal.to_i.to_s(16).rjust 2, '0'
|
27
17
|
end
|
28
18
|
end
|
29
19
|
|
30
20
|
def get_rgb_decimals
|
31
|
-
get_rgb_decimal_arguments.split
|
21
|
+
get_rgb_decimal_arguments.split ','
|
32
22
|
end
|
33
23
|
|
34
24
|
def get_rgb_decimal_arguments
|
data/lib/html/element.rb
CHANGED
@@ -1,25 +1,19 @@
|
|
1
|
-
require 'forwardable'
|
2
|
-
|
3
1
|
class Html2Bbcode
|
4
2
|
class Html
|
5
3
|
class Element
|
6
4
|
extend Forwardable
|
7
5
|
extend Attributes
|
8
6
|
|
7
|
+
def_delegator :element_color, :get, :font_color
|
8
|
+
|
9
9
|
def initialize element, options = {}
|
10
10
|
@element = element
|
11
11
|
@options = options
|
12
|
-
|
13
|
-
add_src_to_text if name.eql? 'img'
|
14
12
|
end
|
15
13
|
|
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
14
|
def process
|
15
|
+
add_src_to_text if name.eql? 'img'
|
16
|
+
|
23
17
|
children.map(&:to_bbcode_element).join
|
24
18
|
end
|
25
19
|
|
@@ -33,20 +27,22 @@ class Html2Bbcode
|
|
33
27
|
end
|
34
28
|
end
|
35
29
|
|
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
30
|
def font_size
|
45
31
|
get_size.to_i if get_font_size
|
46
32
|
end
|
47
33
|
|
48
34
|
private
|
49
35
|
|
36
|
+
attr_reader :element, :options
|
37
|
+
|
38
|
+
def_delegators :element, :attributes, :element?, :name, :text, :text?, :add_child, :styles, :inner_html
|
39
|
+
|
40
|
+
has_html_attributes :src, :href, :size, :color
|
41
|
+
|
42
|
+
def build_bbcode_element
|
43
|
+
bbcode_opening_tags + bbcode_content + bbcode_closing_tags
|
44
|
+
end
|
45
|
+
|
50
46
|
def get_size
|
51
47
|
if size_unit
|
52
48
|
Unit.new(get_font_size_value, get_font_size_unit).to(size_unit).scalar
|
data/lib/html2bbcode.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'forwardable'
|
1
2
|
require 'nokogiri'
|
2
3
|
require 'nokogiri-styles'
|
3
4
|
require 'html/attributes'
|
@@ -16,15 +17,13 @@ class Html2Bbcode
|
|
16
17
|
@options = options
|
17
18
|
end
|
18
19
|
|
19
|
-
attr_reader :parsed_html
|
20
|
-
|
21
20
|
def convert
|
22
21
|
html_element.process
|
23
22
|
end
|
24
23
|
|
25
24
|
private
|
26
25
|
|
27
|
-
attr_reader :options
|
26
|
+
attr_reader :options, :parsed_html
|
28
27
|
|
29
28
|
def html_element
|
30
29
|
Html::Element.new parsed_html, options
|
@@ -1,18 +1,17 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Html2Bbcode::Bbcode::ClosingTags do
|
4
|
-
let(:bbcode_element) { double 'bbcode element'}
|
5
|
-
subject{ described_class.new bbcode_element }
|
6
|
-
|
7
4
|
describe '#tags' do
|
5
|
+
subject { described_class.new names }
|
6
|
+
|
8
7
|
context 'the names contain the * (li html) tag' do
|
9
|
-
|
8
|
+
let(:names) { %w(*) }
|
10
9
|
|
11
10
|
its(:tags) { should eq '' }
|
12
11
|
end
|
13
12
|
|
14
13
|
context 'the names contain any other tag' do
|
15
|
-
|
14
|
+
let(:names) { %w(b) }
|
16
15
|
|
17
16
|
its(:tags) { should eq '[/b]' }
|
18
17
|
end
|
@@ -1,17 +1,23 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Html2Bbcode::Bbcode::OpeningTags do
|
4
|
-
|
5
|
-
|
4
|
+
subject { described_class.new bbcode_element }
|
5
|
+
|
6
|
+
let(:html_element) { double 'html element' }
|
7
|
+
let(:bbcode_element) { double 'bbcode element', html_element: html_element }
|
6
8
|
|
7
9
|
describe '#tags' do
|
8
|
-
before {
|
10
|
+
before { bbcode_element.stub names: %w(b u) }
|
9
11
|
|
10
12
|
its(:tags) { should eq '[b][u]' }
|
11
13
|
|
12
14
|
context 'the names contain the url tag' do
|
13
15
|
let(:href) { 'www.example.com' }
|
14
|
-
|
16
|
+
|
17
|
+
before do
|
18
|
+
bbcode_element.stub names: %w(url)
|
19
|
+
html_element.stub href: href, text: text
|
20
|
+
end
|
15
21
|
|
16
22
|
context 'the text is the same like the href attribute' do
|
17
23
|
let(:text) { href }
|
@@ -27,36 +33,42 @@ describe Html2Bbcode::Bbcode::OpeningTags do
|
|
27
33
|
end
|
28
34
|
|
29
35
|
context 'the names contain the size tag' do
|
30
|
-
before
|
36
|
+
before do
|
37
|
+
bbcode_element.stub names: %w(size)
|
38
|
+
html_element.stub font_size: 15
|
39
|
+
end
|
31
40
|
|
32
41
|
its(:tags) { should eq "[size=15]" }
|
33
42
|
end
|
34
43
|
|
35
44
|
context 'the names contain the color tag' do
|
36
|
-
before
|
45
|
+
before do
|
46
|
+
bbcode_element.stub names: %w(color)
|
47
|
+
html_element.stub font_color: 'blue'
|
48
|
+
end
|
37
49
|
|
38
50
|
its(:tags) { should eq "[color=blue]" }
|
39
51
|
end
|
40
52
|
|
41
53
|
context 'the names contain the * (li html) tag' do
|
42
|
-
before {
|
54
|
+
before { bbcode_element.stub names: %w(*) }
|
43
55
|
|
44
56
|
its(:tags) { should eq '[*]' }
|
45
57
|
end
|
46
58
|
|
47
59
|
context 'the names contain the list tag' do
|
48
|
-
before {
|
60
|
+
before { bbcode_element.stub names: %w(list) }
|
49
61
|
|
50
62
|
context 'the html tag is ul' do
|
51
|
-
before {
|
63
|
+
before { html_element.stub name: 'ul' }
|
52
64
|
|
53
|
-
its(:tags) { should eq '[list]'}
|
65
|
+
its(:tags) { should eq '[list]' }
|
54
66
|
end
|
55
67
|
|
56
68
|
context 'the html tag is ol' do
|
57
|
-
before {
|
69
|
+
before { html_element.stub name: 'ol' }
|
58
70
|
|
59
|
-
its(:tags) { should eq '[list=1]'}
|
71
|
+
its(:tags) { should eq '[list=1]' }
|
60
72
|
end
|
61
73
|
end
|
62
74
|
end
|
@@ -1,9 +1,12 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Html2Bbcode::Bbcode::Rules do
|
4
|
-
let(:bbcode_element) { double 'bbcode elemenet' }
|
5
4
|
subject { described_class.new bbcode_element }
|
6
|
-
|
5
|
+
|
6
|
+
let(:html_element) { double 'html element' }
|
7
|
+
let(:bbcode_element) { double 'bbcode elemenet', html_element: html_element }
|
8
|
+
|
9
|
+
before { html_element.stub name: html_name }
|
7
10
|
|
8
11
|
%w(b i u s img table tr td).each do |tag_name|
|
9
12
|
context "html element name is: #{tag_name}" do
|
@@ -15,7 +18,7 @@ describe Html2Bbcode::Bbcode::Rules do
|
|
15
18
|
|
16
19
|
context 'html element name is: a' do
|
17
20
|
let(:html_name) { 'a' }
|
18
|
-
before {
|
21
|
+
before { html_element.stub href: href }
|
19
22
|
|
20
23
|
context 'html element contains a href attribute' do
|
21
24
|
let(:href) { 'www.example.com' }
|
@@ -69,20 +72,26 @@ describe Html2Bbcode::Bbcode::Rules do
|
|
69
72
|
context 'html element name is: font' do
|
70
73
|
let(:html_name) { 'font' }
|
71
74
|
|
72
|
-
before {
|
75
|
+
before { html_element.stub font_size: nil, font_color: nil }
|
73
76
|
|
74
|
-
its(:bbcode_name) { should
|
77
|
+
its(:bbcode_name) { should eq [] }
|
75
78
|
|
76
79
|
context 'attributes contain the size' do
|
77
|
-
before {
|
80
|
+
before { html_element.stub font_size: '15' }
|
78
81
|
|
79
|
-
its(:bbcode_name) { should eq
|
82
|
+
its(:bbcode_name) { should eq %w(size) }
|
80
83
|
end
|
81
84
|
|
82
85
|
context 'attributes contain the color' do
|
83
|
-
before {
|
86
|
+
before { html_element.stub font_color: 'red' }
|
87
|
+
|
88
|
+
its(:bbcode_name) { should eq %w(color) }
|
89
|
+
end
|
90
|
+
|
91
|
+
context 'attributes contain the size and color' do
|
92
|
+
before { html_element.stub font_color: 'red', font_size: '15' }
|
84
93
|
|
85
|
-
its(:bbcode_name) { should eq
|
94
|
+
its(:bbcode_name) { should eq %w(color size) }
|
86
95
|
end
|
87
96
|
end
|
88
97
|
|
@@ -106,7 +115,7 @@ describe Html2Bbcode::Bbcode::Rules do
|
|
106
115
|
|
107
116
|
context 'html element name is: span' do
|
108
117
|
let(:html_name) { 'span' }
|
109
|
-
before {
|
118
|
+
before { html_element.stub styles: style }
|
110
119
|
|
111
120
|
context 'html element does not contain a style' do
|
112
121
|
let(:style) { {} }
|
@@ -14,13 +14,13 @@ describe Html2Bbcode::Html::Attributes do
|
|
14
14
|
before { subject.stub element: html_element }
|
15
15
|
|
16
16
|
context 'attributes does not contain given attribute' do
|
17
|
-
before { html_element.
|
17
|
+
before { allow(html_element).to receive :[] }
|
18
18
|
|
19
19
|
its(:style) { should be_nil }
|
20
20
|
end
|
21
21
|
|
22
22
|
context 'attributes contains given attribute' do
|
23
|
-
before { html_element.
|
23
|
+
before { allow(html_element).to receive(:[]).with('style').and_return 'color: red;' }
|
24
24
|
|
25
25
|
its(:style) { should eq 'color: red;' }
|
26
26
|
end
|
@@ -23,6 +23,12 @@ describe Html2Bbcode::Html::Element do
|
|
23
23
|
its(:process) { should eq 'normal [b] bolded words [i] italic and bolded [/i] bolded [/b] normal'}
|
24
24
|
end
|
25
25
|
|
26
|
+
context 'contains the font tag with color and size' do
|
27
|
+
let(:input_html) { '<font size="10px" color="red">a text</font>' }
|
28
|
+
|
29
|
+
its(:process) { should eq '[color=red][size=10]a text[/size][/color]'}
|
30
|
+
end
|
31
|
+
|
26
32
|
context 'contains the img tag' do
|
27
33
|
let(:input_html) { 'normal <img src="secret.png"></img>' }
|
28
34
|
|
metadata
CHANGED
@@ -1,41 +1,41 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: html2bbcode
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Szymon Kieloch
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-11-
|
11
|
+
date: 2013-11-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri-styles
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - '>='
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - '>='
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: ruby-units
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - '>='
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
@@ -44,16 +44,14 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - ~>
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
48
|
-
My4xMg==
|
47
|
+
version: '3.12'
|
49
48
|
type: :development
|
50
49
|
prerelease: false
|
51
50
|
version_requirements: !ruby/object:Gem::Requirement
|
52
51
|
requirements:
|
53
52
|
- - ~>
|
54
53
|
- !ruby/object:Gem::Version
|
55
|
-
version:
|
56
|
-
My4xMg==
|
54
|
+
version: '3.12'
|
57
55
|
- !ruby/object:Gem::Dependency
|
58
56
|
name: bundler
|
59
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -86,28 +84,28 @@ dependencies:
|
|
86
84
|
name: rspec
|
87
85
|
requirement: !ruby/object:Gem::Requirement
|
88
86
|
requirements:
|
89
|
-
- -
|
87
|
+
- - '>='
|
90
88
|
- !ruby/object:Gem::Version
|
91
89
|
version: '0'
|
92
90
|
type: :development
|
93
91
|
prerelease: false
|
94
92
|
version_requirements: !ruby/object:Gem::Requirement
|
95
93
|
requirements:
|
96
|
-
- -
|
94
|
+
- - '>='
|
97
95
|
- !ruby/object:Gem::Version
|
98
96
|
version: '0'
|
99
97
|
- !ruby/object:Gem::Dependency
|
100
98
|
name: pry
|
101
99
|
requirement: !ruby/object:Gem::Requirement
|
102
100
|
requirements:
|
103
|
-
- -
|
101
|
+
- - '>='
|
104
102
|
- !ruby/object:Gem::Version
|
105
103
|
version: '0'
|
106
104
|
type: :development
|
107
105
|
prerelease: false
|
108
106
|
version_requirements: !ruby/object:Gem::Requirement
|
109
107
|
requirements:
|
110
|
-
- -
|
108
|
+
- - '>='
|
111
109
|
- !ruby/object:Gem::Version
|
112
110
|
version: '0'
|
113
111
|
description: A gem which allows you converting the HTML tags to the BBCode ones
|
@@ -116,13 +114,14 @@ executables: []
|
|
116
114
|
extensions: []
|
117
115
|
extra_rdoc_files:
|
118
116
|
- LICENSE.txt
|
119
|
-
- README.
|
117
|
+
- README.markdown
|
120
118
|
files:
|
121
119
|
- .document
|
120
|
+
- .travis.yml
|
122
121
|
- Gemfile
|
123
122
|
- Gemfile.lock
|
124
123
|
- LICENSE.txt
|
125
|
-
- README.
|
124
|
+
- README.markdown
|
126
125
|
- Rakefile
|
127
126
|
- VERSION
|
128
127
|
- html2bbcode.gemspec
|
@@ -155,17 +154,17 @@ require_paths:
|
|
155
154
|
- lib
|
156
155
|
required_ruby_version: !ruby/object:Gem::Requirement
|
157
156
|
requirements:
|
158
|
-
- -
|
157
|
+
- - '>='
|
159
158
|
- !ruby/object:Gem::Version
|
160
159
|
version: '0'
|
161
160
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
162
161
|
requirements:
|
163
|
-
- -
|
162
|
+
- - '>='
|
164
163
|
- !ruby/object:Gem::Version
|
165
164
|
version: '0'
|
166
165
|
requirements: []
|
167
166
|
rubyforge_project:
|
168
|
-
rubygems_version: 2.
|
167
|
+
rubygems_version: 2.0.6
|
169
168
|
signing_key:
|
170
169
|
specification_version: 4
|
171
170
|
summary: A gem for converting HTML to BBCode
|
data/README.rdoc
DELETED
@@ -1,19 +0,0 @@
|
|
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
|
-
|