aboutsummaryrefslogtreecommitdiff
path: root/_plugins/generate-media-permalink.rb
diff options
context:
space:
mode:
Diffstat (limited to '_plugins/generate-media-permalink.rb')
-rw-r--r--_plugins/generate-media-permalink.rb61
1 files changed, 61 insertions, 0 deletions
diff --git a/_plugins/generate-media-permalink.rb b/_plugins/generate-media-permalink.rb
new file mode 100644
index 0000000..db49286
--- /dev/null
+++ b/_plugins/generate-media-permalink.rb
@@ -0,0 +1,61 @@
+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