Giter Site home page Giter Site logo

waiting-for-dev / front_matter_parser Goto Github PK

View Code? Open in Web Editor NEW
100.0 3.0 12.0 120 KB

Ruby library to parse files or strings with a front matter. It has automatic syntax detection.

License: MIT License

Ruby 99.14% Shell 0.43% Dockerfile 0.43%
front-matter-parser frontmatter ruby-gem

front_matter_parser's Issues

Front matter data in documents without content parsed as content

For a document that contains only a front matter block but no content, FrontMatterParser returns the front matter data as content:

FrontMatterParser::Parser.new(:md).call("---\ntest: 1\n---")
 => #<FrontMatterParser::Parsed:0x0000000128c1d008 @front_matter={}, @content="---\ntest: 1\n---">

(Adding just a single newline after the block makes it parse correctly)

Parser can be too greedy sometimes

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?

License clarification

Is this gem under the MIT license?

I see that there are 3 different license files in the root folders and I don't know why.

Would it make sense to keep only the MIT license and remove the other files if they are not relevant?

Date parsing fails

I have frontmatter like this:

---
title: 'cool title'
date: 2012-01-11
categories: foo
---

This fails with:

FrontMatterParser::Parser.parse_file(str)
#=> Psych::DisallowedClass: Tried to load unspecified class: Date

I'm pretty sure the root cause is ruby/psych#262 and that the parser is attempting to run Date.new('2012-01-11'), but I haven't nailed it down yet.

I might be able to write a customer parser to overwrite how the frontmatter is being parsed, I guess?

Was wondering if there was a different solution or if anyone had any insight?

Jekyll: undefined method `to_liquid'

Jekyll 3.6.2
Ruby 2.4.1

I'm running into this error when using this with Jekyll. Can't seem to lock down the issue.

Liquid Exception: undefined method to_liquid' for #FrontMatterParser::Parsed:0x007fa26ea88f78...`

Here is the code I'm trying to execute as a Liquid filter in parse_fm.rb

require "front_matter_parser"

module Jekyll
  module ParseFM

    par = ""
    def frontm(string = '')
      par = FrontMatterParser::Parser.new(:md).call(string)
    end
    par
  end
end

Liquid::Template.register_filter(Jekyll::ParseFM)

I'm using it in a liquid template {{yaml | frontm}}

I am simply trying to pass a string like below:

--- blog: https://example.com/page1 background-img: https://example.com/files/image.jpg buttonText: Download download: true---

