Skip to content
Browse files

Fix section accessor behavier / Ignore empty line between entries or …

…sections
  • Loading branch information...
1 parent 68582d2 commit 4729850c417ee92e97c83dcf41955f1920c257ab @labocho committed Feb 25, 2012
View
10 lib/movable_type_format/parser.rb
@@ -37,9 +37,11 @@ def parse_entry(lines)
while lines.next?
case lines.peek
- when "--------\n"
+ when Entry::DELIMITER
lines.next
break
+ when "\n" # ignore empty line between entries or sections
+ lines.next
else
sections << parse_section(lines)
end
@@ -76,17 +78,17 @@ def parse_section(lines)
section.body << line
context = :body
end
- when "-----\n"
+ when Section::Base::DELIMITER
# end of section
+ section.body.chomp! if section.body
return section
else
section.body ||= ""
section.body << line
context = :body
end
end
- section.body.chomp! if section.body
- section
+ raise "Section delimiter not found!"
end
end
end
View
19 lib/movable_type_format/section/base.rb
@@ -27,15 +27,10 @@ def self.define_field_accessors(keys)
keys.each do |key|
name = key.downcase.gsub(/ /, "_")
define_method name do
- fields.find{|field| field.key == key}
+ find_or_create_field(key).value
end
define_method "#{name}=" do |v|
- field = send(name)
- unless field
- field = Field.new(key)
- fields << field
- end
- field.value = v
+ find_or_create_field(key).value = v
end
end
end
@@ -73,6 +68,16 @@ def to_mt
buffer << "#{body}\n" if body
buffer << DELIMITER
end
+
+ private
+ def find_or_create_field(key)
+ field = fields.find{|field| field.key == key}
+ unless field
+ field = Field.new(key)
+ fields << field
+ end
+ field
+ end
end
end
end
View
39 spec/movable_type_format/parser_spec.rb
@@ -12,14 +12,53 @@ module MovableTypeFormat
end
subject { @entries }
it { should be_a Enumerator }
+ it "include two entries" do
+ subject.to_a.count.should == 2
+ end
+ it "include each entry" do
+ first, second = subject.to_a
+ first.title.should == "A dummy title"
+ second.title.should == "Here is a new entry"
+ end
context "first entry" do
before(:each) do
@entry = @entries.first
end
subject { @entry }
it { should be_an Entry}
end
+ it "ignore empty line between entries and sections" do
+ mt = <<-MT
+ TITLE: One
+ -----
+ BODY:
+ Body
+ -----
+
+ EXCERPT:
+ Excerpt
+ -----
+ --------
+ TITLE: Two
+ -----
+ BODY:
+ Body Two
+ -----
+
+ --------
+ MT
+ mt.gsub!(/^\s+/, "")
+
+ entries = MovableTypeFormat.parse(mt).to_a
+ entries.count.should == 2
+ one, two = entries
+ one.title.should == "One"
+ one.body.should == "Body"
+ one.excerpt.should == "Excerpt"
+ two.title.should == "Two"
+ two.body.should == "Body Two"
+ end
end
end
end

0 comments on commit 4729850

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