Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
def parse_zsff(url) uri = URI.parse(url) errors = [] warnings = [] attrs = {} begin results = Net::HTTP.get(uri) rescue errors.push("Invalid URL") end begin raw = results.split(/\n\n/) errors.push("Must have two line breaks between Attributes section and Links section") if raw.size < 2 raw_attrs = raw.first.split(/\n/)[1..-1] links = raw.last.split(/\n/) attrs[:version] = raw.first.split(/\n/).first raw_attrs.each do |att| parts = att.split(":") errors.push("Invalid attribute declaration: #{att}") and next unless parts.size > 0 attrs[parts.first.downcase.to_sym] = parts[1..-1].join(":").strip end errors.push("Invalid ZSFF Version declaration") if attrs[:version].nil? || attrs[:version].match(/ZSFF 1\.[0-9]/).nil? errors.push("Invalid Author declaration") if attrs[:author].nil? || attrs[:author].blank? errors.push("Invalid Title declaration") if attrs[:title].nil? || attrs[:title].blank? errors.push("Invalid Site declaration") if attrs[:site].nil? || attrs[:site].blank? warnings.push("Site declaration does not appear to be a valid URL") if attrs[:site].nil? || !is_valid_url?(attrs[:site]) errors.push("Invalid Subtitle declaration") if attrs[:subtitle].nil? || attrs[:subtitle].blank? errors.push("Invalid Copyright declaration") if attrs[:copyright].nil? || attrs[:copyright].blank? errors.push("Invalid Content-type declaration") if attrs[:"content-type"].nil? || attrs[:"content-type"].match(/.*\/.*/).nil? links.each do |link| warnings.push("Link '#{link}' does not appear to be a valid URL") unless is_valid_url?(link) end rescue Exception => e errors.push("Cannot parse ZSFF file") end {:success => errors.size == 0, :errors => errors, :warnings => warnings, :attrs => attrs} end def is_valid_url?(url) !url.match(/(^$)|(^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$)/ix).nil? end
This paste will be private.
From the Design Piracy series on my blog: