Hey S,
I think you’re well on the way to a solution. The `system()` call needs one argument, so you’ll want to join all the pieces into a single command using + (rather than comma which you’re using now). Maybe that already makes it work?
I’ll post the solution that I use for my own web site. Maybe it can inspire you! It is not the prettiest code, but it works, and that is the most important thing.
First is a filter that generates HTML pages from which to generate the images:
Nanoc::Filter.define(:card_html) do |content, _params = {}|
content = @item.compiled_content(snapshot: :last)
doc = Nokogiri::HTML(content)
doc.search('body').each do |body|
body['class'] = 'minimal ' + body['class']
end
title = doc.at_css('title')
title.add_next_sibling('<meta name="robots" content="noindex">')
doc.to_html
end
Then, a `card_png` filter, which creates a PNG by using Chrome’s screenshot functionality:
require 'tempfile'
Class.new(Nanoc::Filter) do
identifier :card_png
type :text => :binary
def run(content, _params = {})
# Ensure all assets exist (explicitly generate dependencies, because Chrome won’t know)
@items.find_all('/assets/fonts/**').each { _1.reps[:default].raw_path(snapshot: :last) }
Tempfile.create(['screenshot', '.png']) do |tmpfile|
system(
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
'--headless',
'--screenshot=' + tmpfile.path,
'--disable-gpu',
'--hide-scrollbars',
'--window-size=1200,630',
@item.reps[:card_html].raw_path(snapshot: :last)
)
FileUtils.mv(tmpfile.path, output_filename)
end
end
end
In the Rules file, it looks like this:
compile '/articles/*.{md,md.erb,dmark}', rep: :card_html do
filter :card_html
filter :relativize_paths, type: :html
write item.identifier.without_exts + '/card.html'
end
compile '/articles/*.{md,md.erb,dmark}', rep: :card_png do
filter :card_png
write item.identifier.without_exts + '/card.png'
end
It is not perfect — it leaves behind a `…/card.html` file (hidden from search engines though). I could fix that by not writing the `…/card.html` file at all, but write it to a temporary location (using Tempfile) in the `card_png` filter. I’ve not gotten around to fixing that, but it’s not very important.
Hope this helps,
Denis Defreyne
[dəˈni] • pronouns: he/him