Improve catpage_for.rb

Allow to filter pages with many pages variables
Allow to filter pages using dynamic variables as values
This commit is contained in:
Saivann 2013-06-21 21:49:20 -04:00
parent 96f7a4c727
commit 33dc7127bf
5 changed files with 28 additions and 20 deletions

View file

@ -1,6 +1,8 @@
#catpage_for allows to loop in site.pages sorted by custom page #filter_for allows to loop in site.pages sorted and filtered
#variables and filtered by page.category. #by custom page variables. Example :
#Example : {% catpage_for p in site.pages sort_by:date category:release %} #{% filter_for p in site.pages sort_by:date category:release lang:{{page.lang}} %}
# ..
#{% endfilter_for %}
module Jekyll module Jekyll
module SortedForImpl module SortedForImpl
@ -8,17 +10,23 @@ module Jekyll
sorted_collection = collection_to_sort context sorted_collection = collection_to_sort context
return if sorted_collection.empty? return if sorted_collection.empty?
sort_attr = @attributes['sort_by'] sort_attr = @attributes['sort_by']
category_attr = @attributes['category'] @attributes.delete('sort_by')
#filter page by given category #filter page by given attributes
s = [] s = []
for x in sorted_collection for x in sorted_collection
if x.to_liquid.has_key?('category') && x.to_liquid['category'] == category_attr catch :root do
@attributes.each do |at,atval|
atval = Liquid::Template.parse(atval).render context
throw :root unless x.to_liquid.has_key?(at) and x.to_liquid[at] == atval
end
s.push(x) s.push(x)
end end
end end
sorted_collection = s sorted_collection = s
#sort collection by given variable #sort collection by given variable
sorted_collection = sorted_collection.sort_by { |i| i.to_liquid[sort_attr] } if sorted_collection.length > 1
sorted_collection = sorted_collection.sort_by { |i| i.to_liquid[sort_attr] }
end
#return modified array #return modified array
original_name = @collection_name original_name = @collection_name
result = nil result = nil
@ -41,9 +49,9 @@ module Jekyll
end end
def end_tag def end_tag
'endcatpage_for' 'endfilter_for'
end end
end end
end end
Liquid::Template.register_tag('catpage_for', Jekyll::SortedForTag) Liquid::Template.register_tag('filter_for', Jekyll::SortedForTag)

View file

@ -7,25 +7,25 @@ title: Network status and alerts - Bitcoin
<link rel="alternate" type="application/rss+xml" href="/en/rss/alerts.rss" title="Bitcoin network status and alerts"> <link rel="alternate" type="application/rss+xml" href="/en/rss/alerts.rss" title="Bitcoin network status and alerts">
<div class="versiontext"> <div class="versiontext">
<h1>Network status and alerts<a type="application/rss+xml" href="/en/rss/alerts.rss"><img src="/img/icon_rss.svg" alt="rss" class="rssicon"></a></h1> <h1>Network status and alerts<a type="application/rss+xml" href="/en/rss/alerts.rss"><img src="/img/icon_rss.svg" alt="rss" class="rssicon"></a></h1>
{% assign active = false %}{% catpage_for p in site.pages reversed sort_by:date category:alert %}{% if p.active == true %}{% assign active = true %}{% endif %}{% endcatpage_for %} {% assign active = false %}{% filter_for p in site.pages reversed sort_by:date category:alert %}{% if p.active == true %}{% assign active = true %}{% endif %}{% endfilter_for %}
{% if active == false %} {% if active == false %}
<div class="alertstatusinactive">There is no ongoing event on the Bitcoin network.</div> <div class="alertstatusinactive">There is no ongoing event on the Bitcoin network.</div>
{% else %} {% else %}
<div class="alertstatusactive"">The following network event is ongoing.</div> <div class="alertstatusactive"">The following network event is ongoing.</div>
<ul class="alertsactive"> <ul class="alertsactive">
{% catpage_for p in site.pages reversed sort_by:date category:alert %}{% if p.active == true %} {% filter_for p in site.pages reversed sort_by:date category:alert %}{% if p.active == true %}
<li> <li>
{{ p.date | date:"%Y-%m-%d" }} - <a href="{{ p.url | replace:'.html','' }}">{{ p.title }}</a> {{ p.date | date:"%Y-%m-%d" }} - <a href="{{ p.url | replace:'.html','' }}">{{ p.title }}</a>
</li> </li>
{% endif %}{% endcatpage_for %} {% endif %}{% endfilter_for %}
</ul> </ul>
{% endif %} {% endif %}
<ul class="alertsinactive"> <ul class="alertsinactive">
{% catpage_for p in site.pages reversed sort_by:date category:alert %}{% if p.active == false %} {% filter_for p in site.pages reversed sort_by:date category:alert %}{% if p.active == false %}
<li> <li>
{{ p.date | date:"%Y-%m-%d" }} - <a href="{{ p.url | replace:'.html','' }}">{{ p.title }}</a> {{ p.date | date:"%Y-%m-%d" }} - <a href="{{ p.url | replace:'.html','' }}">{{ p.title }}</a>
</li> </li>
{% endif %}{% endcatpage_for %} {% endif %}{% endfilter_for %}
</ul> </ul>
<p>See the complete <a href="https://en.bitcoin.it/wiki/Common_Vulnerabilities_and_Exposures">CVE list</a>.</p> <p>See the complete <a href="https://en.bitcoin.it/wiki/Common_Vulnerabilities_and_Exposures">CVE list</a>.</p>
</div> </div>

