Skip to content

Commit

Permalink
Specify subtitle with title: heading
Browse files Browse the repository at this point in the history
  • Loading branch information
pocke committed Jul 11, 2020
1 parent 61c8d97 commit eaeba29
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions lib/hatenikki/cli.rb
Expand Up @@ -19,6 +19,9 @@ def run
# Load today's diary.
private def load
if entry = todays_entry
if match = entry.title.match(/ - (?<subtitle>.+)/)
puts "title: #{match[:subtitle]}"
end
puts entry.content
end
end
Expand All @@ -28,24 +31,34 @@ def run
content = STDIN.read
categories = []

title, content = title_and_content(content)

if entry = todays_entry
client.update_entry(
entry.id,
today,
title,
content,
categories,
entry.draft,
)
else
client.post_entry(
today,
title,
content,
categories,
'yes', # draft
)
end
end

private def title_and_content(content)
if match = content.match(/\Atitle: (?<subtitle>.+)/)
["#{today} - #{match[:subtitle]}", content.sub(/.+\n/, '')]
else
[today, content]
end
end

# Publish drafts except today's
private def publish
client.entries.each do |e|
Expand All @@ -66,8 +79,12 @@ def run
@argv[0]
end

private def todays_entry?(entry)
entry.title.match?(/\A#{Regexp.escape(today)}\b/)
end

private def todays_entry
client.entries.find{|entry| entry.title == today }
client.entries.find{|entry| todays_entry?(entry) }
end

private def today
Expand Down

0 comments on commit eaeba29

Please sign in to comment.