bin/* creates conflicts

The files in the bin folder should probably not be included in the final gem, as these seem to be intended for development only.

spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }

Since there are other offending gems that have binaries with the same name, when installing either of them, users get this error:

front_matter_parser's executable "console" conflicts with request-log-analyzer
Overwrite the executable? [yN]  n
ERROR:  Error installing eucalypt:
        "console" from front_matter_parser conflicts with installed executable from request-log-analyzer

Bring back .parse

I would love if I could call parse on a string, which doesn't seem to work with the new rewrite. I tried the following three, and all throw exceptions.

FrontMatterParser.parse(body)
prsr = FrontMatterParser::Parser.new(:slim)
fmp = prsr.parse(body)
FrontMatterParser::Parser.parse(body)

Failures when front matter contains dates

When I try to parse a doc that looks like this:

---
title: Another test
date: 2022-06-04
---

Test

It fails with:

Psych::DisallowedClass in PostsController#index
Tried to load unspecified class: Date

Full trace:

[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/class_loader.rb:97:in `find'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/class_loader.rb:28:in `load'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/class_loader.rb:39:in `block (2 levels) in <class:ClassLoader>'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/scalar_scanner.rb:60:in `tokenize'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:60:in `deserialize'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:123:in `visit_Psych_Nodes_Scalar'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/visitor.rb:16:in `visit'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/visitor.rb:6:in `accept'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:32:in `accept'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:340:in `block in revive_hash'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:338:in `each'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:338:in `each_slice'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:338:in `revive_hash'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:162:in `visit_Psych_Nodes_Mapping'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/visitor.rb:16:in `visit'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/visitor.rb:6:in `accept'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:32:in `accept'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:313:in `visit_Psych_Nodes_Document'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/visitor.rb:16:in `visit'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/visitor.rb:6:in `accept'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:32:in `accept'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych.rb:360:in `safe_load'](http://localhost:3000/posts/#)
[front_matter_parser (1.0.1) lib/front_matter_parser/loader/yaml.rb:22:in `call'](http://localhost:3000/posts/#)
[front_matter_parser (1.0.1) lib/front_matter_parser/parser.rb:67:in `call'](http://localhost:3000/posts/#)
[front_matter_parser (1.0.1) lib/front_matter_parser/parser.rb:25:in `block in parse_file'](http://localhost:3000/posts/#)
[front_matter_parser (1.0.1) lib/front_matter_parser/parser.rb:24:in `open'](http://localhost:3000/posts/#)
[front_matter_parser (1.0.1) lib/front_matter_parser/parser.rb:24:in `parse_file'](http://localhost:3000/posts/#)
[app/controllers/posts_controller.rb:8:in `block in index'](http://localhost:3000/posts/#)
[app/controllers/posts_controller.rb:7:in `map'](http://localhost:3000/posts/#)
[app/controllers/posts_controller.rb:7:in `index'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/abstract_controller/base.rb:220:in `process_action'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_controller/metal/rendering.rb:53:in `process_action'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/abstract_controller/callbacks.rb:249:in `block in process_action'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) activesupport/lib/active_support/callbacks.rb:118:in `block in run_callbacks'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actiontext/lib/action_text/rendering.rb:20:in `with_renderer'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actiontext/lib/action_text/engine.rb:71:in `block (4 levels) in <class:Engine>'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) activesupport/lib/active_support/callbacks.rb:127:in `instance_exec'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) activesupport/lib/active_support/callbacks.rb:127:in `block in run_callbacks'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) activesupport/lib/active_support/callbacks.rb:138:in `run_callbacks'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/abstract_controller/callbacks.rb:248:in `process_action'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_controller/metal/rescue.rb:22:in `process_action'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_controller/metal/instrumentation.rb:67:in `block in process_action'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) activesupport/lib/active_support/notifications.rb:206:in `block in instrument'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) activesupport/lib/active_support/notifications/instrumenter.rb:58:in `instrument'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) activesupport/lib/active_support/notifications.rb:206:in `instrument'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_controller/metal/instrumentation.rb:66:in `process_action'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_controller/metal/params_wrapper.rb:259:in `process_action'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) activerecord/lib/active_record/railties/controller_runtime.rb:27:in `process_action'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/abstract_controller/base.rb:156:in `process'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionview/lib/action_view/rendering.rb:39:in `process'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_controller/metal.rb:204:in `dispatch'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_controller/metal.rb:267:in `dispatch'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/routing/route_set.rb:49:in `dispatch'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/routing/route_set.rb:32:in `serve'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/journey/router.rb:50:in `block in serve'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/journey/router.rb:32:in `each'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/journey/router.rb:32:in `serve'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/routing/route_set.rb:852:in `call'](http://localhost:3000/posts/#)
[rack (2.2.4) lib/rack/tempfile_reaper.rb:15:in `call'](http://localhost:3000/posts/#)
[rack (2.2.4) lib/rack/etag.rb:27:in `call'](http://localhost:3000/posts/#)
[rack (2.2.4) lib/rack/conditional_get.rb:27:in `call'](http://localhost:3000/posts/#)
[rack (2.2.4) lib/rack/head.rb:12:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/http/permissions_policy.rb:38:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/http/content_security_policy.rb:36:in `call'](http://localhost:3000/posts/#)
[rack (2.2.4) lib/rack/session/abstract/id.rb:266:in `context'](http://localhost:3000/posts/#)
[rack (2.2.4) lib/rack/session/abstract/id.rb:260:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/middleware/cookies.rb:698:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) activerecord/lib/active_record/migration.rb:612:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) activesupport/lib/active_support/callbacks.rb:99:in `run_callbacks'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/middleware/callbacks.rb:26:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/middleware/executor.rb:14:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/middleware/actionable_exceptions.rb:17:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/middleware/debug_exceptions.rb:28:in `call'](http://localhost:3000/posts/#)
[web-console (4.2.0) lib/web_console/middleware.rb:132:in `call_app'](http://localhost:3000/posts/#)
[web-console (4.2.0) lib/web_console/middleware.rb:28:in `block in call'](http://localhost:3000/posts/#)
[web-console (4.2.0) lib/web_console/middleware.rb:17:in `catch'](http://localhost:3000/posts/#)
[web-console (4.2.0) lib/web_console/middleware.rb:17:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/middleware/show_exceptions.rb:26:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) railties/lib/rails/rack/logger.rb:37:in `call_app'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) railties/lib/rails/rack/logger.rb:24:in `block in call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) activesupport/lib/active_support/tagged_logging.rb:114:in `block in tagged'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) activesupport/lib/active_support/tagged_logging.rb:38:in `tagged'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) activesupport/lib/active_support/tagged_logging.rb:114:in `tagged'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) railties/lib/rails/rack/logger.rb:24:in `call'](http://localhost:3000/posts/#)
[sprockets-rails (3.4.2) lib/sprockets/rails/quiet_assets.rb:13:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/middleware/remote_ip.rb:92:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/middleware/request_id.rb:26:in `call'](http://localhost:3000/posts/#)
[rack (2.2.4) lib/rack/method_override.rb:24:in `call'](http://localhost:3000/posts/#)
[rack (2.2.4) lib/rack/runtime.rb:22:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) activesupport/lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/middleware/server_timing.rb:20:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/middleware/executor.rb:14:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/middleware/static.rb:23:in `call'](http://localhost:3000/posts/#)
[rack (2.2.4) lib/rack/sendfile.rb:110:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/middleware/host_authorization.rb:138:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) railties/lib/rails/engine.rb:529:in `call'](http://localhost:3000/posts/#)
[puma (5.6.4) lib/puma/configuration.rb:252:in `call'](http://localhost:3000/posts/#)
[puma (5.6.4) lib/puma/request.rb:77:in `block in handle_request'](http://localhost:3000/posts/#)
[puma (5.6.4) lib/puma/thread_pool.rb:340:in `with_force_shutdown'](http://localhost:3000/posts/#)
[puma (5.6.4) lib/puma/request.rb:76:in `handle_request'](http://localhost:3000/posts/#)
[puma (5.6.4) lib/puma/server.rb:441:in `process_client'](http://localhost:3000/posts/#)
[puma (5.6.4) lib/puma/thread_pool.rb:147:in `block in spawn_thread'](http://localhost:3000/posts/#)

This is my PostsController:

class PostsController < ApplicationController

  def index
    files = Dir.glob(File.join(Rails.root, "posts", "*.md"))
    @posts = files.map do |f|
      parsed = FrontMatterParser::Parser.parse_file(f)
      Post.new(f, parsed.front_matter)
    end
  end

My goal is to iterate over my blog posts, authored in markdown with front matter that contains title and date and render a simple list. My bet is we need to pass some permitted classes to the yaml loader?

This conversation seems relevant: ruby/psych#262

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.