【问题标题】:Jekyll's is removing static file during cleanupJekyll 正在清理期间删除静态文件
【发布时间】: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%}

【问题讨论】:

    标签: plugins jekyll


    【解决方案1】:

    这是我正在使用的确切代码。它只有 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)
    

    【讨论】:

      猜你喜欢
      • 2012-03-10
      • 1970-01-01
      • 1970-01-01
      • 2016-06-04
      • 2019-05-15
      • 2017-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多