From ce74d352411f33d6d74fcb3cdc6c84a35559dffe Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Wed, 29 Jun 2022 16:06:43 +0200 Subject: [PATCH] jekyll: allow to set a file as destination for fetch remote plugin Signed-off-by: CrazyMax --- _plugins/fetch_remote.rb | 56 ++++++++++++++++++++++---------------- _plugins/fix_urls.rb | 2 -- _plugins/update_api_toc.rb | 2 -- 3 files changed, 33 insertions(+), 27 deletions(-) diff --git a/_plugins/fetch_remote.rb b/_plugins/fetch_remote.rb index 35c284ee99..bcb3a3857d 100644 --- a/_plugins/fetch_remote.rb +++ b/_plugins/fetch_remote.rb @@ -6,38 +6,40 @@ require 'open-uri' require 'rake' module Jekyll - def self.download(url, dest) - uri = URI.parse(url) - result = File.join(dest, File.basename(uri.path)) - puts " Downloading #{url}" - IO.copy_stream(URI.open(url), result) - return result - end + class FetchRemote < Octopress::Hooks::Site + priority :highest - def self.copy(src, dest) - if (tmp = Array.try_convert(src)) - tmp.each do |s| - s = File.path(s) - yield s, File.join(dest, File.basename(s)) - end - else - src = File.path(src) - if File.directory?(dest) - yield src, File.join(dest, File.basename(src)) + def self.download(url, dest) + uri = URI.parse(url) + result = File.join(dest, File.basename(uri.path)) + puts " Downloading #{url}" + IO.copy_stream(URI.open(url), result) + return result + end + + def self.copy(src, dest) + if (tmp = Array.try_convert(src)) + tmp.each do |s| + s = File.path(s) + yield s, File.join(dest, File.basename(s)) + end else - yield src, File.path(dest) + src = File.path(src) + if File.directory?(dest) + yield src, File.join(dest, File.basename(src)) + else + yield src, File.path(dest) + end end end - end - class FetchRemote < Octopress::Hooks::Site def pre_read(site) beginning_time = Time.now puts "Starting plugin fetch_remote.rb..." site.config['fetch-remote'].each do |entry| puts " Repo #{entry['repo']} (#{entry['ref']})" Dir.mktmpdir do |tmpdir| - tmpfile = Jekyll.download("#{entry['repo']}/archive/#{entry['ref']}.zip", tmpdir) + tmpfile = FetchRemote.download("#{entry['repo']}/archive/#{entry['ref']}.zip", tmpdir) Dir.mktmpdir do |ztmpdir| puts " Extracting #{tmpfile}" Archive::Zip.extract( @@ -46,7 +48,15 @@ module Jekyll :create => true ) entry['paths'].each do |path| - FileUtils.mkdir_p path['dest'] + if File.extname(path['dest']) != "" + if path['src'].size > 1 + raise "Cannot use file destination #{path['dest']} with multiple sources" + end + FileUtils.mkdir_p File.dirname(path['dest']) + else + FileUtils.mkdir_p path['dest'] + end + puts " Copying files" # prepare file list to be copied @@ -60,7 +70,7 @@ module Jekyll end files.each do |file| - Jekyll.copy(file, path['dest']) do |s, d| + FetchRemote.copy(file, path['dest']) do |s, d| s = File.realpath(s) # traverse source directory FileUtils::Entry_.new(s, nil, false).wrap_traverse(proc do |ent| diff --git a/_plugins/fix_urls.rb b/_plugins/fix_urls.rb index 09ac25af69..ee7d297f50 100644 --- a/_plugins/fix_urls.rb +++ b/_plugins/fix_urls.rb @@ -2,7 +2,6 @@ require 'jekyll' require 'octopress-hooks' module Jekyll - class FetchRemote < Octopress::Hooks::Site def post_read(site) beginning_time = Time.now @@ -20,5 +19,4 @@ module Jekyll Jekyll.logger.info "done in #{(end_time - beginning_time)} seconds" end end - end diff --git a/_plugins/update_api_toc.rb b/_plugins/update_api_toc.rb index c71adbc0f7..b8e6170a3c 100644 --- a/_plugins/update_api_toc.rb +++ b/_plugins/update_api_toc.rb @@ -2,7 +2,6 @@ require 'jekyll' require 'octopress-hooks' module Jekyll - class UpdateApiToc < Octopress::Hooks::Site def pre_read(site) beginning_time = Time.now @@ -22,5 +21,4 @@ module Jekyll Jekyll.logger.info "done in #{(end_time - beginning_time)} seconds" end end - end