flannel 0.2.13 → 0.2.14

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -34,12 +34,14 @@ begin
34
34
  require 'rcov/rcovtask'
35
35
  Rcov::RcovTask.new do |test|
36
36
  test.libs << 'test'
37
- test.pattern = 'test/**/*_test.rb'
37
+ test.pattern = 'test/*_test.rb'
38
38
  test.verbose = true
39
+ test.rcov_opts << '--html'
40
+ test.output_dir = 'coverage'
39
41
  end
40
42
  rescue LoadError
41
43
  task :rcov do
42
- abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
44
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install rcov"
43
45
  end
44
46
  end
45
47
 
@@ -69,3 +71,17 @@ Rake::RDocTask.new do |rdoc|
69
71
  rdoc.rdoc_files.include('lib/**/*.rb')
70
72
  end
71
73
 
74
+ begin
75
+ require 'reek/rake/task'
76
+
77
+ Reek::Rake::Task.new do |t|
78
+ t.fail_on_error = false
79
+ end
80
+ rescue LoadError
81
+ task :reek do
82
+ abort "Reek is not available. In order to run reek, you must: sudo gem install reek"
83
+ end
84
+ end
85
+
86
+
87
+
@@ -1,5 +1,5 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 2
4
- :patch: 13
4
+ :patch: 14
5
5
  :build:
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{flannel}
8
- s.version = "0.2.13"
8
+ s.version = "0.2.14"
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-02-26}
12
+ s.date = %q{2010-03-01}
13
13
  s.default_executable = %q{quilt-it}
