pricecut 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+ --order random
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.3
5
+ - jruby-18mode # JRuby in 1.8 mode
6
+ - jruby-19mode # JRuby in 1.9 mode
7
+ - rbx-18mode
8
+ - rbx-19mode
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Pricecut
1
+ # Pricecut [![Build Status][build_status_image]][build_status]
2
2
 
3
3
  Pricecut takes your HTML and marks it down.
4
4
 
@@ -18,7 +18,44 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- Usage instructions coming soon.
21
+ ```ruby
22
+ require "pricecut"
23
+
24
+ html = <<-HTML
25
+ <h1>Pricecut is Awesome!</h1>
26
+
27
+ <p>
28
+ Pricecut lets me take my <strong>HTML</strong> and
29
+ <em>mark it down</em>.
30
+ </p>
31
+
32
+ <p><img src="awesome.png" alt="Awesome" /></p>
33
+ HTML
34
+
35
+ Pricecut.parse(html)
36
+ # => # Pricecut is Awesome! #
37
+ # Pricecut lets me take my **HTML** and _mark it down_.
38
+ # ![Awesome](awesome.png)
39
+ ```
40
+
41
+ ## Supported Platforms
42
+
43
+ * Ruby 1.8.7
44
+ * Ruby 1.9.3
45
+ * JRuby _(1.8 mode)_
46
+ * JRuby _(1.9 mode)_
47
+ * Rubinius _(1.8 mode)_
48
+ * Rubinius _(1.9 mode)_
49
+
50
+ ## TODO
51
+
52
+ * Decouple `MarkdownVisitor` from `Pricecut::Elements`.
53
+ * Document code.
54
+ * Add support for Markdown code blocks.
55
+ * Add indentation support.
56
+ * Add word-wrap support. _(80 characters)_
57
+ * Make `Pricecut::Elements` classes configurable.
58
+ _(e.g. Use your own custom class for `<hr />` if you want.)_
22
59
 
23
60
  ## Contributing
24
61
 
@@ -27,3 +64,11 @@ Usage instructions coming soon.
27
64
  3. Commit your changes (`git commit -am 'Added some feature'`)
28
65
  4. Push to the branch (`git push origin my-new-feature`)
29
66
  5. Create new Pull Request
67
+
68
+ ## Copyright
69
+ Copyright (c) 2012 [Adam Tanner][email]. See [License][license] for details.
70
+
71
+ [email]: mailto:adam@adamtanner.org
72
+ [license]: https://github.com/adamtanner/pricecut/blob/master/LICENSE.md
73
+ [build_status]: http://travis-ci.org/adamtanner/pricecut
74
+ [build_status_image]: https://secure.travis-ci.org/adamtanner/pricecut.png
data/Rakefile CHANGED
@@ -1,2 +1,8 @@
1
1
  #!/usr/bin/env rake
2
2
  require "bundler/gem_tasks"
3
+
4
+ require "rspec/core/rake_task"
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :test => :spec
8
+ task :default => :spec
data/lib/pricecut.rb CHANGED
@@ -18,7 +18,8 @@ require "pricecut/elements/a"
18
18
 
19
19
  module Pricecut
20
20
  def self.parse(html)
21
- root = Nokogiri::HTML::DocumentFragment.parse(html)
21
+ root = Nokogiri::HTML::Document.parse(html) {|c| c.noblanks }
22
+
22
23
  MarkdownVisitor.new.visit(root).output
23
24
  end
24
25
  end
@@ -2,7 +2,7 @@ module Pricecut
2
2
  module Elements
3
3
  class Blockquote < Element
4
4
  def output!
5
- p "> "; yield_children
5
+ p "> "; yield_children
6
6
  end
7
7
  end
8
8
  end
@@ -2,13 +2,17 @@ module Pricecut
2
2
  module Elements
3
3
  class Li < Element
4
4
  def output!
5
- case node.parent.name
6
- when "ol"
5
+ if ordered_list?
7
6
  p "1. "; yield_children; eol
8
- when "ul"
7
+ else
9
8
  p "- "; yield_children; eol
10
9
  end
11
10
  end
11
+
12
+ private
13
+ def ordered_list?
14
+ node.parent.name == "ol"
15
+ end
12
16
  end
13
17
  end
14
18
  end
@@ -2,7 +2,30 @@ module Pricecut
2
2
  module Elements
3
3
  class Text < Element
4
4
  def output!
