aboutsummaryrefslogtreecommitdiff
path: root/_plugins/generate-media-permalink.rb
blob: ae635bd175d66fc66f2fc4336094a6a582a680f3 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
module Jekyll
  class MediaPermalinkGenerator < Generator
    safe true

    MEDIA_EXTENSION = {
      'podcasts' => 'ogg',
      'screencasts' => 'webm'
    }

    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