rubbcoder 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/rubbcoder.rb +142 -0
  3. metadata +58 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 403ac0bc747d23c8796d69894bcc28defb60211c
4
+ data.tar.gz: 4cb0793c271c637d90ca87e816fe79d1e4bb60d9
5
+ SHA512:
6
+ metadata.gz: e1f03cc810c18a78e1153503016cbf26556c10569e4980e42035a07af84df0dfbfd74b00558bebb803fef5ccadc10f594fb98c5a5176bb01b87fc6a8fdb4fef5
7
+ data.tar.gz: c89ddf9c7ddd550e657ca04522e7b498ba1a88b25109c21e9941e129280d789ed641c3c19542c4f07a348da903e9c5cdd2df2aa62e3340361ce616c2901d2177
@@ -0,0 +1,142 @@
1
+ class RuBBCoder
2
+
3
+ class Tag
4
+
5
+ attr_accessor :coder
6
+ attr_reader :block
7
+
8
+ def initialize(text, opts = {}, &blk)
9
+ @coder = nil
10
+ @text = text
11
+ @example = opts[:example]
12
+ @decription = opts[:description]
13
+ @block = opts[:block]
14
+ @block = true if @block.nil?
15
+ @front = opts[:front]
16
+ @back = opts[:back] if @block
17
+ @custom = blk
18
+ end
19
+
20
+ def encode(contents, attribute)
21
+ attribute.gsub!(/^=/, "") if attribute
22
+ attribute = "" if attribute.nil?
23
+ if @custom
24
+ return @custom.call(contents, attribute, @coder.options)
25
+ elsif @block
26
+ return "#{@front}#{contents}#{@back}"
27
+ else
28
+ return @front
29
+ end
30
+ end
31
+
32
+ def self.clean_css(css)
33
+ css = css.split(";").first
34
+ return css.split("\"").first
35
+ end
36
+ end
37
+
38
+ attr_reader :options
39
+
40
+ def initialize(extra_codes = {}, disable_list = [], opts={})
41
+ @tags = RuBBCoder::DEFAULT_TAGS.clone
42
+ disable_list.each{|key| @tags.delete(key.to_s.downcase)}
43
+ @tags.merge!(extra_codes)
44
+ @tags.each {|k,tag| tag.coder = self }
45
+
46
+ @options = {:text_size_max => 40, :text_size_min => 8, :video_width => 400, :video_height => 300}
47
+ @options = @options.merge(opts)
48
+ @options[:coder] = self
49
+ end
50
+
51
+ def to_html(bbcode)
52
+ html = bbcode.clone
53
+ #html.gsub!(/\n/, "<br>")
54
+ # block tags
55
+ /(.*)\[([A-Za-z]+)(=.*)?\](.*)\[\/\2\](.*)/m.match(html) { |data|
56
+ tag = @tags[data[2].downcase]
57
+ if(tag)
58
+ html = "#{data[1]}#{tag.encode(data[4], data[3])}#{data[5]}"
59
+ else
60
+ html = "#{data[1]}&#91;#{data[2]}#{data[3]}&#93;#{to_html(data[4])}&#91;/#{data[2]}&#93;#{to_html(data[5])}"
61
+ end
62
+ # puts "HTML before R: #{html}"
63
+ html = to_html(html)
64
+ # puts "HTML after: #{html}"
65
+ }
66
+ # non block tags
67
+ /(.*)\[([A-Za-z]+)(=.*)?\](.*)/.match(html) { |data|
68
+ tag = @tags[data[2].downcase]
69
+ #puts data[2].to_sym
70
+ #puts tag.inspect
71
+ if(tag && tag.block == false)
72
+ html = "#{data[1]}#{tag.encode(nil, data[3])}#{data[4]}"
73
+ else
74
+ html = "#{data[1]}&#91;#{data[2]}#{data[3]}&#93;#{to_html(data[4])}"
75
+ end
76
+ html = to_html(html)
77
+ }
78
+
79
+ return html
80
+ end
81
+
82
+ DEFAULT_TAGS = {
83
+ "br" => RuBBCoder::Tag.new("br", :example => "Line 1[br]line 2", :description => "Use [br] for new line.",
84
+ :block => false, :front => "<br>\n"
85
+ ),
86
+ "b" => RuBBCoder::Tag.new("b", :example => "normal [b]BOLD text[/b] normal",
87
+ :description => "Use [b] for bold text, end with [/b].",
88
+ :front => "<strong>", :back => "</strong>"
89
+ ),
90
+ "i" => RuBBCoder::Tag.new("i", :example => "normal [i]ITALIC text[/i] normal",
91
+ :description => "Use [i] for italic text, end with [/i].",
92
+ front: "<em>", back: "</em>"
93
+ ),
94
+ "u" => RuBBCoder::Tag.new("u", :front => "<u>", :back => "</u>", :example => "normal [u]underlined text[/u] normal",
95
+ :description => "Use [u] for underline text, end with [/u]."
96
+ ),
97
+ "s" => RuBBCoder::Tag.new("s", :front => "<del>", :back => "</del>",
98
+ :example => "normal [s]strike-through text[/s] noraml",
99
+ :description => "Use [s] for strike-through text, end with [/s]."
100
+ ),
101
+ "color" => RuBBCoder::Tag.new("color", :example => "normal [color=blue]blue text[/color] normal",
102
+ :description => "Change color of your text with [color=tag], end with [/color].") {|c, a, o|
103
+ "<span style=\"color:#{a.length > 2 ? Tag.clean_css(a) : 'red'};\">#{c}</span>"
104
+ },
105
+ "list" => RuBBCoder::Tag.new("list", :front => "<ul>", :back => "</ul>",
106
+ :example => "[list] [*] item 1 [*] item 2 [/list]",
107
+ :description => "Creates a list of items with [*] as bullets."
108
+ ){ |c, a, o|
109
+ c = o[:coder].to_html(c)
110
+ lis = c.split("[*]").collect{|li| li.strip}
111
+ lis.delete_at(0)
112
+ c = "<ul>\n<li>" + lis.join("</li>\n<li>") + "</li>\n</ul>"
113
+ },
114
+ "size" => RuBBCoder::Tag.new("size", :example => "normal [size=30]big text[/size] normal",
115
+ :description => "[size] tag used to alter the size in pixels of the text."
116
+ ){ |c, a, o|
117
+ max = o[:text_size_max]
118
+ min = o[:text_size_min]
119
+ a = a.to_i
120
+ a = max if a > max
121
+ a = min if a < min
122
+ "<span style=\"font-size:#{a}px;\">#{c}</span>"
123
+ },
124
+ "img" => RuBBCoder::Tag.new("img", :example => "[img]http://www.enmasse.com/game.png[/img]",
125
+ :description => "[img] tag used to place an image in the forums.") {|c, a| "<img src=\"#{c}\">" },
126
+ "url" => RuBBCoder::Tag.new("url", :example => "[url=http://tera.enmasse.com]Link Text[/url]",
127
+ :description => "make a link with ") {|c, a| "<a href=\"#{a || c}\">#{c}</a>" },
128
+ "table" => RuBBCoder::Tag.new("table", :front => "<table>\n", :back => "</table>\n",
129
+ :example => "[table][tr][td]data[/td][/tr][/table]",
130
+ :description => "Makes a table."
131
+ ),
132
+ "tr" => RuBBCoder::Tag.new("tr", :front => "<tr>", :back => "</tr>\n"),
133
+ "td" => RuBBCoder::Tag.new("td", :front => "<td>", :back => "</td>"),
134
+ "quote" => RuBBCoder::Tag.new("quote", :front => "<blockquote>", :back => "</blockquote>"),
135
+ "youtube" => RuBBCoder::Tag.new("youtube") {|c, a| <<-YOUTUBE
136
+ <iframe width="#{opts[:video_width]}" height="#{opts[:video_height]}" src="//youtube.com/embed/#{attribute}">
137
+ </iframe>
138
+ YOUTUBE
139
+ }
140
+ }
141
+
142
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubbcoder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Chris Reister
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: BBCode to HTML for Ruby based apps.
28
+ email: creister@enmasse.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/rubbcoder.rb
34
+ homepage: http://rubygems.org/gems/rubbcoder
35
+ licenses:
36
+ - MIT
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 2.1.5
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: BBCode to HTML for Ruby based apps.
58
+ test_files: []