Jekyll正在清理过程中删除静态文件

时间:2015-05-01 15:37:13

标签: plugins jekyll

我想在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%}

1 个答案:

答案 0 :(得分:0)

这是我正在使用的确切代码。它只有Liquid块和jekyll StaticFile

class GNUplotFile < Jekyll::StaticFile
    def write(dest)
      puts "WRITE---->>>>>>>>>>>"
      #File.write('_site/media/BTTTTT.svg', DateTime.now)
      gnuplot(@commands)
      # do nothing
    end
    def gnuplot(commands)
      IO.popen("gnuplot", "w") { |io| io.puts commands }
    end
    def givemethecommands(commands)
      @commands = commands
    end
end

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 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 = GNUplotFile.new(site, site.source, "_site/media/", "#{@file}")
    gnuplot.givemethecommands commands
    site.static_files << gnuplot 
    # site.static_files << Jekyll::StaticFile.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)