Skip to content

Instantly share code, notes, and snippets.

@jaceju
Created November 29, 2011 16:54
Show Gist options
  • Save jaceju/1405513 to your computer and use it in GitHub Desktop.
Save jaceju/1405513 to your computer and use it in GitHub Desktop.
Aside Categories List Generator
# encoding: utf-8
#
# Aside categories list generator
#
# @author: jaceju
# @version: 0.0.1 alpha
module Jekyll
class Site
def build_categories_list
self.build_categories_template
self.build_categories_html
end
def build_categories_template
unless self.config['default_asides'].include?(file)
file = File.join("asides", "categories.html")
path = File.join("_includes", file)
self._source_write_file path, self._get_template
self.config['default_asides'][1, 0] = [file]
end
end
def _get_template
root = self.config["root"]
content = """
<section>
<h1>Categories</h1>
<ul id=\"categories\">
</ul>
</section>
<script>
$.domReady(function(){
$.ajax({
url: \"#{root}categories.html\",
method: \"get\",
error: function (err) { },
success: function (html) {
$(\"#categories\").html(html);
}
});
});
</script>
"""
end
def build_categories_html
html = ""
self.categories.keys.each do |category|
url = '/' + File.join(self.config['category_dir'], category.gsub(/_|\P{Word}/, '-').gsub(/-{2,}/, '-').downcase)
html += "<li><a href=\"" + url + "\">" + category + "</a></li>\n"
end
self._source_write_file "categories.html", html
end
def _source_write_file(path, content)
path = File.join(self.source, path);
FileUtils.mkdir_p(File.dirname(path))
File.open(path, 'w') do |f|
f.write(html)
end
end
end
class AsideCategories < Generator
safe true
priority :low
def generate(site)
site.build_categories_list
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment