Skip to content

Instantly share code, notes, and snippets.

@zetter
Created December 27, 2020 13:57
Show Gist options
  • Save zetter/6679b0744331803b81163475b9db66d1 to your computer and use it in GitHub Desktop.
Save zetter/6679b0744331803b81163475b9db66d1 to your computer and use it in GitHub Desktop.
Script for caching external links from a jekyll site.
require "jekyll"
require "ostruct"
require "faraday"
def data_for_file(path)
content = File.read(path)
if content =~ Jekyll::Document::YAML_FRONT_MATTER_REGEXP
content = $POSTMATCH
data = SafeYAML.load(Regexp.last_match(1))
return data
end
end
files = Dir["./source/_cookbook/*.md"]
files.each do |file|
data = data_for_file(file)
link = data["link"]
name = File.basename(file, ".*")
cache_file = "./recipe_cache/#{name}.html"
if link
if File.exists?(cache_file)
puts "#{name} already cached"
else
response = Faraday.get(link)
File.write(cache_file, response.body)
puts "#{name} cached"
end
else
puts "ERROR: #{name} has no link!"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment