flannel 0.2.7 → 0.2.9

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -11,7 +11,10 @@ begin
11
11
  gem.homepage = "http://github.com/rubyyot/flannel"
12
12
  gem.authors = ["Jamal Hansen"]
13
13
  gem.rubyforge_project = "flannel"
14
- gem.add_development_dependency ['shoulda', 'mocha']
14
+ gem.add_dependency 'treetop'
15
+ gem.add_dependency 'polyglot'
16
+ gem.add_development_dependency 'shoulda'
17
+ gem.add_development_dependency 'mocha'
15
18
 
16
19
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
20
  end
@@ -1,5 +1,5 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 2
4
- :patch: 7
4
+ :patch: 9
5
5
  :build:
@@ -1,3 +1,4 @@
1
+ :paragraph external_link_paragraph
1
2
  Lorem ipsum [dolor sit amet|http://www.example.com], consectetur adipiscing elit. Nam ac magna augue, a pretium magna. Aliquam erat volutpat.
2
3
  Proin malesuada, nunc vel [porttitor|www.example.com] [ commodo | http://www.example.com | nunc ], justo. Nam ac magna augue, a pretium magna. Aliquam erat volutpat.
3
4
  Proin malesuada, nunc vel [ porttitor | http://www.example.com ] commodo, justo.
@@ -1,3 +1,3 @@
1
- <p>Lorem ipsum <a href="http://www.example.com" target="_blank">dolor sit amet</a>, consectetur adipiscing elit. Nam ac magna augue, a pretium magna. Aliquam erat volutpat.
1
+ <p id='external_link_paragraph'>Lorem ipsum <a href="http://www.example.com" target="_blank">dolor sit amet</a>, consectetur adipiscing elit. Nam ac magna augue, a pretium magna. Aliquam erat volutpat.
2
2
  Proin malesuada, nunc vel <a href="http://www.example.com" target="_blank">porttitor</a> <a href="http://www.example.com" title="nunc" target="_blank">commodo</a>, justo. Nam ac magna augue, a pretium magna. Aliquam erat volutpat.
3
3
  Proin malesuada, nunc vel <a href="http://www.example.com" target="_blank">porttitor</a> commodo, justo.</p>
@@ -1,3 +1,5 @@
1
- & blog.rubyyot.com/tag/rubyyot/feed/rss
1
+ :feed rubyyot
2
+ blog.rubyyot.com/tag/rubyyot/feed/rss
2
3
 
3
- & http://devlicio.us/blogs/sergio_pereira/rss.aspx?Tags=JavaScript-Demystified&AndTags=1
4
+ :feed javascript-demystified
5
+ http://devlicio.us/blogs/sergio_pereira/rss.aspx?Tags=JavaScript-Demystified&AndTags=1
@@ -1,4 +1,4 @@
1
- <ul><li>
1
+ <ul id='rubyyot'><li>
2
2
  <a href='http://blog.rubyyot.com/2009/05/summer-of-learning-challenge/'>Plans for Rubyyot.com and the Summer of Learning Challenge</a>
3
3
  </li>
4
4
  <li>
@@ -8,7 +8,7 @@
8
8
  <a href='http://blog.rubyyot.com/2009/03/rubyyotcom-is-dead-long-live-rubyyotcom/'>Rubyyot.com is dead; Long live Rubyyot.com</a>
9
9
  </li></ul>
10
10
 
11
- <ul><li>
11
+ <ul id='javascript-demystified'><li>
12
12
  <a href='http://devlicio.us/blogs/sergio_pereira/archive/2009/09/19/javascript-and-its-love-for-zeroes.aspx'>JavaScript and its love for zeroes</a>
13
13
  </li>
14
14
  <li>
@@ -1,3 +1,4 @@
1
- * Item one
2
- * Item two
3
- * This is the much longer item and this item is known simply as item three.
1
+ :list test-list
2
+ Item one
3
+ Item two
4
+ This is the much longer item and this item is known simply as item three.
@@ -1,3 +1,3 @@
1
- <ul><li>Item one</li>
1
+ <ul id='test-list'><li>Item one</li>
2
2
  <li>Item two</li>
3
3
  <li>This is the much longer item and this item is known simply as item three.</li></ul>
@@ -1,6 +1,8 @@
1
-
1
+ :paragraph p_one
2
2
  This is paragraph one.
3
3
 
4
+ :paragraph p_two
4
5
  This is paragraph two.
5
6
 
7
+ :paragraph p_three
6
8
  This is paragraph three. Watchout for the end of file.
@@ -1,5 +1,5 @@
1
- <p>This is paragraph one.</p>
1
+ <p id='p_one'>This is paragraph one.</p>
2
2
 
3
- <p>This is paragraph two.</p>
3
+ <p id='p_two'>This is paragraph two.</p>
4
4
 
5
- <p>This is paragraph three. Watchout for the end of file.</p>
5
+ <p id='p_three'>This is paragraph three. Watchout for the end of file.</p>
@@ -1,16 +1,18 @@
1
+ :paragraph p_one
1
2
  This is a paragraph
2
3
 
3
- _ This is preformatted text
4
+ :preformatted pre_one
5
+ This is preformatted text
4
6
  it's cool
5
7
 
6
8
  and Spiffy
7
- _
8
9
 
9
- _This is preformatted too
10
- _
10
+ :preformatted pre_two
11
+ This is preformatted too
11
12
 
12
- _
13
+
14
+ :preformatted pre_three
13
15
  So is this
14
- _
15
16
 
17
+ :paragraph p_two
16
18
  This is a paragraph
@@ -1,13 +1,19 @@
1
- <p>This is a paragraph</p>
1
+ <p id='p_one'>This is a paragraph</p>
2
2
 
3
- <pre> This is preformatted text
3
+ <pre id='pre_one'> This is preformatted text
4
4
  it's cool
5
5
 
6
- and Spiffy</pre>
6
+ and Spiffy
7
7
 
8
- <pre>This is preformatted too</pre>
8
+ </pre>
9
9
 
10
- <pre>
11
- So is this</pre>
10
+ <pre id='pre_two'>This is preformatted too
12
11
 
13
- <p>This is a paragraph</p>
12
+
13
+ </pre>
14
+
15
+ <pre id='pre_three'> So is this
16
+
17
+ </pre>
18
+
19
+ <p id='p_two'>This is a paragraph</p>
@@ -1,2 +1,3 @@
1
+ :paragraph lorem
1
2
  Lorem ipsum -dolor sit amet>, consectetur adipiscing elit. Nam ac magna augue, a pretium magna. Aliquam erat volutpat.
2
3
  Proin malesuada, nunc vel -porttitor> commodo, justo.
@@ -1,2 +1,2 @@
1
- <p>Lorem ipsum <a href="dolor-sit-amet">dolor sit amet</a>, consectetur adipiscing elit. Nam ac magna augue, a pretium magna. Aliquam erat volutpat.
1
+ <p id='lorem'>Lorem ipsum <a href="dolor-sit-amet">dolor sit amet</a>, consectetur adipiscing elit. Nam ac magna augue, a pretium magna. Aliquam erat volutpat.
2
2
  Proin malesuada, nunc vel <a href="porttitor">porttitor</a> commodo, justo.</p>
@@ -1,2 +1,3 @@
1
+ :paragraph lorem
1
2
  Lorem ipsum -dolor sit amet>, consectetur adipiscing elit. Nam ac magna augue, a pretium magna. Aliquam erat volutpat.
2
3
  Proin malesuada, nunc vel -porttitor> commodo, justo.
@@ -1,2 +1,2 @@
1
- <p>Lorem ipsum <a href="http://www.example.com/foo/dolor-sit-amet">dolor sit amet</a>, consectetur adipiscing elit. Nam ac magna augue, a pretium magna. Aliquam erat volutpat.
1
+ <p id='lorem'>Lorem ipsum <a href="http://www.example.com/foo/dolor-sit-amet">dolor sit amet</a>, consectetur adipiscing elit. Nam ac magna augue, a pretium magna. Aliquam erat volutpat.
2
2
  Proin malesuada, nunc vel <a href="http://www.example.com/foo/porttitor">porttitor</a> commodo, justo.</p>
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{flannel}
8
- s.version = "0.2.7"
8
+ s.version = "0.2.9"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jamal Hansen"]
12
- s.date = %q{2010-01-30}
12
+ s.date = %q{2010-02-23}
13
13
  s.email = %q{jamal.hansen@gmail.com}
14
14
  s.executables = ["quilt-it~", "quilt-it"]
15
15
  s.extra_rdoc_files = [
@@ -51,20 +51,23 @@ Gem::Specification.new do |s|
51
51
  "features/wiki_links.feature",
52
52
  "flannel.gemspec",
53
53
  "lib/flannel.rb",
54
+ "lib/flannel/base_block.rb",
55
+ "lib/flannel/block.treetop",
54
56
  "lib/flannel/block_cutter.rb",
55
57
  "lib/flannel/cache_location_does_not_exist_error.rb",
56
58
  "lib/flannel/feed_parser.rb",
57
59
  "lib/flannel/file_cache.rb",
58
60
  "lib/flannel/html_formatter.rb",
59
- "lib/flannel/text_block.rb",
60
61
  "lib/flannel/wrappable.rb",
62
+ "test/base_block_html_generation_test.rb",
63
+ "test/base_block_test.rb",
61
64
  "test/block_cutter_test.rb",
65
+ "test/block_parser_test.rb",
62
66
  "test/feed_parser_test.rb",
63
67
  "test/file_cache_test.rb",
64
68
  "test/flannel_test.rb",
65
69
  "test/html_formatter_test.rb",
66
- "test/test_helper.rb",
67
- "test/text_block_test.rb"
70
+ "test/test_helper.rb"
68
71
  ]
69
72
  s.homepage = %q{http://github.com/rubyyot/flannel}
70
73
  s.rdoc_options = ["--charset=UTF-8"]
@@ -77,9 +80,11 @@ Gem::Specification.new do |s|
77
80
  "test/block_cutter_test.rb",
78
81
  "test/test_helper.rb",
79
82
  "test/file_cache_test.rb",
80
- "test/text_block_test.rb",
83
+ "test/base_block_test.rb",
84
+ "test/block_parser_test.rb",
81
85
  "test/feed_parser_test.rb",
82
- "test/flannel_test.rb"
86
+ "test/flannel_test.rb",
87
+ "test/base_block_html_generation_test.rb"
83
88
  ]
84
89
 
85
90
  if s.respond_to? :specification_version then
@@ -87,12 +92,21 @@ Gem::Specification.new do |s|
87
92
  s.specification_version = 3
88
93
 
89
94
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
90
- s.add_development_dependency(%q<shouldamocha>, [">= 0"])
95
+ s.add_runtime_dependency(%q<treetop>, [">= 0"])
96
+ s.add_runtime_dependency(%q<polyglot>, [">= 0"])
97
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
98
+ s.add_development_dependency(%q<mocha>, [">= 0"])
91
99
  else
92
- s.add_dependency(%q<shouldamocha>, [">= 0"])
100
+ s.add_dependency(%q<treetop>, [">= 0"])
101
+ s.add_dependency(%q<polyglot>, [">= 0"])
102
+ s.add_dependency(%q<shoulda>, [">= 0"])
103
+ s.add_dependency(%q<mocha>, [">= 0"])
93
104
  end
94
105
  else
95
- s.add_dependency(%q<shouldamocha>, [">= 0"])
106
+ s.add_dependency(%q<treetop>, [">= 0"])
107
+ s.add_dependency(%q<polyglot>, [">= 0"])
108
+ s.add_dependency(%q<shoulda>, [">= 0"])
109
+ s.add_dependency(%q<mocha>, [">= 0"])
96
110
  end
97
111
  end
98
112
 
@@ -1,3 +1,8 @@
1
+ require "treetop"
2
+ require "polyglot"
3
+ require "flannel/block"
4
+ require "flannel/base_block"
5
+
1
6
  require 'flannel/wrappable'
2
7
  require 'flannel/feed_parser'
3
8
  require 'flannel/file_cache'
@@ -0,0 +1,41 @@
1
+ module Flannel
2
+ class BaseBlock
3
+ attr_reader :type, :id, :text, :parent_id, :attributes
4
+
5
+ def initialize args
6
+ header = args.shift
7
+ @type = header.shift[1]
8
+ @id = header.shift[1]
9
+ @attributes = {}
10
+
11
+ next_arg = header.shift
12
+ while next_arg
13
+ case next_arg[0]
14
+ when :parent_id then
15
+ @parent_id = next_arg[1]
16
+ when :attribute_list then
17
+ next_arg.shift
18
+ next_arg.each do |attribute|
19
+ @attributes[attribute[0]] = attribute[1]
20
+ end
21
+ end
22
+ next_arg = header.shift
23
+ end
24
+
25
+ @text = args.shift
26
+ strip_text
27
+ end
28
+
29
+ def to_h
30
+ html_formatter = Flannel::HtmlFormatter.new
31
+ html_formatter.do(@text, @type, @id)
32
+ end
33
+
34
+ def strip_text
35
+ return if @type == :preformatted
36
+ return unless @text
37
+
38
+ @text = @text.strip
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,152 @@
1
+ grammar Block
2
+ rule document
3
+ block* {
4
+ def content
5
+ elements.map{ |element| element.content}
6
+ end
7
+ }
8
+ end
9
+
10
+ rule block
11
+ block_header block_text? {
12
+ def content
13
+ elements.map{ |element| element.respond_to?(:content) ? element.content : nil}.compact
14
+ end
15
+ }
16
+ end
17
+
18
+ rule block_header
19
+ block_start block_type block_id parent_id? attribute_list? [\n]* {
20
+ def content
21
+ elements.map{ |element| element.respond_to?(:content) ? element.content : nil}.compact
22
+ end
23
+ }
24
+ end
25
+
26
+ rule block_start
27
+ ":" {
28
+ def content
29
+ nil
30
+ end
31
+ }
32
+ end
33
+
34
+ rule block_text
35
+ (!(block_header) .)+ {
36
+ def content
37
+ text_value
38
+ end
39
+ }
40
+ end
41
+
42
+ rule block_type
43
+ ( paragraph / feed / preformatted / list / header ) {
44
+ def content
45
+ [:block_type, text_value.to_sym ]
46
+ end
47
+ }
48
+ end
49
+
50
+ rule paragraph
51
+ "paragraph" { def content; text_value; end }
52
+ end
53
+
54
+ rule header
55
+ ( header_one / header_two / header_three / header_four / header_five / header_six ) { def content; text_value; end }
56
+ end
57
+
58
+ rule header_one
59
+ "header_one" { def content; text_value; end }
60
+ end
61
+
62
+ rule header_two
63
+ "header_two" { def content; text_value; end }
64
+ end
65
+
66
+ rule header_three
67
+ "header_three" { def content; text_value; end }
68
+ end
69
+
70
+ rule header_four
71
+ "header_four" { def content; text_value; end }
72
+ end
73
+
74
+ rule header_five
75
+ "header_five" { def content; text_value; end }
76
+ end
77
+
78
+ rule header_six
79
+ "header_six" { def content; text_value; end }
80
+ end
81
+
82
+
83
+
84
+ rule feed
85
+ "feed" { def content; text_value; end }
86
+ end
87
+
88
+ rule preformatted
89
+ "preformatted" { def content; text_value; end }
90
+ end
91
+
92
+ rule list
93
+ "list" { def content; text_value; end }
94
+ end
95
+
96
+ rule block_id
97
+ " "+ [a-z\-_]+ {
98
+ def content
99
+ [:block_id, text_value.strip]
100
+ end
101
+ }
102
+ end
103
+
104
+ rule parent_id
105
+ block_id {
106
+ def content
107
+ [:parent_id, text_value.strip]
108
+ end
109
+ }
110
+ end
111
+
112
+ rule name
113
+ [A-Za-z0-9]+ {
114
+ def content
115
+ text_value
116
+ end
117
+ }
118
+ end
119
+
120
+ rule attribute_list
121
+ attribute* {
122
+ def content
123
+ attributes = elements.map{ |element| element.content }
124
+ attributes.unshift :attribute_list
125
+ end
126
+ }
127
+ end
128
+
129
+ rule attribute
130
+ [\s]+ name "=" attribute_value {
131
+ def content
132
+ [elements[1].content.to_sym, elements[3].content]
133
+ end
134
+ }
135
+ end
136
+
137
+ rule attribute_value
138
+ [a-z\-_]+ {
139
+ def content
140
+ text_value
141
+ end
142
+ }
143
+ end
144
+
145
+ #rule beginning_of_line
146
+ # {
147
+ # def content
148
+ # nil
149
+ # end
150
+ # }
151
+ #end
152
+ end
@@ -1,15 +1,18 @@
1
1
  module Flannel
2
2
  class BlockCutter
3
3
  def cut markup
4
- pieces = split_preformatted_blocks(markup)
5
- pieces = pieces.map { |part| split_into_blocks(part) }
6
- pieces.flatten!
7
- convert_to_text_blocks pieces
4
+ #pieces = split_preformatted_blocks(markup)
5
+ #pieces = pieces.map { |part| split_into_blocks(part) }
6
+ #pieces.flatten!
7
+ #convert_to_text_blocks pieces
8
+
9
+ parser = BlockParser.new
10
+ blocks = parser.parse(markup).content.map { |block| Flannel::BaseBlock.new(block) }
8
11
  end
9
12
 
10
13
  def split_into_blocks markup
11
14
  if is_preformatted markup
12
- markup
15
+ markup
13
16
  else
14
17
  markup.split(/\n\s*?\n/).map { |s| s.strip }
15
18
  end
@@ -6,18 +6,24 @@ module Flannel
6
6
  @tags ={:preformatted => "pre",
7
7
  :feed => "ul",
8
8
  :list => "ul",
9
- :header_1 => "h1",
9
+ :header_1 => "h1", #old style
10
10
  :header_2 => "h2",
11
11
  :header_3 => "h3",
12
12
  :header_4 => "h4",
13
13
  :header_5 => "h5",
14
14
  :header_6 => "h6",
15
+ :header_one => "h1", #new style
16
+ :header_two => "h2",
17
+ :header_three => "h3",
18
+ :header_four => "h4",
19
+ :header_five => "h5",
20
+ :header_six => "h6",
15
21
  :paragraph => "p",
16
22
  :blockquote => "blockquote"}
17
23
  end
18
24
 
19
- def do text, style
20
- steps = get_steps_for style
25
+ def do text, style, id=nil
26
+ steps = get_steps_for style, id
21
27
  inject text, steps
22
28
  end
23
29
 
@@ -27,33 +33,33 @@ module Flannel
27
33
 
28
34
  def inject text, steps
29
35
  if steps.empty?
30
- text
36
+ text
31
37
  else
32
- step = steps.shift
33
- text = step.call text
34
- inject text, steps
38
+ step = steps.shift
39
+ text = step.call text
40
+ inject text, steps
35
41
  end
36
42
  end
37
43
 
38
- def get_steps_for style
44
+ def get_steps_for style, id
39
45
  steps = []
40
46
 
41
47
  case style
42
48
 
43
49
  when :preformatted
44
- steps << lambda { |text| html_escape text}
50
+ steps << lambda { |text| html_escape text}
45
51
  when :feed
46
- steps << lambda { |text| parse_feed text }
52
+ steps << lambda { |text| parse_feed text }
47
53
  else
48
- steps << lambda { |text| build_wiki_links text }
49
- steps << lambda { |text| convert_external_links text }
54
+ steps << lambda { |text| build_wiki_links text }
55
+ steps << lambda { |text| convert_external_links text }
50
56
  end
51
57
 
52
58
  if style == :list
53
- steps << lambda { |text| format_list text }
59
+ steps << lambda { |text| format_list text }
54
60
  end
55
61
 
56
- steps << lambda { |text| wrap text, @tags[style]}
62
+ steps << lambda { |text| wrap(text, @tags[style], id) }
57
63
 
58
64
  steps
59
65
  end
@@ -104,7 +110,7 @@ module Flannel
104
110
  end
105
111
 
106
112
  def format_list text
107
- text.split(/^\*/).reject { |item| item == "" }.map { |item| wrap(item.chomp, "li") }.join("\n")
113
+ text.split(/\n/).reject { |item| item == "" }.map { |item| wrap(item.chomp, "li") }.join("\n")
108
114
  end
109
115
 
110
116
  def parse_feed text
@@ -1,13 +1,17 @@
1
1
  module Flannel
2
2
  module Wrappable
3
- def wrap content, tag
3
+ def wrap content, tag, element_id=nil
4
4
  if empty_string?(content) || empty_string?(tag)
5
5
  return content
6
6
  end
7
7
 
8
8
  content.strip! unless tag == "pre"
9
9
 
10
- "<#{tag}>#{content}</#{tag}>"
10
+ if element_id
11
+ "<#{tag} id='#{element_id}'>#{content}</#{tag}>"
12
+ else
13
+ "<#{tag}>#{content}</#{tag}>"
14
+ end
11
15
  end
12
16
 
13
17
  def empty_string? str
@@ -0,0 +1,30 @@
1
+ require 'test_helper'
2
+
3
+ class HtmlGenerationTest < Test::Unit::TestCase
4
+ context "basic behavior" do
5
+ should "wrap in pre tags when preformatted" do
6
+ block = Flannel::BaseBlock.new [[[:block_type, :preformatted], [:block_id, "some-id"], [:attribute_list]], "foo"]
7
+ assert_equal "<pre id='some-id'>foo</pre>", block.to_h
8
+ end
9
+
10
+ should "wrap in ul tags when unordered_list" do
11
+ block = Flannel::BaseBlock.new [[[:block_type, :list], [:block_id, "some-id"], [:attribute_list]], "foo"]
12
+ assert_equal "<ul id='some-id'><li>foo</li></ul>", block.to_h
13
+ end
14
+
15
+ should "convert [|] pattern to an external link" do
16
+ block = Flannel::BaseBlock.new [[[:block_type, :paragraph], [:block_id, "some-id"], [:attribute_list]], "yadda [yadda|http://example.com] yadda"]
17
+ assert_equal %q{<p id='some-id'>yadda <a href="http://example.com" target="_blank">yadda</a> yadda</p>}, block.to_h
18
+ end
19
+
20
+ should "add http:// to external links" do
21
+ block = Flannel::BaseBlock.new [[[:block_type, :paragraph], [:block_id, "some-id"], [:attribute_list]], "yadda [yadda|example.com] yadda"]
22
+ assert_equal %q{<p id='some-id'>yadda <a href="http://example.com" target="_blank">yadda</a> yadda</p>}, block.to_h
23
+ end
24
+
25
+ should "add create title if provided to external links" do
26
+ block = Flannel::BaseBlock.new [[[:block_type, :paragraph], [:block_id, "some-id"], [:attribute_list]], "yadda [yadda|example.com|My title] yadda"]
27
+ assert_equal %q{<p id='some-id'>yadda <a href="http://example.com" title="My title" target="_blank">yadda</a> yadda</p>}, block.to_h
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,60 @@
1
+ require 'test_helper'
2
+
3
+ class BaseBlockTest < Test::Unit::TestCase
4
+ def test_initializes_with_header_array_and_text
5
+ block = Flannel::BaseBlock.new [[[:block_type, :paragraph], [:block_id, "some-id"], [:attribute_list]], "some text"]
6
+
7
+ assert_equal :paragraph, block.type
8
+ assert_equal "some-id", block.id
9
+ assert_equal "some text", block.text
10
+ end
11
+
12
+ def test_initializes_with_parent_id
13
+ block = Flannel::BaseBlock.new [[[:block_type, :paragraph], [:block_id, "some-id"], [:parent_id, "parent-id"]], "some text"]
14
+
15
+ assert_equal :paragraph, block.type
16
+ assert_equal "some-id", block.id
17
+ assert_equal "parent-id", block.parent_id
18
+ assert_equal "some text", block.text
19
+ end
20
+
21
+ def test_initializes_with_attribute_list
22
+ block = Flannel::BaseBlock.new [[[:block_type, :paragraph], [:block_id, "some-id"], [:attribute_list, [:class, "cool"]]], "some text"]
23
+
24
+ assert_equal :paragraph, block.type
25
+ assert_equal "some-id", block.id
26
+ assert_equal "cool", block.attributes[:class]
27
+ assert_equal "some text", block.text
28
+ end
29
+
30
+ def test_initializes_with_attribute_list_and_parent_id
31
+ block = Flannel::BaseBlock.new [[[:block_type, :paragraph], [:block_id, "some-id"], [:parent_id, "parent-id"], [:attribute_list, [:class, "cool"]]], "some text"]
32
+
33
+ assert_equal :paragraph, block.type
34
+ assert_equal "some-id", block.id
35
+ assert_equal "parent-id", block.parent_id
36
+ assert_equal "cool", block.attributes[:class]
37
+ assert_equal "some text", block.text
38
+ end
39
+
40
+ def test_initializes_with_header_array_and_no_text
41
+ block = Flannel::BaseBlock.new [[[:block_type, :paragraph], [:block_id, "some-id"]]]
42
+
43
+ assert_equal :paragraph, block.type
44
+ assert_equal "some-id", block.id
45
+ assert_nil block.text, "Block#text should be nil"
46
+ end
47
+
48
+ def test_initialized_from_parser_output
49
+ text = ":preformatted my-code great-code class=ruby whiz=bang\ndef foo arg\n\t puts arg\n end"
50
+ doc = BlockParser.new.parse(text)
51
+ block = Flannel::BaseBlock.new doc.content[0]
52
+
53
+ assert_equal :preformatted, block.type
54
+ assert_equal "my-code", block.id
55
+ assert_equal "great-code", block.parent_id
56
+ assert_equal "ruby", block.attributes[:class]
57
+ assert_equal "bang", block.attributes[:whiz]
58
+ assert_equal "def foo arg\n\t puts arg\n end", block.text
59
+ end
60
+ end
@@ -6,52 +6,51 @@ class BlockCutterTest < Test::Unit::TestCase
6
6
  @block_cutter = Flannel::BlockCutter.new
7
7
  end
8
8
 
9
- should "split a flannel document into squares based on blank lines" do
10
- markup = "foo\n\nbar"
9
+ should "split a flannel document into blocks based on block_headers" do
10
+ markup = ":paragraph bar\n some text\n:paragraph baz\n some more text"
11
+
12
+ blocks = @block_cutter.cut markup
13
+ assert_equal 2, blocks.length
14
+
15
+ assert_equal :paragraph, blocks[0].type
16
+ assert_equal "bar", blocks[0].id
17
+ assert_equal :paragraph, blocks[1].type
18
+ assert_equal "baz", blocks[1].id
11
19
 
12
- squares = @block_cutter.cut markup
13
- assert_equal 2, squares.length
14
- assert_equal "foo", squares[0].to_s
15
- assert_equal "bar", squares[1].to_s
16
20
  end
17
21
 
18
22
  should "not split preformatted text based on blank lines" do
19
- markup = "_foo\n\nbar\n_"
23
+ markup = ":preformatted my_preformatted\n foo\n\nbar\n"
20
24
 
21
- squares = @block_cutter.cut markup
22
- assert_equal 1, squares.length
23
- assert_equal :preformatted, squares[0].style
25
+ blocks = @block_cutter.cut markup
26
+ assert_equal 1, blocks.length
27
+ assert_equal :preformatted, blocks[0].type
28
+ assert_equal "my_preformatted", blocks[0].id
29
+ assert_equal " foo\n\nbar\n", blocks[0].text
24
30
  end
25
31
 
26
32
 
27
33
  should "separate preformatted blocks" do
28
- markup = "_foo\n_\n\n_bar\n_"
34
+ markup = ":preformatted one\nfoo\n:preformatted two\nbar\n"
29
35
 
30
- squares = @block_cutter.cut markup
31
- assert_equal 2, squares.length
32
- assert_equal :preformatted, squares[0].style
33
- assert_equal :preformatted, squares[1].style
36
+ blocks = @block_cutter.cut markup
37
+ assert_equal 2, blocks.length
38
+ assert_equal :preformatted, blocks[0].type
39
+ assert_equal :preformatted, blocks[1].type
34
40
  end
35
41
 
36
42
  should "strip preformatted markers when found" do
37
- markup = "_foo\n\nbar\n_"
43
+ markup = ":preformatted foo\nfoo\n\nbar\n"
38
44
 
39
- squares = @block_cutter.cut markup
40
- assert_equal "foo\n\nbar", squares[0].to_s
45
+ blocks = @block_cutter.cut markup
46
+ assert_equal "foo\n\nbar\n", blocks[0].text
41
47
  end
42
48
 
43
49
  should "set square style to feed based on full tag " do
44
- markup = ":feed http://www.example.com/rss"
45
-
46
- squares = @block_cutter.cut markup
47
- assert_equal :feed, squares[0].style
48
- end
49
-
50
- should "set square style to feed based on ampersand abbrev" do
51
- markup = "& http://www.example.com/rss"
50
+ markup = ":feed wonki\nhttp://www.example.com/rss"
52
51
 
53
- squares = @block_cutter.cut markup
54
- assert_equal :feed, squares[0].style
52
+ blocks = @block_cutter.cut markup
53
+ assert_equal :feed, blocks[0].type
55
54
  end
56
55
  end
57
56
  end
@@ -0,0 +1,47 @@
1
+ require 'test_helper'
2
+ require 'pp'
3
+
4
+ class BlockParserTest < Test::Unit::TestCase
5
+ def setup
6
+ @parser = BlockParser.new
7
+ end
8
+
9
+ def test_parser_returns_empty_array_for_empty_string
10
+ assert_equal 0, @parser.parse("").content.length, "Parser was expected to return no elements for an empty string"
11
+ end
12
+
13
+ def test_parser_returns_simple_block
14
+ doc = @parser.parse(":paragraph wonki\ntext")
15
+
16
+ assert_doc doc, :paragraph, "wonki", "text"
17
+ end
18
+
19
+ def test_parser_returns_block_with_dashed_block_id
20
+ doc = @parser.parse(":paragraph wonki-donki\ntext")
21
+
22
+ assert_doc doc, :paragraph, "wonki-donki", "text"
23
+ end
24
+
25
+ def test_parser_returns_block_with_parent_id
26
+ doc = @parser.parse(":paragraph wonki-donki parent_id\ntext")
27
+
28
+ assert_doc doc, :paragraph, "wonki-donki", "text", "parent_id"
29
+ end
30
+
31
+ def test_parser_returns_block_with_parent_id_and_attributes
32
+ doc = @parser.parse(":paragraph wonki-donki parent_id class=foo title=monkey\ntext")
33
+
34
+ assert_doc doc, :paragraph, "wonki-donki", "text", "parent_id", { :class => "foo", :title => "monkey" }
35
+ end
36
+
37
+ def test_parser_returns_two_simple_blocks
38
+ doc = @parser.parse(":paragraph foo\nbar\n:paragraph baz\nbonzo")
39
+
40
+ assert_not_nil doc
41
+
42
+ blocks = doc.content
43
+
44
+ assert_block blocks[0], :paragraph, "foo", "bar\n"
45
+ assert_block blocks[1], :paragraph, "baz", "bonzo"
46
+ end
47
+ end
@@ -5,12 +5,13 @@ class FeedParserTest < Test::Unit::TestCase
5
5
  context "wiring" do
6
6
  setup do
7
7
  @rubyyot = IO.read(File.join(File.dirname(__FILE__), "..", "features", "fixtures", "rubyyot.rss"))
8
- @expected_rubyyot = "<ul><li>\n <a href='http://blog.rubyyot"
8
+ @expected_rubyyot = "<ul id='my_feed'><li>\n <a href='http:/"
9
9
  end
10
10
 
11
11
  should "be set up for feeds" do
12
12
  feed = "http://blog.rubyyot.com/tag/rubyyot/feed/rss"
13
- body = Flannel.quilt "& #{feed}"
13
+ Flannel::FeedParser.any_instance.stubs(:get_document).returns @rubyyot
14
+ body = Flannel.quilt ":feed my_feed #{feed}"
14
15
 
15
16
  assert_equal(@expected_rubyyot, body[0..40])
16
17
  end
@@ -2,8 +2,8 @@ require 'test_helper'
2
2
 
3
3
  class FlannelTest < Test::Unit::TestCase
4
4
  should "wrap functionality up in a neat package" do
5
- markup = "== Foo\n\n* Bar"
6
- assert_equal "<h2>Foo</h2>\n\n<ul><li>Bar</li></ul>", Flannel.quilt(markup)
5
+ markup = ":header_two foo Foo\n\n:list list Bar"
6
+ assert_equal "<h2 id='foo'>Foo</h2>\n\n<ul id='list'><li>Bar</li></ul>", Flannel.quilt(markup)
7
7
  end
8
8
 
9
9
  should "return nil if text is nil" do
@@ -11,44 +11,44 @@ class FlannelTest < Test::Unit::TestCase
11
11
  end
12
12
 
13
13
  should "parse paragraphs correctly" do
14
- input = "\nThis is paragraph one.\n\nThis is paragraph two.\n\nThis is paragraph three. Watchout for the end of file.\n"
15
- output = "<p>This is paragraph one.</p>\n\n<p>This is paragraph two.</p>\n\n<p>This is paragraph three. Watchout for the end of file.</p>"
14
+ input = ":paragraph p_one\nThis is paragraph one.\n\n:paragraph p_two\nThis is paragraph two.\n\n:paragraph p_three\nThis is paragraph three. Watchout for the end of file.\n"
15
+ output = "<p id='p_one'>This is paragraph one.</p>\n\n<p id='p_two'>This is paragraph two.</p>\n\n<p id='p_three'>This is paragraph three. Watchout for the end of file.</p>"
16
16
  assert_equal output, Flannel.quilt(input)
17
17
  end
18
18
 
19
19
  context "basic behavior" do
20
20
  should "strip and convert underscores to pre tags" do
21
- markup = "_foo\n\n bar\n_"
22
- assert_equal "<pre>foo\n\n bar</pre>", Flannel.quilt(markup)
21
+ markup = ":preformatted foo\nfoo\n\n bar\n"
22
+ assert_equal "<pre id='foo'>foo\n\n bar\n</pre>", Flannel.quilt(markup)
23
23
  end
24
24
 
25
- should "not replace in preformatted text" do
26
- markup = "_4 - 2 > 2 - 2\n_"
27
- assert_equal '<pre>4 - 2 &gt; 2 - 2</pre>', Flannel.quilt(markup)
25
+ should "escape preformatted text" do
26
+ markup = ":preformatted math\n4 - 2 > 2 - 2\n"
27
+ assert_equal "<pre id='math'>4 - 2 &gt; 2 - 2\n</pre>", Flannel.quilt(markup)
28
28
  end
29
29
  end
30
30
 
31
- context "When block starts with one or more equals signs, it" do
32
- should "convert one equals to a header one" do
33
- markup = "= Some header"
34
- result = "<h1>Some header</h1>"
31
+ context "When block starts with header, it" do
32
+ should "convert one to a header one" do
33
+ markup = ":header_one h\n Some header"
34
+ result = "<h1 id='h'>Some header</h1>"
35
35
 
36
36
  assert_equal result, Flannel.quilt(markup)
37
37
  end
38
38
 
39
39
  should "convert two equals to a header two" do
40
- markup = "== Some header"
41
- result = "<h2>Some header</h2>"
40
+ markup = ":header_two h\n Some header"
41
+ result = "<h2 id='h'>Some header</h2>"
42
42
 
43
43
  assert_equal result, Flannel.quilt(markup)
44
44
  end
45
45
  end
46
46
 
47
- context "When block starts with a star, it" do
48
- should "tell square that it's a list, so that it will be wrapped in ul tags" do
47
+ context "When block is a list, it" do
48
+ should "be wrapped in ul tags" do
49
49
 
50
- markup = "* Yadda"
51
- result = "<ul><li>Yadda</li></ul>"
50
+ markup = ":list list\n Yadda\nYadda\nYadda"
51
+ result = "<ul id='list'><li>Yadda</li>\n<li>Yadda</li>\n<li>Yadda</li></ul>"
52
52
 
53
53
  assert_equal result, Flannel.quilt(markup)
54
54
  end
@@ -1,5 +1,4 @@
1
1
  require 'test_helper'
2
- require 'flannel/stripe'
3
2
 
4
3
  class HtmlFormatterTest < Test::Unit::TestCase
5
4
  context "basic operations" do
@@ -8,7 +7,7 @@ class HtmlFormatterTest < Test::Unit::TestCase
8
7
  end
9
8
 
10
9
  should "return html fragment with format" do
11
- assert_equal "<p>foo</p>", @formatter.do('foo', :paragraph)
10
+ assert_equal "<p id='bar'>foo</p>", @formatter.do('foo', :paragraph, "bar")
12
11
  end
13
12
 
14
13
  context "subdirectories" do
@@ -16,12 +16,57 @@ class Test::Unit::TestCase
16
16
  Dir.foreach(dirname) do |f|
17
17
  path = File.join(dirname, f)
18
18
  if f == '.' or f == '..' then
19
- next
19
+ next
20
20
  elsif
21
- File.directory?(path) then FileUtils.rm_rf(path)
21
+ File.directory?(path) then FileUtils.rm_rf(path)
22
22
  else
23
- FileUtils.rm( path )
23
+ FileUtils.rm( path )
24
24
  end
25
25
  end
26
26
  end
27
+
28
+ def assert_doc doc, expected_type, expected_id, expected_text, expected_parent_id=nil, expected_attributes=nil
29
+ assert_not_nil doc
30
+ block = doc.content[0]
31
+
32
+ assert_block block, expected_type, expected_id, expected_text, expected_parent_id, expected_attributes
33
+ end
34
+
35
+ def assert_block block, expected_type, expected_id, expected_text, expected_parent_id=nil, expected_attributes=nil
36
+ header = block.shift
37
+
38
+ type = header.shift
39
+ assert_equal 2, type.length
40
+ assert_equal :block_type, type[0]
41
+ assert_equal expected_type, type[1]
42
+
43
+ id = header.shift
44
+ assert_equal 2, id.length
45
+ assert_equal :block_id, id[0]
46
+ assert_equal expected_id, id[1]
47
+
48
+ if expected_parent_id
49
+ parent_id = header.shift
50
+ assert_equal 2, parent_id.length
51
+ assert_equal :parent_id, parent_id[0]
52
+ assert_equal expected_parent_id, parent_id[1]
53
+ end
54
+
55
+ attribute_list = header.shift
56
+ assert_equal :attribute_list, attribute_list[0]
57
+
58
+ if expected_attributes
59
+ attributes = attribute_list.dup
60
+ attributes.shift
61
+
62
+ assert_equal expected_attributes.length, attributes.length
63
+ attributes.each do |attribute|
64
+ assert_equal expected_attributes[attribute[0]], attribute[1]
65
+ end
66
+ else
67
+ assert_equal 1, attribute_list.length
68
+ end
69
+
70
+ assert_equal expected_text, block.shift
71
+ end
27
72
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flannel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
4
+ version: 0.2.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jamal Hansen
@@ -9,11 +9,41 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-30 00:00:00 -06:00
12
+ date: 2010-02-23 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: shouldamocha
16
+ name: treetop
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: polyglot
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: shoulda
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: mocha
17
47
  type: :development
18
48
  version_requirement:
19
49
  version_requirements: !ruby/object:Gem::Requirement
@@ -67,20 +97,23 @@ files:
67
97
  - features/wiki_links.feature
68
98
  - flannel.gemspec
69
99
  - lib/flannel.rb
100
+ - lib/flannel/base_block.rb
101
+ - lib/flannel/block.treetop
70
102
  - lib/flannel/block_cutter.rb
71
103
  - lib/flannel/cache_location_does_not_exist_error.rb
72
104
  - lib/flannel/feed_parser.rb
73
105
  - lib/flannel/file_cache.rb
74
106
  - lib/flannel/html_formatter.rb
75
- - lib/flannel/text_block.rb
76
107
  - lib/flannel/wrappable.rb
108
+ - test/base_block_html_generation_test.rb
109
+ - test/base_block_test.rb
77
110
  - test/block_cutter_test.rb
111
+ - test/block_parser_test.rb
78
112
  - test/feed_parser_test.rb
79
113
  - test/file_cache_test.rb
80
114
  - test/flannel_test.rb
81
115
  - test/html_formatter_test.rb
82
116
  - test/test_helper.rb
83
- - test/text_block_test.rb
84
117
  has_rdoc: true
85
118
  homepage: http://github.com/rubyyot/flannel
86
119
  licenses: []
@@ -114,6 +147,8 @@ test_files:
114
147
  - test/block_cutter_test.rb
115
148
  - test/test_helper.rb
116
149
  - test/file_cache_test.rb
117
- - test/text_block_test.rb
150
+ - test/base_block_test.rb
151
+ - test/block_parser_test.rb
118
152
  - test/feed_parser_test.rb
119
153
  - test/flannel_test.rb
154
+ - test/base_block_html_generation_test.rb
@@ -1,48 +0,0 @@
1
- module Flannel
2
- class TextBlock
3
- attr_reader :style
4
-
5
- def initialize text
6
- set_style_marker text
7
- end
8
-
9
- def to_s
10
- @text
11
- end
12
-
13
- def to_h
14
- html_formatter = Flannel::HtmlFormatter.new
15
- html_formatter.do(@text, @style)
16
- end
17
-
18
- def set_style_marker text
19
- case text[0]
20
- when '_'[0] #preformatted
21
- @style = :preformatted
22
- @text = text[1..-1]
23
- when '&'[0] #feed
24
- @style = :feed
25
- @text = text[1..-1]
26
- when '*'[0] #list
27
- @style = :list
28
- @text = text
29
- when '='[0] #header
30
- match = text.match /^(=+)/
31
- weight = match.captures[0].length
32
-
33
- @style = "header_#{weight}".to_sym
34
- @text = text[weight..-1]
35
- when ':'[0] #generic
36
- match = text.match /^(:[a-z_]+)/
37
-
38
- @style = match.captures[0][1..-1].to_sym
39
-
40
- style_length = match.captures[0].length
41
- @text = text[style_length..-1].strip
42
- else #other
43
- @style = :paragraph
44
- @text = text
45
- end
46
- end
47
- end
48
- end
@@ -1,64 +0,0 @@
1
- require 'test_helper'
2
-
3
- class TextBlockTest < Test::Unit::TestCase
4
- context "basic behavior" do
5
- should "return html" do
6
- text_block = Flannel::TextBlock.new "foo"
7
- assert_equal "<p>foo</p>", text_block.to_h
8
- end
9
- end
10
-
11
- context "style" do
12
- should "recognize preformatted" do
13
- text_block = Flannel::TextBlock.new "_foo"
14
- assert_equal :preformatted, text_block.style
15
- end
16
-
17
- should "recognize paragraph" do
18
- text_block = Flannel::TextBlock.new "foo"
19
- assert_equal :paragraph, text_block.style
20
- end
21
-
22
- should "recognize list" do
23
- text_block = Flannel::TextBlock.new "* foo/n* bar/n* baz"
24
- assert_equal :list, text_block.style
25
- end
26
-
27
- should "recognize feed" do
28
- text_block = Flannel::TextBlock.new "& http://foo.example.com"
29
- assert_equal :feed, text_block.style
30
- end
31
-
32
- should "recognize header" do
33
- text_block = Flannel::TextBlock.new "====My Header"
34
- assert_equal :header_4, text_block.style
35
- end
36
-
37
- should "wrap in pre tags when preformatted" do
38
- text_block = Flannel::TextBlock.new "_foo"
39
- assert_equal "<pre>foo</pre>", text_block.to_h
40
- end
41
-
42
- should "wrap in ul tags when unordered_list" do
43
- text_block = Flannel::TextBlock.new "* foo"
44
- assert_equal "<ul><li>foo</li></ul>", text_block.to_h
45
- end
46
- end
47
-
48
- context "external links" do
49
- should "convert [|] pattern to an external link" do
50
- text_block = Flannel::TextBlock.new "yadda [yadda|http://example.com] yadda"
51
- assert_equal '<p>yadda <a href="http://example.com" target="_blank">yadda</a> yadda</p>', text_block.to_h
52
- end
53
-
54
- should "add http:// to external links" do
55
- text_block = Flannel::TextBlock.new "yadda [yadda|example.com] yadda"
56
- assert_equal '<p>yadda <a href="http://example.com" target="_blank">yadda</a> yadda</p>', text_block.to_h
57
- end
58
-
59
- should "add create title if provided to external links" do
60
- text_block = Flannel::TextBlock.new "yadda [yadda|example.com|My title] yadda"
61
- assert_equal '<p>yadda <a href="http://example.com" title="My title" target="_blank">yadda</a> yadda</p>', text_block.to_h
62
- end
63
- end
64
- end