Browse Source

Add new post/page rake task

Michael Rose 5 years ago
parent
commit
ccad0d8c0f
1 changed files with 89 additions and 0 deletions
  1. 89
    0
      Rakefile.rb

+ 89
- 0
Rakefile.rb View File

@@ -0,0 +1,89 @@
1
+require "rubygems"
2
+require "bundler/setup"
3
+require "stringex"
4
+
5
+## -- Config -- ##
6
+
7
+public_dir      = "public"    # compiled site directory
8
+posts_dir       = "_posts"    # directory for blog files
9
+new_post_ext    = "md"  # default new post file extension when using the new_post task
10
+new_page_ext    = "md"  # default new page file extension when using the new_page task
11
+
12
+
13
+#############################
14
+# Create a new Post or Page #
15
+#############################
16
+
17
+# usage rake new_post
18
+desc "Create a new post in #{posts_dir}"
19
+task :new_post, :title do |t, args|
20
+  if args.title
21
+    title = args.title
22
+  else
23
+    title = get_stdin("Enter a title for your post: ")
24
+  end
25
+  filename = "#{posts_dir}/#{Time.now.strftime('%Y-%m-%d')}-#{title.to_url}.#{new_post_ext}"
26
+  if File.exist?(filename)
27
+    abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
28
+  end
29
+  tags = get_stdin("Enter tags to classify your post (comma separated): ")
30
+  puts "Creating new post: #{filename}"
31
+  open(filename, 'w') do |post|
32
+    post.puts "---"
33
+    post.puts "layout: post"
34
+    post.puts "title: \"#{title.gsub(/&/,'&')}\""
35
+    post.puts "modified: #{Time.now.strftime('%Y-%m-%d %H:%M:%S %z')}"
36
+    post.puts "tags: [#{tags}]"
37
+    post.puts "image:"
38
+    post.puts "  feature: "
39
+    post.puts "  credit: "
40
+    post.puts "  creditlink: "
41
+    post.puts "comments: "
42
+    post.puts "share: "
43
+    post.puts "---"
44
+  end
45
+end
46
+
47
+# usage rake new_page
48
+desc "Create a new page"
49
+task :new_page, :title do |t, args|
50
+  if args.title
51
+    title = args.title
52
+  else
53
+    title = get_stdin("Enter a title for your page: ")
54
+  end
55
+  filename = "#{title.to_url}.#{new_page_ext}"
56
+  if File.exist?(filename)
57
+    abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
58
+  end
59
+  tags = get_stdin("Enter tags to classify your page (comma separated): ")
60
+  puts "Creating new page: #{filename}"
61
+  open(filename, 'w') do |page|
62
+    page.puts "---"
63
+    page.puts "layout: page"
64
+    page.puts "permalink: /#{title.to_url}/"
65
+    page.puts "title: \"#{title}\""
66
+    page.puts "modified: #{Time.now.strftime('%Y-%m-%d %H:%M')}"
67
+    page.puts "tags: [#{tags}]"
68
+    page.puts "image:"
69
+    page.puts "  feature: "
70
+    page.puts "  credit: "
71
+    page.puts "  creditlink: "
72
+    page.puts "share: "
73
+    page.puts "---"
74
+  end
75
+end
76
+
77
+def get_stdin(message)
78
+  print message
79
+  STDIN.gets.chomp
80
+end
81
+
82
+def ask(message, valid_options)
83
+  if valid_options
84
+    answer = get_stdin("#{message} #{valid_options.to_s.gsub(/"/, '').gsub(/, /,'/')} ") while !valid_options.include?(answer)
85
+  else
86
+    answer = get_stdin(message)
87
+  end
88
+  answer
89
+end

Loading…
Cancel
Save