diff --git a/LICENSE b/LICENSE index ba1c8afb451d358c458aa8c532cc00b5bb916f9d..255f8f03a8112cec69a05da1307c5e4c2c7537ec 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2012-2013 Nicolás Reynolds <fauno@endefensadelsl.org> +Copyright (c) 2012-2015 Nicolás Reynolds <fauno@endefensadelsl.org> 2012-2013 Mauricio Pasquier Juan <mpj@endefensadelsl.org> 2013 Brian Candler <b.candler@pobox.com> diff --git a/bin/imponer b/bin/imponer index 0d5493a2d12475b07d12f97e3fa3c0174bd80cd9..066eb98ed7526f9538e1bfd0981fbefd95ebab1e 100755 --- a/bin/imponer +++ b/bin/imponer @@ -1,4 +1,27 @@ #!/usr/bin/env ruby +# Copyright (c) 2012-2015 Nicolás Reynolds <fauno@endefensadelsl.org> +# 2012-2013 Mauricio Pasquier Juan <mpj@endefensadelsl.org> +# 2013 Brian Candler <b.candler@pobox.com> +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + file = ARGV.first exit if not File.exist? file diff --git a/lib/jekyll-pandoc-multiple-formats.rb b/lib/jekyll-pandoc-multiple-formats.rb index d2f488d7713df58920b9f53b52febb1e0cbad721..20c87512d38394f5157ca374d0163bb6e7b67dd4 100644 --- a/lib/jekyll-pandoc-multiple-formats.rb +++ b/lib/jekyll-pandoc-multiple-formats.rb @@ -1,140 +1,27 @@ +# Copyright (c) 2012-2015 Nicolás Reynolds <fauno@endefensadelsl.org> +# 2012-2013 Mauricio Pasquier Juan <mpj@endefensadelsl.org> +# 2013 Brian Candler <b.candler@pobox.com> +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + require 'open3' require 'jekyll-pandoc-multiple-formats/version' - -module Jekyll - -class PandocGenerator < Generator - def generate(site) - outputs = site.config['pandoc']['outputs'] - flags = site.config['pandoc']['flags'] - - outputs.each_pair do |output, extra_flags| - - # Skip conversion if we're skipping, but still cleanup the outputs hash - next if site.config['pandoc']['skip'] - - # If there isn't a config entry for pandoc's output throw it with the rest - base_dir = File.join(site.source, site.config['pandoc']['output']) || site.source - - site.posts.each do |post| - - post_path = File.join(base_dir, output, File.dirname(post.url)) - - puts "Creating #{post_path}" - FileUtils.mkdir_p(post_path) - - filename = post.url.gsub(/\.html$/, ".#{output}") - # Have a filename! - filename = "#{post.url.gsub(/\//, "-").gsub(/-$/, "")}.#{output}" if filename =~ /\/$/ - - filename_with_path = File.join(base_dir, output, filename) - - # Special cases, stdout is disabled for these - if ['pdf', 'epub', 'odt', 'docx'].include?(output) - output_flag = "-o #{filename_with_path}" - else - output_flag = "-t #{output} -o #{filename_with_path}" - end - - # Add cover if epub - if output == "epub" and not post.data['cover'].nil? - output_flag << " --epub-cover-image=#{post.data['cover']}" - end - - # The command - # Move to the source dir since everything will be relative to # that - pandoc = "pandoc #{flags} #{output_flag} #{extra_flags}" - - # Inform what's being done - puts pandoc - - # Make the markdown header so pandoc receives metadata - content = "#{post.data.to_yaml}\n---\n" - content << post.content - - # Do the stuff - Dir::chdir(site.config['source']) do - Open3::popen3(pandoc) do |stdin, stdout, stderr| - stdin.puts content - stdin.close - STDERR.print stderr.read - end - end - - # Skip failed files - next if not File.exist? filename_with_path - - # If output is PDF, we also create the imposed PDF - if output == 'pdf' and site.config['pandoc']['impose'] - Open3::popen3("imponer '#{filename_with_path}'") do |stdin, stdout, stderr| - STDERR.print stderr.read - STDOUT.print stdout.read - end - - site.static_files << StaticFile.new(site, base_dir, output, filename.gsub(/\.pdf$/, '-imposed.pdf')) - end - - # Add them to the static files list - site.static_files << StaticFile.new(site, base_dir, output, filename) - end - end - end -end -end - -# Namespacing and metaprogramming FTW -module JekyllPandocMultipleFormats - # Determines the correct module where it lives the converter - def self.namespace - Jekyll::VERSION >= '1.0.0' ? Jekyll::Converters : Jekyll - end - - # Determines the correct class name. Jekyll has the converters class kinda - # hardcoded - def self.class_name - Jekyll::VERSION >= '1.0.0' ? 'Markdown' : 'MarkdownConverter' - end - - def self.build - namespace::const_get(class_name).send :include, ConverterMethods - end - - # When included in the correspondant markdown class this module redefines the - # three needed Converter instance methods - module ConverterMethods - def self.included(base) - base.class_eval do - # Just return html5 - def convert(content) - flags = "#{@config['pandoc']['flags']} #{@config['pandoc']['site_flags']}" - - output = '' - Dir::chdir(@config['source']) do - Open3::popen3("pandoc -t html5 #{flags}") do |stdin, stdout, stderr| - stdin.puts content - stdin.close - - output = stdout.read.strip - STDERR.print stderr.read - - end - end - - output - end - - def matches(ext) - rgx = '(' + @config['markdown_ext'].gsub(',','|') +')' - ext =~ Regexp.new(rgx, Regexp::IGNORECASE) - end - - def output_ext(ext) - ".html" - end - end - end - end -end - -# Conjures the metamagic -JekyllPandocMultipleFormats.build +require 'jekyll-pandoc-multiple-formats/generator' +require 'jekyll-pandoc-multiple-formats/converter' diff --git a/lib/jekyll-pandoc-multiple-formats/converter.rb b/lib/jekyll-pandoc-multiple-formats/converter.rb new file mode 100644 index 0000000000000000000000000000000000000000..2c98a4a62ad6240cb6b8713bffb96f058ca90c12 --- /dev/null +++ b/lib/jekyll-pandoc-multiple-formats/converter.rb @@ -0,0 +1,79 @@ +# Copyright (c) 2012-2015 Nicolás Reynolds <fauno@endefensadelsl.org> +# 2012-2013 Mauricio Pasquier Juan <mpj@endefensadelsl.org> +# 2013 Brian Candler <b.candler@pobox.com> +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +# Namespacing and metaprogramming FTW +module JekyllPandocMultipleFormats + # Determines the correct module where it lives the converter + def self.namespace + Jekyll::VERSION >= '1.0.0' ? Jekyll::Converters : Jekyll + end + + # Determines the correct class name. Jekyll has the converters class kinda + # hardcoded + def self.class_name + Jekyll::VERSION >= '1.0.0' ? 'Markdown' : 'MarkdownConverter' + end + + def self.build + namespace::const_get(class_name).send :include, ConverterMethods + end + + # When included in the correspondant markdown class this module redefines the + # three needed Converter instance methods + module ConverterMethods + def self.included(base) + base.class_eval do + # Just return html5 + def convert(content) + flags = "#{@config['pandoc']['flags']} #{@config['pandoc']['site_flags']}" + + output = '' + Dir::chdir(@config['source']) do + Open3::popen3("pandoc -t html5 #{flags}") do |stdin, stdout, stderr| + stdin.puts content + stdin.close + + output = stdout.read.strip + STDERR.print stderr.read + + end + end + + output + end + + def matches(ext) + rgx = '(' + @config['markdown_ext'].gsub(',','|') +')' + ext =~ Regexp.new(rgx, Regexp::IGNORECASE) + end + + def output_ext(ext) + ".html" + end + end + end + end +end + +# Conjures the metamagic +JekyllPandocMultipleFormats.build diff --git a/lib/jekyll-pandoc-multiple-formats/generator.rb b/lib/jekyll-pandoc-multiple-formats/generator.rb new file mode 100644 index 0000000000000000000000000000000000000000..c8503f51d481e6acec124ea667b1c0ed8b8a6aa0 --- /dev/null +++ b/lib/jekyll-pandoc-multiple-formats/generator.rb @@ -0,0 +1,103 @@ +# Copyright (c) 2012-2015 Nicolás Reynolds <fauno@endefensadelsl.org> +# 2012-2013 Mauricio Pasquier Juan <mpj@endefensadelsl.org> +# 2013 Brian Candler <b.candler@pobox.com> +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +module Jekyll + +class PandocGenerator < Generator + def generate(site) + outputs = site.config['pandoc']['outputs'] + flags = site.config['pandoc']['flags'] + + outputs.each_pair do |output, extra_flags| + + # Skip conversion if we're skipping, but still cleanup the outputs hash + next if site.config['pandoc']['skip'] + + # If there isn't a config entry for pandoc's output throw it with the rest + base_dir = File.join(site.source, site.config['pandoc']['output']) || site.source + + site.posts.each do |post| + + post_path = File.join(base_dir, output, File.dirname(post.url)) + + puts "Creating #{post_path}" + FileUtils.mkdir_p(post_path) + + filename = post.url.gsub(/\.html$/, ".#{output}") + # Have a filename! + filename = "#{post.url.gsub(/\//, "-").gsub(/-$/, "")}.#{output}" if filename =~ /\/$/ + + filename_with_path = File.join(base_dir, output, filename) + + # Special cases, stdout is disabled for these + if ['pdf', 'epub', 'odt', 'docx'].include?(output) + output_flag = "-o #{filename_with_path}" + else + output_flag = "-t #{output} -o #{filename_with_path}" + end + + # Add cover if epub + if output == "epub" and not post.data['cover'].nil? + output_flag << " --epub-cover-image=#{post.data['cover']}" + end + + # The command + # Move to the source dir since everything will be relative to # that + pandoc = "pandoc #{flags} #{output_flag} #{extra_flags}" + + # Inform what's being done + puts pandoc + + # Make the markdown header so pandoc receives metadata + content = "#{post.data.to_yaml}\n---\n" + content << post.content + + # Do the stuff + Dir::chdir(site.config['source']) do + Open3::popen3(pandoc) do |stdin, stdout, stderr| + stdin.puts content + stdin.close + STDERR.print stderr.read + end + end + + # Skip failed files + next if not File.exist? filename_with_path + + # If output is PDF, we also create the imposed PDF + if output == 'pdf' and site.config['pandoc']['impose'] + Open3::popen3("imponer '#{filename_with_path}'") do |stdin, stdout, stderr| + STDERR.print stderr.read + STDOUT.print stdout.read + end + + site.static_files << StaticFile.new(site, base_dir, output, filename.gsub(/\.pdf$/, '-imposed.pdf')) + end + + # Add them to the static files list + site.static_files << StaticFile.new(site, base_dir, output, filename) + end + end + end +end +end