markabb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .DS_STORE
6
+ doc/*
7
+ .yardoc/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in markabb.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/Readme.md ADDED
@@ -0,0 +1,4 @@
1
+ # Markabb
2
+ [RDoc](http://rubydoc.info/github/Arcath/Markabb/master/frames) [www](http://markabb.arcath.net)
3
+
4
+ Provides BBCode for Ruby and Rails
@@ -0,0 +1,50 @@
1
+ module Markabb
2
+ # Markab parser
3
+ # Takes a Markabb::Config object and a string as an input
4
+ class Parser
5
+ attr_reader :output
6
+
7
+ # Creates the parser object
8
+ def initialize(config, s)
9
+ @string = s
10
+ @config = config
11
+ @output = @string
12
+ parse
13
+ end
14
+
15
+ private
16
+
17
+ def parse
18
+ disable_html if @config.disable_html
19
+ add_line_breaks
20
+ run_tags
21
+ end
22
+
23
+ def run_tags
24
+ Markabb::Tags.each do |k, v|
25
+ if v.is_a? Hash
26
+ unless @config["disable_#{k}".to_sym]
27
+ v.each do |sk, sv|
28
+ apply_tag(sv) unless @config["disable_#{sk}".to_sym]
29
+ end
30
+ end
31
+ else
32
+ apply_tag(v)
33
+ end
34
+ end
35
+ end
36
+
37
+ def apply_tag(tag)
38
+ @output = tag.run(@output, @config)
39
+ end
40
+
41
+ def disable_html
42
+ @output = @output.gsub("<","&lt;").gsub(">","&gt;")
43
+ end
44
+
45
+ def add_line_breaks
46
+ @output = @output.gsub(/\n/, '<br />')
47
+ @output = @output.gsub(/<br \/>\[(ul|li|ol|tr|td|th)/,'[\1').gsub(/<br \/>\[\/(ul|li|ol|table|tr|td|th)/,'[/\1')
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,61 @@
1
+ module Markabb
2
+ # The hash which markabb stores all the avaliable tags in
3
+ Tags = {}
4
+
5
+ # Inserts a tag into Markabb::Tags
6
+ #
7
+ # Takes 3 inputs:
8
+ # name - a symbol which is used as its key
9
+ # tag - a Markabb::Tag object
10
+ # group - a way to group similar tags so they can all be disabled
11
+ #
12
+ # register_tag(:foo, Markabb::Tag, :bar)
13
+ #
14
+ # would produce a tags hash of
15
+ #
16
+ # {:bar => {:foo => Markabb::Tag}}
17
+ def self.register_tag(name, tag, group = nil)
18
+ if group
19
+ Tags[group] ||= {}
20
+ Tags[group][name] = tag
21
+ else
22
+ Tags[name] = tag
23
+ end
24
+ end
25
+
26
+ # Removes a tag from Markabb::Tags
27
+ # takes the symbol for the key to delete
28
+ def self.remove_tag(name)
29
+ Tags.delete(name)
30
+ end
31
+
32
+ # Used for all tags (passed to register_tag)
33
+ # Takes a REGEX matcher and a string to replace it with
34
+ class Tag
35
+ attr_reader :matcher, :replace
36
+
37
+ # Creates the Markabb::Tag object
38
+ def initialize(matcher, replace)
39
+ @matcher = matcher
40
+ @replace = replace
41
+ end
42
+
43
+ # Runs the tag on the input string
44
+ #
45
+ # Takes the target string and a Markabb::Config object
46
+ def run(s, config)
47
+ s.gsub(@matcher, generate_replacement(@replace, config))
48
+ end
49
+
50
+ # Generates the replacement string
51
+ #
52
+ # Takes the replacement string and a config object
53
+ def generate_replacement(replace, config)
54
+ if replace.scan(/config\[:(.*?)\]/) != [] then
55
+ return replace.gsub(/config\[(.*?)\]/, config[$1.to_sym])
56
+ else
57
+ return replace
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,47 @@
1
+ module Markabb
2
+ # The config class, inherits from Hash
3
+ # Borrowed from mina (https://github.com/nadarei/mina)
4
+ class Config < Hash
5
+ def initialize
6
+ super
7
+ load_default_config
8
+ end
9
+
10
+ # Allows access to the hash though methods
11
+ # Provides all getter, setter and query methods for any key in the hash
12
+ def method_missing(meth, *args, &blk)
13
+ name = meth.to_s
14
+ return evaluate(self[meth]) if name.size == 1
15
+ # Ruby 1.8.7 doesn't let you do string[-1]
16
+ key, suffix = name[0..-2].to_sym, name[-1..-1]
17
+ case suffix
18
+ when '='
19
+ self[key] = args.first
20
+ when '?'
21
+ include? key
22
+ when '!'
23
+ raise Error, "Setting :#{key} is not set" unless include?(key)
24
+ evaluate self[key]
25
+ else
26
+ evaluate self[meth]
27
+ end
28
+ end
29
+
30
+ # Returns a value or runs a proc depending on the value in the hash
31
+ def evaluate(value)
32
+ if value.is_a?(Proc)
33
+ value.call
34
+ else
35
+ value
36
+ end
37
+ end
38
+
39
+ # Sets the default config values
40
+ def load_default_config
41
+ self[:disable_html] ||= true
42
+ self[:url_target] ||= "_BLANK"
43
+ self[:image_alt] ||= "Posted Image"
44
+ self[:table_width] ||= "100%"
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,29 @@
1
+ module Markabb
2
+ # Passes the string to an instance of Markabb::Parser
3
+ #
4
+ # Can be passed a block to change the config
5
+ def self.parse(s)
6
+ # Set the config
7
+ config = @config
8
+ if block_given?
9
+ config = Config.new unless @config
10
+ yield(config)
11
+ else
12
+ raise "Please configure Markabb before using it" unless @config
13
+ end
14
+ output = Parser.new(config, s).output
15
+ if needs_html_safe?
16
+ return output.html_safe
17
+ else
18
+ return output
19
+ end
20
+ end
21
+
22
+ def self.needs_html_safe?
23
+ if defined? Rails
24
+ return Rails.version =~ /^3\./
25
+ else
26
+ return false
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,10 @@
1
+ Markabb.register_tag :bold, Markabb::Tag.new(/\[b\](.*?)\[\/b\]/, '<b>\1</b>'), :formatting
2
+ Markabb.register_tag :italic, Markabb::Tag.new(/\[i\](.*?)\[\/i\]/, '<i>\1</i>'), :formatting
3
+ Markabb.register_tag :underline, Markabb::Tag.new(/\[u\](.*?)\[\/u\]/, '<u>\1</u>'), :formatting
4
+ Markabb.register_tag :center, Markabb::Tag.new(/\[center\](.*?)\[\/center\]/, '<center>\1</center>'), :formatting
5
+ Markabb.register_tag :left, Markabb::Tag.new(/\[left\](.*?)\[\/left\]/, '<left>\1</left>'), :formatting
6
+ Markabb.register_tag :right, Markabb::Tag.new(/\[right\](.*?)\[\/right\]/, '<right>\1</right>'), :formatting
7
+ Markabb.register_tag :heading_1, Markabb::Tag.new(/\[h1\](.*?)\[\/h1\]/, '<h1>\1</h1>'), :formatting
8
+ Markabb.register_tag :heading_2, Markabb::Tag.new(/\[h2\](.*?)\[\/h2\]/, '<h2>\1</h2>'), :formatting
9
+ Markabb.register_tag :heading_3, Markabb::Tag.new(/\[h3\](.*?)\[\/h3\]/, '<h3>\1</h3>'), :formatting
10
+ Markabb.register_tag :heading_4, Markabb::Tag.new(/\[h4\](.*?)\[\/h4\]/, '<h4>\1</h4>'), :formatting
@@ -0,0 +1,2 @@
1
+ Markabb.register_tag :image, Markabb::Tag.new(/\[img\](.*?)\[\/img\]/, '<img src="\1" alt="config[:image_alt]" />'), :image
2
+ Markabb.register_tag :image_with_alt, Markabb::Tag.new(/\[img alt=(.*?)\](.*?)\[\/img\]/, '<img src="\2" alt="\1" />'), :image
@@ -0,0 +1,2 @@
1
+ Markabb.register_tag :url, Markabb::Tag.new(/\[url\](.*?)\[\/url\]/, '<a href="\1" target="config[:url_target]">\1</a>'), :link
2
+ Markabb.register_tag :url_with_title, Markabb::Tag.new(/\[url=(.*?)\](.*?)\[\/url\]/, '<a href="\1" target="config[:url_target]">\2</a>'), :link
@@ -0,0 +1,3 @@
1
+ Markabb.register_tag :un_ordered, Markabb::Tag.new(/\[ul\](.*?)\[\/ul\]/, '<ul>\1</ul>'), :list
2
+ Markabb.register_tag :ordered, Markabb::Tag.new(/\[ol\](.*?)\[\/ol\]/, '<ol>\1</ol>'), :list
3
+ Markabb.register_tag :list_item, Markabb::Tag.new(/\[li\](.*?)\[\/li\]/, '<li>\1</li>'), :list
@@ -0,0 +1,4 @@
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
4
+ Markabb.register_tag :table_header, Markabb::Tag.new(/\[th\](.*?)\[\/th\]/, '<th>\1</th>'), :table
@@ -0,0 +1,4 @@
1
+ module Markabb
2
+ # Markabbs Version Number
3
+ VERSION = "0.0.1"
4
+ end
data/lib/markabb.rb ADDED
@@ -0,0 +1,32 @@
1
+ # Top Level Requires
2
+ require "markabb/config"
3
+ require "markabb/parse"
4
+ require "markabb/version"
5
+ # Clases
6
+ require "markabb/classes/parser"
7
+ require "markabb/classes/tag"
8
+
9
+ # Main Markabb Module, all code is a sub of this
10
+ module Markabb
11
+ # Exposes a new config object to a block
12
+ # Called with:
13
+ # Markabb.configure do |c|
14
+ # c.foo = 'bar'
15
+ # end
16
+ def self.configure
17
+ @config = Config.new
18
+ yield @config
19
+ end
20
+
21
+ # Exposes the config object for use in your code
22
+ def self.config
23
+ @config
24
+ end
25
+ end
26
+
27
+ # Tags
28
+ require "markabb/tags/formatting"
29
+ require "markabb/tags/image"
30
+ require "markabb/tags/link"
31
+ require "markabb/tags/list"
32
+ require "markabb/tags/table"
data/markabb.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "markabb/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "markabb"
7
+ s.version = Markabb::VERSION
8
+ s.authors = ["Adam \"Arcath\" Laycock"]
9
+ s.email = ["gems@arcath.net"]
10
+ s.homepage = "http://markabb.arcath.net"
11
+ s.summary = %q{Provides BBCode for Ruby and Rails}
12
+ s.description = %q{Provides BBCode for Ruby and Rails}
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ s.add_development_dependency "rspec"
20
+ end
@@ -0,0 +1,25 @@
1
+ module MarkabbMacros
2
+ module Formatting
3
+ def self.included(base)
4
+ base.extend(ClassMethods)
5
+ end
6
+
7
+ module ClassMethods
8
+ def it_should_apply_formatting(*formats)
9
+ formats.each do |format|
10
+ it "should apply the #{format[0]} tag to text" do
11
+ default_config
12
+ Markabb.parse("[#{format[1]}]#{format[0]}[/#{format[1]}]").should eq("<#{format[1]}>#{format[0]}</#{format[1]}>")
13
+ end
14
+
15
+ it "should not apply the bold tag to text if it is disbaled" do
16
+ default_config
17
+ Markabb.parse "[#{format[1]}]#{format[0]}[/#{format[1]}]" do |c|
18
+ c["disable_#{format[0]}".to_sym] = true
19
+ end.should eq("[#{format[1]}]#{format[0]}[/#{format[1]}]")
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ describe Markabb, "#parse" do
4
+ it "should take a string" do
5
+ default_config
6
+ Markabb.parse "this"
7
+ end
8
+
9
+ it "should raise a exception if Markabb is not configured" do
10
+ Markabb.blank_config
11
+ lambda { Markabb.parse "this" }.should raise_exception
12
+ end
13
+
14
+ it "should allow for config via a block" do
15
+ default_config
16
+ Markabb.parse "this" do |c|
17
+ c.foo = 'bar'
18
+ end
19
+ end
20
+
21
+ it "should allow for no config if using a block" do
22
+ Markabb.blank_config
23
+ Markabb.parse "this" do |c|
24
+ c.foo = 'bar'
25
+ end
26
+ end
27
+
28
+ it "should disable html" do
29
+ default_config
30
+ Markabb.parse("<b>BOLD</b>").should eq "&lt;b&gt;BOLD&lt;/b&gt;"
31
+ end
32
+
33
+ it "should allow html if told to" do
34
+ default_config
35
+ Markabb.parse "<b>BOLD</b>" do |c|
36
+ c.disable_html = false
37
+ end.should eq "<b>BOLD</b>"
38
+ end
39
+
40
+ it "should add line breaks" do
41
+ default_config
42
+ Markabb.parse("this\nthat").should eq "this<br />that"
43
+ end
44
+
45
+ it "should correct line breaks" do
46
+ default_config
47
+ Markabb.parse("this\nthat\n[table]\n[tr][/tr]\n[/table]").should eq "this<br />that<br /><table width=\"100%\"><tr></tr></table>"
48
+ end
49
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe Markabb do
4
+ it "should take a block" do
5
+ Markabb.configure do |c|
6
+ end
7
+ end
8
+
9
+ it "should store config" do
10
+ Markabb.configure do |c|
11
+ c.foo = 'bar'
12
+ end
13
+
14
+ Markabb.config.foo.should eq 'bar'
15
+ end
16
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe Markabb::Tag do
4
+ it "should have a tags hash" do
5
+ Markabb::Tags.should be_a Hash
6
+ end
7
+
8
+ it "should allow tag registration" do
9
+ Markabb.register_tag :foo, Markabb::Tag.new(/foo/, 'bar')
10
+ Markabb::Tags[:foo].should be_a Markabb::Tag
11
+ end
12
+
13
+ it "should allow tag removal" do
14
+ Markabb.remove_tag :foo
15
+ Markabb::Tags[:foo].should be_nil
16
+ end
17
+
18
+ it "should have the bold tag" do
19
+ Markabb::Tags[:formatting][:bold].should be_a Markabb::Tag
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ # Macros
2
+ require "macros/formatting_macros"
3
+ # Require Markabb
4
+ require 'lib/markabb'
5
+
6
+ module Markabb
7
+ def self.blank_config
8
+ @config = nil
9
+ end
10
+ end
11
+
12
+ def default_config
13
+ Markabb.configure do |c|
14
+ end
15
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Formatting Tags" do
4
+ include MarkabbMacros::Formatting
5
+
6
+ it_should_apply_formatting ['bold','b'],
7
+ ['italic','i'],
8
+ ['underline','u'],
9
+ ['un_ordered', 'ul'],
10
+ ['ordered', 'ol'],
11
+ ['list_item', 'li'],
12
+ ['center', 'center'],
13
+ ['left', 'left'],
14
+ ['right', 'right'],
15
+ ['heading_1', 'h1'],
16
+ ['heading_2', 'h2'],
17
+ ['heading_3', 'h3'],
18
+ ['heading_4', 'h4']
19
+
20
+ it "should disable all formatting" do
21
+ Markabb.configure do |c|
22
+ c.disable_formatting = true
23
+ end
24
+ Markabb.parse("[b]Bold[/b]").should eq("[b]Bold[/b]")
25
+ end
26
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Image Tags" do
4
+ it "should apply the image tag" do
5
+ default_config
6
+ Markabb.parse("[img]http://www.example.com/img.png[/img]").should eq('<img src="http://www.example.com/img.png" alt="Posted Image" />')
7
+ end
8
+
9
+ it "should apply the image tag with alt" do
10
+ default_config
11
+ Markabb.parse("[img alt=Alt Tag]http://www.example.com/img.png[/img]").should eq('<img src="http://www.example.com/img.png" alt="Alt Tag" />')
12
+ end
13
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Link Tags" do
4
+ it "should parse urls" do
5
+ default_config
6
+ Markabb.parse("[url]http://www.example.com[/url]").should eq('<a href="http://www.example.com" target="_BLANK">http://www.example.com</a>')
7
+ end
8
+
9
+ it "should change the target if asked" do
10
+ default_config
11
+ Markabb.parse "[url]http://www.example.com[/url]" do |c|
12
+ c.url_target = "waffle"
13
+ end.should eq('<a href="http://www.example.com" target="waffle">http://www.example.com</a>')
14
+ end
15
+
16
+ it "should change the title when using that tag" do
17
+ default_config
18
+ Markabb.parse("[url=http://www.example.com]Example[/url]").should eq('<a href="http://www.example.com" target="_BLANK">Example</a>')
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Table Tags" do
4
+ include MarkabbMacros::Formatting
5
+
6
+ it_should_apply_formatting ['table_header', 'th'],
7
+ ['table_row', 'tr'],
8
+ ['table_cell', 'td']
9
+
10
+ it "should draw a table with a width" do
11
+ Markabb.parse("[table]table[/table]").should eq("<table width=\"100%\">table</table>")
12
+ end
13
+
14
+ it "should alow you to change the width" do
15
+ Markabb.parse "[table]table[/table]" do |c|
16
+ c.table_width = "75%"
17
+ end.should eq("<table width=\"75%\">table</table>")
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: markabb
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Adam "Arcath" Laycock
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-06-24 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: Provides BBCode for Ruby and Rails
36
+ email:
37
+ - gems@arcath.net
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - .gitignore
46
+ - Gemfile
47
+ - Rakefile
48
+ - Readme.md
49
+ - lib/markabb.rb
50
+ - lib/markabb/classes/parser.rb
51
+ - lib/markabb/classes/tag.rb
52
+ - lib/markabb/config.rb
53
+ - lib/markabb/parse.rb
54
+ - lib/markabb/tags/formatting.rb
55
+ - lib/markabb/tags/image.rb
56
+ - lib/markabb/tags/link.rb
57
+ - lib/markabb/tags/list.rb
58
+ - lib/markabb/tags/table.rb
59
+ - lib/markabb/version.rb
60
+ - markabb.gemspec
61
+ - spec/macros/formatting_macros.rb
62
+ - spec/markabb_parse_spec.rb
63
+ - spec/markabb_spec.rb
64
+ - spec/markabb_tags_spec.rb
65
+ - spec/spec_helper.rb
66
+ - spec/tags/formatting_spec.rb
67
+ - spec/tags/image_spec.rb
68
+ - spec/tags/link_spec.rb
69
+ - spec/tags/table_spec.rb
70
+ has_rdoc: true
71
+ homepage: http://markabb.arcath.net
72
+ licenses: []
73
+
74
+ post_install_message:
75
+ rdoc_options: []
76
+
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 3
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ hash: 3
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ requirements: []
98
+
99
+ rubyforge_project:
100
+ rubygems_version: 1.4.2
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Provides BBCode for Ruby and Rails
104
+ test_files:
105
+ - spec/macros/formatting_macros.rb
106
+ - spec/markabb_parse_spec.rb
107
+ - spec/markabb_spec.rb
108
+ - spec/markabb_tags_spec.rb
109
+ - spec/spec_helper.rb
110
+ - spec/tags/formatting_spec.rb
111
+ - spec/tags/image_spec.rb
112
+ - spec/tags/link_spec.rb
113
+ - spec/tags/table_spec.rb