生成JSON文件旁边的HTML文件

生成JSON文件旁边的HTML文件

本文介绍了杰基尔 - 生成JSON文件旁边的HTML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打杰基尔创建一个HTML文件,并为每个页面和后一个JSON文件。这是我提供杰基尔博客的JSON API - 例如一个帖子可以被访问或者在 /posts/2012/01/01/my-post.html /职位/ 2012/01/01 /我-post.json

I'd like to make Jekyll create an HTML file and a JSON file for each page and post. This is to offer a JSON API of my Jekyll blog - e.g. a post can be accessed either at /posts/2012/01/01/my-post.html or /posts/2012/01/01/my-post.json

有谁知道,如果有一个哲基尔插件,或者我怎么会开始写这样的插件,产生两组文件并排侧?

Does anyone know if there's a Jekyll plugin, or how I would begin to write such a plugin, to generate two sets of files side-by-side?

推荐答案

我一直在寻找这样的事情太多了,所以我了解了一些红宝石,并提出产生哲基尔博客帖子JSON重新presentations的脚本。我仍然工作就可以了,但大部分是存在的。

I was looking for something like this too, so I learned a bit of ruby and made a script that generates JSON representations of Jekyll blog posts. I’m still working on it, but most of it is there.

我Gruntjs,萨斯,Backbonejs,Requirejs和CoffeeScript的把这个在一起。如果你喜欢,你可以看看哲基尔骨干项目。

I put this together with Gruntjs, Sass, Backbonejs, Requirejs and Coffeescript. If you like, you can take a look at 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
# [email protected]
# 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

这篇关于杰基尔 - 生成JSON文件旁边的HTML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 02:05