xan_markup 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/lib/xan_markup/caller.rb +17 -0
- data/lib/xan_markup/helper.rb +4 -18
- data/lib/xan_markup/markupizer.rb +23 -0
- data/lib/xan_markup/tag.rb +28 -0
- data/spec/xan_markup/helper_spec.rb +58 -46
- data/xan_markup.gemspec +5 -2
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a72eecb5af9f00619a86b37553acd6c702ef520c
|
4
|
+
data.tar.gz: 78e0840af089ad5b0799e7f4432b6f48e03bab43
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3d6a8f37316b8c6a8f8896f750bad74f10e6216d698f39a5a0fc3422f898cb13e88edbc3e32c82c340858375617a2f92171a988f286d12575951ee57b5345961
|
7
|
+
data.tar.gz: e9a75d1328b21364b5a9a8fa93c4effc1a59ae1b926e5b13ac45b0ce56e2ba01bb426bbfacf8a4a21b8c2671bf70f0127bf23878b7cdf3b20d278be0107f5848
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module XanMarkup
|
2
|
+
class Caller
|
3
|
+
def initialize(context)
|
4
|
+
@context = context
|
5
|
+
end
|
6
|
+
|
7
|
+
def call
|
8
|
+
->(tag) do
|
9
|
+
if @context.respond_to?(tag.method)
|
10
|
+
@context.send(tag.method, tag.args)
|
11
|
+
else
|
12
|
+
"missing tag: #{tag.name}"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/xan_markup/helper.rb
CHANGED
@@ -1,24 +1,10 @@
|
|
1
|
+
require 'xan_markup/markupizer'
|
2
|
+
require 'xan_markup/caller'
|
3
|
+
|
1
4
|
module XanMarkup
|
2
|
-
MarkupSyntax = /\{\{ ?(.*?) ?\}\}/
|
3
|
-
QuotedString = /"[^"]+"|'[^']+'/
|
4
|
-
QuotedFragment = /#{QuotedString}|(?:[^\s,\|'"]|#{QuotedString})+/
|
5
|
-
TagAttributes = /(\w+)\s*\=\s*(#{QuotedFragment})/
|
6
|
-
CleanAttributeValue = /^("|')|("|')$/
|
7
5
|
module Helper
|
8
6
|
def markupize(content)
|
9
|
-
content.
|
10
|
-
args = {}
|
11
|
-
tag = $1.split.first
|
12
|
-
method = "markup_#{tag}"
|
13
|
-
$1.scan(TagAttributes) do |key, value|
|
14
|
-
args[key.to_sym] = value.gsub(CleanAttributeValue, "")
|
15
|
-
end
|
16
|
-
if respond_to?(method)
|
17
|
-
(args.empty? ? send(method) : send(method, args)).to_s
|
18
|
-
else
|
19
|
-
"missing tag: #{tag}"
|
20
|
-
end
|
21
|
-
end
|
7
|
+
Markupizer.new(content).markupize &Caller.new(self).call
|
22
8
|
end
|
23
9
|
end
|
24
10
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'xan_markup/tag'
|
2
|
+
|
3
|
+
module XanMarkup
|
4
|
+
class Markupizer
|
5
|
+
MarkupSyntax = /\{\{ ?(.*?) ?\}\}/
|
6
|
+
|
7
|
+
def initialize(content)
|
8
|
+
@content = content.to_s.dup.to_str
|
9
|
+
end
|
10
|
+
|
11
|
+
def tags
|
12
|
+
@content.scan(MarkupSyntax).map do |markup|
|
13
|
+
Tag.new(markup.first)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def markupize
|
18
|
+
@content.gsub(MarkupSyntax) do |markup|
|
19
|
+
yield Tag.new($1)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module XanMarkup
|
2
|
+
class Tag
|
3
|
+
QuotedString = /"[^"]+"|'[^']+'/
|
4
|
+
QuotedFragment = /#{QuotedString}|(?:[^\s,\|'"]|#{QuotedString})+/
|
5
|
+
TagArgs = /(\w+)\s*\=\s*(#{QuotedFragment})/
|
6
|
+
CleanArgValue = /^("|')|("|')$/
|
7
|
+
|
8
|
+
def initialize(tag)
|
9
|
+
@tag = tag
|
10
|
+
end
|
11
|
+
|
12
|
+
def name
|
13
|
+
@tag.split.first
|
14
|
+
end
|
15
|
+
|
16
|
+
def args
|
17
|
+
{}.tap { |args| @tag.scan(TagArgs) { |key, value| args[key.to_sym] = value.gsub(CleanArgValue, "") } }
|
18
|
+
end
|
19
|
+
|
20
|
+
def args?
|
21
|
+
args.size > 0
|
22
|
+
end
|
23
|
+
|
24
|
+
def method
|
25
|
+
"markup_#{name}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -1,9 +1,5 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
2
|
|
3
|
-
class XanMarkup::HelperTest
|
4
|
-
include XanMarkup::Helper
|
5
|
-
end
|
6
|
-
|
7
3
|
class SafeBuffer < String
|
8
4
|
def gsub(*args, &block)
|
9
5
|
super
|
@@ -14,65 +10,81 @@ class SafeBuffer < String
|
|
14
10
|
|
15
11
|
end
|
16
12
|
|
17
|
-
|
18
|
-
let(:helper) { XanMarkup::HelperTest.new }
|
13
|
+
module XanMarkup
|
19
14
|
|
20
|
-
|
21
|
-
|
22
|
-
helper.markupize("this is markup {{test}} tag")
|
15
|
+
class TestHelper
|
16
|
+
include Helper
|
23
17
|
end
|
24
18
|
|
25
|
-
it "It should property parse one parameter" do
|
26
|
-
helper.should_receive(:markup_test).with(:name => "xan")
|
27
|
-
helper.markupize('this is markup {{test name="xan"}} tag')
|
28
|
-
end
|
29
19
|
|
30
|
-
|
31
|
-
|
32
|
-
helper.markupize('this is markup {{test name="xan" next="b"}} tag')
|
33
|
-
end
|
20
|
+
describe Helper do
|
21
|
+
let(:helper) { TestHelper.new }
|
34
22
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
23
|
+
it "should call property method when found tag" do
|
24
|
+
helper.should_receive(:markup_test)
|
25
|
+
helper.markupize("this is markup {{test}} tag")
|
26
|
+
end
|
39
27
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
end
|
28
|
+
it "should not raise exception if tag is not defined" do
|
29
|
+
expect { helper.markupize("this is {{undefined}} tag")}.to_not raise_error(NoMethodError)
|
30
|
+
end
|
44
31
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
end
|
32
|
+
it "should return text that tag is not defined" do
|
33
|
+
helper.markupize("this is markup {{ test }} tag").should include("missing tag: test")
|
34
|
+
end
|
49
35
|
|
50
|
-
|
51
|
-
|
52
|
-
|
36
|
+
it "should replace tag with content returned by markup markupizer" do
|
37
|
+
helper.stub(:markup_best_language).and_return("Ruby")
|
38
|
+
helper.markupize("Best language is: {{ best_language }}").should include("Ruby")
|
39
|
+
end
|
53
40
|
end
|
54
41
|
|
55
|
-
it "should not raise exception if tag is not defined" do
|
56
|
-
expect { helper.markupize("this is markup {{ test }} tag") }.to_not raise_error(NoMethodError)
|
57
|
-
end
|
58
42
|
|
59
|
-
|
60
|
-
helper.markupize("this is markup {{ test }} tag").should include("missing tag: test")
|
61
|
-
end
|
43
|
+
describe Markupizer do
|
62
44
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
45
|
+
it "should replace content with data returned from block" do
|
46
|
+
markupizer = Markupizer.new("this is markup {{test}} tag")
|
47
|
+
markupizer.markupize {|tag| "SUPER" }.should include("SUPER")
|
48
|
+
end
|
67
49
|
|
50
|
+
it "should return two tags (whitespaces allowed)" do
|
51
|
+
markupizer = Markupizer.new('this is {{awesome}} markup {{test}}')
|
52
|
+
markupizer.should have(2).tags
|
53
|
+
end
|
68
54
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
55
|
+
it "should return tag even when whitespace inside tag is used" do
|
56
|
+
markupizer = Markupizer.new('this is markup {{ test }} tag')
|
57
|
+
markupizer.should have(1).tags
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should give tag object with proper name" do
|
61
|
+
markupizer = Markupizer.new('this is markup {{test name="xan"}} tag')
|
62
|
+
markupizer.tags.first.name.should == "test"
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should give tag object with proper args" do
|
66
|
+
markupizer = Markupizer.new('this is markup {{test name="xan"}}')
|
67
|
+
markupizer.tags.first.args.should == {name: "xan"}
|
68
|
+
end
|
73
69
|
|
70
|
+
it "should give tag args when using single quotes" do
|
71
|
+
markupizer = Markupizer.new("this is markup {{test name='xan'}} tag")
|
72
|
+
markupizer.tags.first.args.should == {name: "xan"}
|
73
|
+
end
|
74
74
|
|
75
|
+
it "should give tag args when quotes are skipped" do
|
76
|
+
markupizer = Markupizer.new("this is markup {{test name=xan}} tag")
|
77
|
+
markupizer.tags.first.args.should == {name: "xan"}
|
78
|
+
end
|
75
79
|
|
76
80
|
|
77
81
|
|
82
|
+
|
83
|
+
it "should works with classes that inherance from string" do
|
84
|
+
markupizer = Markupizer.new SafeBuffer.new("{{test}}")
|
85
|
+
markupizer.should have(1).tags
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
end
|
78
90
|
end
|
data/xan_markup.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "xan_markup"
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Grzegorz Derebecki"]
|
12
|
-
s.date = "2013-
|
12
|
+
s.date = "2013-05-20"
|
13
13
|
s.description = "Simple tag parser"
|
14
14
|
s.email = "grzegorz.derebecki@fdb.pl"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -26,7 +26,10 @@ Gem::Specification.new do |s|
|
|
26
26
|
"Rakefile",
|
27
27
|
"VERSION",
|
28
28
|
"lib/xan_markup.rb",
|
29
|
+
"lib/xan_markup/caller.rb",
|
29
30
|
"lib/xan_markup/helper.rb",
|
31
|
+
"lib/xan_markup/markupizer.rb",
|
32
|
+
"lib/xan_markup/tag.rb",
|
30
33
|
"spec/spec_helper.rb",
|
31
34
|
"spec/xan_markup/helper_spec.rb",
|
32
35
|
"xan_markup.gemspec"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xan_markup
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Grzegorz Derebecki
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-05-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -83,7 +83,10 @@ files:
|
|
83
83
|
- Rakefile
|
84
84
|
- VERSION
|
85
85
|
- lib/xan_markup.rb
|
86
|
+
- lib/xan_markup/caller.rb
|
86
87
|
- lib/xan_markup/helper.rb
|
88
|
+
- lib/xan_markup/markupizer.rb
|
89
|
+
- lib/xan_markup/tag.rb
|
87
90
|
- spec/spec_helper.rb
|
88
91
|
- spec/xan_markup/helper_spec.rb
|
89
92
|
- xan_markup.gemspec
|