ruby – Jekyll – 在HTML文件旁边生成JSON文件
发布时间:2020-12-16 23:34:52 所属栏目:百科 来源:网络整理
导读:我想让Jekyll为每个页面和帖子创建一个HTML文件和一个JSON文件.这是为了提供我的Jekyll博客的JSON API – 例如可以在/posts/2012/01/01/my-post.html或/posts/2012/01/01/my-post.json访问帖子 有谁知道是否有一个Jekyll插件,或者我将如何开始编写这样的插件
我想让Jekyll为每个页面和帖子创建一个HTML文件和一个JSON文件.这是为了提供我的Jekyll博客的JSON API – 例如可以在/posts/2012/01/01/my-post.html或/posts/2012/01/01/my-post.json访问帖子
有谁知道是否有一个Jekyll插件,或者我将如何开始编写这样的插件,并排生成两组文件? 解决方法
我也在寻找类似的东西,所以我学到了一些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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |