Jekyll – 与HTML文件一起生成JSON文件

我想让Jekyll为每个页面和post创建一个HTML文件和一个JSON文件。 这是为了提供我的Jekyll博客的JSON API – 例如,可以在/posts/2012/01/01/my-post.html/posts/2012/01/01/my-post.json访问/posts/2012/01/01/my-post.json

有谁知道是否有一个Jekyll插件,或者我将如何开始编写这样的插件,并排生成两组文件?

我也在寻找类似的东西,所以我学到了一些ruby并制作了一个脚本,可以生成Jekyll博客文章的JSON表示。 我还在努力,但大部分都在那里。

我把它和Gruntjs,Sass,Backbonejs,Requirejs和Coffeescript放在一起。 如果您愿意,可以在Github上查看我的jekyll-backbone项目 。

 # 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 

根据您的需要,有两种方法可以实现此目的。 如果要使用布局来完成任务,则需要使用Generator 。 您将遍历站点的每个页面并生成页面的新.json版本。 您可以选择根据site.config或页面的YAML前端中是否存在变量来生成哪些页面生成条件。 Jekyll使用生成器来处理将博客文章切换为具有每页给定数量的post的索引。

第二种方法是使用转换器 (相同的链接,向下滚动)。 转换器允许您在内容上执行任意代码,以将其转换为其他格式。 有关其工作原理的示例,请查看Jekyll附带的降价转换器 。

我认为这是一个很酷的主意!

看看JekyllBot和以下代码 。

 require 'json' module Jekyll class JSONPostGenerator < Generator safe true def generate(site) site.posts.each do |post| render_json(post,site) end site.pages.each do |page| render_json(page,site) end end def render_json(post, site) #add `json: false` to YAML to prevent JSONification if post.data.has_key? "json" and !post.data["json"] return end path = post.destination( site.source ) #only act on post/pages index in /index.html return if /\/index\.html$/.match(path).nil? #change file path path['/index.html'] = '.json' #render post using no template(s) post.render( {}, site.site_payload) #prepare output for JSON post.data["related_posts"] = related_posts(post,site) output = post.to_liquid output["next"] = output["next"].id unless output["next"].nil? output["previous"] = output["previous"].id unless output["previous"].nil? #write #todo, figure out how to overwrite post.destination #so we can just use post.write FileUtils.mkdir_p(File.dirname(path)) File.open(path, 'w') do |f| f.write(output.to_json) end end def related_posts(post, site) related = [] return related unless post.instance_of?(Post) post.related_posts(site.posts).each do |post| related.push :url => post.url, :id => post.id, :title => post.to_liquid["title"] end related end end end 

两者都应该完全符合你的要求。