Module: Jekyll::OctopodFilters

Defined in:
lib/jekyll/octopod_filters.rb

Constant Summary collapse

JSON_ENTITIES =
{ '&' => '\u0026', '>' => '\u003E', '<' => '\u003C', "'" => '\u0027' }

Instance Method Summary collapse

Instance Method Details

#audio(hsh, key = nil) ⇒ Object

Returns the audio file name of a given hash. Is no key as second parameter given, it trys first “mp3”, than “m4a” and than it will return a more or less random value.

{{ post.audio | audio:"m4a" }} => "my-episode.m4a"


57
58
59
60
61
62
63
64
65
# File 'lib/jekyll/octopod_filters.rb', line 57

def audio(hsh, key = nil)
  if !hsh.nil? && hsh.length
    if key.nil?
      hsh['mp3'] ? hsh['mp3'] : hsh['m4a'] ? hsh['m4a'] : hsh['ogg'] ? hsh['ogg'] : hsh['opus'] ? hsh['opus'] : hsh.values.first
    else
      hsh[key]
    end
  end
end

#audio_type(hsh) ⇒ Object

Returns the audio-type of a given hash. Is no key as second parameter given, it trys first “mp3”, than “m4a” and than it will return a more or less random value.

{{ post.audio | audiotype }} => "my-episode.m4a"


73
74
75
76
77
# File 'lib/jekyll/octopod_filters.rb', line 73

def audio_type(hsh)
  if !hsh.nil? && hsh.length
    hsh['mp3'] ? mime_type('mp3') : hsh['m4a'] ? mime_type('m4a') : hsh['ogg'] ? mime_type('ogg') : mime_type('opus')
  end
end

#cdata_escape(input) ⇒ Object

Escapes some text for CDATA



9
10
11
# File 'lib/jekyll/octopod_filters.rb', line 9

def cdata_escape(input)
  input.gsub(/<!\[CDATA\[/, '&lt;![CDATA[').gsub(/\]\]>/, ']]&gt;')
end

#disqus_config(site, page = nil) ⇒ Object

Generates the config for disqus integration If a page object is given, it generates the config variables only for this page. Otherwise it generate only the global config variables.

{{ site | disqus_config }}
{{ site | disqus_config:page }}


203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/jekyll/octopod_filters.rb', line 203

def disqus_config(site, page = nil)
  if page
    disqus_vars = {
      'disqus_identifier'  => page['url'],
      'disqus_url'         => "#{site['url']}#{page['url']}",
      'disqus_category_id' => page['disqus_category_id'] || site['disqus_category_id'],
      'disqus_title'       => j(page['title'] || site['site'])
    }
  else
    disqus_vars = {
      'disqus_developer'   => site['disqus_developer'],
      'disqus_shortname'   => site['disqus_shortname']
    }
  end

  disqus_vars.delete_if { |_, v| v.nil? }
  disqus_vars.map { |k, v| "var #{k} = '#{v}';" }.compact.join("\n")
end

#download_url_with_fallback(site) ⇒ Object

Returns the dowrload url with fallback to the site’s episode folder url



125
126
127
128
129
130
131
# File 'lib/jekyll/octopod_filters.rb', line 125

def download_url_with_fallback(site)
  if site["download_url"] == "" or site["download_url"] == nil
    site["url"] + "/episodes"
  else
    site["download_url"]
  end
end

#episode_feeds(site, except = nil) ⇒ Object

Returns an array of all episode feeds named by the convetion ‘episodes.<episode_file_format>.rss’ within the root directory. Also it contains all additional feeds specified by ‘additional_feeds’ in the ‘_config.yml’. If an ‘episode_file_format’ or key of ‘additional_feeds’ equals the optional parameter ‘except’, it will be skipped.

