Module: Jekyll::FlattrFilters

Defined in:
lib/jekyll/flattr_filters.rb

Instance Method Summary collapse

Instance Method Details

#flattr_atom(site, page = nil) ⇒ Object

Returns a ATOM payment link.

{{ site | flattr_atom:post }}


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

def flattr_atom(site, page = nil)
  return if site['flattr_uid'].nil?
  link =  '<link rel="payment" href="https://flattr.com/submit/auto?'
  link << %Q{#{flattr_feed_options(site, page)}" type="text/html" />}
end

#flattr_button(site, page = nil) ⇒ Object

Returns a flattr button

{{ site | flattr_button:page }}


20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/jekyll/flattr_filters.rb', line 20

def flattr_button(site, page = nil)
  return if site['flattr_uid'].nil?

  keys = %w[url title description uid popout button category language tags]
  options = flattr_options(site, page, keys)

  button =  '<a class="FlattrButton" style="display:none;" '
  button << %Q{title="#{options.delete('title')}" href="#{options.delete('url')}" }
  button << options.map { |k, v|
    %Q{data-flattr-#{k}="#{v}"} unless k == 'description'
  }.join(' ')
  button << ">\n\n#{options['description'].gsub(/<\/?[^>]*>/, "")}\n</a>"
end

#flattr_feed_options(site, page) ⇒ Object



97
98
99
100
101
102
# File 'lib/jekyll/flattr_filters.rb', line 97

def flattr_feed_options(site, page)
  keys = %w[url uid]
  options = flattr_options(site, page, keys).map { |k, v|
    "#{k == 'uid' ? 'user_id' : k}=#{ERB::Util.url_encode(v)}"
  }.join('&amp;')
end

#flattr_loader_options(site) ⇒ Object

Generates the query string part for the flattr load.js from the configurations in _config.yml

{{ site | flattr_loader_options }}


9
10
11
12
13
14
15
# File 'lib/jekyll/flattr_filters.rb', line 9

def flattr_loader_options(site)
  return if site['flattr_uid'].nil?
  keys = %w[mode https popout uid button language category]
  options = flattr_options(site, nil, keys).delete_if { |_, v| v.to_s.empty? }

  options.map { |k, v| "#{k}=#{ERB::Util.url_encode(v)}" }.join('&')
end

#flattr_options(site, page, keys) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/jekyll/flattr_filters.rb', line 69

def flattr_options(site, page, keys)
  page = {} if page.nil?
  site = flattrize(site)
  page = flattrize(page)
  options = {}

  keys.each { |k|
    case k
    when 'https'
      options[k] = 1
    when 'url'
      options[k] = "#{site['url']}#{page['url']}"
    when 'description'
      options[k] = page['content'] || site['description'] || site['title']
    when 'category'
      options[k] = page['category'] || site['category'] || 'audio'
    when 'language'
      options[k] = page['language'] || site['language'] || 'en_GB'
    when 'tags'
      options[k] = page['tags'].join(', ') if page['tags']
    else
      options[k] = page[k] || site[k]
    end
   }

   options
end

#flattr_rss(site, page = nil) ⇒ Object

Returns a RSS payment link.

{{ site | flattr_rss:post }}


37
38
39
40
41
# File 'lib/jekyll/flattr_filters.rb', line 37

def flattr_rss(site, page = nil)
  return if site['flattr_uid'].nil?
  link =  '<atom:link rel="payment" href="https://flattr.com/submit/auto?'
  link << %Q{#{flattr_feed_options(site, page)}" type="text/html" />}
end

#flattrize(hsh) ⇒ Object

Removes all leading “flattr_” from the keys of the given hash.

flattrize({ 'octopod' => 'awesome', 'flattr_uid' => 'pattex' })
=> { "octopod" => "awesome", "uid" => "pattex" }


56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/jekyll/flattr_filters.rb', line 56

def flattrize(hsh)
  config = {}
  hsh.to_hash.each { |k, v|
    if new_key = k.to_s.match(/\Aflattr_(.*)\z/)
      config[new_key[1]] = v
    else
      config[k] = v
    end
  }

  config
end