【发布时间】:2015-05-01 15:37:12
【问题描述】:
我想在 Markdown 页面中包含一个 gnuplot 代码,并在我保存它时让 Jekyll 编译图表。正在保存图形图像。但它在 jekyll 的清理过程中被删除。我找到的最接近解决方案的是Copying generated files from a Jekyll plugin to a site resource folder。但是,我不了解 Jekyll 的整体流程以及我如何防止静态文件被删除。我添加了site.static_files << Jekyll::StaticFile.new(site, site.source, path, filename),但没有结果。
如果我在 _site 文件夹之外创建一个虚拟文件,Jekyll 会将我的文件安全地保存在 _site 文件中。我宁愿不必创建那个虚拟文件。
这是我的插件的代码。任何帮助都会很棒。
class RenderGNUplot < Liquid::Block
def initialize(tag_name, markup, tokens)
super
@markup = markup
@attributes = {}
markup.scan(Liquid::TagAttributes) do |key, value| @attributes[key.to_sym] = value end
end
def gnuplot(commands)
IO.popen("gnuplot", "w") { |io| io.puts commands }
end
def render(context)
site = context.registers[:site]
@file = ""
commands = super
if ( commands =~ /set output "(.*)"/ )
setfile_regex = Regexp.new(/set output "((.*))"/)
filepath = commands[setfile_regex, 1]
@file = File.basename filepath
commands = commands.sub!(commands[setfile_regex], 'set output "_site/media/' + @file +'"' )
p commands
end
gnuplot(commands)
site.static_files << Jekyll::StaticFile.new(site, site.source, "_site/media/", "#{@file}")
# site.static_files << Jekyll::StaticSitemapFile.new(site, site.dest, '/', 'sitemap.xml')
"<object id='' type='image/svg+xml' data='#{site.baseurl}/media/{@file}'>Your browser does not support SVG</object>"
end
end
Liquid::Template.register_tag('test', RenderGNUplot)
还有 Markdown 页面
---
layout: post
title: "Thin Server"
date: 2015-04-28 10:42:56
categories: thin
---
{% test location: Test%}
set terminal svg size 600,400 dynamic enhanced fname 'arial' fsize 10 #mousing jsdir 'http://localhost:4000/media/' name "histograms_1" butt dashlength 1.0
set output "media/curves.svg"
set key inside left top vertical Right noreverse enhanced autotitle box lt black linewidth 1.000 dashtype solid
set samples 50, 50
set title "Simple Plots"
set title font ",20" norotate
plot [-10:10] sin(x),atan(x),cos(atan(x))
{% endtest%}
【问题讨论】: