11require "rubygems"
22require 'rake'
33
4- ## -- Misc Configs --##
5- jekyll_dir = "."
6- posts_dir = "_posts"
7- new_post_ext = "md"
4+ SOURCE = "."
5+ CONFIG = {
6+ 'themes' => File . join ( SOURCE , "_includes" , "themes" ) ,
7+ 'layouts' => File . join ( SOURCE , "_layouts" ) ,
8+ 'posts' => File . join ( SOURCE , "_posts" ) ,
9+ 'post_ext' => "md"
10+ }
811
912# usage rake new_post[my-new-post] or rake new_post['my new post'] or rake new_post (defaults to "new-post")
10- desc "Begin a new post in #{ jekyll_dir } / #{ posts_dir } "
13+ desc "Begin a new post in #{ CONFIG [ 'posts' ] } "
1114task :new_post , :title do |t , args |
12- raise "### Cannot locate the #{ posts_dir } ." unless File . directory? ( posts_dir )
13- mkdir_p "#{ jekyll_dir } /#{ posts_dir } "
15+ abort ( "rake aborted: '#{ CONFIG [ 'posts' ] } ' directory not found." ) unless FileTest . directory? ( CONFIG [ 'posts' ] )
1416 args . with_defaults ( :title => 'new-post' )
1517 slug = args . title . downcase . strip . gsub ( ' ' , '-' ) . gsub ( /[^\w -]/ , '' )
16- title = args . title . gsub ( /-/ , ' ' )
17- filename = "#{ jekyll_dir } /#{ posts_dir } /#{ Time . now . strftime ( '%Y-%m-%d' ) } -#{ slug } .#{ new_post_ext } "
18+ filename = File . join ( CONFIG [ 'posts' ] , "#{ Time . now . strftime ( '%Y-%m-%d' ) } -#{ slug } .#{ CONFIG [ 'post_ext' ] } " )
1819 if File . exist? ( filename )
1920 abort ( "rake aborted!" ) if ask ( "#{ filename } already exists. Do you want to overwrite?" , [ 'y' , 'n' ] ) == 'n'
2021 end
22+
2123 puts "Creating new post: #{ filename } "
2224 open ( filename , 'w' ) do |post |
2325 post . puts "---"
2426 post . puts "layout: post"
25- post . puts "title: \" #{ title } \" "
27+ post . puts "title: \" #{ args . title . gsub ( /-/ , ' ' ) } \" "
2628 post . puts "comments: true"
2729 post . puts "category: "
2830 post . puts "tags: []"
2931 post . puts "---"
3032 end
31- end
32-
33- def ask ( message , valid_options )
34- if valid_options
35- answer = get_stdin ( "#{ message } #{ valid_options . to_s . gsub ( /"/ , '' ) . gsub ( /, / , '/' ) } " ) while !valid_options . include? ( answer )
36- else
37- answer = get_stdin ( message )
38- end
39- answer
40- end
41-
42- def get_stdin ( message )
43- print message
44- STDIN . gets . chomp
45- end
33+ end # task :new_post
4634
4735desc "Switch between Jekyll-bootstrap themes."
4836task :switch_theme , :theme do |t , args |
49- theme_path = File . join ( File . dirname ( __FILE__ ) , "_includes" , "themes" , args . theme )
50- layouts_path = File . join ( File . dirname ( __FILE__ ) , "_layouts" )
37+ theme_path = File . join ( CONFIG [ 'themes' ] , args . theme )
5138
52- abort ( "rake aborted: './_includes/ themes/#{ args . theme } ' directory not found." ) unless FileTest . directory? ( theme_path )
53- abort ( "rake aborted: './_layouts' directory not found." ) unless FileTest . directory? ( layouts_path )
39+ abort ( "rake aborted: '#{ CONFIG [ ' themes' ] } /#{ args . theme } ' directory not found." ) unless FileTest . directory? ( theme_path )
40+ abort ( "rake aborted: '#{ CONFIG [ 'layouts' ] } ' directory not found." ) unless FileTest . directory? ( CONFIG [ 'layouts' ] )
5441
5542 Dir . glob ( "#{ theme_path } /*" ) do |filename |
5643 puts "Generating '#{ args . theme } ' layout: #{ File . basename ( filename ) } "
5744
58- open ( " #{ layouts_path } / #{ File . basename ( filename ) } " , 'w' ) do |page |
45+ open ( File . join ( CONFIG [ 'layouts' ] , File . basename ( filename ) ) , 'w' ) do |page |
5946 if File . basename ( filename , ".html" ) . downcase == "default"
6047 page . puts "---"
6148 page . puts "---"
@@ -73,4 +60,18 @@ end # task :switch_theme
7360desc "Launch preview environment"
7461task :preview do
7562 system "jekyll --auto --server"
63+ end # task :preview
64+
65+ def ask ( message , valid_options )
66+ if valid_options
67+ answer = get_stdin ( "#{ message } #{ valid_options . to_s . gsub ( /"/ , '' ) . gsub ( /, / , '/' ) } " ) while !valid_options . include? ( answer )
68+ else
69+ answer = get_stdin ( message )
70+ end
71+ answer
7672end
73+
74+ def get_stdin ( message )
75+ print message
76+ STDIN . gets . chomp
77+ end
0 commit comments