episode_feeds(site, except = nil) =>
[
  ["m4a Episode RSS-Feed", "/episodes.m4a.rss"],
  ["mp3 Episode RSS-Feed", "/episodes.mp3.rss"],
  ["Torrent Feed m4a", "http://bitlove.org/octopod/octopod_m4a/feed"],
  ["Torrent Feed mp3", "http://bitlove.org/octopod/octopod_mp3/feed"]
]


286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/jekyll/octopod_filters.rb', line 286

def episode_feeds(site, except = nil)
  feeds = []

  if site['episode_feed_formats']
    site['episode_feed_formats'].map { |f|
     feeds << ["#{f} Episode RSS-Feed", "#{site['url']}/episodes.#{f}.rss"] unless f == except
    }
  end
  if site['additional_feeds']
    site['additional_feeds'].each { |k, v|
      feeds << [k.gsub('_', ' '), v] unless k == except
    }
  end

  feeds
end

#episode_feeds_html(site, except = nil) ⇒ Object

Returns HTML links to all episode feeds named by the convetion ‘episodes.<episode_file_format>.rss’ within the root directory. Also it returns all additional feeds specified by ‘additional_feeds’ in the ‘_config.yml’. If an ‘episode_file_format’ or key of ‘additional_feeds’ equals the optional parameter ‘except’, it will be skipped.

{{ site | episode_feeds_html:'m4a' }} =>
<link rel="alternate" type="application/rss+xml" title="mp3 Episode RSS-Feed" href="/episodes.mp3.rss" />
<link rel="alternate" type="application/rss+xml" title="Torrent Feed m4a" href="http://bitlove.org/octopod/octopod_m4a/feed" />
<link rel="alternate" type="application/rss+xml" title="Torrent Feed mp3" href="http://bitlove.org/octopod/octopod_mp3/feed" />


313
314
315
316
317
# File 'lib/jekyll/octopod_filters.rb', line 313

def episode_feeds_html(site, except = nil)
  episode_feeds(site, except).map { |f|
    %Q{<link rel="alternate" type="application/rss+xml" title="#{f.first || f.last}" href="#{f.last}" />}
  }.join("\n")
end

#episode_feeds_rss(site, except = nil) ⇒ Object

Returns RSS-XML links to all episode feeds named by the convetion ‘episodes.<episode_file_format>.rss’ within the root directory. Also it returns all additional feeds specified by ‘additional_feeds’ in the ‘_config.yml’. If an ‘episode_file_format’ or key of ‘additional_feeds’ equals the optional parameter ‘except’, it will be skipped.

{{ site | episode_feeds_rss:'m4a' }} =>
<atom:link rel="alternate" href="/episodes.mp3.rss" type="application/rss+xml" title="mp3 Episode RSS-Feed"/>
<atom:link rel="alternate" href="http://bitlove.org/octopod/octopod_m4a/feed" type="application/rss+xml" title="Torrent Feed m4a"/>
<atom:link rel="alternate" href="http://bitlove.org/octopod/octopod_mp3/feed" type="application/rss+xml" title="Torrent Feed mp3"/>


329
330
331
332
333
# File 'lib/jekyll/octopod_filters.rb', line 329

def episode_feeds_rss(site, except = nil)
  episode_feeds(site, except).map { |f|
    %Q{<atom:link rel="alternate" href="#{f.last}" type="application/rss+xml" title="#{f.first || f.last}"/>}
  }.join("\n")
end

#expand_urls(input, url = '') ⇒ Object

Replaces relative urls with full urls

{{ "about.html" | expand_urls }}             => "/about.html"
{{ "about.html" | expand_urls:site.url }}  => "http://example.com/about.html"


24
25
26
27
28
29
# File 'lib/jekyll/octopod_filters.rb', line 24

