markabb 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,16 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.3
5
+ - rbx-18mode
6
+ - rbx-19mode
7
+ - ruby-head
8
+ env:
9
+ - "rake=0.8"
10
+ script: "bundle exec rspec"
11
+ matrix:
12
+ allow_failures:
13
+ - rvm: rbx-18mode
14
+ - rvm: rbx-19mode
15
+ notifications:
16
+ email: false
data/Rakefile CHANGED
@@ -1 +1,2 @@
1
- require "bundler/gem_tasks"
1
+ require 'rubygems'
2
+ require 'bundler/gem_tasks'
data/Readme.md CHANGED
@@ -1,4 +1,20 @@
1
1
  # Markabb
2
- [RDoc](http://rubydoc.info/github/Arcath/Markabb/master/frames) [www](http://markabb.arcath.net)
2
+ [RDoc](http://rubydoc.info/github/Arcath/Markabb/master/frames) | [www](http://markabb.arcath.net) | [Gempage](http://rubygems.org/gems/markabb) | [![Status](https://secure.travis-ci.org/Arcath/Markabb.png?branch=master)](http://travis-ci.org/Arcath/Markabb)
3
3
 
4
- Provides BBCode for Ruby and Rails
4
+ Markabb is an all ruby BBCode parser which can be fully configured and extended.
5
+
6
+ To get the latest stable version install the gem by adding
7
+
8
+ gem 'markabb'
9
+
10
+ to your Gemfile
11
+
12
+ ## Documentation
13
+
14
+ See the [Wiki](https://github.com/Arcath/Markabb/wiki) for the documentation.
15
+
16
+ ## Developing & Testing
17
+
18
+ Makrabb uses RSpec for its tests, to run them just run `rspec` in the root directory.
19
+
20
+ Please make sure that the tests pass before you submit a pull request and if you add any new features please add the tests for them.
@@ -17,9 +17,20 @@ module Markabb
17
17
  def parse
18
18
  disable_html if @config.disable_html
19
19
  add_line_breaks
20
+ highlight_syntax
21
+ no_bbc_tag
20
22
  run_tags
21
23
  end
22
24
 
25
+ def highlight_syntax
26
+ highlighter = SyntaxHighlighters[@config.syntax_highlighter].new(@config)
27
+ @output = highlighter.run(@output)
28
+ end
29
+
30
+ def no_bbc_tag
31
+ apply_tag(Markabb::NobbcTag.new) unless @config[:disable_nobbc]
32
+ end
33
+
23
34
  def run_tags
24
35
  Markabb::Tags.each do |k, v|
25
36
  if v.is_a? Hash
@@ -0,0 +1,9 @@
1
+ # Ruby String object, used to add .markabb
2
+ class String
3
+
4
+ # Runs Markabb parser for the string
5
+ def markabb
6
+ Markabb.parse self do
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,34 @@
1
+ module Markabb
2
+ # The hash which markabb stores all available syntax highlighters in
3
+ SyntaxHighlighters = {}
4
+
5
+ # Adds a syntac highlighter to the hash
6
+ def self.register_highlighter(name, highlighter)
7
+ SyntaxHighlighters[name] = highlighter
8
+ end
9
+
10
+ # Removes a syntax highlighter from the hash
11
+ def self.remove_highlighter(name)
12
+ SyntaxHighlighters.delete(name)
13
+ end
14
+
15
+ # Super class for syntax highlighters
16
+ #
17
+ # All syntax highlighters need to inherit from this class
18
+ class SyntaxHighlighter
19
+ def initialize(config)
20
+ @config = config
21
+ end
22
+
23
+ # Runs the syntax highlighter
24
+ #
25
+ # Finds all instances of the code tag and passes it to the parse function
26
+ def run(s)
27
+ out = s
28
+ out.scan(/\[code lang=(.+?)\](.+?)\[\/code\]/m).each do |lang, parse|
29
+ out = out.gsub("[code lang=#{lang}]#{parse}[/code]", parse(lang, parse))
30
+ end
31
+ return out
32
+ end
33
+ end
34
+ end
@@ -15,7 +15,9 @@ module Markabb
15
15
  #
16
16
  # {:bar => {:foo => Markabb::Tag}}
17
17
  def self.register_tag(name, tag, group = nil)
18
+ tag.name = name
18
19
  if group
20
+ tag.group = group
19
21
  Tags[group] ||= {}
20
22
  Tags[group][name] = tag
21
23
  else
@@ -33,6 +35,7 @@ module Markabb
33
35
  # Takes a REGEX matcher and a string to replace it with
34
36
  class Tag
35
37
  attr_reader :matcher, :replace
38
+ attr_accessor :name, :group
36
39
 
37
40
  # Creates the Markabb::Tag object
38
41
  def initialize(matcher, replace)
@@ -1,6 +1,6 @@
1
1
  module Markabb
2
2
  # The config class, inherits from Hash
3
- # Borrowed from mina (https://github.com/nadarei/mina)
3
+ # Based on the config class from mina (https://github.com/nadarei/mina)
4
4
  class Config < Hash
5
5
  def initialize
6
6
  super
@@ -42,6 +42,7 @@ module Markabb
42
42
  self[:url_target] ||= "_BLANK"
43
43
  self[:image_alt] ||= "Posted Image"
44
44
  self[:table_width] ||= "100%"
45
+ self[:syntax_highlighter] ||= :raw
45
46
  end
46
47
  end
47
48
  end
@@ -0,0 +1,27 @@
1
+ module Markabb
2
+ module Highlighters
3
+ # Raw syntax highlighter
4
+ #
5
+ # Just wraps the code in a specified tag.
6
+ class Raw < Markabb::SyntaxHighlighter
7
+ # Parses the inputted text, takes 2 options, the language of the code and the code.
8
+ def parse(lang, s)
9
+ return "<#{open_tag}>[nobbc]#{s}[/nobbc]</#{close_tag}>"
10
+ end
11
+
12
+ private
13
+
14
+ def open_tag
15
+ tag = (@config.raw_highlighter_tag || "code")
16
+ return tag + " #{@config.raw_highlighter_tag_options}" if @config.raw_highlighter_tag_options
17
+ return tag
18
+ end
19
+
20
+ def close_tag
21
+ (@config.raw_highlighter_tag || "code")
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ Markabb.register_highlighter(:raw, Markabb::Highlighters::Raw)
@@ -0,0 +1,15 @@
1
+ module Markabb
2
+ # NoBBC Tag class, can't use the Markabb::Tag class as this one needs special parsing
3
+ class NobbcTag
4
+ attr_accessor :name, :group
5
+
6
+ # Runs the NoBBC Tag
7
+ #
8
+ # The same as Markabb::Tag.parse
9
+ def run(s, config)
10
+ s.gsub(/\[nobbc\](.*?)\[\/nobbc\]/) { |b| b.gsub(/\[nobbc\]/,'').gsub(/\[\/nobbc\]/,'').gsub("[","&#91;").gsub("]","&#93") }
11
+ end
12
+ end
13
+ end
14
+
15
+ # Markabb.register_tag :nobbc, Markabb::NobbcTag.new
@@ -1,4 +1,6 @@
1
1
  Markabb.register_tag :table, Markabb::Tag.new(/\[table\](.*?)\[\/table\]/, '<table width="config[:table_width]">\1</table>'), :table
2
- Markabb.register_tag :table_row, Markabb::Tag.new(/\[tr\](.*?)\[\/tr\]/, '<tr>\1</tr>'), :table
3
- Markabb.register_tag :table_cell, Markabb::Tag.new(/\[td\](.*?)\[\/td\]/, '<td>\1</td>'), :table
2
+ Markabb.register_tag :table_tr, Markabb::Tag.new(/\[tr\](.*?)\[\/tr\]/, '<tr>\1</tr>'), :table
3
+ Markabb.register_tag :table_td, Markabb::Tag.new(/\[td\](.*?)\[\/td\]/, '<td>\1</td>'), :table
4
4
  Markabb.register_tag :table_header, Markabb::Tag.new(/\[th\](.*?)\[\/th\]/, '<th>\1</th>'), :table
5
+ Markabb.register_tag :table_row, Markabb::Tag.new(/\[row\](.*?)\[\/row\]/, '<tr>\1</tr>'), :table
6
+ Markabb.register_tag :table_cell, Markabb::Tag.new(/\[cell\](.*?)\[\/cell\]/, '<td>\1</td>'), :table
@@ -1,4 +1,4 @@
1
1
  module Markabb
2
2
  # Markabbs Version Number
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
data/lib/markabb.rb CHANGED
@@ -5,6 +5,10 @@ require "markabb/version"
5
5
  # Clases
6
6
  require "markabb/classes/parser"
7
7
  require "markabb/classes/tag"
8
+ require "markabb/classes/string"
9
+ require "markabb/classes/syntax_highlighter"
10
+ # Highlighters
11
+ require "markabb/highlighters/raw"
8
12
 
9
13
  # Main Markabb Module, all code is a sub of this
10
14
  module Markabb
@@ -22,6 +26,10 @@ module Markabb
22
26
  def self.config
23
27
  @config
24
28
  end
29
+
30
+ # Contains all stock provided syntax highlighters
31
+ module Highlighters
32
+ end
25
33
  end
26
34
 
27
35
  # Tags
@@ -29,4 +37,5 @@ require "markabb/tags/formatting"
29
37
  require "markabb/tags/image"
30
38
  require "markabb/tags/link"
31
39
  require "markabb/tags/list"
40
+ require "markabb/tags/nobbc"
32
41
  require "markabb/tags/table"
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe Markabb::Highlighters::Raw do
4
+ it "should be able to parse a code tag" do
5
+ "[code lang=ruby]def foo(bar)
6
+ return bar
7
+ end[/code]".markabb.should eq "<code>def foo(bar)<br /> return bar<br /> end</code>"
8
+ end
9
+
10
+ it "should let you change the wrapping tag" do
11
+ Markabb.configure do |c|
12
+ c.raw_highlighter_tag = "div"
13
+ end
14
+ "[code lang=ruby]def foo(bar)
15
+ return bar
16
+ end[/code]".markabb.should eq "<div>def foo(bar)<br /> return bar<br /> end</div>"
17
+ end
18
+
19
+ it "should let you change html options of the tag" do
20
+ Markabb.configure do |c|
21
+ c.raw_highlighter_tag = "div"
22
+ c.raw_highlighter_tag_options = "class=\"well\""
23
+ end
24
+ "[code lang=ruby]def foo(bar)
25
+ return bar
26
+ end[/code]".markabb.should eq "<div class=\"well\">def foo(bar)<br /> return bar<br /> end</div>"
27
+ end
28
+
29
+ it "should be able to parse 2 code tags" do
30
+ default_config
31
+ "[code lang=ruby]def foo(bar)
32
+ return bar
33
+ end[/code] foo bar foo
34
+ [code lang=ruby]:foo => :bar[/code]".markabb.should eq "<code>def foo(bar)<br /> return bar<br /> end</code> foo bar foo<br /> <code>:foo =&gt; :bar</code>"
35
+ end
36
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe String do
4
+ it "should run markabb with the default config for #markabb" do
5
+ Markabb.blank_config
6
+ "[b]this[/b]".markabb.should eq "<b>this</b>"
7
+ end
8
+
9
+ it "should inherit from main config" do
10
+ default_config
11
+ Markabb.configure do |c|
12
+ c.disable_bold = true
13
+ end
14
+ "[b]this[/b]".markabb.should eq "[b]this[/b]"
15
+ end
16
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Syntax Highlighters" do
4
+ it "should have the SytaxHighlighters hash" do
5
+ Markabb::SyntaxHighlighters.should be_a Hash
6
+ end
7
+
8
+ it "should allow for highlighter registration" do
9
+ Markabb.register_highlighter(:foo, Markabb::SyntaxHighlighter.new(Markabb.config))
10
+ Markabb::SyntaxHighlighters[:foo].should be_a Markabb::SyntaxHighlighter
11
+ end
12
+
13
+ it "should allow for highlighter deletion" do
14
+ Markabb.remove_highlighter(:foo)
15
+ end
16
+
17
+ it "should have a run action" do
18
+ syntax_highlighter = Markabb::SyntaxHighlighter.new(Markabb.config)
19
+ syntax_highlighter.run("foo")
20
+ end
21
+ end
@@ -18,4 +18,17 @@ describe Markabb::Tag do
18
18
  it "should have the bold tag" do
19
19
  Markabb::Tags[:formatting][:bold].should be_a Markabb::Tag
20
20
  end
21
+
22
+ it "should have a name" do
23
+ sample_tag.name.should be_a Symbol
24
+ end
25
+
26
+ it "should have a group" do
27
+ sample_tag.group.should be_a Symbol
28
+ end
29
+
30
+ it "should have the nobbc tag" do
31
+ default_config
32
+ "blah [nobbc][b]bold[/b][/nobbc] blah".markabb.should eq "blah &#91;b&#93bold&#91;/b&#93 blah"
33
+ end
21
34
  end
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # Macros
2
2
  require "macros/formatting_macros"
3
3
  # Require Markabb
4
- require 'lib/markabb'
4
+ require 'markabb'
5
5
 
6
6
  module Markabb
7
7
  def self.blank_config
@@ -12,4 +12,8 @@ end
12
12
  def default_config
13
13
  Markabb.configure do |c|
14
14
  end
15
+ end
16
+
17
+ def sample_tag
18
+ Markabb.register_tag :test, Markabb::Tag.new(/foo/, 'bar'), :group
15
19
  end
@@ -6,9 +6,6 @@ describe "Formatting Tags" do
6
6
  it_should_apply_formatting ['bold','b'],
7
7
  ['italic','i'],
8
8
  ['underline','u'],
9
- ['un_ordered', 'ul'],
10
- ['ordered', 'ol'],
11
- ['list_item', 'li'],
12
9
  ['center', 'center'],
13
10
  ['left', 'left'],
14
11
  ['right', 'right'],
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe "List Tags" do
4
+ include MarkabbMacros::Formatting
5
+
6
+ it_should_apply_formatting ['un_ordered', 'ul'],
7
+ ['ordered', 'ol'],
8
+ ['list_item', 'li']
9
+ end
@@ -4,8 +4,8 @@ describe "Table Tags" do
4
4
  include MarkabbMacros::Formatting
5
5
 
6
6
  it_should_apply_formatting ['table_header', 'th'],
7
- ['table_row', 'tr'],
8
- ['table_cell', 'td']
7
+ ['table_tr', 'tr'],
8
+ ['table_td', 'td']
9
9
 
10
10
  it "should draw a table with a width" do
11
11
  Markabb.parse("[table]table[/table]").should eq("<table width=\"100%\">table</table>")
@@ -16,4 +16,12 @@ describe "Table Tags" do
16
16
  c.table_width = "75%"
17
17
  end.should eq("<table width=\"75%\">table</table>")
18
18
  end
19
+
20
+ it "should work for [row]" do
21
+ "[row]..[/row]".markabb.should eq "<tr>..</tr>"
22
+ end
23
+
24
+ it "should work for [cell]" do
25
+ "[cell]..[/cell]".markabb.should eq "<td>..</td>"
26
+ end
19
27
  end
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: markabb
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
5
- prerelease:
4
+ prerelease: false
6
5
  segments:
7
6
  - 0
8
7
  - 0
9
- - 1
10
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
11
10
  platform: ruby
12
11
  authors:
13
12
  - Adam "Arcath" Laycock
@@ -15,18 +14,16 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2012-06-24 00:00:00 +01:00
17
+ date: 2012-07-06 00:00:00 +01:00
19
18
  default_executable:
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
21
  name: rspec
23
22
  prerelease: false
24
23
  requirement: &id001 !ruby/object:Gem::Requirement
25
- none: false
26
24
  requirements:
27
25
  - - ">="
28
26
  - !ruby/object:Gem::Version
29
- hash: 3
30
27
  segments:
31
28
  - 0
32
29
  version: "0"
@@ -43,29 +40,38 @@ extra_rdoc_files: []
43
40
 
44
41
  files:
45
42
  - .gitignore
43
+ - .travis.yml
46
44
  - Gemfile
47
45
  - Rakefile
48
46
  - Readme.md
49
47
  - lib/markabb.rb
50
48
  - lib/markabb/classes/parser.rb
49
+ - lib/markabb/classes/string.rb
50
+ - lib/markabb/classes/syntax_highlighter.rb
51
51
  - lib/markabb/classes/tag.rb
52
52
  - lib/markabb/config.rb
53
+ - lib/markabb/highlighters/raw.rb
53
54
  - lib/markabb/parse.rb
54
55
  - lib/markabb/tags/formatting.rb
55
56
  - lib/markabb/tags/image.rb
56
57
  - lib/markabb/tags/link.rb
57
58
  - lib/markabb/tags/list.rb
59
+ - lib/markabb/tags/nobbc.rb
58
60
  - lib/markabb/tags/table.rb
59
61
  - lib/markabb/version.rb
60
62
  - markabb.gemspec
63
+ - spec/highlighters/raw_spec.rb
61
64
  - spec/macros/formatting_macros.rb
62
65
  - spec/markabb_parse_spec.rb
63
66
  - spec/markabb_spec.rb
67
+ - spec/markabb_string_spec.rb
68
+ - spec/markabb_syntax_highlighter_spec.rb
64
69
  - spec/markabb_tags_spec.rb
65
70
  - spec/spec_helper.rb
66
71
  - spec/tags/formatting_spec.rb
67
72
  - spec/tags/image_spec.rb
68
73
  - spec/tags/link_spec.rb
74
+ - spec/tags/list_spec.rb
69
75
  - spec/tags/table_spec.rb
70
76
  has_rdoc: true
71
77
  homepage: http://markabb.arcath.net
@@ -77,37 +83,37 @@ rdoc_options: []
77
83
  require_paths:
78
84
  - lib
79
85
  required_ruby_version: !ruby/object:Gem::Requirement
80
- none: false
81
86
  requirements:
82
87
  - - ">="
83
88
  - !ruby/object:Gem::Version
84
- hash: 3
85
89
  segments:
86
90
  - 0
87
91
  version: "0"
88
92
  required_rubygems_version: !ruby/object:Gem::Requirement
89
- none: false
90
93
  requirements:
91
94
  - - ">="
92
95
  - !ruby/object:Gem::Version
93
- hash: 3
94
96
  segments:
95
97
  - 0
96
98
  version: "0"
97
99
  requirements: []
98
100
 
99
101
  rubyforge_project:
100
- rubygems_version: 1.4.2
102
+ rubygems_version: 1.3.6
101
103
  signing_key:
102
104
  specification_version: 3
103
105
  summary: Provides BBCode for Ruby and Rails
104
106
  test_files:
107
+ - spec/highlighters/raw_spec.rb
105
108
  - spec/macros/formatting_macros.rb
106
109
  - spec/markabb_parse_spec.rb
107
110
  - spec/markabb_spec.rb
111
+ - spec/markabb_string_spec.rb
112
+ - spec/markabb_syntax_highlighter_spec.rb
108
113
  - spec/markabb_tags_spec.rb
109
114
  - spec/spec_helper.rb
110
115
  - spec/tags/formatting_spec.rb
111
116
  - spec/tags/image_spec.rb
112
117
  - spec/tags/link_spec.rb
118
+ - spec/tags/list_spec.rb
113
119
  - spec/tags/table_spec.rb