Jekyll: Writing a Jekyll::Generator
There is annoying little documentation about writing a generator for Jekyll, particularly since when searching for examples, there is so much noise in form of hits for "static site generator." Two decent examples are from Ricardo Lopes and Starr Horne of Honeybadger.
Starting at the beginning, you want to be putting my_generator.rb
in _plugins/
in the root directory of your
Jekyll site
directory.
For an absolute
"Hello, World" we can do:
class Generator < Jekyll::Generator
def generate(site)
puts "Hello, World!"
end
end
bundle exec jekyll serve
. Look for it
after Generating...
Let's just print out some file types
class Generator < Jekyll::Generator
def generate(site)
site.pages.each do |page|
print "title: ", page.data["title"], "\n"
print "file: ", page.path, "\n"
end
end
end
Using Site
Variables, we have a selection of file types we can
have a look at:
site.posts.each do |page|
print "title: ", page.data["title"], "\n"
print "file: ", page.path, "\n"
end
site.static_files.each do |page|
print "name: ", page.name, "\n"
print "file: ", page.path, "\n"
end
site.documents.each do |page|
print "title: ", page.data["title"], "\n"
print "file: ", page.path, "\n"
end
html_pages
and html_files
don't seem to
work in this context...?