Browse code

Introduce distinction between preview/productive site generation

Posts which contain the YAML attribute `published: false` are usually
not generated by Jekyll. With this patch they can be previewed just
like published posts on localhost using `rake watch`or `rake preview`.

NOTICE: Before pushing to the productive environment, use `rake generate`
to update the public directory and remove posts which are flagged not to be
published.

Frederic Hemberger authored on 23/11/2011 at 13:37:30
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
 
82 82
new file mode 100644
... ...
@@ -0,0 +1,45 @@
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'
27
+            post.published = true
28
+          end
29
+
30
+          if post.published && (self.future || post.date <= self.time)
31
+            self.posts << post
32
+            post.categories.each { |c| self.categories[c] << post }
33
+            post.tags.each { |c| self.tags[c] << post }
34
+          end
35
+        end
36
+      end
37
+
38
+      self.posts.sort!
39
+
40
+      # limit the posts if :limit_posts option is set
41
+      self.posts = self.posts[-limit_posts, limit_posts] if limit_posts
42
+    end
43
+  end
44
+end
0 45
\ No newline at end of file