waiting-for-dev / front_matter_parser

Ruby library to parse files or strings with a front matter. It has automatic syntax detection.
MIT License
105 stars 12 forks source link

Parser can be too greedy sometimes #4

Closed adamhollett closed 7 years ago

adamhollett commented 7 years ago

Hi @waiting-for-dev!

I'm building a Rails app that parses and displays Jekyll-style pages: Markdown files with YAML front matter. I'm using this gem to parse the front matter from the pages into @page.data and I assign the rest of the content to @page.content.

A problem comes up when any of my pages have a sequence of three hyphens in them ---. FrontMatterParser will always read until the last sequence of --- in the file instead of stopping at the --- after the front matter like I expect it to.

For example, if I have this file:

---
title: About email notifications
---

This is the page about email notifications

The parser works as expected; I get @page.data = { title: 'About email notifications' } and @page.content = 'This is the page about email notifications'.

But if the file has a sequence of three hyphens:

---
title: About email notifications
---

In some cases. you might receive a plain-text email like this:

------------- Warning -------------
Your credit card is about to expire!

The parser gives me @page.data = { title: 'About email notifications' } but @page.content = 'Your credit card is about to expire!'

Is there a way to make the parser less greedy, and stop it from gobbling all my page content if the content contains sequences of hyphens?

waiting-for-dev commented 7 years ago

Hi @admhlt . Well, right now you could write your custom parser as expained in the Readme. But it is clearly a bug. I'll take a look as soon as possible.

Thanks

adamhollett commented 7 years ago

Thanks! I'll find a solution for myself in the meantime. The app's not in production so there's no rush.

waiting-for-dev commented 7 years ago

Hey @admhlt . This commit in master should fix it. Please, confirm that it works in your case and then I'll release a new version with the fix to rubygems :)

adamhollett commented 7 years ago

Amazing! Yeah, this has fixed the issue for me. Thanks a lot!

waiting-for-dev commented 7 years ago

Great! Released as 0.1.1 :+1: