Module: Jekyll::OctopodFilters
- Defined in:
- lib/jekyll/octopod_filters.rb
Constant Summary collapse
- JSON_ENTITIES =
{ '&' => '\u0026', '>' => '\u003E', '<' => '\u003C', "'" => '\u0027' }
Instance Method Summary collapse
-
#audio(hsh, key = nil) ⇒ Object
Returns the value of a given hash.
-
#cdata_escape(input) ⇒ Object
Escapes some text for CDATA.
-
#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.
-
#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.
-
#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.
-
#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.
-
#expand_urls(input, url = '') ⇒ Object
Replaces relative urls with full urls.
-
#file_size(path, rel = nil) ⇒ Object
Returns the size of a given file in bytes.
-
#host_from_url(url) ⇒ Object
Returns the host a given url.
- #http_only(input) ⇒ Object
-
#j(str) ⇒ Object
Escapes HTML entities in JSON strings.
-
#mime_type(format) ⇒ Object
Returns the MIME-Type of a given file format.
-
#navigation_list(site, page) ⇒ Object
Returns a, ready to use, navigation list of all pages that have
navigation
set in their YAML front matter. - #navigation_list_item(url, title, active = false) ⇒ Object
-
#otherwise(first, second) ⇒ Object
Returns the first argument if it’s not nil or empty otherwise it returns the second one.
-
#remove_script_and_audio(input) ⇒ Object
Removes scripts tag and audio tags in multiline moderator.
-
#sha1(str, lenght = 8) ⇒ Object
Returns the hex-encoded hash value of a given string.
-
#slug(page) ⇒ Object
Returns a slug based on the id of a given page.
-
#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.
-
#string_of_duration(duration) ⇒ Object
Gets a number of seconds and returns an human readable duration string of it.
-
#string_of_size(bytes) ⇒ Object
Gets a number of bytes and returns an human readable string of it.
- #talk_list(site, page) ⇒ Object
-
#time_to_rssschema(time) ⇒ Object
Formats a Time to be RSS compatible like “Wed, 15 Jun 2005 19:00:00 GMT”.
Instance Method Details
#audio(hsh, key = nil) ⇒ Object
Returns the value 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"
61 62 63 64 65 66 67 |
# File 'lib/jekyll/octopod_filters.rb', line 61 def audio(hsh, key = nil) 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 |
#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\[/, '<![CDATA[').gsub(/\]\]>/, ']]>') 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 }}
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/jekyll/octopod_filters.rb', line 171 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 |
#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"]
]
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/jekyll/octopod_filters.rb', line 252 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" />
279 280 281 282 283 |
# File 'lib/jekyll/octopod_filters.rb', line 279 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"/>
295 296 297 298 299 |
# File 'lib/jekyll/octopod_filters.rb', line 295 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 (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
88 89 90 91 92 93 |
# File 'lib/jekyll/octopod_filters.rb', line 88 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"
161 162 163 |
# File 'lib/jekyll/octopod_filters.rb', line 161 def host_from_url(url) URI.parse(url).host end |
#http_only(input) ⇒ Object
36 37 38 |
# File 'lib/jekyll/octopod_filters.rb', line 36 def http_only(input) input.gsub(/https/,"http") 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"
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/jekyll/octopod_filters.rb', line 72 def mime_type(format) types = { 'mp3' => 'mpeg', 'm4a' => 'mp4a-latm', 'ogg' => 'ogg; codecs=vorbis', 'opus' => 'ogg; codecs=opus' } "audio/#{types[format]}" end |
#navigation_list(site, page) ⇒ Object
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 }}
205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/jekyll/octopod_filters.rb', line 205 def (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.has_key?('next') && File.join(p.dir, p.basename) == '/index') (File.join(site['url'], p.url), p.data['title'], active) } list.join("\n") end |
#navigation_list_item(url, title, active = false) ⇒ Object
234 235 236 237 |
# File 'lib/jekyll/octopod_filters.rb', line 234 def (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 }}
51 52 53 54 |
# File 'lib/jekyll/octopod_filters.rb', line 51 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"
195 196 197 198 |
# File 'lib/jekyll/octopod_filters.rb', line 195 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'
98 99 100 |
# File 'lib/jekyll/octopod_filters.rb', line 98 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'
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/jekyll/octopod_filters.rb', line 115 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"
131 132 133 134 135 136 137 |
# File 'lib/jekyll/octopod_filters.rb', line 131 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"
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/jekyll/octopod_filters.rb', line 142 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
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/jekyll/octopod_filters.rb', line 218 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.has_key?('next') && File.join(p.dir, p.basename) == '/index') (File.join(site['url'], p.url), p.data['title'], active) } list << ['</ul></li>'] list.join("\n") 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 }}
43 44 45 |
# File 'lib/jekyll/octopod_filters.rb', line 43 def time_to_rssschema(time) time.strftime("%a, %d %b %Y %H:%M:%S %z") end |