move from md to haml partials

means the files need an _ in the beginning
we can just render
post must start with text as the title is generated
h2 is the first heading
This commit is contained in:
Torsten Ruger 2017-06-12 21:57:05 +03:00
parent 1e20958d8c
commit 2734d1caf7
8 changed files with 37 additions and 33 deletions

View File

@ -5,8 +5,9 @@ class Page
def initialize(path)
@dir = File.dirname(path)
base , @ext = File.basename(path).split(".")
raise "must be partial, statr with _ not:#{base}" unless base[0] == "_"
@words = base.split("-")
@year = parse_int(@words.shift , 2100)
@year = parse_int(@words.shift[1 .. -1] , 2100)
@month = parse_int(@words.shift , 12)
@day = parse_int(@words.shift , 32)
raise "Invalid path #{path}" unless @words
@ -18,20 +19,27 @@ class Page
def title
@words.join(" ")
end
def template_name
"#{date}-#{slug}"
end
def date
"#{year}-#{month}-#{day}"
end
def parse_int( value , max)
ret = value.to_i
raise "invalid value #{value} > #{max}" if ret > max or ret < 1
ret
end
def content
File.open("#{@dir}/#{year}-#{month}-#{day}-#{slug}.#{ext}" ).read
File.open("#{@dir}/_#{template_name}.#{ext}" ).read
end
def summary
content.split("%h2").first.gsub("%p", "")
end
def self.pages
return @@pages if @@pages
@@pages ={}
Dir["#{self.blog_path}/2*.haml"].each do |file|
Dir["#{self.blog_path}/_2*.haml"].each do |file|
page = Page.new(file)
@@pages[page.slug] = page
end

View File

@ -1,5 +1,5 @@
%p
some text that will be the short summary at the beggining.
some text that will be the short summary at the beginning.
Remembering that the file name becomes the title (h1)
%h2 Sub Headings should start with 2

View File

@ -1,5 +1,5 @@
%h1 Index
- @pages.each do |title , page|
%h3= link_to page.title , blog_page_path(page.title)
%p= page.content
%h3= link_to page.title , blog_page_path(page.slug)
%p= page.summary

View File

@ -1,3 +1,3 @@
%h1= @page.title
= page_content( @page)
= render( @page.template_name )

View File

@ -1,8 +1,6 @@
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
config.blog_path = Rails.root.to_s + "/spec/blog"
# The test environment is used exclusively to run your application's
# test suite. You never need to work with it otherwise. Remember that
# your test database is "scratch space" for the test suite and is wiped

View File

@ -1,8 +0,0 @@
some text that will be the short summary at the beggining.
Remembering that the file name becomes the title (h1)
## Sub Headings should start with 2
They have more text possibly with links <http://github.com/
Paragraphs are created when there is a blank line

View File

@ -2,12 +2,12 @@
feature 'Page show' do
scenario 'page shows for correct title' do
visit_url blog_page_url("title")
expect(page).to have_content("Blog")
visit_url blog_page_url("Basic-instructions-for-writing-blogs")
expect(page).to have_content("instructions")
end
scenario 'page title shows for correct title' do
visit_url blog_page_url("title")
expect(page).to have_content("Bitcoin")
visit_url blog_page_url("Basic-instructions-for-writing-blogs")
expect(page).to have_content("Headings")
end
end

View File

@ -4,17 +4,17 @@ RSpec.describe Page, type: :model do
describe "creation" do
it "ok with valid slug" do
page = Page.new("1993-2-4-title")
page = Page.new("_1993-2-4-title")
expect(page).not_to eq nil
end
it "raises with invalid slug" do
expect{Page.new("1993-4-title")}.to raise_error RuntimeError
expect{Page.new("_1993-4-title")}.to raise_error RuntimeError
end
end
describe "basic api" do
before :each do
@page = Page.new("1993-2-4-title")
@page = Page.new("_1993-2-4-title")
end
it "returns title" do
expect(@page.title).to eq "title"
@ -24,26 +24,32 @@ RSpec.describe Page, type: :model do
expect(@page.month).to eq 2
expect(@page.day).to eq 4
end
it "returns date" do
expect(@page.date).to eq "1993-2-4"
end
it "returns file_name" do
expect(@page.template_name).to eq "1993-2-4-title"
end
end
it "must start with a year" do
expect{Page.new("no-num-4-title")}.to raise_error RuntimeError
expect{Page.new("_no-num-4-title")}.to raise_error RuntimeError
end
it "must start with a underscore" do
expect{Page.new("_no-num-4-title")}.to raise_error RuntimeError
end
it "returns whole title" do
page = Page.new("1993-2-4-Multi-word-title")
page = Page.new("_1993-2-4-Multi-word-title")
expect(page.title).to eq "Multi word title"
end
it "returns slug" do
page = Page.new("1993-2-4-Multi-word-title")
page = Page.new("_1993-2-4-Multi-word-title")
expect(page.slug).to eq "Multi-word-title"
end
it "returns title without extension if given file name" do
page = Page.new("1993-2-4-title.rb")
page = Page.new("_1993-2-4-title.rb")
expect(page.title).to eq "title"
end
it 'returns blog path' do
expect(Page.blog_path.ends_with?("spec/blog")).to be true
end
it "return a list of pages" do
expect(Page.pages.values.first.content.length).to be > 10
end