M .gitignore => .gitignore +2 -1
@@ 1,2 1,3 @@
bin
-lib>
\ No newline at end of file
+lib
+.DS_store<
\ No newline at end of file
A => +73 -0
@@ 0,0 1,73 @@
require "spec"
require "../src/extract_jekyll_front_matter.cr"
describe "Extracting Jekyll front matter" do
it "Extracts the simple front matter" do
input = <<-HEREDOC
---
title: hello
---
some
other text
HEREDOC
output = extract_jekyll_front_matter(input)
output[:front_matter].should eq({"title" => "hello"})
output[:doc].should eq(<<-EXPECTED_DOC
some
other text
EXPECTED_DOC
)
end
it "Extracts the more advanced front matter" do
input = <<-HEREDOC
---
title: hello
there: world
this:
- is
- a list
---
some
other text
HEREDOC
output = extract_jekyll_front_matter(input)
output[:front_matter].should eq({"title" => "hello",
"there" => "world",
"this" => ["is", "a list"]})
output[:doc].should eq(<<-EXPECTED_DOC
some
other text
EXPECTED_DOC
)
end
it "Extracts the doc alone if there is no front matter" do
input = <<-HEREDOC
some
other text
HEREDOC
output = extract_jekyll_front_matter(input)
output[:front_matter].as_nil.nil?.should be_true
output[:doc].should eq(<<-EXPECTED_DOC
some
other text
EXPECTED_DOC
)
end
it "Detects when front matter is not finished properly" do
input = <<-HEREDOC
---
some
other text
HEREDOC
expect_raises(Exception, "Front matter not closed") do
output = extract_jekyll_front_matter(input)
end
end
end
A => +31 -0
@@ 0,0 1,31 @@
require "yaml"
def extract_jekyll_front_matter(input : String)
step = :first_line
front_matter = String::Builder.new
markdown = String::Builder.new
lines = input.strip.each_line do |line|
case step
when :first_line
if (line == "---")
step = :front_matter
else
step = :markdown
markdown << line << "\n"
end
when :front_matter
if line == "---"
step = :markdown
else
front_matter << line << "\n"
end
when :markdown
markdown << line << "\n"
end
end
raise "Front matter not closed" if step == :front_matter
return {front_matter: YAML.parse(front_matter.to_s),
doc: markdown.to_s.strip}
end