1. Introduction
1.介绍
In this quick article, we’ll demonstrate how to generate a book from an AsciiDoc document, and how to customize your book with various style options.
在这篇快速文章中,我们将演示如何从AsciiDoc文档生成一本书,以及如何用各种风格选项来定制你的书。
If you’re not familiar with AsciiDoc in Java, you can read our Introduction to AsciiDoctor.
如果你不熟悉Java中的AsciiDoc,你可以阅读我们的AsciiDoctor的介绍。
2. Backend Book Type
2.后端书籍类型
The simplest way to generate a book with AsciiDoctorj is with Maven like in the previously mentioned article. The only difference is that you have to specify doctype tag and set it to “book”.
用AsciiDoctorj生成书籍的最简单方法是使用Maven,就像前面提到的文章一样。唯一的区别是,你必须指定doctype标签,并将其设置为“book”。
<backend>pdf</backend>
<doctype>book</doctype>
With defined doctype, AsciiDoctorj knows that you want to build a book, so it creates:
有了定义的doctype,AsciiDoctorj知道你想建立一本书,所以它创建了。
- A title page
- A table of contents
- The first page of body content
- Parts and chapters
To get mentioned parts, Asciidoc document should have defined title, sections and other parts which are normal for a book.
为了获得所提到的部分,Asciidoc文件应该有定义的标题、章节和其他部分,这对一本书来说是正常的。
3. Defining a Custom Style
3.定义自定义风格
While writing a book, it’s natural that we want to use some custom styling. It’s possible to do this with AsciiDoc specific formatting language defined in the simple YAML file.
在写书的时候,我们很自然地想使用一些自定义的样式。用AsciiDoc特定的格式化语言在简单的YAML文件中定义,就可以做到这一点。
For example, this snippet of code will define how each page in a book will look like. We want to be in the portrait mode, 0.75-inch margin on top and bottom, and 1-inch margin on the sides on A4 paper format:
例如,这段代码将定义一本书中的每一页是什么样子。我们希望采用纵向模式,顶部和底部有0.75英寸的空白,两侧有1英寸的空白,采用A4纸格式。
page:
layout: portrait
margin: [0.75in, 1in, 0.75in, 1in]
size: A4
Also, we can define a custom style for book’s footer and header:
此外,我们还可以为书的页脚和页眉定义一个自定义样式。
header:
height: 0.5in
line_height: 1
recto_content:
center: '{document-title}'
verso_content:
center: '{document-title}'
footer:
height: 0.5in
line_height: 1
recto_content:
right: '{chapter-title} | *{page-number}*'
verso_content:
left: '*{page-number}* | {chapter-title}
More formatting options can be found on Github page of AsciiDoctorj.
更多的格式化选项可以在AsciiDoctorj的Github页面找到。
To include the custom theme in a book generation process, we have to define the path where our style file is located. The location is specified in attributes part in pom.xml:
为了在书籍生成过程中包含自定义主题,我们必须定义我们的样式文件所在的路径。这个位置在pom.xml的属性部分被指定:。
<pdf-stylesdir>${project.basedir}/src/themes</pdf-stylesdir>
<pdf-style>custom</pdf-style>
The first line defines the path where our style is defined and the second line specifies the name of the file without extension.
第一行定义了我们的样式所处的路径,第二行指定了没有扩展名的文件名。
With these changes, our pom.xml looks like this:
有了这些变化,我们的pom.xml看起来像这样。
<configuration>
<sourceDirectory>src/docs/asciidoc</sourceDirectory>
<outputDirectory>target/docs/asciidoc</outputDirectory>
<attributes>
<pdf-stylesdir>${project.basedir}/src/themes</pdf-stylesdir>
<pdf-style>custom</pdf-style>
</attributes>
<backend>pdf</backend>
<doctype>book</doctype>
</configuration>
4. Generating Book
4.生成书
To generate your book, you just have to run Maven in the project directory, and generated book can be found in target/docs/asciidoctor/ directory.
要生成书,你只需在项目目录下运行Maven,生成的书可以在target/docs/asciidoctor/目录下找到。
5. Conclusion
5.结论
In this tutorial, we showed you how to generate book decorated with simple style with Maven.
在本教程中,我们向您展示了如何用Maven生成具有简单风格的书籍装饰。
As always, the code from this article can be found over on GitHub.
像往常一样,本文的代码可以在GitHub上找到over。