-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
156 lines (136 loc) · 3.65 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
145
146
147
148
149
150
151
152
153
154
155
156
require 'time'
def convert_block_area(file_name)
flag_in_block = false
lines = IO.readlines(file_name).map do |l|
if l.chomp.match /(\s*)```(.*)$/
if flag_in_block
flag_in_block = false
"{% endhighlight %}\n"
else
if $2.length > 0
flag_in_block = true
"#{$1}{% highlight #{$2} %}\n"
else
l
end
end
else
l
end
end
File.open(file_name,'w') do |f|
f.puts lines
end
end
desc "Auto Check on my jekyll server."
task :autocheck do
exec "god start -c .autocheck.god"
end
desc "Check Github update and build on my jekyll server."
task :check_and_build do
Dir.chdir File.expand_path('..',__FILE__)
remote_head = `git ls-remote origin`.lines.map(&:split).first[0]
locale_head = `git show-ref`.lines.map(&:split).first[0]
unless remote_head == locale_head
system 'git checkout .'
system 'git pull origin master > /dev/null'
system "jekyll build"
end
end
desc "Build my site ..."
task :build do
Dir.chdir File.expand_path('..',__FILE__)
Dir['_posts/*.md'].each do |md_file|
convert_block_area md_file
end
system "jekyll build"
system "cd ../echohn.github.io && git add ./ && git commit -m 'auto commit' && git push origin master"
end
desc "Start a locally server"
task :s do
system "jekyll server -s . -d _site -P 3000 -w"
end
# Usage: rake post title="A Title" [date="2014-04-14"]
desc "Create a new post"
task :new do
unless FileTest.directory?('./_posts')
abort("rake aborted: '_posts' directory not found.")
end
title = ENV["title"] || "new-post"
slug = title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
begin
date = (ENV['date'] ? Time.parse(ENV['date']) : Time.now)
.strftime('%Y-%m-%d')
rescue Exception => e
puts "Error: date format must be YYYY-MM-DD!"
exit -1
end
filename = File.join('.', '_posts', "#{date}-#{slug}.md")
if File.exist?(filename)
abort("rake aborted: #{filename} already exists.")
end
yaml_header = <<-EOF.gsub(/^\s+\|/,'')
|---
|layout: post
|title: \"#{title.gsub(/-/,' ')}\"
|subtitle: \'\'
|date: #{date}
|header-img: \"img/post-bg-unix-linux.jpg\"
|author: \"Echo\"
|tags:
|keywords:
|---
EOF
puts "Creating new post: #{filename}"
open(filename, 'w') do |post|
post.puts yaml_header
end
exec "open #{filename}"
end
# Usage: rake post title="A Title" [date="2014-04-14"]
desc "Create a new slide"
task :slide do
unless FileTest.directory?('./_slides')
abort("rake aborted: '_slides' directory not found.")
end
title = ENV["title"] || "new-slide"
slug = title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
begin
date = (ENV['date'] ? Time.parse(ENV['date']) : Time.now)
.strftime('%Y-%m-%d')
rescue Exception => e
puts "Error: date format must be YYYY-MM-DD!"
exit -1
end
filename = File.join('.', '_slides', "#{date}-#{slug}.haml")
if File.exist?(filename)
abort("rake aborted: #{filename} already exists.")
end
slide_home_page = <<-EOF.gsub(/^\s+\|/,'')
|%section
| %h1 #{title}
| %h3 #{date.gsub(/\-/,'.')}
| %p
| %small
| Created by
| %a{:href => "http://echohn.github.io"} Echo Hon
| \/
| %a{:href => "http://twitter.com/echo_hon"} @echohn
EOF
yaml_header = <<-EOF.gsub(/^\s+\|/,'')
|---
|layout: slide
|title: \"#{title.gsub(/-/,' ')}\"
|date: #{date}
|author: \"Echo\"
|tags:
|keywords:
|---
EOF
puts "Creating new slide: #{filename}"
open(filename, 'w') do |post|
post.puts yaml_header
post.puts slide_home_page
end
exec "open #{filename}"
end