5
- p node.text.strip
5
+ return if whitespace_only?
6
+
7
+ output = node.text
8
+
9
+ output.tr!("\n", " ")
10
+ output.squeeze!(" ")
11
+
12
+ output.lstrip! unless previous_sibling?
13
+ output.rstrip! unless next_sibling?
14
+
15
+ p output
16
+ end
17
+
18
+ private
19
+ def whitespace_only?
20
+ node.blank?
21
+ end
22
+
23
+ def previous_sibling?
24
+ node.previous_sibling
25
+ end
26
+
27
+ def next_sibling?
28
+ node.next_sibling
6
29
  end
7
30
  end
8
31
  end
@@ -9,10 +9,10 @@ module Pricecut
9
9
  end
10
10
 
11
11
  def visit(node)
12
- begin
12
+ if Pricecut::Elements.const_defined?(node.name.capitalize)
13
13
  element = Pricecut::Elements.const_get(node.name.capitalize)
14
14
  element.new(self, node).output!
15
- rescue NameError
15
+ else
16
16
  # Unsupported element, continue visiting children.
17
17
  visit_children(node)
18
18
  end
@@ -1,3 +1,3 @@
1
1
  module Pricecut
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/pricecut.gemspec CHANGED
@@ -17,5 +17,6 @@ Gem::Specification.new do |gem|
17
17
 
18
18
  gem.add_runtime_dependency "nokogiri"
19
19
 
20
+ gem.add_development_dependency "rake"
20
21
  gem.add_development_dependency "rspec"
21
22
  end
@@ -12,9 +12,9 @@ describe Pricecut::Elements::A do
12
12
 
13
13
  describe "#output!" do
14
14
  it "appends a Markdown link to the output" do
15
- subject.output!
15
+ subject.output!
16
16
 
17
- visitor.output.should eq("[Example](http://example.com)")
17
+ visitor.output.should eq("[Example](http://example.com)")
18
18
  end
19
19
  end
20
20
  end
@@ -12,9 +12,9 @@ describe Pricecut::Elements::Blockquote do
12
12
 
13
13
  describe "#output!" do
14
14
  it "appends a Markdown blockquote to the output" do
15
- subject.output!
15
+ subject.output!
16
16
 
17
- visitor.output.should eq("> Blockquote")
17
+ visitor.output.should eq("> Blockquote")
18
18
  end
19
19
  end
20
20
  end
@@ -12,9 +12,9 @@ describe Pricecut::Elements::Em do
12
12
 
13
13
  describe "#output!" do
14
14
  it "appends a Markdown emphasis to the output" do
15
- subject.output!
15
+ subject.output!
16
16
 
17
- visitor.output.should eq("_Emphasized._")
17
+ visitor.output.should eq("_Emphasized._")
18
18
  end
19
19
  end
20
20
  end
@@ -12,9 +12,9 @@ describe Pricecut::Elements::H1 do
12
12
 
13
13
  describe "#output!" do
14
14
  it "appends a Markdown header with newline to the output" do
15
- subject.output!
15
+ subject.output!
16
16
 
17
- visitor.output.should eq("# Heading #\n")
17
+ visitor.output.should eq("# Heading #\n")
18
18
  end
19
19
  end
20
20
  end
@@ -12,9 +12,9 @@ describe Pricecut::Elements::H2 do
12
12
 
13
13
  describe "#output!" do
14
14
  it "appends a Markdown header with newline to the output" do
15
- subject.output!
15
+ subject.output!
16
16
 
17
- visitor.output.should eq("## Heading ##\n")
17
+ visitor.output.should eq("## Heading ##\n")
18
18
  end
19
19
  end
20
20
  end
@@ -12,9 +12,9 @@ describe Pricecut::Elements::H3 do
12
12
 
13
13
  describe "#output!" do
14
14
  it "appends a Markdown header with newline to the output" do
15
- subject.output!
15
+ subject.output!
16
16
 
17
- visitor.output.should eq("### Heading ###\n")
17
+ visitor.output.should eq("### Heading ###\n")
18
18
  end
19
19
  end
20
20
  end
@@ -12,9 +12,9 @@ describe Pricecut::Elements::H4 do
12
12
 
13
13
  describe "#output!" do
14
14
  it "appends a Markdown header with newline to the output" do
15
- subject.output!
15
+ subject.output!
16
16
 
17
- visitor.output.should eq("#### Heading ####\n")
17
+ visitor.output.should eq("#### Heading ####\n")
18
18
  end
19
19
  end
20
20
  end
@@ -12,9 +12,9 @@ describe Pricecut::Elements::H5 do
12
12
 
13
13
  describe "#output!" do
14
14
  it "appends a Markdown header with newline to the output" do
