Skip to content

Instantly share code, notes, and snippets.

@nompute
Created March 11, 2013 00:44
Show Gist options
  • Save nompute/5131217 to your computer and use it in GitHub Desktop.
Save nompute/5131217 to your computer and use it in GitHub Desktop.
A quick-and-dirty webcomic generator plugin for Jekyll.
module Jekyll
class ComicPage < Page
def initialize(site, base, comic, img_path, img_file, total)
@site = site
@base = base
@dir = "/comics/#{comic}"
@comic = comic
@page_num = File.basename(img_file, File.extname(img_file))
@total = total
@name = @page_num + '.html'
puts "Generating #{@comic} page #{@page_num}"
self.process(@name)
self.data ||= Hash.new
self.data['src'] = "/#{img_path.chomp('/')}/#{img_file}"
self.data['next'] = next_page
self.data['prev'] = prev_page
self.data['num'] = @page_num
self.data['last'] = last_page
self.data['first'] = first_page
self.read_yaml(File.join(base, '_layouts'), comic + '.html')
end
def next_page
return nil if @page_num.to_i >= (@total - 1)
return @dir + '/' + pad(@page_num.to_i + 1) + '.html'
end
def prev_page
return nil if @page_num.to_i == 0
return @dir + '/' + pad(@page_num.to_i - 1) + '.html'
end
def last_page
return @dir + '/last.html'
end
def first_page
return @dir + '/' + pad(0) + '.html'
end
def pad(i)
width = @page_num.length
return i.to_s.rjust(width, '0')
end
end
class ComicLastPage < ComicPage
def initialize(site, base, comic, img_path, img_file, total)
super
@name = 'last.html'
self.process(@name)
end
end
class ComicIndexPage < Page
def initialize(site, base, comic, images)
@site = site
@base = base
@dir = "/comics/#{comic}"
@comic = comic
@name = 'index.html'
self.data ||= Hash.new
self.data['images'] = images.collect{ |img| File.basename(img, File.extname(img)) }
self.process(@name)
self.read_yaml(File.join(base, '_layouts'), "#{comic}_index.html")
end
end
class GenerateComics < Generator
safe true
priority :normal
def generate(site)
return unless Dir.exists?('_comics')
Dir.glob('_comics/*.yml').each do |file|
comic = File.basename(file, '.yml')
config = YAML.load_file(file)
self.write_comic(site, comic, config['images'])
end
end
def write_comic(site, comic, img_path)
images = Dir.glob("#{img_path}/*.{jpg,png,jpeg,gif,bmp}").select { |f| File.basename(f) =~ /(\d)+\.(jpg|png|jpeg|gif|bmp)/ }
images.each do |file|
c = ComicPage.new(site, site.source, comic, img_path, File.basename(file), images.length)
c.render(site.layouts, site.site_payload)
c.write(site.dest)
site.pages << c
site.static_files << c
end
l = ComicLastPage.new(site, site.source, comic, img_path, File.basename(images[-1]), images.length)
l.render(site.layouts, site.site_payload)
site.pages << l
site.static_files << l
i = ComicIndexPage.new(site, site.source, comic, images)
i.render(site.layouts, site.site_payload)
site.pages << i
site.static_files << i
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment