mirror of
https://github.com/samsonjs/samhuri.net.git
synced 2026-04-27 14:57:40 +00:00
commit root _data.json when publishing/unpublishing & create year indexes
That file contains the posts on the homepage, which obviously change on publish/unpublish. Should have been more obvious to me earlier when I wrote the code, but hey.
This commit is contained in:
parent
d870dc9e7d
commit
8b356c50d4
1 changed files with 47 additions and 17 deletions
|
|
@ -86,11 +86,13 @@ class HarpBlog
|
||||||
read_post('drafts', id, draft: true)
|
read_post('drafts', id, draft: true)
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_post(title, body, url, extra_fields = nil)
|
def create_post(title, body, url, extra_fields = nil, options = nil)
|
||||||
if !title || title.strip.length == 0
|
if !title || title.strip.length == 0
|
||||||
title = url && find_title(url) or 'Untitled'
|
title = url && find_title(url) or 'Untitled'
|
||||||
end
|
end
|
||||||
extra_fields ||= {}
|
extra_fields ||= {}
|
||||||
|
options ||= {}
|
||||||
|
options[:commit] = true unless options.has_key?(:commit)
|
||||||
fields = extra_fields.merge({
|
fields = extra_fields.merge({
|
||||||
title: title,
|
title: title,
|
||||||
link: url,
|
link: url,
|
||||||
|
|
@ -109,7 +111,11 @@ class HarpBlog
|
||||||
if existing_post
|
if existing_post
|
||||||
raise PostExistsError.new("post exists: #{post.dir}/#{post.id}")
|
raise PostExistsError.new("post exists: #{post.dir}/#{post.id}")
|
||||||
else
|
else
|
||||||
save_post('create post', post)
|
save_post(post)
|
||||||
|
if options[:commit]
|
||||||
|
git_commit("create post '#{post.title}'", [post_path(post.dir)])
|
||||||
|
end
|
||||||
|
post
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -118,21 +124,31 @@ class HarpBlog
|
||||||
post.body = body
|
post.body = body
|
||||||
post.link = link
|
post.link = link
|
||||||
post.timestamp = timestamp if timestamp
|
post.timestamp = timestamp if timestamp
|
||||||
save_post('update post', post)
|
save_post(post)
|
||||||
|
git_commit("update post '#{post.title}'", [post_path(post.dir)])
|
||||||
|
post
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete_post(year, month, id)
|
def delete_post(year, month, id)
|
||||||
delete_post_from_dir(File.join(year, month), id)
|
dir = File.join(year, month)
|
||||||
|
delete_post_from_dir(dir, id)
|
||||||
|
git_commit("delete post #{year}/#{month}/#{id}", [post_path(dir)])
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete_draft(id)
|
def delete_draft(id, options = nil)
|
||||||
|
options ||= {}
|
||||||
|
options[:commit] = true unless options.has_key?(:commit)
|
||||||
delete_post_from_dir('drafts', id)
|
delete_post_from_dir('drafts', id)
|
||||||
|
if options[:commit]
|
||||||
|
git_commit("delete draft #{id}", [post_path('drafts')])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def publish_post(post)
|
def publish_post(post)
|
||||||
if post.draft?
|
if post.draft?
|
||||||
new_post = create_post(post.title, post.body, post.link)
|
new_post = create_post(post.title, post.body, post.link, {}, {commit: false})
|
||||||
delete_post_from_dir('drafts', post.id)
|
delete_post_from_dir('drafts', post.id)
|
||||||
|
git_commit("publish '#{quote(post.title)}'", [post_path('drafts'), post_path(new_post.dir), root_data_path])
|
||||||
new_post
|
new_post
|
||||||
else
|
else
|
||||||
raise PostAlreadyPublishedError.new("post is already published: #{post.dir}/#{post.id}")
|
raise PostAlreadyPublishedError.new("post is already published: #{post.dir}/#{post.id}")
|
||||||
|
|
@ -143,15 +159,19 @@ class HarpBlog
|
||||||
if post.draft?
|
if post.draft?
|
||||||
raise PostNotPublishedError.new("post is not published: #{post.dir}/#{post.id}")
|
raise PostNotPublishedError.new("post is not published: #{post.dir}/#{post.id}")
|
||||||
else
|
else
|
||||||
new_post = create_post(post.title, post.body, post.link, draft: true)
|
new_post = create_post(post.title, post.body, post.link, {draft: true}, {commit: false})
|
||||||
delete_post_from_dir(post.dir, post.id)
|
delete_post_from_dir(post.dir, post.id)
|
||||||
|
git_commit("unpublish '#{quote(post.title)}'", [post_path(post.dir), post_path('drafts'), root_data_path])
|
||||||
new_post
|
new_post
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def publish(env)
|
def publish(env)
|
||||||
target = env.to_s == 'production' ? 'publish' : 'publish_beta'
|
is_prod = env.to_s == 'production'
|
||||||
run("make #{target}")
|
target = is_prod ? 'publish' : 'publish_beta'
|
||||||
|
success, output = run("make #{target}")
|
||||||
|
commit_root_data if is_prod
|
||||||
|
[success, output]
|
||||||
end
|
end
|
||||||
|
|
||||||
def sync
|
def sync
|
||||||
|
|
@ -186,6 +206,10 @@ class HarpBlog
|
||||||
File.join(@path, *components)
|
File.join(@path, *components)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def root_data_path
|
||||||
|
path_for('public/_data.json')
|
||||||
|
end
|
||||||
|
|
||||||
def post_path(dir, id = nil)
|
def post_path(dir, id = nil)
|
||||||
args = ['public/posts', dir]
|
args = ['public/posts', dir]
|
||||||
args << "#{id}.md" if id
|
args << "#{id}.md" if id
|
||||||
|
|
@ -234,11 +258,10 @@ class HarpBlog
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def save_post(action, post)
|
def save_post(post)
|
||||||
update_if_needed
|
update_if_needed
|
||||||
begin
|
begin
|
||||||
write_post(post)
|
write_post(post)
|
||||||
git_commit(action, post.title, post_path(post.dir))
|
|
||||||
post
|
post
|
||||||
|
|
||||||
rescue => e
|
rescue => e
|
||||||
|
|
@ -254,7 +277,6 @@ class HarpBlog
|
||||||
post_dir = post_path(post_dir)
|
post_dir = post_path(post_dir)
|
||||||
delete_post_body(post_dir, id)
|
delete_post_body(post_dir, id)
|
||||||
delete_post_index(post_dir, id)
|
delete_post_index(post_dir, id)
|
||||||
git_commit('delete', id, post_dir)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def write_post(post)
|
def write_post(post)
|
||||||
|
|
@ -311,6 +333,10 @@ class HarpBlog
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def commit_root_data
|
||||||
|
git_commit('commit root _data.json', [root_data_path])
|
||||||
|
end
|
||||||
|
|
||||||
def read_post_data(dir)
|
def read_post_data(dir)
|
||||||
post_data_filename = File.join(dir, '_data.json')
|
post_data_filename = File.join(dir, '_data.json')
|
||||||
if File.exist?(post_data_filename)
|
if File.exist?(post_data_filename)
|
||||||
|
|
@ -387,12 +413,16 @@ class HarpBlog
|
||||||
output.strip
|
output.strip
|
||||||
end
|
end
|
||||||
|
|
||||||
def git_commit(action, title, *files)
|
def git_commit(message, files)
|
||||||
quoted_files = files.map { |f| "\"#{quote(f)}\"" }
|
quoted_files = files.map { |f| "\"#{quote(f)}\"" }
|
||||||
message = "#{action} '#{quote(title || 'Untitled')}'"
|
puts ">>> git add -A #{quoted_files.join(' ')} && git commit -m \"#{message}\""
|
||||||
success, _ = run("git add -A #{quoted_files.join(' ')} && git commit -m \"#{message}\"")
|
success, output = run("git add -A #{quoted_files.join(' ')} && git commit -m \"#{message}\"")
|
||||||
@mutated = true if success
|
if success
|
||||||
success
|
@mutated = true
|
||||||
|
else
|
||||||
|
puts output
|
||||||
|
end
|
||||||
|
[success, output]
|
||||||
end
|
end
|
||||||
|
|
||||||
def git_reset_hard(ref = nil)
|
def git_reset_hard(ref = nil)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue