implement previewing posts & drafts

This commit is contained in:
Sami Samhuri 2015-04-19 10:41:47 -07:00
parent 1dbd243008
commit e44afbe80d
2 changed files with 21 additions and 10 deletions

View file

@ -80,16 +80,16 @@ class HarpBlog
read_post(File.join(year, month), id) read_post(File.join(year, month), id)
end end
def get_post_html(year, month, id)
raise 'unimplemented'
end
def get_draft(id) def get_draft(id)
read_post('drafts', id, draft: true) read_post('drafts', id, draft: true)
end end
def get_draft_html(id) def preview_post(year, month, id)
raise 'unimplemented' read_rendered_post(File.join(year, month), id)
end
def preview_draft(id)
read_rendered_post('drafts', id)
end end
def create_post(title, body, url, extra_fields = nil) def create_post(title, body, url, extra_fields = nil)
@ -191,8 +191,8 @@ class HarpBlog
path_for(*args) path_for(*args)
end end
def drafts_path(*components) def rendered_post_path(dir, id)
post_path('drafts', *components) path_for('www/posts', dir, id, 'index.html')
end end
def read_posts(post_dir, extra_fields = nil) def read_posts(post_dir, extra_fields = nil)
@ -235,6 +235,17 @@ class HarpBlog
end end
end end
def read_rendered_post(post_dir, id)
post_filename = rendered_post_path(post_dir, id)
if File.exists?(post_filename)
File.read(post_filename)
else
message = "missing rendered HTML for post #{post_dir}/#{id} at path #{post_filename}"
$stderr.puts "[HarpBlog#read_rendered_post] #{message}"
raise InvalidDataError.new(message)
end
end
def save_post(action, post) def save_post(action, post)
git_fetch git_fetch
git_reset_hard('origin/master') git_reset_hard('origin/master')

View file

@ -174,7 +174,7 @@ get '/posts/:year/:month/:id' do |year, month, id|
elsif request.accept?('text/html') elsif request.accept?('text/html')
status 200 status 200
headers 'Content-Type' => 'text/html' headers 'Content-Type' => 'text/html'
blog.get_post_html(year, month, id) blog.preview_post(year, month, id)
else else
status 400 status 400
"content not available in an acceptable format: #{request.accept.join(', ')}" "content not available in an acceptable format: #{request.accept.join(', ')}"
@ -202,7 +202,7 @@ get '/drafts/:id' do |id|
elsif request.accept?('text/html') elsif request.accept?('text/html')
status 200 status 200
headers 'Content-Type' => 'text/html' headers 'Content-Type' => 'text/html'
blog.get_draft_html(id) blog.preview_draft(id)
else else
status 400 status 400
"content not available in an acceptable format: #{request.accept.join(', ')}" "content not available in an acceptable format: #{request.accept.join(', ')}"