diff --git a/README.md b/README.md index 7a21854a92de8001fa4f8cc77382f23bad802638..3afa31a3da3a9a7f7e37b0e1c2a1c1a2504c7e2c 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,10 @@ Add to `_config.yml`: markdown: pandoc pandoc: - skip: false + skip: + full: false + posts: false + categories: false bundle_permalink: ':output_ext/:slug.:output_ext' papersize: 'a5paper' sheetsize: 'a4paper' @@ -59,8 +62,10 @@ pandoc: * `markdown: pandoc` will instruct jekyll to use the pandoc html converter. -* `skip` allows you to skip the other formats generation and proceed with the -regular jekyll site build. +* `skip` allows you to skip the other formats generation and proceed + with the regular jekyll site build. You can skip some of the + generation process or all of it. Older versions of this plugin + required `true` or `false` to skip the process altogether. * `full_flags` if `full_file` is defined, these flags are used on it. By default are set to `--top-level-division=part` so each category is diff --git a/lib/jekyll-pandoc-multiple-formats/config.rb b/lib/jekyll-pandoc-multiple-formats/config.rb index 3030621cf55eab9ce6fe9b92044a2546bab734e5..8530d34621ed2bbff5c3710b4d3ec941ce41de91 100644 --- a/lib/jekyll-pandoc-multiple-formats/config.rb +++ b/lib/jekyll-pandoc-multiple-formats/config.rb @@ -1,7 +1,11 @@ module JekyllPandocMultipleFormats class Config DEFAULTS = { - 'skip' => false, + 'skip' => { + 'posts' => false, + 'categories' => false, + 'full' => false + }, 'bundle_permalink' => ':output_ext/:slug.:output_ext', 'papersize' => 'a5paper', 'sheetsize' => 'a4paper', @@ -23,7 +27,7 @@ module JekyllPandocMultipleFormats end def skip? - @config['skip'] + @config['skip'].values.all? end def imposition? @@ -34,13 +38,21 @@ module JekyllPandocMultipleFormats @config['binder'] end - def full_file? - @config['full_file'] - end - # TODO magic def outputs @config['outputs'] end + + def generate_posts? + !@config.dig('skip', 'posts') + end + + def generate_categories? + !@config.dig('skip', 'categories') + end + + def generate_full_file? + !@config.dig('skip', 'full') + end end end diff --git a/lib/jekyll-pandoc-multiple-formats/generator.rb b/lib/jekyll-pandoc-multiple-formats/generator.rb index 622bbae3cbf2cdae6896f286690d66791189f48d..f8e948e9259b6b5601ea7db344e0aadd890cc740 100644 --- a/lib/jekyll-pandoc-multiple-formats/generator.rb +++ b/lib/jekyll-pandoc-multiple-formats/generator.rb @@ -28,6 +28,55 @@ class PandocGenerator < Generator attr_accessor :site, :config + def generate_post_for_output(post, output) + Jekyll.logger.debug 'Pandoc:', post.data['title'] + Jekyll::Hooks.trigger :posts, :pre_render, post, { format: output } + + pandoc_file = PandocFile.new(@site, output, post) + return unless pandoc_file.write + + Jekyll::Hooks.trigger :posts, :post_render, post, { format: output } + + @site.keep_files << pandoc_file.relative_path + @pandoc_files << pandoc_file + end + + def generate_category_for_output(category, posts, output) + Jekyll.logger.info 'Pandoc:', "Generating category #{category}" + posts.sort! + pandoc_file = PandocFile.new(@site, output, posts, category) + + if @site.keep_files.include? pandoc_file.relative_path + Jekyll.logger.warn 'Pandoc:', + "#{pandoc_file.relative_path} is a category file AND a post file. Change the category name to fix this" + return + end + + return unless pandoc_file.write + + @site.keep_files << pandoc_file.relative_path + @pandoc_files << pandoc_file + end + + def general_full_for_output(output) + title = @site.config.dig('title') + Jekyll.logger.info 'Pandoc:', "Generating full file #{title}" + # For parts to make sense, we order articles by date and then by + # category, so each category is ordered by date. + # + # cat1 - art1 + # cat1 - art3 + # cat2 - art2 + full = @site.posts.docs.reject { |p| p.data.dig('full') }.sort_by do |p| + [ p.data['date'], p.data['categories'].first.to_s ] + end + + full_file = PandocFile.new(@site, output, full, title, { full: true }) + full_file.write + @site.keep_files << full_file.relative_path + @pandoc_files << full_file + end + def generate(site) @site ||= site @config ||= JekyllPandocMultipleFormats::Config.new(@site.config['pandoc']) @@ -39,54 +88,19 @@ class PandocGenerator < Generator @config.outputs.each_pair do |output, _| Jekyll.logger.info 'Pandoc:', "Generating #{output}" - @site.posts.docs.each do |post| - Jekyll.logger.debug 'Pandoc:', post.data['title'] - Jekyll::Hooks.trigger :posts, :pre_render, post, { format: output } - - pandoc_file = PandocFile.new(@site, output, post) - next unless pandoc_file.write - - Jekyll::Hooks.trigger :posts, :post_render, post, { format: output } - - @site.keep_files << pandoc_file.relative_path - @pandoc_files << pandoc_file - end - - @site.post_attr_hash('categories').each_pair do |title, posts| - Jekyll.logger.info 'Pandoc:', "Generating category #{title}" - posts.sort! - pandoc_file = PandocFile.new(@site, output, posts, title) - - if @site.keep_files.include? pandoc_file.relative_path - puts "#{pandoc_file.relative_path} is a category file AND a post file" - puts 'change the category name to fix this' - next + if @config.generate_posts? + @site.posts.docs.each do |post| + generate_post_for_output post, output end - - next unless pandoc_file.write - - @site.keep_files << pandoc_file.relative_path - @pandoc_files << pandoc_file end - if @config.full_file? - title = @site.config.dig('title') - Jekyll.logger.info 'Pandoc:', "Generating full file #{title}" - # For parts to make sense, we order articles by date and then by - # category, so each category is ordered by date. - # - # cat1 - art1 - # cat1 - art3 - # cat2 - art2 - full = @site.posts.docs.reject { |p| p.data.dig('full') }.sort_by do |p| - [ p.data['date'], p.data['categories'].first.to_s ] + if @config.generate_categories? + @site.post_attr_hash('categories').each_pair do |title, posts| + generate_category_for_output title, posts, output end - - full_file = PandocFile.new(@site, output, full, title, { full: true }) - full_file.write - @site.keep_files << full_file.relative_path - @pandoc_files << full_file end + + general_full_for_output(output) if @config.generate_full_file? end @pandoc_files.each do |pandoc_file|