def expand_urls(input, url='')
  url ||= '/'
  input.gsub /(\s+(href|src)\s*=\s*["|']{1})(\/[^\"'>]*)/ do
    $1+url+$3
  end
end

#file_size(path, rel = nil) ⇒ Object

Returns the size of a given file in bytes. If there is just a filename without a path, this method assumes that the file is an episode audio file which lives in /episodes.

{{ "example.m4a" | file_size }} => 4242


99
100
101
102
103
104
# File 'lib/jekyll/octopod_filters.rb', line 99

def file_size(path, rel = nil)
  return 0 if path.nil?
  path = path =~ /\// ? path : File.join('episodes', path)
  path = rel + path if rel
  File.size(path)
end

#host_from_url(url) ⇒ Object

Returns the host a given url

{{ 'https://github.com/pattex/octopod' | host_from_url }} => "github.com"


193
194
195
# File 'lib/jekyll/octopod_filters.rb', line 193

def host_from_url(url)
  URI.parse(url).host
end

#image_with_fallback(page) ⇒ Object

Returns the image of the post or the default logo.

{{ page | image_with_fallback }} => '/path/to/image.png'


116
117
118
119
120
121
122
# File 'lib/jekyll/octopod_filters.rb', line 116

def image_with_fallback(page)
  if page["image"]
    "/img/" + page["image"]
  else
    "/img/logo-itunes.jpg"
  end
end

#j(str) ⇒ Object

Escapes HTML entities in JSON strings. More or less a copy of the equivalent method in Active Support. github.com/rails/rails/tree/master/activesupport



16
17
18
# File 'lib/jekyll/octopod_filters.rb', line 16

def j(str)
  str.to_s.gsub(/[&"><']/) { |e| JSON_ENTITIES[e] }
end

#mime_type(format) ⇒ Object

Returns the MIME-Type of a given file format.

{{ "m4a" | mime_type }} => "audio/mp4a-latm"


83
84
85
86
87
88
89
90
91
92
# File 'lib/jekyll/octopod_filters.rb', line 83

def mime_type(format)
  types = {
    'mp3'  => 'mpeg',
    'm4a'  => 'mp4a-latm',
    'ogg'  => 'ogg; codecs=vorbis',
    'opus' => 'ogg; codecs=opus'
  }

  "audio/#{types[format]}"
end

Returns a, ready to use, navigation list of all pages that have navigation set in their YAML front matter. The list is sorted by the value of navigation.

{{ site | navigation_list:page }}


237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/jekyll/octopod_filters.rb', line 237

def navigation_list(site, page)
  pages = site['pages'].select { |p|
    p.data['navigation'] && p.data['title']
  }.sort_by { |p| p.data['navigation'] }

  list = []
  list << pages.map { |p|
    active = (p.url == page['url']) || (page.key?('next') && File.join(p.dir, p.basename) == '/index')
    navigation_list_item(File.join(site['url'], p.url), p.data['title'], active)
  }
  list.join("\n")
end


268
269
270
271
# File 'lib/jekyll/octopod_filters.rb', line 268

def navigation_list_item(url, title, active = false)
  a_class = active ? ' class="active"' : ''
  %Q{<li#{a_class}><a #{a_class} href="#{url}">#{title}</a></li>}
end

#otherwise(first, second) ⇒ Object

Returns the first argument if it’s not nil or empty otherwise it returns the second one.

{{ post.author | otherwise:site.author }}


47
48
49
50
# File 'lib/jekyll/octopod_filters.rb', line 47

def otherwise(first, second)
  first = first.to_s
  first.empty? ? second : first
end

#remove_script_and_audio(input) ⇒ Object

Removes scripts tag and audio tags in multiline moderator



32
33
34
# File 'lib/jekyll/octopod_filters.rb', line 32

def remove_script_and_audio(input)
  input.gsub(/<audio.*audio>/m, '').gsub(/<script.*script>/m, '')
end

#sha1(str, lenght = 8) ⇒ Object

Returns the hex-encoded hash value of a given string. The optional second argument defines the length of the returned string.

{{ "Octopod" | sha1 }} => "8b20a59c"
{{ "Octopod" | sha1:23 }} => "8b20a59c8e2dcb5e1f845ba"


227
228
229
230
# File 'lib/jekyll/octopod_filters.rb', line 227

def sha1(str, lenght = 8)
  sha1 = Digest::SHA1.hexdigest(str)
  sha1[0, lenght.to_i]
end

#slug(page) ⇒ Object

Returns a slug based on the id of a given page.

{{ page | slug }} => '2012_10_02_octopod'


109
110
111
# File 'lib/jekyll/octopod_filters.rb', line 109

def slug(page)
  page['id'][1..-1].gsub('/', '_')
end

#split_chapter(chapter_str, attribute = nil) ⇒ Object

Splits a chapter, like it is written to the post YAML front matter into the components ‘start’ which refers to a single point in time relative to the beginning of the media file nad ‘title’ which defines the text to be the title of the chapter.

{{ '00:00:00.000 Welcome to Octopod!' | split_chapter }}
  => { 'start' => '00:00:00.000', 'title' => 'Welcome to Octopod!' }

{{ '00:00:00.000 Welcome to Octopod!' | split_chapter:'title' }}
  => 'Welcome to Octopod!'

{{ '00:00:00.000 Welcome to Octopod!' | split_chapter:'start' }}
  => '00:00:00.000'


147
148
149
150
151
152
153
154
155
156
# File 'lib/jekyll/octopod_filters.rb', line 147

def split_chapter(chapter_str, attribute = nil)
  attributes = chapter_str.split(/ /, 2)
  return nil unless attributes.first.match(/\A(\d|:|\.)+\z/)

  if attribute.nil?
    { 'start' => attributes.first, 'title' => attributes.last }
  else
    attribute == 'start' ? attributes.first : attributes.last
  end
end

#string_of_duration(duration) ⇒ Object

Gets a number of seconds and returns an human readable duration string of it.

{{ 1252251 | string_of_duration }} => "00:03:13"


163
164
165
166
167
168
169
# File 'lib/jekyll/octopod_filters.rb', line 163

def string_of_duration(duration)
  seconds = duration.to_i
  minutes = seconds / 60
  hours   = minutes / 60

  "#{"%02d" % hours}:#{"%02d" % (minutes % 60)}:#{"%02d" % (seconds % 60)}"
end

#string_of_size(bytes) ⇒ Object

Gets a number of bytes and returns an human readable string of it.

{{ 1252251 | string_of_size }} => "1.19M"


174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/jekyll/octopod_filters.rb', line 174

def string_of_size(bytes)
  bytes = bytes.to_i.to_f
  out = '0'
  return out if bytes == 0.0

  jedec = %w[b K M G]
  [3, 2, 1, 0].each { |i|
    if bytes > 1024 ** i
      out = "%.1f#{jedec[i]}" % (bytes / 1024 ** i)
      break
    end
  }

  return out
end

#talk_list(site, page) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/jekyll/octopod_filters.rb', line 250

def talk_list(site, page)
  pages = site['pages'].select { |p|
    p.data['talk'] && p.data['title']
  }.sort_by { |p| p.data['talk'] }

  list =  ['<li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false"> Talks <span class="caret"></span>
            </a><ul class="dropdown-menu">']
  list << pages.map { |p|
    active = (p.url == page['url']) || (page.key?('next') && File.join(p.dir, p.basename) == '/index')
    navigation_list_item(File.join(site['url'], p.url), p.data['title'], active)
  }
  list << ['</ul></li>']

  if pages.any?
    list.join("\n")
  end
end

#time_to_rssschema(time) ⇒ Object

Formats a Time to be RSS compatible like “Wed, 15 Jun 2005 19:00:00 GMT”

{{ site.time | time_to_rssschema }}


39
40
41
# File 'lib/jekyll/octopod_filters.rb', line 39

def time_to_rssschema(time)
  time.strftime("%a, %d %b %Y %H:%M:%S %z")
end

#version_string(site) ⇒ Object

Prints out current version



336
337
338
# File 'lib/jekyll/octopod_filters.rb', line 336

def version_string(site)
  Jekyll::Octopod::VERSION::STRING
end