From eaeba298dd2c5a686cb5f7374f04ac7cc554d0f8 Mon Sep 17 00:00:00 2001 From: Masataka Pocke Kuwabara Date: Sat, 11 Jul 2020 16:54:32 +0900 Subject: [PATCH] Specify subtitle with `title:` heading --- lib/hatenikki/cli.rb | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/hatenikki/cli.rb b/lib/hatenikki/cli.rb index 83e8653..f0e9d04 100644 --- a/lib/hatenikki/cli.rb +++ b/lib/hatenikki/cli.rb @@ -19,6 +19,9 @@ def run # Load today's diary. private def load if entry = todays_entry + if match = entry.title.match(/ - (?.+)/) + puts "title: #{match[:subtitle]}" + end puts entry.content end end @@ -28,17 +31,19 @@ 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 @@ -46,6 +51,14 @@ def run end end + private def title_and_content(content) + if match = content.match(/\Atitle: (?.+)/) + ["#{today} - #{match[:subtitle]}", content.sub(/.+\n/, '')] + else + [today, content] + end + end + # Publish drafts except today's private def publish client.entries.each do |e| @@ -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