【问题标题】:nanoc: How can I pass options to pandoc-ruby?nanoc:如何将选项传递给 pandoc-ruby?
【发布时间】:2013-02-01 12:52:00
【问题描述】:

我正在尝试将 nanoc 3.5.0 与使用 pandoc-rubypandoc 过滤器一起使用。具体来说,我无法从我的Rules 文件中传递几个选项,因此对PandocRuby.convert() 的最终调用如下所示:

PandocRuby.convert(content,
                   {:from => :markdown, :to => :html}, :no_wrap, 
                   :table_of_contents, :mathjax, :standalone,
                   {"template" => Dir.getwd + '/layouts/pandocTemplate.html'})

当我将上述调用放在自定义过滤器中时,一切正常。但是,我想在 Rules 中指定 pandoc 选项,这样我就不必为每组选项创建一个特殊的过滤器。

默认的 pandoc 过滤器被定义为函数run(content, params={}) 并简单地调用PandocRuby.convert(content, params)。如何设置 params 以便正确调用 PandocRuby.convert()Rules 中的以下指令不起作用:

filter :pandoc, :params => { :from => :markdown, :to => :html, :no_wrap, :table_of_contents, :mathjax, :standalone, "template" => Dir.getwd + '/layouts/pandocTemplate.html' }
filter :pandoc, :params => { :from => :markdown, :to => :html, :no_wrap => true, :table_of_contents => true, :mathjax => true, :standalone => true, "template" => Dir.getwd + '/layouts/pandocTemplate.html' }

第一个指令导致 Ruby 错误,第二个指令运行但给了我一个空白页,表明 pandoc 没有被正确调用。我对 Ruby 不是很熟悉,所以我目前的努力只是在黑暗中摸索。

【问题讨论】:

    标签: ruby pandoc nanoc


    【解决方案1】:

    nanoc 附带的pandoc 过滤器此时无法正确执行此操作。给过滤器的参数直接传递给PandocRuby.convert

    def run(content, params={})
      PandocRuby.convert(content, params)
    end
    

    (source)

    您对过滤器的调用有两个以上的参数,这就是它崩溃的原因。过滤器当然需要更新(我对如何调用它的想法太天真了)。如果您想尝试改进过滤器,当然欢迎您提交拉取请求!同时,我已将此问题报告为问题 (link)。

    (希望我能尽快用正确的答案更新这个答案!)

    【讨论】:

    • 感谢您的快速回复!如果我正确理解 pandoc-ruby 文档,则第二个参数必须是 {:from => ..., :to => ...} 哈希,然后是其他选项。我会尝试更改当前的过滤器,但无法预测是否成功。
    【解决方案2】:

    我编写了一个基本的 nanoc pandoc 过滤器,它调用 pandoc 目录而不使用pandoc-ruby

    # All files in the 'lib' directory will be loaded
    # before nanoc starts compiling.
    # encoding: utf-8
    
    module Nanoc::Filters
    
      class PandocSystem < Nanoc::Filter
        identifier :pandoc_system
        type :text => :text
    
        def run(content, params = {})
          if item[:extension] == 'org'
            `pandoc -f org -t html < #{item.raw_filename}`
          elsif ["md", "markdown"].index(item[:extension])
            `pandoc -f markdown -t html < #{item.raw_filename}`
          end
        end
    
      end
    
    end
    

    您可以根据item[:extension] 将自己的选项传递给 pandoc。希望对您有所帮助。


    更新,我创建了一个新的 gist,它为 nanoc 提供了 pandoc 过滤器的改进版本,检查:https://gist.github.com/xiaohanyu/9866531

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-16
    • 2011-07-14
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    相关资源
    最近更新 更多