This repository was archived by the owner on Feb 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRakefile
More file actions
146 lines (133 loc) · 3.8 KB
/
Rakefile
File metadata and controls
146 lines (133 loc) · 3.8 KB
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
145
146
## Site Configuration Variables
show_home_link = false # Should an automatically generated 'Home' link be added to site navigation
show_blog_link = true # Show automatically generated 'Blog' link in the menu
blog_link_name = "Blog" # Link text to be used for the 'Blog' link
default_author = "ajclarkson" # Name attributed to authoring pages/blog posts by default. Can be
# overridden in the page/post front matter
##### DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU"RE DOING!
require 'rubygems'
require 'bundler/setup'
require 'yaml'
require 'ya2yaml'
require 'stringex'
task :generate do
puts "Generating your site..."
Rake::Task["generate_page_navigation"].execute
Rake::Task["generate_post_index"].execute
puts "Completed successfully. Deploy your site to make changes live!"
end
task :generate_page_navigation do
puts "Creating Site Navigation..."
pages = []
if show_home_link
pages.push({
:title => "Home",
:slug => '/'
})
end
if show_blog_link
pages.push({
:title => blog_link_name,
:slug => '/blog'
})
end
files = Dir.glob("pages/*")
files.each do |file|
file_contents = File.read(file)
unless File.basename(file) == "index.md"
begin
if (markdown = file_contents.match(/^(?<headers>---\s*\n.*?\n?)^(---\s*$\n?)/m))
article = markdown.post_match
metadata = YAML.load(markdown[:headers])
pages.push({
:filename => File.basename(file, '.*'),
:title => metadata['title'],
:slug => "/"+metadata['slug']
})
end
rescue => e
puts "YAML Exception reading #{file}: #{e.message}"
end
end
end
File.open('./views/shared/nav.haml', 'w+'){ |f|
f.write("%nav\n\t%ul\n")
pages.each do |page|
f.write("\t\t%li\n\t\t\t%a{:href=>'" + page[:slug] + "'} " + page[:title] + "\n")
end
}
File.open('_page-index.yaml', 'w+'){ |f|
f.write pages.ya2yaml
}
end
task :generate_post_index do
puts "Indexing Blog Entries..."
posts = []
files = Dir.glob("posts/*")
files.each do |file|
file_contents = File.read(file)
begin
if (markdown = file_contents.match(/^(?<headers>---\s*\n.*?\n?)^(---\s*$\n?)/m))
article = markdown.post_match
metadata = YAML.load(markdown[:headers])
posts.push({
:filename => File.basename(file, '.*'),
:title => metadata['title'],
:slug => "/blog/"+metadata['slug'],
:date => metadata['date'],
:author => metadata['author'],
:next => "",
:previous => ""
})
end
rescue => e
puts "YAML Exception reading #{file}: #{e.message}"
end
end
posts = posts.sort_by{|post| post[:date]}.reverse
posts.each_with_index do |post, index|
if index != 0
post[:next] = posts[index-1][:slug]
end
if index != posts.size-1
post[:previous] = posts[index+1][:slug]
end
end
File.open('_post-index.yaml', 'w+'){ |f|
f.write posts.sort_by{|post| post[:date]}.reverse.ya2yaml
}
end
task :new_page do
title = get_stdin("Enter a title for your new page: ")
filename = "./pages/#{title.to_url}.md"
puts "Creating new page: #{filename}"
File.open(filename, 'w+') do |page|
page.puts("---")
page.puts("title: #{title}")
page.puts("date: #{Time.now.strftime('%Y-%m-%d %H:%M')}")
page.puts("slug: #{title.to_url}")
page.puts("author: #{default_author}")
page.puts("---")
end
end
task :new_post, :title do |t, args|
if args.title
title = args.title
else
title = get_stdin("Enter a title for your new post: ")
end
filename = "./posts/#{Time.now.strftime('%Y-%m-%d')}-#{title.to_url}.md"
puts "Creating new post: #{filename}"
File.open(filename, 'w+') do |post|
post.puts("---")
post.puts("title: #{title}")
post.puts("date: #{Time.now.strftime('%Y-%m-%d %H:%M')}")
post.puts("slug: #{title.to_url}")
post.puts("author: #{default_author}")
post.puts("---")
end
end
def get_stdin(message)
print message
STDIN.gets.chomp
end