-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
153 lines (127 loc) · 4.36 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
# encoding: UTF-8
require "rubygems"
require "tmpdir"
require "bundler/setup"
require "jekyll"
require "bourbon"
# Change your GitHub reponame
GITHUB_REPONAME = "nandomoreirame/end2end"
GITHUB_REPO_BRANCH = "gh-pages"
SOURCE = "source/"
DEST = "_site"
CONFIG = {
'layouts' => File.join(SOURCE, "_layouts"),
'posts' => File.join(SOURCE, "_posts"),
'post_ext' => "md",
'categories' => File.join(SOURCE, "categories"),
'tags' => File.join(SOURCE, "tags")
}
task default: %w[publish]
desc "Generate blog files"
task :generate do
Jekyll::Site.new(Jekyll.configuration({
"source" => "source/",
"destination" => "_site",
"config" => "_config.yml"
})).process
end
desc "Generate and publish blog to gh-pages"
task :publish => [:generate] do
Dir.mktmpdir do |tmp|
cp_r "_site/.", tmp
pwd = Dir.pwd
Dir.chdir tmp
system "git init"
system "git checkout --orphan #{GITHUB_REPO_BRANCH}"
system "git add ."
message = "Site updated at #{Time.now.utc}"
system "git commit -am #{message.inspect}"
system "git remote add origin git@github.com:#{GITHUB_REPONAME}.git"
system "git push origin #{GITHUB_REPO_BRANCH} --force"
Dir.chdir pwd
end
end
desc "Begin a new post in #{CONFIG['posts']}"
task :post do
abort("rake aborted: '#{CONFIG['posts']}' directory not found.") unless FileTest.directory?(CONFIG['posts'])
title = ENV["title"] || "Novo post"
tags = ""
categories = ""
# tags
env_tags = ENV["tags"] || ""
tags = strtag(env_tags)
# categorias
env_cat = ENV["category"] || ""
categories = strtag(env_cat)
# slug do post
slug = mount_slug(title)
begin
date = (ENV['date'] ? Time.parse(ENV['date']) : Time.now).strftime('%Y-%m-%d')
time = (ENV['date'] ? Time.parse(ENV['date']) : Time.now).strftime('%T')
rescue => e
puts "Error - date format must be YYYY-MM-DD, please check you typed it correctly!"
exit -1
end
filename = File.join(CONFIG['posts'], "#{date}-#{slug}.#{CONFIG['post_ext']}")
if File.exist?(filename)
abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
end
puts "Creating new post: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "layout: post"
post.puts "title: \"#{title.gsub(/-/,' ')}\""
post.puts "permalink: #{slug}"
post.puts "date: #{date} #{time}"
post.puts "comments: true"
post.puts "description: \"#{title}\""
post.puts 'keywords: ""'
post.puts "categories:"
post.puts "#{categories}"
post.puts "tags:"
post.puts "#{tags}"
post.puts "---"
end
end # task :post
desc "Create a new page."
task :page do
name = ENV["name"] || "new-page.md"
filename = File.join(SOURCE, "#{name}")
filename = File.join(filename, "index.html") if File.extname(filename) == ""
title = File.basename(filename, File.extname(filename)).gsub(/[\W\_]/, " ").gsub(/\b\w/){$&.upcase}
if File.exist?(filename)
abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
end
slug = mount_slug(title)
mkdir_p File.dirname(filename)
puts "Creating new page: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "layout: page"
post.puts "title: \"#{title}\""
post.puts 'description: ""'
post.puts 'keywords: ""'
post.puts "permalink: \"#{slug}\""
post.puts "slug: \"#{slug}\""
post.puts "---"
post.puts "{% include JB/setup %}"
end
end # task :page
def mount_slug(title)
slug = str_clean(title)
slug = slug.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
return slug
end
def str_clean(title)
return title.tr("ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž", "AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz")
end
def strtag(str_tags)
tags = "";
if !str_tags.nil?
arr_tags = str_tags.split(",")
arr_tags.each do |t|
tags = tags + "- " + t.delete(' ') + "\n"
end
end
return tags
end