-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
executable file
·174 lines (145 loc) · 5.31 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# encoding: UTF-8
require "rubygems"
SOURCE = "source/"
CONFIG = {
'posts' => File.join(SOURCE, "_posts"),
'post_ext' => "md",
'categories' => File.join(SOURCE, "categories"),
'tags' => File.join(SOURCE, "tags")
}
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)
# categories
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
desc "Begin a new category in #{CONFIG['categories']}"
task :category do
abort("rake aborted: '#{CONFIG['categories']}' directory not found.") unless FileTest.directory?(CONFIG['categories'])
title = ENV["title"] || "new-category"
slug = mount_slug(title)
filename = File.join(CONFIG['categories'], "category-#{slug}.html")
if File.exist?(filename)
abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
end
puts "Creating new category: #{filename}"
open(filename, 'w') do |category|
category.puts "---"
category.puts "layout: category"
category.puts "title: \"#{title.gsub(/-/,' ')}\""
category.puts "slug: #{slug}"
category.puts "permalink: /category/#{slug}/"
category.puts "---"
end
puts "Write in categories.yml file"
open("#{SOURCE}_data/categories.yml", 'ab+') do |category|
category.puts ""
category.puts "- slug: #{slug}"
category.puts " name: #{title}"
end
puts "Successfully created!"
end # task :category
desc "Begin a new tag in #{CONFIG['tags']}"
task :tag do
abort("rake aborted: '#{CONFIG['tags']}' directory not found.") unless FileTest.directory?(CONFIG['tags'])
title = ENV["title"] || "new-tag"
slug = mount_slug(title)
filename = File.join(CONFIG['tags'], "tag-#{slug}.html")
if File.exist?(filename)
abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
end
puts "Creating new tag: #{filename}"
open(filename, 'w') do |tag|
tag.puts "---"
tag.puts "layout: tag"
tag.puts "title: \"#{title.gsub(/-/,' ')}\""
tag.puts "slug: #{slug}"
tag.puts "permalink: /tag/#{slug}/"
tag.puts "---"
end
puts "Write in tags.yml file"
open("#{SOURCE}_data/tags.yml", 'ab+') do |tag|
tag.puts ""
tag.puts "- slug: #{slug}"
tag.puts " name: #{title}"
end
puts "Successfully created!"
end # task :tag
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