Last active
October 15, 2015 01:01
-
-
Save netbe/8e89571a7b0637c5b703 to your computer and use it in GitHub Desktop.
Get last Jekyll article's title and url
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TWEET_FILE="new-article.txt" | |
desc "Check if we have a new article and create the message" | |
task :prepare_new_article_tweet do | |
title, article_url = last_article | |
# Check if article already published | |
uri = URI(article_url) | |
res = Net::HTTP.get_response(uri) | |
if res.is_a?(Net::HTTPNotFound) | |
puts "If successful deployment, we should tweet" | |
message = "#{title} - #{article_url}" | |
system "echo #{message} > #{TWEET_FILE}" | |
else | |
puts "Last article already published" | |
end | |
end | |
desc "Tweet the newly published article" | |
task :tweet do | |
if File.exists?(TWEET_FILE) | |
message = File.read(TWEET_FILE) | |
puts "Tweeting #{message}" | |
`bundle exec t update "#{message}"` | |
end | |
end | |
def last_article | |
filename = nil | |
Dir.chdir("_posts") do | |
filenames = `git ls-files`.split("\n") | |
filename = filenames.last | |
end | |
title = nil | |
# Try to find title inside article | |
File.read("_posts/#{filename}").each_line do |line| | |
next if /^---$/.match(line) | |
if match = /title: (.*)/.match(line) | |
title = match[1] | |
break | |
end | |
end | |
basename = filename.split('.')[0] | |
matched, year, month, day, name = /^(\d{4})-(\d{2})-(\d{2})-(.*)$/.match(basename).to_a | |
if title.nil? | |
# build title from filename if not found | |
title = name.gsub(/-/, ' ').capitalize | |
end | |
article_url = "https://netbe.github.io/#{year}/#{month}/#{day}/#{name}" | |
return title, article_url | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment