Skip to content

Commit

Permalink
FileModel#parse_metadata raises MetadataParseError.
Browse files Browse the repository at this point in the history
This is cleaner API, and allows us to drop the FileModel#metadata?
private method, simplifying the impementation of metadata parsing
plugins.
  • Loading branch information
gma committed Mar 3, 2012
1 parent 18879c5 commit 4bd4a2b
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/nesta/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
Tilt.register Tilt::RedcarpetTemplate, 'mdown'

module Nesta
class MetadataParseError < RuntimeError; end

class FileModel
FORMATS = [:mdown, :haml, :textile]
@@cache = {}
Expand Down Expand Up @@ -124,6 +126,8 @@ def flagged_as?(flag)
end

def parse_metadata(first_paragraph)
is_metadata = first_paragraph.split("\n").first =~ /^[\w ]+:/
raise MetadataParseError unless is_metadata
metadata = CaseInsensitiveHash.new
first_paragraph.split("\n").each do |line|
key, value = line.split(/\s*:\s*/, 2)
Expand All @@ -138,19 +142,15 @@ def markup
@markup
end

def metadata?(text)
text.split("\n").first =~ /^[\w ]+:/
end

def parse_file
contents = File.open(@filename).read
rescue Errno::ENOENT
raise Sinatra::NotFound
else
first_paragraph, remaining = contents.split(/\r?\n\r?\n/, 2)
if metadata?(first_paragraph)
begin
return parse_metadata(first_paragraph), remaining
else
rescue MetadataParseError
return {}, contents
end
end
Expand Down

0 comments on commit 4bd4a2b

Please sign in to comment.