Skip to content
Browse files

Add test cases & fix a typo that caused quotes to not be wrapped corr…

…ectly.
  • Loading branch information...
1 parent 39cc7ba commit d8b93208d61319869df121c9c3930eb4dcbffbbc @ludwigschubert committed Mar 13, 2013
View
10 Gemfile.lock
@@ -16,6 +16,7 @@ GEM
debugger-linecache (1.1.2)
debugger-ruby_core_source (>= 1.1.1)
debugger-ruby_core_source (1.2.0)
+ diff-lcs (1.2.1)
method_source (0.8.1)
nokogiri (1.5.6)
pry (0.9.12)
@@ -26,6 +27,14 @@ GEM
debugger (~> 1.3)
pry (~> 0.9.10)
rake (10.0.3)
+ rspec (2.13.0)
+ rspec-core (~> 2.13.0)
+ rspec-expectations (~> 2.13.0)
+ rspec-mocks (~> 2.13.0)
+ rspec-core (2.13.1)
+ rspec-expectations (2.13.0)
+ diff-lcs (>= 1.1.3, < 2.0)
+ rspec-mocks (2.13.0)
slop (3.4.3)
PLATFORMS
@@ -37,3 +46,4 @@ DEPENDENCIES
pry
pry-debugger
rake
+ rspec
View
14 lib/maildown.rb
@@ -10,7 +10,7 @@ def self.from_html html
class Parser
def initialize html
- @doc = Nokogiri::HTML(html.strip)
+ @doc = Nokogiri::HTML(html.gsub(/\n/, ''))
@links = []
end # initialize
@@ -23,13 +23,15 @@ def result
def parse
@result = @doc.children.map { |ele| parse_element(ele) }.join
@result = @result + "\n\n" + @links.join("\n") if @links.any?
- @result.gsub! /\n{3,}/, "\n\n" # Removes lines breaks where there are more than two.
+ @result.gsub!(/\n{3,}/, "\n\n") # Removes lines breaks where there are more than two.
+ @result.strip!
+ @result.lstrip!
return @result
end
def parse_element element
if element.is_a? Nokogiri::XML::Text
- return element.text.strip
+ return element.text.gsub(/^$\n/, '') # remove empty lines
else
if (children = element.children).count > 0
return wrap_node(element, children.map {|element| parse_element(element)}.join )
@@ -64,9 +66,11 @@ def wrap_node(node, contents=nil)
when 'li'
result << "*#{contents}\n"
when 'blockquote'
- contents.split('\n').each do |part|
- result << "> #{part}\n"
+ result << "\n"
+ contents.lines.each do |part|
+ result << "> #{part.strip}\n"
end
+ result << "\n"
when 'b'
result << "**#{contents}**"
when 'strong'
View
1 maildown.gemspec
@@ -24,5 +24,6 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rake"
spec.add_development_dependency "pry-debugger"
spec.add_development_dependency "pry"
+ spec.add_development_dependency "rspec"
end
View
7 spec/fixtures/paragraphs.txt
@@ -0,0 +1,7 @@
+<p>
+<p>Hi Ludwig,</p>
+<p>It’s been a few months since the end of the HCI Online class - we hope that the class was useful in giving you a sense of how interface design happens.</p>
+<p>For the class, we launched some experimental, social features, and thanks for signing up for them! As we’re considering including them in the next run on the HCI Online class, we wanted to find out what you thought.</p>
+<p>We showed you friends who were also taking the class and also assignments that they worked on - what did you think, and how could those interactions have been improved?</p>
+<p>Looking forward to hearing from you soon! <br>
+Ludwig<br></p></p>
View
27 spec/fixtures/quotes.txt
@@ -0,0 +1,27 @@
+Awesome!  Thanks Nicolas.<div><br></div><div>1. Are you currently interested to hire new employees?</div><div><br></div><div>YES!!!<br><br>2. Are you hiring fulltimers, parttimers, Summer interns or a combination?<br><br>Full time<br><br>3. Do you plan to pay them, or you can only offer equity?</div><div><br></div><div>Equity<br><br>4. Any specific positions you are looking to hire?</div><div><br></div><div>ALL<br><br><div class="gmail_quote">On Sat, Feb 2, 2013 at 7:04 PM, Nicolas Kokkalis <span dir="ltr">&lt;<a href="mailto:nicolas@startx.stanford.edu" target="_blank">nicolas@startx.stanford.edu</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Cameron,<br><br>
+
+My name is Nicolas and I am the CTO of StartX. I have been on the StartX team from the very beginning (Sep 2010), but I have been on a short hiatus finishing my PhD, and that's why you may not remember me.<br><br>
+
+I'm excited to tell you that we have a present for you! StartX recently received hundreds of job applications from the Stanford population, and we would like to use this opportunity to help all of you (alumni and current StartX companies) recruit new employees.<br><br>
+
+If you are interested please guide our recommendations by answering the four questions below asap.  We will be sending you candidates based on your responses in the next month or so.<br><div class="HOEnZb"><div class="h5">
+<br>
+
+1. Are you currently interested to hire new employees?<br><br>
+
+2. Are you hiring fulltimers, parttimers, Summer interns or a combination?<br><br>
+
+3. Do you plan to pay them, or you can only offer equity?<br><br>
+
+4. Any specific positions you are looking to hire?<br><br><br>
+
+Best,<br>
+
+Nicolas Kokkalis<br>
+
+CTO of StartX<br>
+</div></div>
+</blockquote>
+</div>
+<br>
+</div>
View
27 spec/fixtures/realworld.txt
@@ -0,0 +1,27 @@
+<body>
+
+<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
+Erika I'm unfortunately in India till the 22nd. I have been involved with the program before but if you think I still need some information, perhaps I can do a quick catchup when I'm back?
+<div><br></div>
+<div>Dinesh</div>
+<div><br></div>
+<div>
+<br><div>
+<div>On Mar 10, 2013, at 8:24 PM, StartX Board of Advisors &lt;<a href="mailto:startx_fba@sse.stanford.edu">startx_fba@sse.stanford.edu</a>&gt; wrote:</div>
+<br class="Apple-interchange-newline"><blockquote type="cite">
+<div><br class="webkit-block-placeholder"></div>
+<div><br class="webkit-block-placeholder"></div>
+<p>Dear Dinesh,</p>
+<p>Thank you for volunteering to serve as an advisor in the Founder Board of Advisors Program for this session. In preparation, <b>we ask that you attend an orientation session on Thursday, March 14th, from 7:30 - 8:30pm at the StartX offices</b> so we can update you on all of the program changes. This is also a great opportunity to meet all of the other StartX advisors.</p>
+<p>If you can no longer attend the orientation, please email me at <a href="mailto:startx_fba@startx.stanford.edu">startx_fba@startx.stanford.edu</a>. If you have already told me (in the form) that you cannot attend the orientation, then I will reach out to you by email to set up a time to chat.</p>
+<p>Thanks again for all of your time and support.</p>
+<p>Best,</p>
+<p>Erika</p>
+<div><br class="webkit-block-placeholder"></div>
+<div><br class="webkit-block-placeholder"></div>
+</blockquote>
+</div>
+<br>
+</div>
+
+</body>
View
10 spec/fixtures/result_paragraphs.txt
@@ -0,0 +1,10 @@
+Hi Ludwig,
+
+It’s been a few months since the end of the HCI Online class - we hope that the class was useful in giving you a sense of how interface design happens.
+
+For the class, we launched some experimental, social features, and thanks for signing up for them! As we’re considering including them in the next run on the HCI Online class, we wanted to find out what you thought.
+
+We showed you friends who were also taking the class and also assignments that they worked on - what did you think, and how could those interactions have been improved?
+
+Looking forward to hearing from you soon!
+Ludwig
View
37 spec/fixtures/result_quotes.txt
@@ -0,0 +1,37 @@
+Awesome!  Thanks Nicolas.
+
+1. Are you currently interested to hire new employees?
+YES!!!
+
+2. Are you hiring fulltimers, parttimers, Summer interns or a combination?
+
+Full time
+
+3. Do you plan to pay them, or you can only offer equity?
+Equity
+
+4. Any specific positions you are looking to hire?
+ALL
+
+On Sat, Feb 2, 2013 at 7:04 PM, Nicolas Kokkalis <nicolas@startx.stanford.edu> wrote:
+
+> Hi Cameron,
+>
+> My name is Nicolas and I am the CTO of StartX. I have been on the StartX team from the very beginning (Sep 2010), but I have been on a short hiatus finishing my PhD, and that's why you may not remember me.
+>
+> I'm excited to tell you that we have a present for you! StartX recently received hundreds of job applications from the Stanford population, and we would like to use this opportunity to help all of you (alumni and current StartX companies) recruit new employees.
+>
+> If you are interested please guide our recommendations by answering the four questions below asap.  We will be sending you candidates based on your responses in the next month or so.
+>
+> 1. Are you currently interested to hire new employees?
+>
+> 2. Are you hiring fulltimers, parttimers, Summer interns or a combination?
+>
+> 3. Do you plan to pay them, or you can only offer equity?
+>
+> 4. Any specific positions you are looking to hire?
+>
+>
+> Best,
+> Nicolas Kokkalis
+> CTO of StartX
View
21 spec/fixtures/result_realworld.txt
@@ -0,0 +1,21 @@
+Erika I'm unfortunately in India till the 22nd. I have been involved with the program before but if you think I still need some information, perhaps I can do a quick catchup when I'm back?
+Dinesh
+
+On Mar 10, 2013, at 8:24 PM, StartX Board of Advisors <startx_fba@sse.stanford.edu> wrote:
+
+>
+>
+> Dear Dinesh,
+>
+> Thank you for volunteering to serve as an advisor in the Founder Board of Advisors Program for this session. In preparation, **we ask that you attend an orientation session on Thursday, March 14th, from 7:30 - 8:30pm at the StartX offices** so we can update you on all of the program changes. This is also a great opportunity to meet all of the other StartX advisors.
+>
+> If you can no longer attend the orientation, please email me at startx_fba@startx.stanford.edu. If you have already told me (in the form) that you cannot attend the orientation, then I will reach out to you by email to set up a time to chat.
+>
+> Thanks again for all of your time and support.
+>
+> Best,
+>
+> Erika
+>
+>
+>
View
7 spec/maildown_spec.rb
@@ -0,0 +1,7 @@
+require 'spec_helper'
+
+describe 'Maildown' do
+ it 'converts via a class method' do
+ Maildown.from_html("<b>Test</b>").should == "**Test**"
+ end
+end
View
18 spec/parser_spec.rb
@@ -0,0 +1,18 @@
+require 'spec_helper'
+
+describe 'Maildown::Parser' do
+ it 'converts simple paragraphs' do
+ parser = Maildown::Parser.new(fixture('paragraphs.txt'))
+ parser.result.should eq(result_fixture('paragraphs.txt'))
+ end
+
+ it 'converts real world html' do
+ parser = Maildown::Parser.new(fixture('realworld.txt'))
+ parser.result.should eq(result_fixture('realworld.txt'))
+ end
+
+ it 'converts real world html' do
+ parser = Maildown::Parser.new(fixture('quotes.txt'))
+ parser.result.should eq(result_fixture('quotes.txt'))
+ end
+end
View
17 spec/spec_helper.rb
@@ -0,0 +1,17 @@
+$:.unshift File.expand_path("../..", __FILE__)
+
+require 'maildown'
+
+def fixture_path(file=nil)
+ path = File.expand_path("../fixtures", __FILE__)
+ path = File.join(path, file) unless file.nil?
+ path
+end
+
+def fixture(file)
+ File.read(File.join(fixture_path, file))
+end
+
+def result_fixture(file)
+ fixture("result_#{file}")
+end

0 comments on commit d8b9320

Please sign in to comment.
Something went wrong with that request. Please try again.