Class: Pressy::PostParser
- Inherits:
-
Object
- Object
- Pressy::PostParser
- Defined in:
- lib/pressy/post_parser.rb
Overview
PostParser transforms the contents of a rendered post into a Pressy::Post.
Instance Attribute Summary collapse
-
#content ⇒ IO
readonly
The rendered content of the post to parse.
-
#format ⇒ String
readonly
The format of the post, derived from the directory the post is stored in.
Class Method Summary collapse
-
.parse(params) ⇒ Pressy::Post
Creates a new parser with the given parameters, and returns the parsed post.
Instance Method Summary collapse
-
#initialize(params) ⇒ PostParser
constructor
Creates a new parser for a rendered post.
-
#parse ⇒ Pressy::Post
Parses the rendered post into a Pressy::Post.
Constructor Details
#initialize(params) ⇒ PostParser
Creates a new parser for a rendered post.
14 15 16 17 18 19 |
# File 'lib/pressy/post_parser.rb', line 14 def initialize(params) @format = params.fetch(:format) @content = params.fetch(:content) @content = StringIO.new(@content) if @content.is_a? String end |
Instance Attribute Details
#content ⇒ IO (readonly)
Returns the rendered content of the post to parse
9 10 11 |
# File 'lib/pressy/post_parser.rb', line 9 def content @content end |
#format ⇒ String (readonly)
Returns the format of the post, derived from the directory the post is stored in
7 8 9 |
# File 'lib/pressy/post_parser.rb', line 7 def format @format end |
Class Method Details
.parse(params) ⇒ Pressy::Post
Note:
Equivalent to Pressy::PostParser.new(params).parse
Creates a new parser with the given parameters, and returns the parsed post.
42 43 44 |
# File 'lib/pressy/post_parser.rb', line 42 def self.parse(params) self.new(params).parse end |
Instance Method Details
#parse ⇒ Pressy::Post
Parses the rendered post into a Pressy::Post.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/pressy/post_parser.rb', line 23 def parse return Pressy::Post.new("post_content" => "", "post_format" => format) if lines.empty? if lines.first.strip == "---" the_lines = lines.drop(1) frontmatter_end_idx = the_lines.index { |line| line.strip == "---" } frontmatter_lines = the_lines[0...frontmatter_end_idx] params = parse_frontmatter(frontmatter_lines) content = the_lines[(frontmatter_end_idx + 1)..-1] Pressy::Post.new(params.merge("post_content" => content.join(""), "post_format" => format)) else Pressy::Post.new("post_content" => lines.join(""), "post_format" => format) end end |