View file

@ -10,12 +10,12 @@
<url>http://bitcoin.org/img/logo_rss.png</url> <url>http://bitcoin.org/img/logo_rss.png</url>
<link>http://bitcoin.org/en/alerts</link> <link>http://bitcoin.org/en/alerts</link>
</image> </image>
{% catpage_for p in site.pages reversed sort_by:date category:alert %} {% filter_for p in site.pages reversed sort_by:date category:alert %}
<item> <item>
<title>{{ p.title }}</title> <title>{{ p.title }}</title>
<link>http://bitcoin.org{{ p.url | replace:'.html','' }}</link> <link>http://bitcoin.org{{ p.url | replace:'.html','' }}</link>
<pubDate>{{ p.date | date: "%a, %d %b %Y" }}</pubDate> <pubDate>{{ p.date | date: "%a, %d %b %Y" }}</pubDate>
</item> </item>
{% endcatpage_for %} {% endfilter_for %}
</channel> </channel>
</rss> </rss>

View file

@ -10,12 +10,12 @@
<url>http://bitcoin.org/img/logo_rss.png</url> <url>http://bitcoin.org/img/logo_rss.png</url>
<link>http://bitcoin.org/en/version-history</link> <link>http://bitcoin.org/en/version-history</link>
</image> </image>
{% catpage_for p in site.pages reversed sort_by:date category:release %} {% filter_for p in site.pages reversed sort_by:date category:release %}
<item> <item>
<title>{{ p.title }}</title> <title>{{ p.title }}</title>
<link>http://bitcoin.org{{ p.url | replace:'.html','' }}</link> <link>http://bitcoin.org{{ p.url | replace:'.html','' }}</link>
<pubDate>{{ p.date | date: "%a, %d %b %Y" }}</pubDate> <pubDate>{{ p.date | date: "%a, %d %b %Y" }}</pubDate>
</item> </item>
{% endcatpage_for %} {% endfilter_for %}
</channel> </channel>
</rss> </rss>

View file

@ -8,10 +8,10 @@ title: Bitcoin-Qt version history
<div class="versiontext"> <div class="versiontext">
<h1>Bitcoin-Qt version history<a type="application/rss+xml" href="/en/rss/releases.rss"><img src="/img/icon_rss.svg" alt="rss" class="rssicon"></a></h1> <h1>Bitcoin-Qt version history<a type="application/rss+xml" href="/en/rss/releases.rss"><img src="/img/icon_rss.svg" alt="rss" class="rssicon"></a></h1>
<ul> <ul>
{% catpage_for p in site.pages reversed sort_by:date category:release %} {% filter_for p in site.pages reversed sort_by:date category:release %}
<li> <li>
{{ p.date | date:"%Y-%m-%d" }} - <a href="{{ p.url | replace:'.html','' }}">{{ p.title }}</a> {{ p.date | date:"%Y-%m-%d" }} - <a href="{{ p.url | replace:'.html','' }}">{{ p.title }}</a>
</li> </li>
{% endcatpage_for %} {% endfilter_for %}
</ul> </ul>
</div> </div>