Skip to content

Commit

Permalink
adding stats to the layout with file_exists plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
fginter committed Dec 21, 2015
1 parent f4c2f01 commit 1e5e8dc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions _layouts/postag.html
Expand Up @@ -6,6 +6,16 @@ <h2><code>{{ page.title }}</code>: {{ page.shortdef }}</h2>

{{ content }}


<!-- "automatic stats" -->
{% capture langdir %}{{ page.path | split:"/" | first | split:"-" | first | replace_first:'_',''}}{% endcapture %}
{% capture statspath %}stats/{{langdir}}/{{page.title}}.md{% endcapture %}
{% capture statsexist %}{% file_exists _includes/{{statspath}} %}{% endcapture %}
{% if statsexist == "true" %}
{% capture statsinclude %}{% include {{statspath}} %}{% endcapture %}
{{ statsinclude | markdownify }}
{% endif %}

<!-- "in other languages" links -->
<hr/>
{% for r in site.data.postags %}{% if r.label == page.title %}
Expand Down
20 changes: 20 additions & 0 deletions _plugins/file_exists.rb
@@ -0,0 +1,20 @@
module Jekyll
class FileExistsTag < Liquid::Tag

def initialize(tag_name, path, tokens)
super
@path = path
end

def render(context)
# ref https://gist.github.com/vanto/1455726
# pipe param through liquid to make additional replacements possible
url = Liquid::Template.parse(@path).render context

# check if file exists (returns true or false)
"#{File.exist?(url.strip!)}"
end
end
end

Liquid::Template.register_tag('file_exists', Jekyll::FileExistsTag)

0 comments on commit 1e5e8dc

Please sign in to comment.