15
- subject.output!
15
+ subject.output!
16
16
 
17
- visitor.output.should eq("##### Heading #####\n")
17
+ visitor.output.should eq("##### Heading #####\n")
18
18
  end
19
19
  end
20
20
  end
@@ -12,9 +12,9 @@ describe Pricecut::Elements::H6 do
12
12
 
13
13
  describe "#output!" do
14
14
  it "appends a Markdown header with newline to the output" do
15
- subject.output!
15
+ subject.output!
16
16
 
17
- visitor.output.should eq("###### Heading ######\n")
17
+ visitor.output.should eq("###### Heading ######\n")
18
18
  end
19
19
  end
20
20
  end
@@ -12,9 +12,9 @@ describe Pricecut::Elements::Img do
12
12
 
13
13
  describe "#output!" do
14
14
  it "appends a Markdown image to the output" do
15
- subject.output!
15
+ subject.output!
16
16
 
17
- visitor.output.should eq("![Image](image.png)")
17
+ visitor.output.should eq("![Image](image.png)")
18
18
  end
19
19
  end
20
20
  end
@@ -4,32 +4,27 @@ require "pricecut"
4
4
  describe Pricecut::Elements::Li do
5
5
  describe "#output" do
6
6
  let(:visitor) { Pricecut::MarkdownVisitor.new }
7
+ let(:root) { parse(%<<li>List item<li>>) }
7
8
 
8
9
  subject { described_class.new(visitor, root) }
9
10
 
10
11
  describe "when in an ordered list" do
11
- let(:root) do
12
- # We need to grab the first child to grab the <li>.
13
- parse(%<<ol><li>Ordered List<li></ol>>).children.first
14
- end
12
+ before { subject.stub(:ordered_list?) { true } }
15
13
 
16
14
  it "appends a Markdown ordered list item with newline to the output" do
17
15
  subject.output!
18
16
 
19
- visitor.output.should eq("1. Ordered List\n")
17
+ visitor.output.should eq("1. List item\n")
20
18
  end
21
19
  end
22
20
 
23
21
  describe "when in an unordered list" do
24
- let(:root) do
25
- # We need to grab the first child to grab the <li>.
26
- parse(%<<ul><li>Unordered List<li></ul>>).children.first
27
- end
22
+ before { subject.stub(:ordered_list?) { false } }
28
23
 
29
- it "appends a Markdown ordered list item with newline to the output" do
24
+ it "appends a Markdown unordered list item with newline to the output" do
30
25
  subject.output!
31
26
 
32
- visitor.output.should eq("- Unordered List\n")
27
+ visitor.output.should eq("- List item\n")
33
28
  end
34
29
  end
35
30
  end
@@ -12,9 +12,9 @@ describe Pricecut::Elements::Ol do
12
12
 
13
13
  describe "#output!" do
14
14
  it "appends a newline wrapped list to the output" do
15
- subject.output!
15
+ subject.output!
16
16
 
17
- visitor.output.should eq("\nOrdered List.\n")
17
+ visitor.output.should eq("\nOrdered List.\n")
18
18
  end
19
19
  end
20
20
  end
@@ -12,9 +12,9 @@ describe Pricecut::Elements::P do
12
12
 
13
13
  describe "#output!" do
14
14
  it "appends the text and a newline to the output" do
15
- subject.output!
15
+ subject.output!
16
16
 
17
- visitor.output.should eq("Paragraph.\n")
17
+ visitor.output.should eq("Paragraph.\n")
18
18
  end
19
19
  end
20
20
  end
@@ -12,9 +12,9 @@ describe Pricecut::Elements::Strong do
12
12
 
13
13
  describe "#output!" do
14
14
  it "appends a Markdown strong to the output" do
15
- subject.output!
15
+ subject.output!
16
16
 
17
- visitor.output.should eq("**Strong.**")
17
+ visitor.output.should eq("**Strong.**")
18
18
  end
19
19
  end
20
20
  end
@@ -3,18 +3,71 @@ require "pricecut"
3
3
 
4
4
  describe Pricecut::Elements::Text do
5
5
  let(:visitor) { Pricecut::MarkdownVisitor.new }
6
-
7
- let(:root) do
8
- parse %<\n\nText.\n\n>
9
- end
6
+ let(:root) { parse %<\n This is \n text.\n > }
10
7
 
11
8
  subject { described_class.new(visitor, root) }
12
9
 
13
10
  describe "#output!" do
14
- it "appends the whitespace-stripped text to the output" do
15
- subject.output!
11
+ describe "with only whitespace" do
12
+ before { subject.stub(:whitespace_only?) { true } }
13
+
14
+ it "does not append anything to the output" do
15
+ subject.output!
16
+
17
+ visitor.output.should be_empty
18
+ end
19
+ end
20
+
21
+ describe "with ample whitespace" do
22
+ before do
23
+ subject.stub(:previous_sibling?) { false }
24
+ subject.stub(:next_sibling?) { false }
25
+ end
26
+
27
+ it "removes insignificant whitespace and appends to the output" do
28
+ subject.output!
29
+
30
+ visitor.output.should eq("This is text.")
31
+ end
32
+ end
33
+
34
+ describe "preceded by an HTML tag" do
35
+ before do
36
+ subject.stub(:previous_sibling?) { true }
37
+ subject.stub(:next_sibling?) { false }
38
+ end
39
+
40
+ it "removes the subsequent whitespace and appends to the output" do
41
+ subject.output!
42
+
43
+ visitor.output.should eq(" This is text.")
44
+ end
45
+ end
46
+
47
+ describe "succeeded by an HTML tag" do
48
+ before do
49
+ subject.stub(:previous_sibling?) { false }
50
+ subject.stub(:next_sibling?) { true }
51
+ end
52
+
53
+ it "removes the preceding whitespace and appends to the output" do
54
+ subject.output!
55
+
56
+ visitor.output.should eq("This is text. ")
57
+ end
58
+ end
59
+
60
+ describe "succeeded and preceded by an HTML tag" do
61
+ before do
62
+ subject.stub(:previous_sibling?) { true }
63
+ subject.stub(:next_sibling?) { true }
64
+ end
65
+
66
+ it "leaves all whitespace and appends to the output" do
67
+ subject.output!
16
68
 
17
- visitor.output.should eq("Text.")
69
+ visitor.output.should eq(" This is text. ")
70
+ end
18
71
  end
19
72
  end
20
73
  end
@@ -27,6 +27,11 @@ describe Pricecut::MarkdownVisitor do
27
27
  end
28
28
  end
29
29
 
30
+ after do
31
+ # Remove Test from the supported elements
32
+ Pricecut::Elements.send(:remove_const, "Test")
33
+ end
34
+
30
35
  it "writes the output of the element to output" do
31
36
  subject.visit(node)
32
37
 
@@ -35,10 +40,6 @@ describe Pricecut::MarkdownVisitor do
35
40
  end
36
41
 
37
42
  describe "with unsupported element" do
38
- before do
39
- # Remove Test from the supported elements
40
- Pricecut::Elements.send(:remove_const, "Test")
41
- end
42
43
  it "skips the unsupported element" do
43
44
  subject.visit(node)
44
45
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pricecut
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-11 00:00:00.000000000 Z
12
+ date: 2012-06-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
16
- requirement: &70324877374400 !ruby/object:Gem::Requirement
16
+ requirement: &70333794231480 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,21 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70324877374400
24
+ version_requirements: *70333794231480
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &70333794231060 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70333794231060
25
36
  - !ruby/object:Gem::Dependency
26
37
  name: rspec
27
- requirement: &70324877373980 !ruby/object:Gem::Requirement
38
+ requirement: &70333794230640 !ruby/object:Gem::Requirement
28
39
  none: false
29
40
  requirements:
30
41
  - - ! '>='
@@ -32,7 +43,7 @@ dependencies:
32
43
  version: '0'
33
44
  type: :development
34
45
  prerelease: false
35
- version_requirements: *70324877373980
46
+ version_requirements: *70333794230640
36
47
  description: Pricecut takes your HTML and marks it down.
37
48
  email:
38
49
  - adam@adamtanner.org
@@ -41,6 +52,8 @@ extensions: []
41
52
  extra_rdoc_files: []
42
53
  files:
43
54
  - .gitignore
55
+ - .rspec
56
+ - .travis.yml
44
57
  - Gemfile
45
58
  - LICENSE
46
59
  - README.md
@@ -98,7 +111,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
98
111
  version: '0'
99
112
  segments:
100
113
  - 0
101
- hash: -2120063507989546998
114
+ hash: -1590595780056887565
102
115
  required_rubygems_version: !ruby/object:Gem::Requirement
103
116
  none: false
104
117
  requirements:
@@ -107,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
120
  version: '0'
108
121
  segments:
109
122
  - 0
110
- hash: -2120063507989546998
123
+ hash: -1590595780056887565
111
124
  requirements: []
112
125
  rubyforge_project:
113
126
  rubygems_version: 1.8.7