杰奇是否生成html,杰奇 - 生成JSON文件旁边的HTML文件(Jekyll - generating JSON fi

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

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

Answer 1:

我一直在寻找这样的事情太多,所以我了解了一些红宝石和制造产生杰奇博客文章的JSON表示的脚本。 我还在工作就可以了,但大部分是存在的。

我Gruntjs,萨斯,Backbonejs,Requirejs和CoffeeScript的把这个在一起。 如果你喜欢,你可以看看在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

Answer 2:

还有,你可以做到这一点,取决于你的需求两个方面。 如果你想使用的布局来完成任务,那么你要使用一个发电机 。 您可以通过您的网站的每个页面会循环并生成页面的新版本上传.json。 你可以任选使该网页被产生于site.config或变量在页面的YAML前面的问题的存在条件。 杰基尔使用发电机来处理切片博客文章成每页的职位给定数目的指数。

第二种方法是使用一个转换器 (同一个链接,向下滚动)。 该转换器将允许您在您的内容执行任意代码翻译成不同的格式。 有关如何工作的实例,检查出的降价转换器自带的化身。

我觉得这是一个很酷的想法!

Answer 3:

看看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

双方应该做的正是你想要的。

文章来源: Jekyll - generating JSON files alongside the HTML files

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值