From 26d869d5e356019e31468a0e270534fb080907ae Mon Sep 17 00:00:00 2001
From: fauno <fauno@endefensadelsl.org>
Date: Mon, 9 Jul 2018 22:33:08 -0300
Subject: [PATCH] only regenerate files when the sources are newer

---
 .../generator.rb                              |  2 +-
 .../pandoc_file.rb                            | 29 ++++++++++++++++++-
 lib/jekyll-pandoc-multiple-formats/version.rb |  2 +-
 3 files changed, 30 insertions(+), 3 deletions(-)

diff --git a/lib/jekyll-pandoc-multiple-formats/generator.rb b/lib/jekyll-pandoc-multiple-formats/generator.rb
index bf4a839..86a44c6 100644
--- a/lib/jekyll-pandoc-multiple-formats/generator.rb
+++ b/lib/jekyll-pandoc-multiple-formats/generator.rb
@@ -1,4 +1,4 @@
-# Copyright (c) 2012-2015 Nicolás Reynolds <fauno@endefensadelsl.org>
+# Copyright (c) 2012-2018 Nicolás Reynolds <fauno@endefensadelsl.org>
 #               2012-2013 Mauricio Pasquier Juan <mpj@endefensadelsl.org>
 #               2013      Brian Candler <b.candler@pobox.com>
 # 
diff --git a/lib/jekyll-pandoc-multiple-formats/pandoc_file.rb b/lib/jekyll-pandoc-multiple-formats/pandoc_file.rb
index 75b2f72..e7ff91f 100644
--- a/lib/jekyll-pandoc-multiple-formats/pandoc_file.rb
+++ b/lib/jekyll-pandoc-multiple-formats/pandoc_file.rb
@@ -26,7 +26,7 @@ module Jekyll
     include Convertible
 
     attr_reader :format, :site, :config, :flags, :posts, :slug, :title, :url
-    attr_reader :papersize, :sheetsize, :signature
+    attr_reader :papersize, :sheetsize, :signature, :sources
 
     def initialize(site, format, posts, title = nil, extra = {})
       @site   = site
@@ -184,6 +184,12 @@ module Jekyll
     end
 
     def write
+      unless rebuild?
+        Jekyll.logger.info "#{relative_path} doesn't need to be rebuilt"
+        Jekyll.logger.debug sources.join(' ')
+        return true
+      end
+
       FileUtils.mkdir_p(File.dirname(path))
       # Remove the file before creating it
       FileUtils.rm_f(path)
@@ -332,6 +338,27 @@ module Jekyll
       @signature ||= find_option 'signature'
     end
 
+    # Finds the source files for this pandoc file
+    def sources
+      return @sources if @sources
+
+      @sources = ['_config.yml']
+      @sources << flags.split(' ').map do |flag|
+        file = File.join(@site.config['source'], flag.split('=').last)
+        file if File.exist? file
+      end
+
+      @sources << posts.map(&:path)
+
+      @sources = @sources.flatten.compact
+    end
+
+    def rebuild?
+      !File.exist?(path) || sources.map do |f|
+        File.ctime(f) > File.ctime(path)
+      end.any?
+    end
+
     private
 
     def single_post
diff --git a/lib/jekyll-pandoc-multiple-formats/version.rb b/lib/jekyll-pandoc-multiple-formats/version.rb
index 8f58975..e77c69e 100644
--- a/lib/jekyll-pandoc-multiple-formats/version.rb
+++ b/lib/jekyll-pandoc-multiple-formats/version.rb
@@ -1,3 +1,3 @@
 module JekyllPandocMultipleFormats
-  VERSION = '0.3.0'
+  VERSION = '0.4.0'
 end
-- 
GitLab