Browse code

Merge pull request #293 from imathis/generate_environment

Introduce distinction between preview/productive site generation

Frederic Hemberger authored on 10/12/2011 at 11:21:59
Showing 2 changed files
... ...
@@ -59,7 +59,7 @@ task :watch do
59 59
   raise "### You haven't set anything up yet. First run `rake install` to set up an Octopress theme." unless File.directory?(source_dir)
60 60
   puts "Starting to watch source with Jekyll and Compass."
61 61
   system "compass compile --css-dir #{source_dir}/stylesheets" unless File.exist?("#{source_dir}/stylesheets/screen.css")
62
-  jekyllPid = Process.spawn("jekyll --auto")
62
+  jekyllPid = Process.spawn({"OCTOPRESS_ENV"=>"preview"}, "jekyll --auto")
63 63
   compassPid = Process.spawn("compass watch")
64 64
 
65 65
   trap("INT") {
... ...
@@ -75,7 +75,7 @@ task :preview do
75 75
   raise "### You haven't set anything up yet. First run `rake install` to set up an Octopress theme." unless File.directory?(source_dir)
76 76
   puts "Starting to watch source with Jekyll and Compass. Starting Rack on port #{server_port}"
77 77
   system "compass compile --css-dir #{source_dir}/stylesheets" unless File.exist?("#{source_dir}/stylesheets/screen.css")
78
-  jekyllPid = Process.spawn("jekyll --auto")
78
+  jekyllPid = Process.spawn({"OCTOPRESS_ENV"=>"preview"}, "jekyll --auto")
79 79
   compassPid = Process.spawn("compass watch")
80 80
   rackupPid = Process.spawn("rackup --port #{server_port}")
81 81
 
... ...
@@ -200,6 +200,13 @@ end
200 200
 
201 201
 desc "Default deploy task"
202 202
 task :deploy do
203
+  # Check if preview posts exist, which should not be published
204
+  if File.exists?(".preview-mode")
205
+    puts "## Found posts in preview mode, regenerating files ..."
206
+    File.delete(".preview-mode")
207
+    Rake::Task[:generate].execute
208
+  end
209
+
203 210
   Rake::Task[:copydot].invoke(source_dir, public_dir)
204 211
   Rake::Task["#{deploy_default}"].execute
205 212
 end
206 213
new file mode 100644
... ...
@@ -0,0 +1,48 @@
0
+# Monkeypatch for Jekyll
1
+# Introduce distinction between preview/productive site generation
2
+# so posts with YAML attribute `published: false` can be previewed
3
+# on localhost without being published to the productive environment.
4
+
5
+module Jekyll
6
+
7
+  class Site
8
+    # Read all the files in <source>/<dir>/_posts and create a new Post
9
+    # object with each one.
10
+    #
11
+    # dir - The String relative path of the directory to read.
12
+    #
13
+    # Returns nothing.
14
+    def read_posts(dir)
15
+      base = File.join(self.source, dir, '_posts')
16
+      return unless File.exists?(base)
17
+      entries = Dir.chdir(base) { filter_entries(Dir['**/*']) }
18
+
19
+      # first pass processes, but does not yet render post content
20
+      entries.each do |f|
21
+        if Post.valid?(f)
22
+          post = Post.new(self, self.source, dir, f)
23
+
24
+          # Monkeypatch:
25
+          # On preview environment (localhost), publish all posts
26
+          if ENV.has_key?('OCTOPRESS_ENV') && ENV['OCTOPRESS_ENV'] == 'preview' && post.data.has_key?('published') && post.data['published'] == false
27
+            post.published = true
28
+            # Set preview mode flag (if necessary), `rake generate` will check for it
29
+            # to prevent pushing preview posts to productive environment
30
+            File.open(".preview-mode", "w") {}
31
+          end
32
+
33
+          if post.published && (self.future || post.date <= self.time)
34
+            self.posts << post
35
+            post.categories.each { |c| self.categories[c] << post }
36
+            post.tags.each { |c| self.tags[c] << post }
37
+          end
38
+        end
39
+      end
40
+
41
+      self.posts.sort!
42
+
43
+      # limit the posts if :limit_posts option is set
44
+      self.posts = self.posts[-limit_posts, limit_posts] if limit_posts
45
+    end
46
+  end
47
+end
0 48
\ No newline at end of file