pricecut 0.0.1 → 0.0.2
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/lib/pricecut/elements/a.rb +12 -0
- data/lib/pricecut/elements/blockquote.rb +9 -0
- data/lib/pricecut/elements/element.rb +28 -0
- data/lib/pricecut/elements/em.rb +12 -0
- data/lib/pricecut/elements/h1.rb +9 -0
- data/lib/pricecut/elements/h2.rb +9 -0
- data/lib/pricecut/elements/h3.rb +9 -0
- data/lib/pricecut/elements/h4.rb +9 -0
- data/lib/pricecut/elements/h5.rb +9 -0
- data/lib/pricecut/elements/h6.rb +9 -0
- data/lib/pricecut/elements/img.rb +12 -0
- data/lib/pricecut/elements/li.rb +14 -0
- data/lib/pricecut/elements/ol.rb +12 -0
- data/lib/pricecut/elements/p.rb +11 -0
- data/lib/pricecut/elements/strong.rb +12 -0
- data/lib/pricecut/elements/text.rb +9 -0
- data/lib/pricecut/markdown_visitor.rb +31 -0
- data/lib/pricecut/version.rb +1 -1
- data/lib/pricecut.rb +22 -3
- data/pricecut.gemspec +4 -0
- data/spec/pricecut/elements/a_spec.rb +20 -0
- data/spec/pricecut/elements/blockquote_spec.rb +20 -0
- data/spec/pricecut/elements/element_spec.rb +43 -0
- data/spec/pricecut/elements/em_spec.rb +20 -0
- data/spec/pricecut/elements/h1_spec.rb +20 -0
- data/spec/pricecut/elements/h2_spec.rb +20 -0
- data/spec/pricecut/elements/h3_spec.rb +20 -0
- data/spec/pricecut/elements/h4_spec.rb +20 -0
- data/spec/pricecut/elements/h5_spec.rb +20 -0
- data/spec/pricecut/elements/h6_spec.rb +20 -0
- data/spec/pricecut/elements/img_spec.rb +20 -0
- data/spec/pricecut/elements/li_spec.rb +36 -0
- data/spec/pricecut/elements/ol_spec.rb +20 -0
- data/spec/pricecut/elements/p_spec.rb +20 -0
- data/spec/pricecut/elements/strong_spec.rb +20 -0
- data/spec/pricecut/elements/text_spec.rb +20 -0
- data/spec/pricecut/markdown_visitor_spec.rb +81 -0
- data/spec/pricecut/pricecut_spec.rb +57 -0
- data/spec/spec_helper.rb +13 -0
- metadata +82 -5
@@ -0,0 +1,28 @@
|
|
1
|
+
module Pricecut
|
2
|
+
module Elements
|
3
|
+
class Element
|
4
|
+
attr_accessor :visitor, :node
|
5
|
+
|
6
|
+
def initialize(visitor, node)
|
7
|
+
@visitor = visitor
|
8
|
+
@node = node
|
9
|
+
end
|
10
|
+
|
11
|
+
def append_newline
|
12
|
+
append_output("\n")
|
13
|
+
end
|
14
|
+
|
15
|
+
alias_method :eol, :append_newline
|
16
|
+
|
17
|
+
def append_output(text)
|
18
|
+
visitor.append_output(text)
|
19
|
+
end
|
20
|
+
|
21
|
+
alias_method :p, :append_output
|
22
|
+
|
23
|
+
def yield_children
|
24
|
+
visitor.visit_children(node)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require "nokogiri"
|
2
|
+
|
3
|
+
module Pricecut
|
4
|
+
class MarkdownVisitor
|
5
|
+
attr_reader :output
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@output = ""
|
9
|
+
end
|
10
|
+
|
11
|
+
def visit(node)
|
12
|
+
begin
|
13
|
+
element = Pricecut::Elements.const_get(node.name.capitalize)
|
14
|
+
element.new(self, node).output!
|
15
|
+
rescue NameError
|
16
|
+
# Unsupported element, continue visiting children.
|
17
|
+
visit_children(node)
|
18
|
+
end
|
19
|
+
|
20
|
+
self
|
21
|
+
end
|
22
|
+
|
23
|
+
def append_output(string)
|
24
|
+
@output << string
|
25
|
+
end
|
26
|
+
|
27
|
+
def visit_children(node)
|
28
|
+
node.children.each {|child| child.accept(self) }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/pricecut/version.rb
CHANGED
data/lib/pricecut.rb
CHANGED
@@ -1,5 +1,24 @@
|
|
1
|
-
require "pricecut/
|
1
|
+
require "pricecut/markdown_visitor"
|
2
|
+
require "pricecut/elements/element"
|
3
|
+
require "pricecut/elements/strong"
|
4
|
+
require "pricecut/elements/em"
|
5
|
+
require "pricecut/elements/ol"
|
6
|
+
require "pricecut/elements/li"
|
7
|
+
require "pricecut/elements/p"
|
8
|
+
require "pricecut/elements/img"
|
9
|
+
require "pricecut/elements/blockquote"
|
10
|
+
require "pricecut/elements/h1"
|
11
|
+
require "pricecut/elements/h2"
|
12
|
+
require "pricecut/elements/h3"
|
13
|
+
require "pricecut/elements/h4"
|
14
|
+
require "pricecut/elements/h5"
|
15
|
+
require "pricecut/elements/h6"
|
16
|
+
require "pricecut/elements/text"
|
17
|
+
require "pricecut/elements/a"
|
2
18
|
|
3
19
|
module Pricecut
|
4
|
-
|
5
|
-
|
20
|
+
def self.parse(html)
|
21
|
+
root = Nokogiri::HTML::DocumentFragment.parse(html)
|
22
|
+
MarkdownVisitor.new.visit(root).output
|
23
|
+
end
|
24
|
+
end
|
data/pricecut.gemspec
CHANGED
@@ -0,0 +1,20 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "pricecut"
|
3
|
+
|
4
|
+
describe Pricecut::Elements::A do
|
5
|
+
let(:visitor) { Pricecut::MarkdownVisitor.new }
|
6
|
+
|
7
|
+
let(:root) do
|
8
|
+
parse %<<a href="http://example.com">Example</a>>
|
9
|
+
end
|
10
|
+
|
11
|
+
subject { described_class.new(visitor, root) }
|
12
|
+
|
13
|
+
describe "#output!" do
|
14
|
+
it "appends a Markdown link to the output" do
|
15
|
+
subject.output!
|
16
|
+
|
17
|
+
visitor.output.should eq("[Example](http://example.com)")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "pricecut"
|
3
|
+
|
4
|
+
describe Pricecut::Elements::Blockquote do
|
5
|
+
let(:visitor) { Pricecut::MarkdownVisitor.new }
|
6
|
+
|
7
|
+
let(:root) do
|
8
|
+
parse %<<blockquote>Blockquote</blockquote>>
|
9
|
+
end
|
10
|
+
|
11
|
+
subject { described_class.new(visitor, root) }
|
12
|
+
|
13
|
+
describe "#output!" do
|
14
|
+
it "appends a Markdown blockquote to the output" do
|
15
|
+
subject.output!
|
16
|
+
|
17
|
+
visitor.output.should eq("> Blockquote")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "pricecut"
|
3
|
+
|
4
|
+
describe Pricecut::Elements::Element do
|
5
|
+
let(:visitor) { Pricecut::MarkdownVisitor.new }
|
6
|
+
let(:node) { parse "<strong><em>I am strongly emphasized.</em></strong>" }
|
7
|
+
|
8
|
+
subject { described_class.new(visitor, node) }
|
9
|
+
|
10
|
+
describe "#initialize" do
|
11
|
+
it "initializes the visitor" do
|
12
|
+
subject.visitor.should be(visitor)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "initializes the node" do
|
16
|
+
subject.node.should be(node)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "#append_newline" do
|
21
|
+
it "appends a newline to the output" do
|
22
|
+
subject.append_newline
|
23
|
+
|
24
|
+
visitor.output.should eq("\n")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "#append_output" do
|
29
|
+
it "appends output to the output" do
|
30
|
+
subject.append_output("Output")
|
31
|
+
|
32
|
+
visitor.output.should eq("Output")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "#yield_children" do
|
37
|
+
it "writes the output of the child nodes to the output" do
|
38
|
+
subject.yield_children
|
39
|
+
|
40
|
+
visitor.output.should eq("_I am strongly emphasized._")
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "pricecut"
|
3
|
+
|
4
|
+
describe Pricecut::Elements::Em do
|
5
|
+
let(:visitor) { Pricecut::MarkdownVisitor.new }
|
6
|
+
|
7
|
+
let(:root) do
|
8
|
+
parse %<<em>Emphasized.</em>>
|
9
|
+
end
|
10
|
+
|
11
|
+
subject { described_class.new(visitor, root) }
|
12
|
+
|
13
|
+
describe "#output!" do
|
14
|
+
it "appends a Markdown emphasis to the output" do
|
15
|
+
subject.output!
|
16
|
+
|
17
|
+
visitor.output.should eq("_Emphasized._")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "pricecut"
|
3
|
+
|
4
|
+
describe Pricecut::Elements::H1 do
|
5
|
+
let(:visitor) { Pricecut::MarkdownVisitor.new }
|
6
|
+
|
7
|
+
let(:root) do
|
8
|
+
parse %<<h1>Heading</h1>>
|
9
|
+
end
|
10
|
+
|
11
|
+
subject { described_class.new(visitor, root) }
|
12
|
+
|
13
|
+
describe "#output!" do
|
14
|
+
it "appends a Markdown header with newline to the output" do
|
15
|
+
subject.output!
|
16
|
+
|
17
|
+
visitor.output.should eq("# Heading #\n")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "pricecut"
|
3
|
+
|
4
|
+
describe Pricecut::Elements::H2 do
|
5
|
+
let(:visitor) { Pricecut::MarkdownVisitor.new }
|
6
|
+
|
7
|
+
let(:root) do
|
8
|
+
parse %<<h2>Heading</h2>>
|
9
|
+
end
|
10
|
+
|
11
|
+
subject { described_class.new(visitor, root) }
|
12
|
+
|
13
|
+
describe "#output!" do
|
14
|
+
it "appends a Markdown header with newline to the output" do
|
15
|
+
subject.output!
|
16
|
+
|
17
|
+
visitor.output.should eq("## Heading ##\n")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "pricecut"
|
3
|
+
|
4
|
+
describe Pricecut::Elements::H3 do
|
5
|
+
let(:visitor) { Pricecut::MarkdownVisitor.new }
|
6
|
+
|
7
|
+
let(:root) do
|
8
|
+
parse %<<h3>Heading</h3>>
|
9
|
+
end
|
10
|
+
|
11
|
+
subject { described_class.new(visitor, root) }
|
12
|
+
|
13
|
+
describe "#output!" do
|
14
|
+
it "appends a Markdown header with newline to the output" do
|
15
|
+
subject.output!
|
16
|
+
|
17
|
+
visitor.output.should eq("### Heading ###\n")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "pricecut"
|
3
|
+
|
4
|
+
describe Pricecut::Elements::H4 do
|
5
|
+
let(:visitor) { Pricecut::MarkdownVisitor.new }
|
6
|
+
|
7
|
+
let(:root) do
|
8
|
+
parse %<<h4>Heading</h4>>
|
9
|
+
end
|
10
|
+
|
11
|
+
subject { described_class.new(visitor, root) }
|
12
|
+
|
13
|
+
describe "#output!" do
|
14
|
+
it "appends a Markdown header with newline to the output" do
|
15
|
+
subject.output!
|
16
|
+
|
17
|
+
visitor.output.should eq("#### Heading ####\n")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "pricecut"
|
3
|
+
|
4
|
+
describe Pricecut::Elements::H5 do
|
5
|
+
let(:visitor) { Pricecut::MarkdownVisitor.new }
|
6
|
+
|
7
|
+
let(:root) do
|
8
|
+
parse %<<h5>Heading</h5>>
|
9
|
+
end
|
10
|
+
|
11
|
+
subject { described_class.new(visitor, root) }
|
12
|
+
|
13
|
+
describe "#output!" do
|
14
|
+
it "appends a Markdown header with newline to the output" do
|
15
|
+
subject.output!
|
16
|
+
|
17
|
+
visitor.output.should eq("##### Heading #####\n")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "pricecut"
|
3
|
+
|
4
|
+
describe Pricecut::Elements::H6 do
|
5
|
+
let(:visitor) { Pricecut::MarkdownVisitor.new }
|
6
|
+
|
7
|
+
let(:root) do
|
8
|
+
parse %<<h6>Heading</h6>>
|
9
|
+
end
|
10
|
+
|
11
|
+
subject { described_class.new(visitor, root) }
|
12
|
+
|
13
|
+
describe "#output!" do
|
14
|
+
it "appends a Markdown header with newline to the output" do
|
15
|
+
subject.output!
|
16
|
+
|
17
|
+
visitor.output.should eq("###### Heading ######\n")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "pricecut"
|
3
|
+
|
4
|
+
describe Pricecut::Elements::Img do
|
5
|
+
let(:visitor) { Pricecut::MarkdownVisitor.new }
|
6
|
+
|
7
|
+
let(:root) do
|
8
|
+
parse %<<img alt="Image" src="image.png" />>
|
9
|
+
end
|
10
|
+
|
11
|
+
subject { described_class.new(visitor, root) }
|
12
|
+
|
13
|
+
describe "#output!" do
|
14
|
+
it "appends a Markdown image to the output" do
|
15
|
+
subject.output!
|
16
|
+
|
17
|
+
visitor.output.should eq("")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "pricecut"
|
3
|
+
|
4
|
+
describe Pricecut::Elements::Li do
|
5
|
+
describe "#output" do
|
6
|
+
let(:visitor) { Pricecut::MarkdownVisitor.new }
|
7
|
+
|
8
|
+
subject { described_class.new(visitor, root) }
|
9
|
+
|
10
|
+
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
|
15
|
+
|
16
|
+
it "appends a Markdown ordered list item with newline to the output" do
|
17
|
+
subject.output!
|
18
|
+
|
19
|
+
visitor.output.should eq("1. Ordered List\n")
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
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
|
28
|
+
|
29
|
+
it "appends a Markdown ordered list item with newline to the output" do
|
30
|
+
subject.output!
|
31
|
+
|
32
|
+
visitor.output.should eq("- Unordered List\n")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "pricecut"
|
3
|
+
|
4
|
+
describe Pricecut::Elements::Ol do
|
5
|
+
let(:visitor) { Pricecut::MarkdownVisitor.new }
|
6
|
+
|
7
|
+
let(:root) do
|
8
|
+
parse %<<ol>Ordered List.</ol>>
|
9
|
+
end
|
10
|
+
|
11
|
+
subject { described_class.new(visitor, root) }
|
12
|
+
|
13
|
+
describe "#output!" do
|
14
|
+
it "appends a newline wrapped list to the output" do
|
15
|
+
subject.output!
|
16
|
+
|
17
|
+
visitor.output.should eq("\nOrdered List.\n")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "pricecut"
|
3
|
+
|
4
|
+
describe Pricecut::Elements::P do
|
5
|
+
let(:visitor) { Pricecut::MarkdownVisitor.new }
|
6
|
+
|
7
|
+
let(:root) do
|
8
|
+
parse %<<p>Paragraph.</p>>
|
9
|
+
end
|
10
|
+
|
11
|
+
subject { described_class.new(visitor, root) }
|
12
|
+
|
13
|
+
describe "#output!" do
|
14
|
+
it "appends the text and a newline to the output" do
|
15
|
+
subject.output!
|
16
|
+
|
17
|
+
visitor.output.should eq("Paragraph.\n")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "pricecut"
|
3
|
+
|
4
|
+
describe Pricecut::Elements::Strong do
|
5
|
+
let(:visitor) { Pricecut::MarkdownVisitor.new }
|
6
|
+
|
7
|
+
let(:root) do
|
8
|
+
parse %<<strong>Strong.</strong>>
|
9
|
+
end
|
10
|
+
|
11
|
+
subject { described_class.new(visitor, root) }
|
12
|
+
|
13
|
+
describe "#output!" do
|
14
|
+
it "appends a Markdown strong to the output" do
|
15
|
+
subject.output!
|
16
|
+
|
17
|
+
visitor.output.should eq("**Strong.**")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "pricecut"
|
3
|
+
|
4
|
+
describe Pricecut::Elements::Text do
|
5
|
+
let(:visitor) { Pricecut::MarkdownVisitor.new }
|
6
|
+
|
7
|
+
let(:root) do
|
8
|
+
parse %<\n\nText.\n\n>
|
9
|
+
end
|
10
|
+
|
11
|
+
subject { described_class.new(visitor, root) }
|
12
|
+
|
13
|
+
describe "#output!" do
|
14
|
+
it "appends the whitespace-stripped text to the output" do
|
15
|
+
subject.output!
|
16
|
+
|
17
|
+
visitor.output.should eq("Text.")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "pricecut"
|
3
|
+
|
4
|
+
describe Pricecut::MarkdownVisitor do
|
5
|
+
describe "#initialize" do
|
6
|
+
it "initializes the output" do
|
7
|
+
described_class.new.output.should_not be_nil
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "#visit" do
|
12
|
+
let(:node) { parse "<test><strong>Test</strong></test>" }
|
13
|
+
|
14
|
+
describe "with supported element" do
|
15
|
+
before do
|
16
|
+
# Define the element class so that
|
17
|
+
# it is supported.
|
18
|
+
# Maps to a <test> element.
|
19
|
+
module Pricecut
|
20
|
+
module Elements
|
21
|
+
class Test < Element
|
22
|
+
def output!
|
23
|
+
p "?? "; yield_children; p " ??"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it "writes the output of the element to output" do
|
31
|
+
subject.visit(node)
|
32
|
+
|
33
|
+
subject.output.should eq("?? **Test** ??")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
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
|
+
it "skips the unsupported element" do
|
43
|
+
subject.visit(node)
|
44
|
+
|
45
|
+
subject.output.should eq("**Test**")
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "#append_output" do
|
51
|
+
it "appends text to the output" do
|
52
|
+
subject.append_output("Output")
|
53
|
+
|
54
|
+
subject.output.should eq("Output")
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe "#visit_children" do
|
59
|
+
before do
|
60
|
+
class Node
|
61
|
+
attr_reader :visited
|
62
|
+
|
63
|
+
def accept(visitor)
|
64
|
+
@visited = true
|
65
|
+
end
|
66
|
+
|
67
|
+
def children
|
68
|
+
@children ||= (1..3).map { Node.new }
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
let(:node) { Node.new }
|
74
|
+
|
75
|
+
it "visits the children of the node" do
|
76
|
+
subject.visit_children(node)
|
77
|
+
|
78
|
+
node.children.all?(&:visited).should be_true
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require "pricecut"
|
2
|
+
|
3
|
+
describe Pricecut do
|
4
|
+
describe ".parse" do
|
5
|
+
it "parses HTML into Markdown" do
|
6
|
+
html = <<-HTML.unindent
|
7
|
+
<h1>H1</h1>
|
8
|
+
<h2>H2</h2>
|
9
|
+
<h3>H3</h3>
|
10
|
+
<h4>H4</h4>
|
11
|
+
<h5>H5</h5>
|
12
|
+
<h6>H6</h6>
|
13
|
+
|
14
|
+
<p>Paragraph</p>
|
15
|
+
|
16
|
+
<ol><li>This is an ordered list.</li></ol>
|
17
|
+
|
18
|
+
<ul><li>This is an unordered list.</li></ul>
|
19
|
+
|
20
|
+
<p><em>This is emphasized.</em></p>
|
21
|
+
<p><i>This is italic.</em></p>
|
22
|
+
|
23
|
+
<p><strong>This is strong.</strong></p>
|
24
|
+
<p><b>This is bold.</b></p>
|
25
|
+
|
26
|
+
<blockquote><p>This is a blockquote.</p></blockquote>
|
27
|
+
|
28
|
+
<p><img alt="Image" src="image.png" /></p>
|
29
|
+
|
30
|
+
<p><a href="http://example.com">Example</a></p>
|
31
|
+
HTML
|
32
|
+
|
33
|
+
Pricecut.parse(html).should eq <<-MARKDOWN.unindent
|
34
|
+
# H1 #
|
35
|
+
## H2 ##
|
36
|
+
### H3 ###
|
37
|
+
#### H4 ####
|
38
|
+
##### H5 #####
|
39
|
+
###### H6 ######
|
40
|
+
Paragraph
|
41
|
+
|
42
|
+
1. This is an ordered list.
|
43
|
+
|
44
|
+
|
45
|
+
- This is an unordered list.
|
46
|
+
|
47
|
+
_This is emphasized._
|
48
|
+
_This is italic._
|
49
|
+
**This is strong.**
|
50
|
+
**This is bold.**
|
51
|
+
> This is a blockquote.
|
52
|
+

