我想让Jekyll为每个页面和帖子创建一个HTML文件和一个JSON文件.这是为了提供我的Jekyll博客的JSON API – 例如可以在/posts/2012/01/01/my-post.html或/posts/2012/01/01/my-post.json访问帖子
解决方法
我也在寻找类似的东西,所以我学到了一些ruby并制作了一个脚本,可以生成Jekyll博客文章的JSON表示.我还在努力,但大部分都在那里.
我把它和Gruntjs,Sass,Backbonejs,Requirejs和Coffeescript放在一起.如果你愿意,你可以看看my jekyll-backbone project on Github.
# encoding: utf-8 # # Title: # ====== # Jekyll to JSON Generator # # Description: # ============ # A plugin for generating JSON representations of your # site content for easy use with JS MVC frameworks like Backbone. # # Author: # ====== # Jezen Thomas # jezenthomas@gmail.com # http://jezenthomas.com module Jekyll require 'json' class JSONGenerator < Generator safe true priority :low def generate(site) # Converter for .md > .html converter = site.getConverterImpl(Jekyll::Converters::Markdown) # Iterate over all posts site.posts.each do |post| # Encode the HTML to JSON hash = { "content" => converter.convert(post.content)} title = post.title.downcase.tr(' ','-').delete("’!") # Start building the path path = "_site/dist/" # Add categories to path if they exist if (post.data['categories'].class == String) path << post.data['categories'].tr(' ','/') elsif (post.data['categories'].class == Array) path << post.data['categories'].join('/') end # Add the sanitized post title to complete the path path << "/#{title}" # Create the directories from the path FileUtils.mkpath(path) unless File.exists?(path) # Create the JSON file and inject the data f = File.new("#{path}/raw.json","w+") f.puts JSON.generate(hash) end end end end