我想为 Asciidoctor 的 Ruby API 指定一个字符串值,并希望 Asciidoctor 将该值替换到输出中。
我从文档中将以下内容拼凑在一起,但它不起作用。
Asciidoctor.convert adoc, attributes: { updated: src_mtime }
其中
adoc
是这样的字符串:
:title: Some Title
:hardbreaks:
Introductory paragraph
== Section title
Paragraphs
[.text-right]
Last updated: {updated}
这只会在最后一段中生成“上次更新:{更新}”,不应用替换。
如果我将
updated
定义为标头属性,就像 :updated: 1 Jan 2000
一样,替换确实会发生,但它会忽略 Asciidoctor.convert
调用中的属性。
我一整天都在为此绞尽脑汁,但毫无结果。
Asciidoctor 需要知道转换的文档类型。
这是一个工作示例:
require 'asciidoctor'
src_mtime = File.mtime('index.adoc')
puts src_mtime
adoc = '
:title: Some Title
:hardbreaks:
Introductory paragraph
== Section title
Paragraphs
[.text-right]
Last updated: {updated}
'
puts Asciidoctor.convert adoc, doctype: :book, attributes: { 'updated' => src_mtime }
输出:
$ ruby convert.rb
2024-06-18 10:50:15 -0700
<div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p>Introductory paragraph</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_section_title">Section title</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Paragraphs</p>
</div>
<div class="paragraph text-right">
<p>Last updated: 2024-06-18 10:50:15 -0700</p>
</div>
</div>
</div>