/
Rakefile
1051 lines (955 loc) · 35.5 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require "rubygems"
require "bundler/setup"
require "stringex"
require 'ruby-progressbar'
## -- Rsync Deploy config -- ##
# Be sure your public key is listed in your server's ~/.ssh/authorized_keys file
ssh_user = "user@domain.com"
ssh_port = "22"
document_root = "~/website.com/"
rsync_delete = false
rsync_args = "" # Any extra arguments to pass to rsync
deploy_default = "push"
# This will be configured for you when you run config_deploy
deploy_branch = "master"
repo_url = "git@github.com:user/user.github.io.git"
use_token = false
## -- Misc Configs -- ##
tmp_dir = File.expand_path(".") # temporary directory for public/deploy
public_dir = "#{tmp_dir}/public" # compiled site directory
source_dir = "source" # source file directory
blog_index_dir = 'source' # directory for your blog's index page (if you put your index in source/blog/index.html, set this to 'source/blog')
deploy_dir = "#{tmp_dir}/_deploy" # deploy directory (for Github pages deployment)
stash_dir = "_stash" # directory to stash posts for speedy generation
full_stash_dir = "#{source_dir}/#{stash_dir}" # full path for stash dir
stash_root_dir = "_stash_root" # directory to stash pages (in /source/)
full_stash_root_dir = "#{source_dir}/#{stash_root_dir}" # full path for stash_root dir
root_stashes = [] # directories to be stashed in /source/
posts_dir = "_posts" # directory for blog files
themes_dir = ".themes" # directory for blog files
new_post_ext = "markdown" # default new post file extension when using the new_post task
new_page_ext = "markdown" # default new page file extension when using the new_page task
server_port = "4000" # port for preview server eg. localhost:4000
word_avoid = "~/.gitavoid" # words which must be avoided to be published
ping_file = "ping.yml" # file of site list for ping
minify = false
js_for_combine = ['footnote.js', 'jquery.githubRepoWidget.min.js', 'monthly_archive.js', 'random-posts.js', 'related-posts.js', 'utils.js']
js_output = "all.js"
js_minify_others = false
#html_for_minify = "all"
html_for_minify = ["*.html", "blog/*/*/*/*/*.html"]
html_not_minify = []
precheck = false
n_cores = 8
if (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
puts '## Set the codepage to 65001 for Windows machines'
`chcp.com 65001`
end
desc "Initial setup for Octopress: copies the default theme into the path of Jekyll's generator. Rake install defaults to rake install[classic] to install a different theme run rake install[some_theme_name]"
task :install, :theme do |t, args|
if File.directory?(source_dir) || File.directory?("sass")
abort("rake aborted!") if ask("A theme is already installed, proceeding will overwrite existing files. Are you sure?", ['y', 'n']) == 'n'
end
# copy theme into working Jekyll directories
theme = args.theme || 'classic'
puts "## Copying "+theme+" theme into ./#{source_dir} and ./sass"
mkdir_p source_dir
cp_r "#{themes_dir}/#{theme}/source/.", source_dir
mkdir_p "sass"
cp_r "#{themes_dir}/#{theme}/sass/.", "sass"
mkdir_p "#{source_dir}/#{posts_dir}"
mkdir_p public_dir
end
#######################
# Working with Jekyll #
#######################
desc "Update stylesheets. Give an argument as nested, expanded, compact or compressed (default) to the output style."
task :css, :style do |t, args|
raise "### You haven't set anything up yet. First run `rake install` to set up an Octopress theme." unless File.directory?(source_dir)
puts "## Update stylesheets"
style = minify ? "-s compressed" : "-s nested"
if args.style
style = "-s #{args.style}"
end
system "compass compile #{style} --css-dir #{source_dir}/stylesheets"
cp_r "#{source_dir}/stylesheets/.", "#{public_dir}/stylesheets/"
end
desc "Generate jekyll site"
task :generate, :opt do |t, args|
raise "### You haven't set anything up yet. First run `rake install` to set up an Octopress theme." unless File.directory?(source_dir)
opt = args.opt ? args.opt : ""
if precheck and not opt.include?('test')
Rake::Task[:check].invoke()
end
puts "## Generating Site with Jekyll"
jekyll_opt = ""
if Rake.application.options.trace or opt.include?('test')
jekyll_opt = "--trace"
end
if opt.include?("unpublished") or opt.include?('test')
jekyll_opt += " --unpublished"
end
jekyll_config = IO.read('_config.yml')
jekyll_config.gsub!(/\n^.*OCTOPRESS_TEST$/, "")
File.open('_config.yml', 'w') do |f|
f.write jekyll_config
end
if opt.include?('test')
jekyll_config = IO.read('_config.yml')
jekyll_config += "\nshare_static: false # OCTOPRESS_TEST"
jekyll_config += "\npage-view: false # OCTOPRESS_TEST"
jekyll_config += "\nhatena_popular_num: 0 # OCTOPRESS_TEST"
File.open('_config.yml', 'w') do |f|
f.write jekyll_config
end
end
ok_failed_raise system("JEKYLL_ENV=production jekyll build #{jekyll_opt}")
if opt.include?('test')
jekyll_config = IO.read('_config.yml')
jekyll_config.gsub!(/\n^.*OCTOPRESS_TEST$/, "")
File.open('_config.yml', 'w') do |f|
f.write jekyll_config
end
end
Rake::Task[:css].execute
Rake::Task[:combine_js].invoke(args.opt)
if not opt.include?('test') and minify
Rake::Task[:minify_js].invoke('force')
if not opt.include?('no_minify')
Rake::Task[:minify_html].execute
end
end
system "rm -f .integrated"
system "rm -f .preview-mode"
end
desc "Same as generate"
task :gen, :opt do |t, args|
Rake::Task[:generate].invoke(args.opt)
end
desc "Generate test (no css, js compiling)"
task :gen_test, :opt do |t, args|
Rake::Task[:generate].invoke('test')
end
desc "Same as gen_test"
task :gt, :opt do |t, args|
Rake::Task[:gen_test].invoke()
end
# usage rake generate_only[my-post]
desc "Generate only specified post (much faster)"
task :generate_only, :filename do |t, args|
trap(:INT) do
raise "Stopped by SIGINT"
end
begin
if precheck
Rake::Task[:check].invoke()
end
Rake::Task[:isolate].invoke(args.filename)
Rake::Task[:generate].invoke('test')
puts "## Restoring stashed posts/pages"
Rake::Task[:integrate].execute
rescue
Rake::Task[:integrate].execute
raise $!
end
end
desc "Same as generate_only"
task :gen_only, :filename do |t, args|
Rake::Task[:generate_only].invoke(args.filename)
end
desc "Same as generate_only"
task :go, :filename do |t, args|
Rake::Task[:generate_only].invoke(args.filename)
end
desc "Watch the site and regenerate when it changes"
task :watch do
raise "### You haven't set anything up yet. First run `rake install` to set up an Octopress theme." unless File.directory?(source_dir)
puts "Starting to watch source with Jekyll and Compass."
system "compass compile --css-dir #{source_dir}/stylesheets" unless File.exist?("#{source_dir}/stylesheets/screen.css")
system "touch .preview-mode"
jekyllPid = Process.spawn("jekyll build --watch --unpublished")
compassPid = Process.spawn("compass watch")
trap("INT") {
[jekyllPid, compassPid].each { |pid| Process.kill(9, pid) rescue Errno::ESRCH }
}
[jekyllPid, compassPid].each { |pid| Process.wait(pid) }
end
# usage rake watch_only[my-post]
desc "watch only specified post"
task :watch_only, :filename do |t, args|
raise "### You haven't set anything up yet. First run `rake install` to set up an Octopress theme." unless File.directory?(source_dir)
Rake::Task[:isolate].invoke(args.filename)
begin
Rake::Task[:watch].execute
rescue
end
puts ""
puts "## Restoring stashed posts/pages"
Rake::Task[:integrate].execute
end
desc "preview the site in a web browser"
task :preview do
raise "### You haven't set anything up yet. First run `rake install` to set up an Octopress theme." unless File.directory?(source_dir)
puts "Starting to watch source with Jekyll and Compass. Starting Rack on port #{server_port}"
system "compass compile --css-dir #{source_dir}/stylesheets" unless File.exist?("#{source_dir}/stylesheets/screen.css")
system "touch .preview-mode"
jekyllPid = Process.spawn("jekyll build --watch --unpublished")
compassPid = Process.spawn("compass watch")
rackupPid = Process.spawn("rackup --port #{server_port}")
trap("INT") {
[jekyllPid, compassPid, rackupPid].each { |pid| Process.kill(9, pid) rescue Errno::ESRCH }
}
[jekyllPid, compassPid, rackupPid].each { |pid| Process.wait(pid) }
end
# usage rake preview_only[my-post]
desc "preview only specified post"
task :preview_only, :filename do |t, args|
raise "### You haven't set anything up yet. First run `rake install` to set up an Octopress theme." unless File.directory?(source_dir)
Rake::Task[:isolate].invoke(args.filename)
begin
Rake::Task[:preview].execute
rescue
end
puts ""
puts "## Restoring stashed posts/pages"
Rake::Task[:integrate].execute
end
# usage rake new_post[my-new-post] or rake new_post['my new post'] or rake new_post (defaults to "new-post")
desc "Begin a new post in #{source_dir}/#{posts_dir}"
task :new_post, :title do |t, args|
if args.title
title = args.title
else
title = get_stdin("Enter a title for your post: ")
end
raise "### You haven't set anything up yet. First run `rake install` to set up an Octopress theme." unless File.directory?(source_dir)
#title_words = title.split(' ')
#tags = title_words
#category = tags.shift
mkdir_p "#{source_dir}/#{posts_dir}"
filename = "#{source_dir}/#{posts_dir}/#{Time.now.strftime('%Y-%m-%d')}-#{title.to_url}.#{new_post_ext}"
if File.exist?(filename)
abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
end
# Change title (make it for tags for some posting sites)
#title_tags = "\##{tags.join(' #')}"
puts "Creating new post: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "layout: post"
post.puts "title: \"#{title.gsub(/&/,'&')}\""
#post.puts "title: \"#{title_tags}\""
post.puts "date: #{Time.now.strftime('%Y-%m-%d %H:%M:%S %z')}"
post.puts "comments: true"
post.puts "categories:"
post.puts "tags:"
#post.puts "categories: #{category}"
#post.puts "tags: #{tags}"
#post.puts "keywords: #{title.gsub(' ',', ')}"
#post.puts "description: "
post.puts "ogimage:"
post.puts "published: false"
post.puts "---"
post.puts ""
post.puts "<!-- more -->"
post.puts "{%include after_excerpt.html%}"
post.puts ""
end
end
# usage rake new_page[my-new-page] or rake new_page[my-new-page.html] or rake new_page (defaults to "new-page.markdown")
desc "Create a new page in #{source_dir}/(filename)/index.#{new_page_ext}"
task :new_page, :filename do |t, args|
raise "### You haven't set anything up yet. First run `rake install` to set up an Octopress theme." unless File.directory?(source_dir)
args.with_defaults(:filename => 'new-page')
page_dir = [source_dir]
site_config = YAML.load(IO.read('_config.yml'))
author = site_config['author']
if args.filename.downcase =~ /(^.+\/)?(.+)/
filename, extension = $2.rpartition('.').reject(&:empty?) # Get filename and extension
title = filename
page_dir.concat($1.downcase.sub(/^\//, '').split('/')) unless $1.nil? # Add path to page_dir Array
if extension.nil?
page_dir << filename
filename = "index"
end
extension ||= new_page_ext
page_dir = page_dir.map! { |d| d = d.to_url }.join('/') # Sanitize path
filename = filename.downcase.to_url
mkdir_p page_dir
file = "#{page_dir}/#{filename}.#{extension}"
if File.exist?(file)
abort("rake aborted!") if ask("#{file} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
end
puts "Creating new page: #{file}"
open(file, 'w') do |page|
page.puts "---"
page.puts "layout: page"
page.puts "title: \"#{title}\""
page.puts "author: \"#{author}\""
page.puts "date: #{Time.now.strftime('%Y-%m-%d %H:%M')}"
page.puts "comments: true"
page.puts "sharing: true"
page.puts "footer: true"
page.puts "published: false"
page.puts "---"
end
else
puts "Syntax error: #{args.filename} contains unsupported characters"
end
end
# usage rake isolate[my-post]
desc "Move all other posts than the one currently being worked on to a temporary stash location (stash) so regenerating the site happens much more quickly."
task :isolate, :filename do |t, args|
if args.filename
filename = args.filename
else
filename = Dir.glob("#{source_dir}/#{posts_dir}/*.#{new_post_ext}").sort_by{|f| File.mtime(f)}.last
end
if filename == nil
puts "## Stashing all posts"
else
puts "## Stashing other posts than #{filename}"
end
FileUtils.mkdir(full_stash_dir) unless File.exist?(full_stash_dir)
Dir.glob("#{source_dir}/#{posts_dir}/*") do |post|
if filename != nil && post.include?(filename)
puts "Remaining #{post}..."
else
FileUtils.mv post, full_stash_dir
end
end
FileUtils.mkdir(full_stash_root_dir) unless File.exist?(full_stash_root_dir)
if defined? root_stashes == nil
if root_stashes.class == String
FileUtils.mv "#{source_dir}/#{root_stashes}", full_stash_root_dir
elsif root_stashes.class == Array
for d in root_stashes do
FileUtils.mv "#{source_dir}/#{d}", full_stash_root_dir if File.exist?("#{source_dir}/#{d}")
end
end
end
system "touch .isolated"
end
desc "Move all stashed posts back into the posts directory, ready for site generation."
task :integrate do
FileUtils.mv Dir.glob("#{full_stash_dir}/*"), "#{source_dir}/#{posts_dir}/"
FileUtils.mv Dir.glob("#{full_stash_root_dir}/*"), "#{source_dir}/"
system "rm -f .isolated"
system "touch .integrated"
end
desc "Clean out caches: .pygments-cache, .gist-cache, .sass-cache, thumbnail"
task :clean do
rm_rf [Dir.glob(".pygments-cache/**"), Dir.glob(".gist-cache/**"), Dir.glob(".sass-cache/**"), "#{source_dir}/stylesheets/screen.css"]
rm_rf [Dir.glob("#{source_dir}/images/**/thumbnail")]
end
desc "Move sass to sass.old, install sass theme updates, replace sass/custom with sass.old/custom"
task :update_style, :theme do |t, args|
theme = args.theme || 'classic'
if File.directory?("sass.old")
puts "removed existing sass.old directory"
rm_r "sass.old", :secure=>true
end
mv "sass", "sass.old"
puts "## Moved styles into sass.old/"
cp_r "#{themes_dir}/"+theme+"/sass/", "sass", :remove_destination=>true
cp_r "sass.old/custom/.", "sass/custom/", :remove_destination=>true
puts "## Updated Sass ##"
end
desc "Move source to source.old, install source theme updates, replace source/_includes/navigation.html with source.old's navigation"
task :update_source, :theme do |t, args|
theme = args.theme || 'classic'
if File.directory?("#{source_dir}.old")
puts "## Removed existing #{source_dir}.old directory"
rm_r "#{source_dir}.old", :secure=>true
end
mkdir "#{source_dir}.old"
cp_r "#{source_dir}/.", "#{source_dir}.old"
puts "## Copied #{source_dir} into #{source_dir}.old/"
cp_r "#{themes_dir}/"+theme+"/source/.", source_dir, :remove_destination=>true
cp_r "#{source_dir}.old/_includes/custom/.", "#{source_dir}/_includes/custom/", :remove_destination=>true
cp "#{source_dir}.old/favicon.png", source_dir
mv "#{source_dir}/index.html", "#{blog_index_dir}", :force=>true if blog_index_dir != source_dir
cp "#{source_dir}.old/index.html", source_dir if blog_index_dir != source_dir && File.exists?("#{source_dir}.old/index.html")
puts "## Updated #{source_dir} ##"
end
##############
# Deploying #
##############
desc "Default deploy task"
task :deploy, :deploy_method do |t, args|
# Check if preview posts exists, which should not be published
if File.exists?(".integrated") or File.exists?(".isolated")
puts "## Found isolated history, regenerating files ..."
system "rm -f .integrated .isolated"
Rake::Task[:integrate].execute
Rake::Task[:generate].execute
end
if File.exists?(".preview-mode")
puts "## Found posts in preview mode, regenerating files ..."
File.delete(".preview-mode")
Rake::Task[:generate].execute
end
Rake::Task[:copydot].invoke(source_dir, public_dir)
# Check if files are fine or not
ok_failed_raise system("if [ -f #{word_avoid} ];then while read a;do if ret=`grep -i -r -q $a #{public_dir}`;then echo \"A word $a is included, must be avoided!!!\"; echo $ret; exit 1;fi; done < #{word_avoid};fi")
if args.deploy_method
deploy_method = args.deploy_method;
else
deploy_method = deploy_default;
end
puts "use #{deploy_method} to push"
Rake::Task["#{deploy_method}"].execute
Rake::Task[:ping].execute
Rake::Task[:superfeedr].execute
end
desc "Generate website and deploy"
task :gen_deploy, :deploy_method do |t, args|
Rake::Task[:integrate].execute
Rake::Task[:generate].execute
Rake::Task[:deploy].invoke(args.deploy_method)
end
desc "Same as gen_deploy"
task :gd, :deploy_method do |t, args|
Rake::Task[:gen_deploy].invoke(args.deploy_method)
end
desc "copy dot files for deployment"
task :copydot, :source, :dest do |t, args|
FileList["#{args.source}/**/.*"].exclude("**/.", "**/..", "**/.DS_Store", "**/._*").each do |file|
cp_r file, file.gsub(/#{args.source}/, "#{args.dest}") unless File.directory?(file)
end
end
desc "Deploy website via rsync"
task :rsync do
exclude = ""
if File.exists?('./rsync-exclude')
exclude = "--exclude-from '#{File.expand_path('./rsync-exclude')}'"
end
puts "## Deploying website via Rsync"
ok_failed system("rsync -avze 'ssh -p #{ssh_port}' #{exclude} #{rsync_args} #{"--delete" unless rsync_delete == false} #{public_dir}/ #{ssh_user}:#{document_root}")
end
desc "deploy public directory to github pages"
multitask :push do
puts "## Deploying branch to Github Pages "
puts "## Pulling any updates from Github Pages "
cd "#{deploy_dir}" do
Bundler.with_clean_env { system "git pull" }
end
(Dir["#{deploy_dir}/*"]).each { |f| rm_rf(f) }
Rake::Task[:copydot].invoke(public_dir, deploy_dir)
puts "\n## Copying #{public_dir} to #{deploy_dir}"
cp_r "#{public_dir}/.", deploy_dir
cd "#{deploy_dir}" do
system "git add -A"
message = "Site updated at #{Time.now.utc}"
puts "\n## Committing: #{message}"
system "git commit -m \"#{message}\""
puts "\n## Pushing generated #{deploy_dir} website"
output = (use_token)? " >/dev/null 2>&1":""
Bundler.with_clean_env { system "git push origin #{deploy_branch} #{output}" }
puts "\n## Github Pages deploy complete"
end
end
desc "deploy public directory to github pages through temporary deploy dir"
multitask :push_ex do
puts "## Deploying branch to Github Pages "
if use_token
puts "## clone the repository..."
else
puts "## clone from #{repo_url}"
end
rm_rf deploy_dir
mkdir_p deploy_dir
cd "#{deploy_dir}" do
system "git init"
system "git remote add origin #{repo_url}"
end
Rake::Task[:copydot].invoke(public_dir, deploy_dir)
puts "\n## Copying #{public_dir} to #{deploy_dir}"
cp_r "#{public_dir}/.", deploy_dir
cd "#{deploy_dir}" do
system "git add -A"
puts "\n## Commiting: Site updated at #{Time.now.utc}"
message = "Site updated at #{Time.now.utc}"
system "git commit -m \"#{message}\" >/dev/null"
system "git branch -m #{deploy_branch}" unless deploy_branch == 'master'
puts "\n## Pushing generated #{deploy_dir} website"
output = (use_token) ? " >/dev/null 2>&1" : ""
Bundler.with_clean_env { system "git push -u -f origin #{deploy_branch} #{output}" }
puts "\n## Github Pages deploy complete"
end
end
desc "Update configurations to support publishing to root or sub directory"
task :set_root_dir, :dir do |t, args|
puts ">>> !! Please provide a directory, eg. rake set_root_dir[publishing/subdirectory]" unless args.dir
if args.dir
if args.dir == "/"
dir = "/"
else
dir = "/" + args.dir.sub(/(\/*)(.+)/, "\\2").sub(/\/$/, '') + "/"
end
rm_rf "#{public_dir}"
rakefile = IO.read(__FILE__)
rakefile.sub!(/public_dir(\s*)=(\s*)(["'])[^"']*public[^"']*(["'])/, "public_dir\\1=\\2\\3\#{tmp_dir}/#{File.join('public', dir)}\\3")
File.open(__FILE__, 'w') do |f|
f.write rakefile
end
compass_config = IO.read('config.rb')
compass_config.sub!(/http_path(\s*)=(\s*)(["'])[\w\-\/]*["']/, "http_path\\1=\\2\\3#{File.join(dir, '/')}\\3")
compass_config.sub!(/http_images_path(\s*)=(\s*)(["'])[\w\-\/]*["']/, "http_images_path\\1=\\2\\3#{File.join(dir, '/images')}\\3")
compass_config.sub!(/http_fonts_path(\s*)=(\s*)(["'])[\w\-\/]*["']/, "http_fonts_path\\1=\\2\\3#{File.join(dir, '/fonts')}\\3")
compass_config.sub!(/css_dir(\s*)=(\s*)(["'])[\w\-\/]*["']/, "css_dir\\1=\\2\\3public#{File.join(dir, '/stylesheets')}\\3")
File.open('config.rb', 'w') do |f|
f.write compass_config
end
jekyll_config = IO.read('_config.yml')
jekyll_config.sub!(/^destination:.+$/, "destination: #{File.join(tmp_dir, "public", dir)}")
jekyll_config.sub!(/^root:.*$/, "root: #{dir}")
File.open('_config.yml', 'w') do |f|
f.write jekyll_config
end
puts "## Site's root directory is now '#{dir}' ##"
end
end
desc "Set up _deploy folder and deploy branch for Github Pages deployment"
task :setup_github_pages, [:repo, :yes] do |t, args|
if args.repo
repo_url = args.repo
else
puts "Enter the read/write url for your repository"
puts "For example, 'git@github.com:your_username/your_username.github.io.git"
puts " 'https://github.com/your_username/repository'"
puts " 'https://${GH_TOKEN}@github.com/your_username/repository'"
repo_url = get_stdin("Repository url: ")
end
protocol = (repo_url.match(/(^git)@/).nil?) ? 'https' : 'git'
if protocol == 'git'
user = repo_url.match(/:([^\/]+)/)[1]
else
user = repo_url.match(/github\.com\/([^\/]+)/)[1]
end
branch = (repo_url.match(/\/[\w-]+\.github\.(?:io|com)/).nil?) ? 'gh-pages' : 'master'
project = (branch == 'gh-pages') ? repo_url.split("/")[-1].sub(/\.git$/, "") : ''
use_token = (repo_url.match(/https:\/\/.*@github.com/))? true : false
use_token_val = use_token ? "true" : "false"
unless (`git remote -v` =~ /origin.+?octopress(?:\.git)?/).nil?
# If octopress is still the origin remote (from cloning) rename it to octopress
system "git remote rename origin octopress"
if branch == 'master'
# If this is a user/organization pages repository, add the correct origin remote
# and checkout the source branch for committing changes to the blog source.
system "git remote add origin #{repo_url}"
if use_token
puts "Added given remote as origin"
else
puts "Added remote #{repo_url} as origin"
end
system "git config branch.master.remote origin"
puts "Set origin as default remote"
system "git branch -m master source"
puts "Master branch renamed to 'source' for committing your blog source files"
end
end
url = blog_url(user, project, source_dir)
jekyll_config = IO.read('_config.yml')
jekyll_config.sub!(/^url:.*$/, "url: #{url}")
jekyll_config.sub!(/^subscribe_rss:.*$/, "subscribe_rss: #{url}/atom.xml")
jekyll_config.sub!(/^feedly_atom:.*$/, "feedly_atom: #{url}/atom.xml")
File.open('_config.yml', 'w') do |f|
f.write jekyll_config
end
rakefile = IO.read(__FILE__)
rakefile.sub!(/deploy_branch(\s*)=(\s*)(["'])[\w-]*["']/, "deploy_branch\\1=\\2\\3#{branch}\\3")
rakefile.sub!(/repo_url(\s*)=(\s*)(["'])[0-9a-zA-Z\-\_\/\@\.\:]*["']/, "repo_url\\1=\\2\\3#{repo_url}\\3")
rakefile.sub!(/use_token(\s*)=(\s*)[0-9a-zA-Z\-\_\/\@\.\:]*/, "use_token\\1=\\2#{use_token_val}")
ext = 'markdown'
if ask("Do you want to use 'md' extension instead of 'markdown'?", ['y', 'n'], args.yes) == "y"
ext = 'md'
end
rakefile.sub!(/new_post_ext(\s*)=(\s*)(["'])[\w-]*["']/, "new_post_ext\\1=\\2\\3#{ext}\\3")
rakefile.sub!(/new_page_ext(\s*)=(\s*)(["'])[\w-]*["']/, "new_page_ext\\1=\\2\\3#{ext}\\3")
if ask("Do you want to push_ex (renew remote repository evrey time)?", ['y', 'n'], args.yes) == 'y'
if yn_check(args.yes) == "y"
dir = ""
else
dir = get_stdin("Enter where you want to put _deploy (current: #{tmp_dir}): ")
end
if dir == ""
dir = tmp_dir
elsif
if dir[-1] == "/"
dir = dir[0..-2]
end
end
rakefile.sub!(/deploy_default(\s*)=(\s*)(["'])[\w-]*["']/, "deploy_default\\1=\\2\\3push_ex\\3")
rakefile.sub!(/(tmp_dir\s*=\s*File.expand_path\()(["'])[^"']*["']/,"\\1\\2#{dir}\\2")
else
rm_rf deploy_dir
mkdir deploy_dir
cd "#{deploy_dir}" do
system "git init"
system "echo 'My Octopress Page is coming soon …' > index.html"
system "git add -A"
system "git commit -m \"Octopress init\""
system "git branch -m #{deploy_branch}" unless branch == 'master'
system "git remote add origin #{repo_url}"
rakefile.sub!(/deploy_default(\s*)=(\s*)(["'])[\w-]*["']/, "deploy_default\\1=\\2\\3push\\3")
end
if use_token
puts "\n---\n## Now you can deploy with `rake deploy` ##"
else
puts "\n---\n## Now you can deploy to #{repo_url} with `rake deploy` ##"
end
end
File.open(__FILE__, 'w') do |f|
f.write rakefile
end
system "rake set_root_dir[#{project}]"
end
def ok_failed(condition)
if (condition)
puts "OK"
else
puts "FAILED"
end
end
def get_stdin(message)
print message
STDIN.gets.chomp
end
def yn_check(yn)
if yn == "y" or yn == "yes" or yn == "-y" or yn == "--yes"
answer = "y"
else
answer = "n"
end
answer
end
def ask(message, valid_options, yn = "")
if valid_options
if valid_options.include?("y") and yn_check(yn) == "y"
answer = "y"
else
answer = get_stdin("#{message} #{valid_options.to_s.gsub(/"/, '').gsub(/, /,'/')}: ") while !valid_options.include?(answer)
end
else
answer = get_stdin(message)
end
answer
end
def blog_url(user, project, source_dir)
cname = "#{source_dir}/CNAME"
url = if File.exists?(cname)
"http://#{IO.read(cname).strip}"
else
"http://#{user.downcase}.github.io"
end
url += "/#{project}" unless project == ''
url
end
desc "list tasks"
task :list do
puts "Tasks: #{(Rake::Task.tasks - [Rake::Task[:list]]).join(', ')}"
puts "(type rake -T for more detail)\n\n"
end
def grep_check(word, grep_option, opt)
vword=word
if word.index("^") == 0
vword=word.sub("^", ":")
end
if opt == 0
grep_option = "-A1 -B1 #{grep_option}"
comment = "An empty line is required around \\\"#{word}\\\"!"
elsif opt == 1
grep_option = "-B1 #{grep_option}"
comment = "An empty line is required before \\\"#{word}\\\"!"
elsif opt == 2
grep_option = "-A1 #{grep_option}"
comment = "An empty line is required after \\\"#{word}\\\"!"
end
print "Checking \"#{word}\"... "
ok_failed_raise system("\
if grep -H -n -e \"#{word}\" #{grep_option} 2>/dev/null|\
grep -v \"#{vword}\";then \
printf \"\\\\n\\\\e[31m\";\
echo #{comment};\
printf \"\\\\e[m\\\\n\";\
exit 1;\
fi")
end
desc 'Check source'
task :check, :opt do |t, args|
grep_files = "-r #{source_dir} --include \"*.#{new_post_ext}\""
if args.opt == "new"
grep_files = "#{source_dir}/#{posts_dir}/*.#{new_post_ext}"
end
puts "\n## Checking codes..."
grep_option = "#{grep_files}|\
grep -v \"^--$\"|\
grep -v \"#{new_post_ext}-[0-9]\\\+-$\"|\
grep -v \"{%\ *raw\ *%}$\""
grep_check("^{%\ *codeblock", grep_option, 1)
#grep_check("^>", grep_option, 0)
#grep_check("^#", grep_option, 1)
grep_check("^- - -$", grep_option, 1)
#grep_check("^<hr>$", grep_option, 1)
print "Checking words to be avoided... "
ok_failed_raise system("\
if [ -f #{word_avoid} ];then \
while read a;do \
if ret=$(grep -i -q $a #{grep_files} 2>/dev/null);then \
echo \"A word $a is included, must be avoided!!!\";\
echo $ret;\
exit 1;\
fi;\
done < #{word_avoid};\
fi")
puts ""
end
desc 'Send to Superfeedr'
task :superfeedr do
site_config = YAML.load(IO.read('_config.yml'))
hub_url = site_config['hub_url']
url = site_config['url']
if hub_url != nil and hub_url != ""
require 'net/http'
require 'uri'
atom_url = "#{url}/atom.xml"
resp, data = Net::HTTP.post_form(URI.parse(hub_url),
{'hub.mode' => 'publish',
'hub.url' => atom_url})
raise "!! Hub notification error: #{resp.code} #{resp.msg}, #{data}" unless resp.code == "204"
puts "## Notified hub (" + hub_url + ") that feed #{atom_url} has been updated"
end
end
#desc 'Ping pingomatic'
#task :ping do
# begin
# require 'xmlrpc/client'
# puts '* Pinging ping-o-matic'
# XMLRPC::Client.new('rpc.pingomatic.com', '/').call('weblogUpdates.extendedPing', 'Nothoughtcontrol.com' , 'http://www.nothoughtcontrol.com', 'http://www.nothoughtcontrol.com/atom.xml')
# rescue LoadError
# puts '! Could not ping ping-o-matic, because XMLRPC::Client could not be found.'
# end
#end
#
#desc 'Notify Google of the new sitemap'
#task :sitemapgoogle do
# begin
# require 'net/http'
# require 'uri'
# puts '* Pinging Google about our sitemap'
# Net::HTTP.get('www.google.com', '/webmasters/tools/ping?sitemap=' + URI.escape('http://www.nothoughtcontrol.com/sitemap.xml'))
# rescue LoadError
# puts '! Could not ping Google about our sitemap, because Net::HTTP or URI could not be found.'
# end
#end
#
#desc 'Notify Bing of the new sitemap'
#task :sitemapbing do
# begin
# require 'net/http'
# require 'uri'
# puts '* Pinging Bing about our sitemap'
# Net::HTTP.get('www.bing.com', '/webmaster/ping.aspx?siteMap=' + URI.escape('http://www.nothoughtcontrol.com/sitemap.xml'))
# rescue LoadError
# puts '! Could not ping Bing about our sitemap, because Net::HTTP or URI could not be found.'
# end
#end
#
#desc "Notify various services about new content"
#task :notify => [:pingomatic, :sitemapgoogle, :sitemapbing] do
#end
#
#desc 'Ping PINGOO'
#task :ping do
# begin
# require 'xmlrpc/client'
# puts '* Pinging PINGOO{'
# XMLRPC::Client.new('pingoo.jp', '/').call('weblogUpdates.extendedPing', 'rcmdnk.github.io' , 'http://rcmdnk.github.io', 'http://rcmdnk.github.io/atom.xml')
# rescue LoadError
# puts '! Could not ping PINGOO, because XMLRPC::Client could not be found.'
# end
#end
#desc 'Ping pingomatic'
#task :ping do
# begin
# require 'xmlrpc/client'
# puts '## Pinging PINGOO'
# #XMLRPC::Client.new('rpc.pingomatic.com', '/').call('weblogUpdates.extendedPing', 'rcmdnk\'s blog' , 'http://rcmdnk.github.io', 'http://rcmdnk.github.io/atom.xml')
# XMLRPC::Client.new2('http://api.my.yahoo.co.jp/RPC2').call('weblogUpdates.ping', 'rcmdnk blog' , 'http://rcmdnk.github.io')
# rescue LoadError
# puts '! Could not ping ping-o-matic, because XMLRPC::Client could not be found.'
# end
#end
#
#-- sending ping --#
desc "Send ping to Web Search Engines"
task :ping do
if File.exist?(ping_file)
require "yaml"
require "xmlrpc/client"
site_config = YAML.load(IO.read('_config.yml'))
blog_title = site_config['title']
blog_url = site_config['url']
ping_url = YAML.load(IO.read('ping.yml'))
ping_url.each do |url|
ping = XMLRPC::Client.new2(url)
begin
result = ping.call('weblogUpdates.ping', blog_title, blog_url)
puts "#{url} : #{result}"
rescue => e
puts "#{url} : #{e}"
end
end
end
end
task :test do
#ok_failed_raise system("if [ -f #{word_avoid} ];then while read a;do if ret=`grep -i -r -q $a #{public_dir}`;then echo \"A word $a is included, must be avoided!!!\"; echo $ret; exit 1;fi; done < #{word_avoid};fi")
#ok_failed_raise system("cd #{public_dir};pwd;cd -")
#raise "eror"
system "git ls >& log"
end
def ok_failed_raise(condition, print_ok = true)
if (condition)
puts "OK" if print_ok
else
raise "FAILD"
end
end
require "yui/compressor"
require "htmlcompressor"
require "parallel"
desc "Minify CSS"
task :minify_css do
puts "## Minifying CSS"
compressor = YUI::CssCompressor.new
Parallel.map(Dir.glob("#{source_dir}/stylesheets/**/*css"), :in_threads=> n_cores) do |name|
puts "Minifying #{name}"
input = File.read(name)
output = File.open("#{name}", "w")
output << compressor.compress(input)
output.close
end
cp_r "#{source_dir}/stylesheets/.", "#{public_dir}/stylesheets/"
end
desc "Minify JS"
task :minify_js, :opt do |t, args|
puts "## Minifying JS"
if js_minify_others
Rake::Task[:minify_other_js].invoke(args.opt)
end
end
desc "Minify JS and combine"
task :combine_js, :opt do |t, args|
puts "## Combining JS"
compressor = YUI::JavaScriptCompressor.new
if not args.opt == nil and (not args.opt and File.exist?("#{source_dir}/javascripts/#{js_output}"))
t_alljs = File.mtime("#{source_dir}/javascripts/#{js_output}")
time_check = false
js_for_combine.each do |j|
if File.mtime("#{source_dir}/javascripts/#{j}") > t_alljs
puts "newer file #{j} is found"
time_check = true
break
end
end
if not time_check
next
end
end
output = File.open("#{source_dir}/javascripts/#{js_output}", "w")
js_for_combine.each do |j|
input = File.read("#{source_dir}/javascripts/#{j}")
if args.opt == nil or args.opt.include?("no")
output << input
else
output << compressor.compress(input)
end
end
output.close
cp_r "#{source_dir}/javascripts/#{js_output}", "#{public_dir}/javascripts/"
end
desc "Minify other JS"
task :minify_other_js, :opt do |t, args|
puts "## Minifying other JS"
n = 0
compressor = YUI::JavaScriptCompressor.new
Parallel.map(Dir.glob("#{source_dir}/javascripts/**/*.js"), :in_threads => n_cores) do |j|
if (js_for_combine+[js_output]).include?(j.sub("#{source_dir}/javascripts/", "")) or\
j.include?("compressed")
next
end
dir = j.split('/')[0..-2].join('/')
name = j.split('/')[-1]
compressed = dir + "/compressed/" + name
if File.directory?(dir+"/compressed/")
if not args.opt == nil and (not args.opt and File.file?(compressed) and File.mtime(compressed) > File.mtime(j))
next
end
else
mkdir_p compressed.split('/')[0..-2].join('/')
end
puts "Minifying #{j}"
input = File.read(j)
output = File.open(compressed, "w")
if args.opt and args.opt.include?("no")
output << input
else
output << compressor.compress(input)
end
output.close
n+=1
end
if n > 0
cp_r "#{source_dir}/javascripts/.", "#{public_dir}/javascripts/"
end
end
desc "Minify HTML"
task :minify_html do