Created
December 27, 2020 13:57
-
-
Save zetter/6679b0744331803b81163475b9db66d1 to your computer and use it in GitHub Desktop.
Script for caching external links from a jekyll site.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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