dtao / safe_yaml

Parse YAML safely
MIT License
217 stars 63 forks source link

Support load file for files with `---` starting content #48

Closed gjtorikian closed 11 years ago

gjtorikian commented 11 years ago

If I have a .txt file like so:


---
title: Blah
key: value

---

Hey, here are some words!

Calling data = YAML.load_file(file) will correctly get me the YAML-as-a-hash in data. However, after requiring this gem, the following error appears:

safe_yaml-0.9.5/lib/safe_yaml/psych_handler.rb:48:in `add_to_current_structure': Don't know how to add to a NilClass! (RuntimeError)

The workaround for now is to do something like

if File.read(file) =~ /\A(---\s*\n(.*?\n?)^---\s*$\n?)(.*)/m
    data = YAML.safe_load($2)
end
dtao commented 11 years ago

OK, the issue is with PsychHandler, then. (Incidentally, with 0.9.6 I get false, which is also obviously wrong.) I will investigate.

dtao commented 11 years ago

(And if you were wondering, yes I am smoking crack. There was no version 0.9.6 as of the time I wrote that last comment, which was, incidentally, false anyway.)

gjtorikian commented 11 years ago

Heh. Thanks for getting to it so quickly!

gjtorikian commented 11 years ago

Hmm, I wonder if the test was too naive? New error:

project/vendor/gems/1.9.3/ruby/1.9.1/gems/safe_yaml-0.9.6/lib/safe_yaml.rb:171:in `parse': (/Users/garentorikian/project/vendor/licenses/agpl.txt): mapping values are not allowed in this context at line 57 column 15 (Psych::SyntaxError)
    from /Users/garentorikian/project/vendor/gems/1.9.3/ruby/1.9.1/gems/safe_yaml-0.9.6/lib/safe_yaml.rb:171:in `safe_load'
    from /Users/garentorikian/project/vendor/gems/1.9.3/ruby/1.9.1/gems/safe_yaml-0.9.6/lib/safe_yaml.rb:184:in `block in safe_load_file'
    from /Users/garentorikian/project/vendor/gems/1.9.3/ruby/1.9.1/gems/safe_yaml-0.9.6/lib/safe_yaml.rb:184:in `open'
    from /Users/garentorikian/project/vendor/gems/1.9.3/ruby/1.9.1/gems/safe_yaml-0.9.6/lib/safe_yaml.rb:184:in `safe_load_file'
    from /Users/garentorikian/project/vendor/gems/1.9.3/ruby/1.9.1/gems/safe_yaml-0.9.6/lib/safe_yaml.rb:151:in `load_file_with_options'
dtao commented 11 years ago

Probably. Am I correct in guessing that this is a file containing some YAML frontmatter, which is otherwise just a lot of non-YAML content?

gjtorikian commented 11 years ago

You are correct.

On Mon, Sep 16, 2013 at 4:10 PM, Dan Tao notifications@github.com wrote:

Probably. Am I correct in guessing that this is a file containing some YAML frontmatter, which is otherwise just a lot of non-YAML content?

— Reply to this email directly or view it on GitHubhttps://github.com/dtao/safe_yaml/issues/48#issuecomment-24552427 .

dtao commented 11 years ago

OK. I'm working out a better solution now.

dtao commented 11 years ago

Just committed a change that should deal with documents like this in a more predictably correct way (basically mimics the way Psych does it internally). Could you try it out (maybe temporarily modify your Gemfile to point to HEAD) and let me know if it seems to work for you? I clearly pushed the last gem version prematurely and don't want to go too crazy with more of that.

gjtorikian commented 11 years ago

:heart: :heart: :heart:

Latest HEAD works locally for me on the same project.