-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathRakefile
144 lines (116 loc) · 3.69 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
require 'date'
require 'json'
require 'yaml'
def jekyll(opts="", path="")
Dir.chdir('jekyll') do
sh "jekyll #{opts}"
end
end
# get lat/lon from macOS shortcuts
def location_shortcut_info
latlon = `shortcuts run "getCoreLocationSimple"`.chomp
unless latlon.empty?
JSON.parse(latlon, { :symbolize_names => true })
else
puts "Output Lat/Lon JSON shortcut unavailable"
[]
end
end
def location_info
# Check if CoreLocationCLI is installed
`command -v CoreLocationCLI`
if $?.success?
location_json = `CoreLocationCLI -format '{ "latitude": %latitude, "longitude": %longitude, "address": "%address" }'`.chomp.gsub("\n", "\\n")
JSON.parse(location_json, { :symbolize_names => true })
else
puts "CoreLocationCLI not installed. Use \"brew cask install corelocationcli\""
[]
end
end
desc "Build site using Jekyll"
task :build do
jekyll('build')
end
desc "Serve jekyll test site"
task :serve do
jekyll('serve')
end
desc "Remove existing _sites directory"
task :clean do
Dir.chdir('jekyll') do
`rm -r _site`
end
end
task :rebuild => [:clean, :build]
desc "Rebuild the Jekyll site"
task :default => :build
namespace :blog do
desc "Create a new entry"
task :new do |t|
puts "What's the title of this post?"
title = STDIN.gets.chomp
raise Error, "You must provide a title" if title.empty?
provision_slug = title.downcase.gsub(' ', '-').gsub(/[^\w\-]/, '')
puts "And the URL-slug? [#{provision_slug}]"
slug = STDIN.gets.chomp
slug = provision_slug if slug.empty?
date = DateTime.now
puts "Tag it? (comma-separated) []"
tag_string = STDIN.gets.chomp
location = location_shortcut_info
unless location.empty?
puts "Located at #{location[:address].gsub("\n", ', ')}" unless location.empty?
end
front_matter = {
'layout' => 'blog',
'category' => 'blog',
'title' => title,
'date' => date.iso8601,
'summary' => ' ',
'tags' => tag_string.empty? ? ' ' : tag_string.split(','),
'geo' => location ? {
'name' => location[:address].gsub("\n", ", "),
'xy' => "#{location[:latitude]},#{location[:longitude]}"
} : nil
}.reject { |k,v| v.nil? }
file_name = "#{DateTime.now.strftime("%Y-%m-%d")}-#{slug}.md"
dir_name = "jekyll/_posts/blog/#{date.year}"
path = "#{dir_name}/#{file_name}"
Dir::mkdir dir_name unless FileTest::directory? dir_name
File.open path, 'w' do |f|
# Start YML Front Matter
f.puts front_matter.to_yaml
f.puts '---'
f.puts "\n\n"
end
`open #{path}`
end
desc "Touch a post and set its publication date to now"
task :touch, [:post] do |t, args|
base = "jekyll/_posts/blog/"
raise ArgumentError, "Must specify a post file" unless args.post
/^([0-9]{4})\-/.match(args.post)
year = $1
file_name = "#{base}#{year}/#{args.post}"
raise "Post not found" unless FileTest::file? file_name
content = File.read file_name
content.sub!(/^date: .*$/, "date: \"#{DateTime.now.iso8601}\"")
File.open file_name, 'w' do |f| f.puts content end
end
desc "Mark a post as being updated"
task :update, [:post] do |t, args|
base = "jekyll/_posts/blog/"
raise ArgumentError, "Must specify a post file" unless args.post
/^([0-9]{4})\-/.match(args.post)
year = $1
file_name = "#{base}#{year}/#{args.post}"
raise "Post not found" unless FileTest::file? file_name
content = File.read file_name
if /^updated:/.match(content)
content.sub!(/^updated: .*$/, "updated: \"#{DateTime.now.iso8601}\"")
else
content.sub!(/^(date: .*)$/, "\\1\nupdated: \"#{DateTime.now.iso8601}\"")
end
File.open file_name, 'w' do |f| f.puts content end
end
end