module Jekyll class MediaPermalinkGenerator < Generator safe true MEDIA_EXTENSION = { 'podcasts' => 'ogg', 'screencasts' => 'mkv' } def generate(site) site.collections.each do |name, collection| if ['podcasts', 'screencasts'].include? name then collection.docs.each do |document| date = document.data['date'].strftime('%Y-%m-%d') slug = document.data['slug'] extension = MEDIA_EXTENSION[name] file = "#{date}-#{slug}.#{extension}" media = "resources/#{name}/#{file}" torrent = "#{media}.torrent" media_link = document.url.gsub(/html$/, extension) torrent_link = "#{media_link}.torrent" media_page = GeneratedSymlinkResourcePage.new(site, site.source, '', media_link) torrent_page = GeneratedSymlinkResourcePage.new(site, site.source, '', torrent_link) media_page.data['source'] = media media_page.data['slug'] = slug media_page.data['extension'] = extension torrent_page.data['source'] = torrent torrent_page.data['slug'] = slug torrent_page.data['extension'] = "#{extension}.torrent" site.pages << media_page site.pages << torrent_page end end end end end class GeneratedSymlinkResourcePage < Jekyll::Page def read_yaml(*) @data ||= { "generated" => true } end def write(dest) source = @data['source'] slug = @data['slug'] extension = @data['extension'] path = destination(dest) path_dirname = File.dirname(path) FileUtils.mkdir_p(path_dirname) stdout = `ln -fs ../../../../#{source} #{path_dirname}/#{slug}.#{extension}` unless $?.success? then raise "Error when running 'ln' command: #{$?}" end unless stdout == '' then raise "Unexpected output of 'ln': #{stdout}" end end end end