【问题标题】:How to include code excerpts using tags in asciidoc?如何在 asciidoc 中使用标签包含代码摘录?
【发布时间】:2015-04-18 11:49:00
【问题描述】:

我可以包含完整的Greet.java 文件

public class Greet {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

从 asciidoc 文件中

== Hello Java
This is how one greets in Java:

[source,java]
.Greet.java
----
include::Greet.java
----

制作文档

但假设我只想包含由标签分隔的代码的摘录。在上面的代码中,假设我只想包含main 函数。

我在documentation 中没有看到符号标签,但this 页面表明它足以编写

public class Greet {
    // tag::helloMethod[]
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
    // end::helloMethod[]
}

== Hello Java
This is how one greets in Java:

[source,java]
.Greet.java
----
include::Greet.java[tags=helloMethod]
----

这只是产生:

您能否建议一种仅包含摘录的方法?我正在使用 asciidoc 8.6.9。

【问题讨论】:

    标签: documentation literate-programming asciidoc


    【解决方案1】:

    您正在做的事情在AsciidoctorRuby implementation of AsciiDoc)中应该可以正常工作,但在AsciiDocthe Python implementation)中却不行。

    请注意获取语法高亮的不同机制。

    要使用 asciidoc 获得语法高亮,可以使用命令行开关 asciidoc -a source-highlighter=pygments file.adoc

    Asciidoctor 不需要(或不可能)命令行开关。使用 AsciiDoctor 语法高亮通过以下方式获得:

    1. 在每个源文件的顶部插入:source-highlighter: pygments,并且
    2. 运行 sudo gem install pygments.rb 以安装 pygments

    【讨论】:

      【解决方案2】:

      Asciidoctor tags 选项也可以包含多个标签;

      [tags="tag 1, tag 2, …"]
      

      一次性引入更多代码摘录……

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-26
        • 2012-05-07
        • 2017-03-24
        • 2016-06-08
        • 1970-01-01
        相关资源
        最近更新 更多