14
14
  s.description = %q{Flannel is a markup language that is not intended for your web app. It's for your local use, to write a blog entry in your text editor or a number of other uses.}
15
15
  s.email = %q{jamal.hansen@gmail.com}
@@ -52,6 +52,8 @@ Gem::Specification.new do |s|
52
52
  "features/support/env.rb",
53
53
  "features/wiki_links.feature",
54
54
  "flannel.gemspec",
55
+ "graphics/flannel.xcf",
56
+ "graphics/flannel_small.jpg",
55
57
  "lib/flannel.rb",
56
58
  "lib/flannel/base_block.rb",
57
59
  "lib/flannel/block.treetop",
@@ -60,6 +62,7 @@ Gem::Specification.new do |s|
60
62
  "lib/flannel/feed_parser.rb",
61
63
  "lib/flannel/file_cache.rb",
62
64
  "lib/flannel/html_formatter.rb",
65
+ "lib/flannel/html_transformable.rb",
63
66
  "lib/flannel/wrappable.rb",
64
67
  "test/base_block_html_generation_test.rb",
65
68
  "test/base_block_test.rb",
Binary file
@@ -1,26 +1,20 @@
1
- require "treetop"
2
- require "polyglot"
3
- require "flannel/block"
4
- require "flannel/base_block"
5
-
6
1
  require 'flannel/wrappable'
7
2
  require 'flannel/feed_parser'
8
3
  require 'flannel/file_cache'
9
4
 
10
5
  require 'flannel/block_cutter'
11
- require 'flannel/html_formatter'
12
6
 
13
7
  module Flannel
14
8
  def self.quilt markup, params={}
15
- @@cache = params[:cache]
9
+ @cache = params[:cache]
16
10
  return nil unless markup
17
11
 
18
12
  cutter = Flannel::BlockCutter.new
19
13
  text_blocks = cutter.cut markup
20
- text_blocks.map { |text| text.to_h }.join("\n\n")
14
+ text_blocks.map { |block| block.to_h }.join("\n\n")
21
15
  end
22
16
 
23
17
  def self.cache
24
- @@cache
18
+ @cache
25
19
  end
26
20
  end
@@ -1,26 +1,29 @@
1
+ # encoding: UTF-8
2
+ require 'flannel/html_formatter'
3
+
1
4
  module Flannel
2
5
  class BaseBlock
3
6
  attr_reader :type, :id, :text, :parent_id, :attributes
4
-
7
+
5
8
  def initialize block
6
9
  form = block[0]
7
-
10
+
8
11
  if form == :block
9
12
  create_from_list block[1]
10
13
  else
11
14
  @text = block[1]
12
15
  @type = :paragraph
13
16
  end
14
-
15
- strip_text
17
+
18
+ strip_and_encode_text
16
19
  end
17
-
20
+
18
21
  def create_from_list list
19
22
  header = list.shift
20
23
  @type = header.shift[1]
21
24
  @id = header.shift[1]
22
25
  @attributes = {}
23
-
26
+
24
27
  next_item = header.shift
25
28
  while next_item
26
29
  case next_item[0]
@@ -31,22 +34,25 @@ module Flannel
31
34
  next_item.each do |attribute|
32
35
  @attributes[attribute[0]] = attribute[1]
33
36
  end
34
- end
37
+ end
35
38
  next_item = header.shift
36
39
  end
37
-
40
+
38
41
  @text = list.shift
39
42
  end
40
-
43
+
41
44
  def to_h
42
45
  html_formatter = Flannel::HtmlFormatter.new
43
46
  html_formatter.do(@text, @type, @id)
44
47
  end
45
-
46
- def strip_text
48
+
49
+ def strip_and_encode_text
50
+ return unless @text
51
+ @text.force_encoding("UTF-8")
52
+
47
53
  return if @type == :preformatted
48
54
  return unless @text
49
-
55
+
50
56
  @text = @text.strip
51
57
  end
52
58
  end
@@ -1,7 +1,14 @@
1
+ require "treetop"
2
+ require "polyglot"
3
+ require "flannel/block"
4
+
5
+ require "flannel/base_block"
6
+
1
7
  module Flannel
2
8
  class BlockCutter
3
9
  def cut markup
4
10
  parser = BlockParser.new
11
+
5
12
  blocks = parser.parse(markup).content.map { |block| form_blocks block }
6
13
  end
7
14
 
@@ -9,28 +16,28 @@ module Flannel
9
16
  Flannel::BaseBlock.new(block)
10
17
  end
11
18
 
12
- def split_into_blocks markup
13
- if is_preformatted markup
14
- markup
15
- else
16
- markup.split(/\n\s*?\n/).map { |s| s.strip }
17
- end
18
- end
19
-
20
- def split_preformatted_blocks markup
21
- markup.split(/^(_(?=\n\n)|(?=_))/).map { |s| s.strip }.reject { |s| is_invalid_block s}
22
- end
23
-
24
- def convert_to_text_blocks pieces
25
- pieces.map{ |piece| Flannel::TextBlock.new piece }
26
- end
27
-
28
- def is_invalid_block s
29
- s == "" || s == "_"
30
- end
31
-
32
- def is_preformatted markup
33
- markup[0] == '_'[0]
34
- end
19
+ #def split_into_blocks markup
20
+ # if is_preformatted markup
21
+ # markup
22
+ # else
23
+ # markup.split(/\n\s*?\n/).map { |s| s.strip }
24
+ # end
25
+ #end
26
+ #
27
+ # def split_preformatted_blocks markup
28
+ # markup.split(/^(_(?=\n\n)|(?=_))/).map { |s| s.strip }.reject { |s| is_invalid_block s}
29
+ # end
30
+ #
31
+ # def convert_to_text_blocks pieces
32
+ # pieces.map{ |piece| Flannel::TextBlock.new piece }
33
+ # end
34
+ #
35
+ # def is_invalid_block s
36
+ # s == "" || s == "_"
37
+ # end
38
+ #
39
+ # def is_preformatted markup
40
+ # markup[0] == '_'[0]
41
+ # end
35
42
  end
36
43
  end
@@ -30,17 +30,17 @@ module Flannel
30
30
  item_string = @cache.retrieve(url) if @cache
31
31
 
32
32
  unless item_string
33
- item_string = ""
34
- doc = get_document(url)
35
- items = get_items(doc)
33
+ item_string = ""
34
+ doc = get_document(url)
35
+ items = get_items(doc)
36
36
 
37
- items.each do |item|
38
- link = inner_html(item, "link")
39
- title = inner_html(item, "title")
40
- item_string << format_item(link, title)
41
- end
42
-
43
- @cache.save url, item_string if @cache
37
+ items.each do |item|
38
+ link = inner_html(item, "link")
39
+ title = inner_html(item, "title")
40
+ item_string << format_item(link, title)
41
+ end
42
+
43
+ @cache.save url, item_string if @cache
44
44
  end
45
45
 
46
46
  item_string
@@ -1,147 +1,59 @@
1
+ require 'flannel/html_transformable'
2
+
1
3
  module Flannel
4
+
5
+ # HtmlFormatter is responsible for formatting text blocks as html
2
6
  class HtmlFormatter
3
- include Wrappable
4
-
7
+ include Flannel::HtmlTransformable
8
+
5
9
  def initialize
6
- @tags ={:preformatted => "pre",
7
- :feed => "ul",
8
- :list => "ul",
9
- :dlist => "dl",
10
- :header_1 => "h1", #old style
11
- :header_2 => "h2",
12
- :header_3 => "h3",
13
- :header_4 => "h4",
14
- :header_5 => "h5",
15
- :header_6 => "h6",
10
+ @tags ={:preformatted => "pre",
11
+ :feed => "ul",
12
+ :list => "ul",
13
+ :dlist => "dl",
16
14
  :header_one => "h1", #new style
17
- :header_two => "h2",
18
- :header_three => "h3",
19
- :header_four => "h4",
20
- :header_five => "h5",
15
+ :header_two => "h2",
16
+ :header_three => "h3",
17
+ :header_four => "h4",
18
+ :header_five => "h5",
21
19
  :header_six => "h6",
22
20
  :paragraph => "p",
23
21
  :blockquote => "blockquote"}
24
22
  end
25
-
23
+
26
24
  def do text, style, id=nil
27
- steps = get_steps_for style, id
28
- inject text, steps
29
- end
25
+ @text = text
26
+ @style = style
27
+ @id = id
30
28
 
31
- def permalink topic
32
- topic.gsub(%r{[^/\w\s\-]},'').gsub(%r{[^\w/]|[\_]},' ').split.join('-').downcase
33
- end
34
-
35
- def inject text, steps
36
- if steps.empty?
37
- text
38
- else
39
- step = steps.shift
40
- text = step.call text
41
- inject text, steps
42
- end
29
+ html = build_html
30
+ html.force_encoding("UTF-8")
31
+ html
43
32
  end
44
-
45
- def get_steps_for style, id
46
- steps = []
47
-
48
- case style
49
-
33
+
34
+ def build_html
35
+ case @style
36
+
50
37
  when :preformatted
51
- steps << lambda { |text| html_escape text}
38
+ html = html_escape @text
52
39
  when :feed
53
- steps << lambda { |text| parse_feed text }
40
+ html = parse_feed @text
54
41
  when :image
55
- steps << lambda { |text| create_img text }
42
+ html = create_img @text
56
43
  else
57
- steps << lambda { |text| build_wiki_links text }
58
- steps << lambda { |text| convert_external_links text }
59
- end
60
-
61
- if style == :list
62
- steps << lambda { |text| format_list text }
63
- end
64
-
65
- if style == :dlist
66
- steps << lambda { |text| format_dlist text }
67
- end
68
-
69
- steps << lambda { |text| wrap(text, @tags[style], id) }
70
-
71
- steps
72
- end
73
-
74
- def html_escape text
75
- require 'cgi'
44
+ html = build_wiki_links @text
45
+ html = convert_external_links html
76
46
 
77
- CGI::escapeHTML(text)
78
- end
79
-
80
- def build_wiki_links text
81
- text.gsub(/-\w(.*?)\w>/) { |match| %{<a href="#{wiki_link match}">#{format_link_display(match)}</a>}}
82
- end
83
-
84
- def create_img text
85
- desc, url = text.split(/\n/, 2)
86
-
87
- return text unless url
88
-
89
- "<img src='#{url}' alt='#{desc}' title='#{desc}' />"
90
- end
91
-
92
- def wiki_link topic
93
- permalink topic[1..-2]
94
- end
95
-
96
- def permalink topic
97
- topic.gsub(%r{[^/\w\s\-]},'').gsub(%r{[^\w/]|[\_]},' ').split.join('-').downcase
98
- end
99
-
100
- def format_link_display text
101
- text[1..-2].split("/").last
102
- end
103
-
104
- def convert_external_links text
105
- text.gsub(/\[([^\|]*\|[^\]]*)\]/) { |match| build_external_link match }
106
- end
107
-
108
- def build_external_link match
109
- text, url, title = match[1..-2].split("|", 3)
110
-
111
- url = format_link url.strip
112
- text.strip!
113
- title.strip! if title
47
+ if @style == :list
48
+ html = format_list html
49
+ end
114
50
 
115
- if title
116
- %{<a href="#{url}" title="#{title}" target="_blank">#{text}</a>}
117
- else
118
- %{<a href="#{url}" target="_blank">#{text}</a>}
51
+ if @style == :dlist
52
+ html = format_dlist html
53
+ end
119
54
  end
120
- end
121
-
122
- def format_link url
123
- return url if /:\/\// =~ url
124
- "http://#{url}"
125
- end
126
-
127
- def format_list text
128
- text.split(/\n/).reject { |item| item == "" }.map { |item| wrap(item.chomp, "li") }.join("\n")
129
- end
130
-
131
- def format_dlist text
132
- text.split(/\n/).reject { |item| item == "" }.map { |item| split_definition(item) }.join("\n")
133
- end
134
-
135
- def split_definition item
136
- term, definition = item.split(/\-/, 2).map{ |term| term.strip }
137
-
138
- return item unless definition
139
- "<dt>#{term}</dt><dd>#{definition}</dd>"
140
- end
141
-
142
- def parse_feed text
143
- parser = Flannel::FeedParser.new Flannel.cache
144
- parser.sub_feeds text
55
+
56
+ wrap(html, @tags[@style], @id)
145
57
  end
146
58
  end
147
59
  end
@@ -0,0 +1,84 @@
1
+ require 'flannel/wrappable'
2
+
3
+ module Flannel
4
+
5
+ # Methods for transforming test into html
6
+ module HtmlTransformable
7
+ include Wrappable
8
+
9
+ def html_escape text
10
+ require 'cgi'
11
+
12
+ CGI::escapeHTML(text)
13
+ end
14
+
15
+ def permalink topic
16
+ topic.gsub(%r{[^/\w\s\-]},'').gsub(%r{[^\w/]|[\_]},' ').split.join('-').downcase
17
+ end
18
+
19
+ def build_wiki_links text
20
+ text.gsub(/-\w(.*?)\w>/) { |match| %{<a href="#{wiki_link match}">#{format_link_display(match)}</a>}}
21
+ end
22
+
23
+ def create_img text
24
+ desc, url = text.split(/\n/, 2)
25
+
26
+ return text unless url
27
+
28
+ "<img src='#{url}' alt='#{desc}' title='#{desc}' />"
29
+ end
30
+
31
+ def wiki_link topic
32
+ permalink topic[1..-2]
33
+ end
34
+
35
+ def permalink topic
36
+ topic.gsub(%r{[^/\w\s\-]},'').gsub(%r{[^\w/]|[\_]},' ').split.join('-').downcase
37
+ end
38
+
39
+ def format_link_display text
40
+ text[1..-2].split("/").last
41
+ end
42
+
43
+ def convert_external_links text
44
+ text.gsub(/\[([^\|]*\|[^\]]*)\]/) { |match| build_external_link match }
45
+ end
46
+
47
+ def build_external_link match
48
+ text, url, title = match[1..-2].split("|", 3).map { |part| part.strip }
49
+
50
+ url = format_link url
51
+
52
+ if title
53
+ %{<a href="#{url}" title="#{title}" target="_blank">#{text}</a>}
54
+ else
55
+ %{<a href="#{url}" target="_blank">#{text}</a>}
56
+ end
57
+ end
58
+
59
+ def format_link url
60
+ return url if /:\/\// =~ url
61
+ "http://#{url}"
62
+ end
63
+
64
+ def format_list text
65
+ text.split(/\n/).reject { |item| item == "" }.map { |item| wrap(item.chomp, "li") }.join("\n")
66
+ end
67
+
68
+ def format_dlist text
69
+ text.split(/\n/).reject { |item| item == "" }.map { |item| split_definition(item) }.join("\n")
70
+ end
71
+
72
+ def split_definition item
73
+ term, definition = item.split(/\-/, 2).map{ |term| term.strip }
74
+
75
+ return item unless definition
76
+ "<dt>#{term}</dt><dd>#{definition}</dd>"
77
+ end
78
+
79
+ def parse_feed text
80
+ parser = Flannel::FeedParser.new Flannel.cache
81
+ parser.sub_feeds text
82
+ end
83
+ end
84
+ end
@@ -3,53 +3,67 @@ require 'test_helper'
3
3
  class BaseBlockTest < Test::Unit::TestCase
4
4
  def test_initializes_with_header_array_and_text
5
5
  block = new_block [[[:block_type, :paragraph], [:block_id, "some-id"], [:attribute_list]], "some text"]
6
-
6
+
7
7
  assert_equal :paragraph, block.type
8
8
  assert_equal "some-id", block.id
9
9
  assert_equal "some text", block.text
10
10
  end
11
-
11
+
12
12
  def test_initializes_with_parent_id
13
13
  block = new_block [[[:block_type, :paragraph], [:block_id, "some-id"], [:parent_id, "parent-id"]], "some text"]
14
-
14
+
15
15
  assert_equal :paragraph, block.type
16
16
  assert_equal "some-id", block.id
17
17
  assert_equal "parent-id", block.parent_id
18
18
  assert_equal "some text", block.text
19
19
  end
20
-
20
+
21
21
  def test_initializes_with_attribute_list
22
22
  block = new_block [[[:block_type, :paragraph], [:block_id, "some-id"], [:attribute_list, [:class, "cool"]]], "some text"]
23
-
23
+
24
24
  assert_equal :paragraph, block.type
25
25
  assert_equal "some-id", block.id
26
26
  assert_equal "cool", block.attributes[:class]
27
27
  assert_equal "some text", block.text
28
28
  end
29
-
29
+
30
+ def test_stores_in_utf_8
31
+ text = "some text"
32
+ text.encode("US-ASCII")
33
+ block = new_block [[[:block_type, :paragraph], [:block_id, "some-id"], [:attribute_list]], text]
34
+ assert_equal Encoding::UTF_8, block.text.encoding
35
+ end
36
+
37
+ def test_outputs_in_utf_8
38
+ text = "some text"
39
+ text.encode("US-ASCII")
40
+ block = new_block [[[:block_type, :paragraph], [:block_id, "some-id"], [:attribute_list]], text]
41
+ assert_equal Encoding::UTF_8, block.to_h.encoding
42
+ end
43
+
30
44
  def test_initializes_with_attribute_list_and_parent_id
31
45
  block = new_block [[[:block_type, :paragraph], [:block_id, "some-id"], [:parent_id, "parent-id"], [:attribute_list, [:class, "cool"]]], "some text"]
32
-
46
+
33
47
  assert_equal :paragraph, block.type
34
48
  assert_equal "some-id", block.id
35
49
  assert_equal "parent-id", block.parent_id
36
50
  assert_equal "cool", block.attributes[:class]
37
51
  assert_equal "some text", block.text
38
52
  end
39
-
53
+
40
54
  def test_initializes_with_header_array_and_no_text
41
55
  block = new_block [[[:block_type, :paragraph], [:block_id, "some-id"]]]
42
-
56
+
43
57
  assert_equal :paragraph, block.type
44
58
  assert_equal "some-id", block.id
45
59
  assert_nil block.text, "Block#text should be nil"
46
60
  end
47
-
61
+
48
62
  def test_initialized_from_parser_output
49
63
  text = ":preformatted my-code great-code class=ruby whiz=bang:\ndef foo arg\n\t puts arg\n end"
50
64
  doc = BlockParser.new.parse(text)
51
65
  block = Flannel::BaseBlock.new doc.content[0]
52
-
66
+
53
67
  assert_equal :preformatted, block.type
54
68
  assert_equal "my-code", block.id
55
69
  assert_equal "great-code", block.parent_id
@@ -1,3 +1,4 @@
1
+
1
2
  require 'test_helper'
2
3
 
3
4
  class FlannelTest < Test::Unit::TestCase
@@ -67,5 +68,6 @@ class FlannelTest < Test::Unit::TestCase
67
68
 
68
69
  assert_equal result, Flannel.quilt(markup)
69
70
  end
71
+
70
72
  end
71
73
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 13
9
- version: 0.2.13
8
+ - 14
9
+ version: 0.2.14
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jamal Hansen
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-02-26 00:00:00 -06:00
17
+ date: 2010-03-01 00:00:00 -06:00
18
18
  default_executable: quilt-it
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -108,6 +108,8 @@ files:
108
108
  - features/support/env.rb
109
109
  - features/wiki_links.feature
110
110
  - flannel.gemspec
111
+ - graphics/flannel.xcf
112
+ - graphics/flannel_small.jpg
111
113
  - lib/flannel.rb
112
114
  - lib/flannel/base_block.rb
113
115
  - lib/flannel/block.treetop
@@ -116,6 +118,7 @@ files:
116
118
  - lib/flannel/feed_parser.rb
117
119
  - lib/flannel/file_cache.rb
118
120
  - lib/flannel/html_formatter.rb
121
+ - lib/flannel/html_transformable.rb
119
122
  - lib/flannel/wrappable.rb
120
123
  - test/base_block_html_generation_test.rb
121
124
  - test/base_block_test.rb