Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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