|
53
|
+
[Example](http://example.com)
|
54
|
+
MARKDOWN
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
def parse(html)
|
2
|
+
Nokogiri::HTML::DocumentFragment.parse(html).children.first
|
3
|
+
end
|
4
|
+
|
5
|
+
class String
|
6
|
+
# Strip leading whitespace from each line that is the same as the
|
7
|
+
# amount of whitespace on the first line of the string.
|
8
|
+
# Leaves _additional_ indentation on later lines intact.
|
9
|
+
# @see http://stackoverflow.com/q/3772864
|
10
|
+
def unindent
|
11
|
+
gsub /^#{self[/\A\s*/]}/, ''
|
12
|
+
end
|
13
|
+
end
|
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.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,29 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
date: 2012-06-11 00:00:00.000000000 Z
|
13
|
-
dependencies:
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: nokogiri
|
16
|
+
requirement: &70324877374400 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70324877374400
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &70324877373980 !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: *70324877373980
|
14
36
|
description: Pricecut takes your HTML and marks it down.
|
15
37
|
email:
|
16
38
|
- adam@adamtanner.org
|
@@ -24,8 +46,44 @@ files:
|
|
24
46
|
- README.md
|
25
47
|
- Rakefile
|
26
48
|
- lib/pricecut.rb
|
49
|
+
- lib/pricecut/elements/a.rb
|
50
|
+
- lib/pricecut/elements/blockquote.rb
|
51
|
+
- lib/pricecut/elements/element.rb
|
52
|
+
- lib/pricecut/elements/em.rb
|
53
|
+
- lib/pricecut/elements/h1.rb
|
54
|
+
- lib/pricecut/elements/h2.rb
|
55
|
+
- lib/pricecut/elements/h3.rb
|
56
|
+
- lib/pricecut/elements/h4.rb
|
57
|
+
- lib/pricecut/elements/h5.rb
|
58
|
+
- lib/pricecut/elements/h6.rb
|
59
|
+
- lib/pricecut/elements/img.rb
|
60
|
+
- lib/pricecut/elements/li.rb
|
61
|
+
- lib/pricecut/elements/ol.rb
|
62
|
+
- lib/pricecut/elements/p.rb
|
63
|
+
- lib/pricecut/elements/strong.rb
|
64
|
+
- lib/pricecut/elements/text.rb
|
65
|
+
- lib/pricecut/markdown_visitor.rb
|
27
66
|
- lib/pricecut/version.rb
|
28
67
|
- pricecut.gemspec
|
68
|
+
- spec/pricecut/elements/a_spec.rb
|
69
|
+
- spec/pricecut/elements/blockquote_spec.rb
|
70
|
+
- spec/pricecut/elements/element_spec.rb
|
71
|
+
- spec/pricecut/elements/em_spec.rb
|
72
|
+
- spec/pricecut/elements/h1_spec.rb
|
73
|
+
- spec/pricecut/elements/h2_spec.rb
|
74
|
+
- spec/pricecut/elements/h3_spec.rb
|
75
|
+
- spec/pricecut/elements/h4_spec.rb
|
76
|
+
- spec/pricecut/elements/h5_spec.rb
|
77
|
+
- spec/pricecut/elements/h6_spec.rb
|
78
|
+
- spec/pricecut/elements/img_spec.rb
|
79
|
+
- spec/pricecut/elements/li_spec.rb
|
80
|
+
- spec/pricecut/elements/ol_spec.rb
|
81
|
+
- spec/pricecut/elements/p_spec.rb
|
82
|
+
- spec/pricecut/elements/strong_spec.rb
|
83
|
+
- spec/pricecut/elements/text_spec.rb
|
84
|
+
- spec/pricecut/markdown_visitor_spec.rb
|
85
|
+
- spec/pricecut/pricecut_spec.rb
|
86
|
+
- spec/spec_helper.rb
|
29
87
|
homepage: ''
|
30
88
|
licenses: []
|
31
89
|
post_install_message:
|
@@ -40,7 +98,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
40
98
|
version: '0'
|
41
99
|
segments:
|
42
100
|
- 0
|
43
|
-
hash: -
|
101
|
+
hash: -2120063507989546998
|
44
102
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
103
|
none: false
|
46
104
|
requirements:
|
@@ -49,11 +107,30 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
49
107
|
version: '0'
|
50
108
|
segments:
|
51
109
|
- 0
|
52
|
-
hash: -
|
110
|
+
hash: -2120063507989546998
|
53
111
|
requirements: []
|
54
112
|
rubyforge_project:
|
55
113
|
rubygems_version: 1.8.7
|
56
114
|
signing_key:
|
57
115
|
specification_version: 3
|
58
116
|
summary: Pricecut takes your HTML and marks it down.
|
59
|
-
test_files:
|
117
|
+
test_files:
|
118
|
+
- spec/pricecut/elements/a_spec.rb
|
119
|
+
- spec/pricecut/elements/blockquote_spec.rb
|
120
|
+
- spec/pricecut/elements/element_spec.rb
|
121
|
+
- spec/pricecut/elements/em_spec.rb
|
122
|
+
- spec/pricecut/elements/h1_spec.rb
|
123
|
+
- spec/pricecut/elements/h2_spec.rb
|
124
|
+
- spec/pricecut/elements/h3_spec.rb
|
125
|
+
- spec/pricecut/elements/h4_spec.rb
|
126
|
+
- spec/pricecut/elements/h5_spec.rb
|
127
|
+
- spec/pricecut/elements/h6_spec.rb
|
128
|
+
- spec/pricecut/elements/img_spec.rb
|
129
|
+
- spec/pricecut/elements/li_spec.rb
|
130
|
+
- spec/pricecut/elements/ol_spec.rb
|
131
|
+
- spec/pricecut/elements/p_spec.rb
|
132
|
+
- spec/pricecut/elements/strong_spec.rb
|
133
|
+
- spec/pricecut/elements/text_spec.rb
|
134
|
+
- spec/pricecut/markdown_visitor_spec.rb
|
135
|
+
- spec/pricecut/pricecut_spec.rb
|
136
|
+
- spec/spec_helper.rb
|