你可以将Quarto YAML元数据用作Lua过滤器中的值吗?

huangapple go评论55阅读模式
英文:

How can you take Quarto YAML metadata and use it as a value in Lua filter?

问题

我目前正在尝试编写一个Quarto的扩展,要求使其可以通过YAML中的选项进行自定义,而不是让用户直接编辑Lua脚本。

以下是我迄今为止拥有的Lua过滤器,但我无论如何都无法获得可工作的代码,以替换颜色和样式的固定值的元数据输入。我尝试了在线找到的各种方法来包含元数据,但都不能正常工作。我确信这足够简单,但如果有人能指导我正确的方向,我将不胜感激。

function HorizontalRule (elem)
    if FORMAT == 'docx' then
    
    local color = "0000FF"
    local style = "double"

    local horizontallinerule = string.format([[
        <w:p>
          <w:pPr>
            <w:pStyle w:val="HorizontalRule"/>
              <w:ind w:firstLine="0"/>
              <w:jc w:val="center"/>
            <w:pBdr>
              <w:bottom w:val="%s" w:color="%s"/>
            </w:pBdr>
          </w:pPr>
          <w:r>
            <w:t></w:t>
          </w:r>
        </w:p>]], style, color)

      return pandoc.RawBlock('openxml', horizontallinerule)
    end
end

如果需要完整的示例,请查看GitHub上项目的测试分支这里

英文:

I am currently trying to write an extension for Quarto, and I've been asked to make it customizable using options in the YAML rather than having users edit the Lua script directly

Here is the lua filter I have so far, but I cannot for the life of me get working code to have metadata inputs to replace the fixed values for color and style. I've tried a variety of ways I found online of including meta, but cannot get any to work. I'm sure it's simple enough, but if someone could point me in the right direction it would be greatly appreciated.

function HorizontalRule (elem)
    if FORMAT == &#39;docx&#39; then
    
    local color = &quot;0000FF&quot;
    local style = &quot;double&quot;

    local horizontallinerule = string.format([[&lt;w:p&gt;
      &lt;w:pPr&gt;
        &lt;w:pStyle w:val=&quot;HorizontalRule&quot;/&gt;
          &lt;w:ind w:firstLine=&quot;0&quot;/&gt;
          &lt;w:jc w:val=&quot;center&quot;/&gt;
        &lt;w:pBdr&gt;
          &lt;w:bottom w:val=&quot;%s&quot; w:color=&quot;%s&quot;/&gt;
        &lt;/w:pBdr&gt;
      &lt;/w:pPr&gt;
      &lt;w:r&gt;
        &lt;w:t&gt;&lt;/w:t&gt;
      &lt;/w:r&gt;
    &lt;/w:p&gt;]], style, color)

      return pandoc.RawBlock(&#39;openxml&#39;, horizontallinerule)
    end
end

If a full repex is useful, see my test branch of the project on GitHub here

答案1

得分: 2

最简单的方法可能是过滤主 Pandoc 文档。该元素包含元数据和实际文档。这里是一个示例:

local function make_hr_handler (remove_hr)
  return function (el)
    if remove_hr then
      return {}
    end
  end
end

function Pandoc (doc)
  local remove_horizontal_rules = doc.meta['remove-horizontal-rules']
  return doc:walk {
    HorizontalRule = make_hr_handler(remove_horizontal_rules),
  }
end

请注意,make_hr_handler 返回一个函数,因此过滤函数是动态生成的。doc:walk 方法接受一个过滤函数的表格;它基本上与定义全局(顶级)过滤函数的方式相同。

相同的效果可以通过两个函数共享的变量来实现,但我觉得上述方法更容易理解。

英文:

The easiest way is probably to filter the main Pandoc document. That element contains both the metadata and the actual document. Here's an example:

local function make_hr_handler (remove_hr)
  return function (el)
    if remove_hr then
      return {}
    end
  end
end

function Pandoc (doc)
  local remove_horizontal_rules = doc.meta[&#39;remove-horizontal-rules&#39;]
  return doc:walk {
    HorizontalRule = make_hr_handler(remove_horizontal_rules),
  }
end

Note that make_hr_handler returns a function, so the filter function is generated dynamically. The doc:walk method takes a table of filter functions; it basically works the same as defining global (top-level) filter functions.

The same effect could be achieved with variables shared by both functions, but I find the above easier.

huangapple
  • 本文由 发表于 2023年6月19日 10:14:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503253.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定