mirror of
https://github.com/samsonjs/samhuri.net.git
synced 2026-03-25 09:05:47 +00:00
delegate preview to a real harp server
This commit is contained in:
parent
8d42efc0e1
commit
5830cb08c1
3 changed files with 7 additions and 39 deletions
|
|
@ -84,14 +84,6 @@ class HarpBlog
|
|||
read_post('drafts', id, draft: true)
|
||||
end
|
||||
|
||||
def preview_post(year, month, id)
|
||||
read_rendered_post(File.join(year, month), id)
|
||||
end
|
||||
|
||||
def preview_draft(id)
|
||||
read_rendered_post('drafts', id)
|
||||
end
|
||||
|
||||
def create_post(title, body, url, extra_fields = nil)
|
||||
if !title || title.strip.length == 0
|
||||
title = find_title(url)
|
||||
|
|
@ -191,10 +183,6 @@ class HarpBlog
|
|||
path_for(*args)
|
||||
end
|
||||
|
||||
def rendered_post_path(dir, id)
|
||||
path_for('www/posts', dir, id, 'index.html')
|
||||
end
|
||||
|
||||
def read_posts(post_dir, extra_fields = nil)
|
||||
extra_fields ||= {}
|
||||
post_data = read_post_data(post_path(post_dir))
|
||||
|
|
@ -235,17 +223,6 @@ class HarpBlog
|
|||
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)
|
||||
git_fetch
|
||||
git_reset_hard('origin/master')
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ CONFIG_DEFAULTS = {
|
|||
host: '127.0.0.1',
|
||||
hostname: `hostname --fqdn`.strip,
|
||||
port: 6706,
|
||||
preview_port: 5000,
|
||||
}
|
||||
|
||||
def env_value(name)
|
||||
|
|
@ -37,6 +38,8 @@ $config.each_key do |name|
|
|||
end
|
||||
end
|
||||
|
||||
$config[:preview_url] = "http://#{$config[:hostname]}:#{$config[:preview_port]}"
|
||||
|
||||
unless File.directory?($config[:path])
|
||||
raise RuntimeError.new("file not found: #{$config[:path]}")
|
||||
end
|
||||
|
|
@ -172,9 +175,8 @@ get '/posts/:year/:month/:id' do |year, month, id|
|
|||
headers 'Content-Type' => 'application/json'
|
||||
JSON.generate(post: post.fields)
|
||||
elsif request.accept?('text/html')
|
||||
status 200
|
||||
headers 'Content-Type' => 'text/html'
|
||||
blog.preview_post(year, month, id)
|
||||
status 302
|
||||
headers "Location: #{$config[:preview_url]}/posts/#{year}/#{month}/#{id}"
|
||||
else
|
||||
status 400
|
||||
"content not available in an acceptable format: #{request.accept.join(', ')}"
|
||||
|
|
@ -200,9 +202,8 @@ get '/drafts/:id' do |id|
|
|||
headers 'Content-Type' => 'application/json'
|
||||
JSON.generate(post: post.fields)
|
||||
elsif request.accept?('text/html')
|
||||
status 200
|
||||
headers 'Content-Type' => 'text/html'
|
||||
blog.preview_draft(id)
|
||||
status 302
|
||||
headers "Location: #{$config[:preview_url]}/posts/drafts/#{id}"
|
||||
else
|
||||
status 400
|
||||
"content not available in an acceptable format: #{request.accept.join(', ')}"
|
||||
|
|
|
|||
|
|
@ -167,16 +167,6 @@ RSpec.describe HarpBlog do
|
|||
end
|
||||
end
|
||||
|
||||
describe "#preview_post" do
|
||||
it "should return some HTML" do
|
||||
@blog.compile
|
||||
first_post_path = File.join(TEST_BLOG_PATH, 'www/posts/2006/02/first-post/index.html')
|
||||
expected_html = File.read(first_post_path)
|
||||
html = @blog.preview_post('2006', '02', 'first-post')
|
||||
expect(html).to eq(expected_html)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#create_post' do
|
||||
it "should create a link post when a link is given" do
|
||||
title = 'test post'
|
||||
|
|
|
|||
Loading…
Reference in a new issue