如何向 Spring 引导控制器返回的生成的 Asciidoc HTML 添加样式?
How to add styles to generated Asciidoc HTML returning by Spring Boot controller?
目标非常简单:在 Spring 引导应用程序中公开文档,使用 Thymeleaf 模板通过单独的控制器
文档存储在多个文件中,将被组装成单个文档,屏幕左侧有 table 个内容
@Controller
@RequestMapping("/documentation")
class DocumentationController {
private final ModelAndView modelAndView;
DocumentationController(@Value("classpath*:path/to/documentation/*.adoc") Resource[] docs) {
Asciidoctor asciidoctor = Asciidoctor.Factory.create();
Options convertOptions = new Options();
convertOptions.setToFile(false);
convertOptions.setSafe(SafeMode.SAFE);
List<File> files = new ArrayList<>();
for (Resource doc : docs) {
try {
files.add(doc.getFile());
} catch (IOException e) {
e.printStackTrace();
}
}
String content = String.join("\n", asciidoctor.convertFiles(files, convertOptions));
modelAndView = new ModelAndView("documentation", "content", content);
}
@GetMapping
ModelAndView documentation() {
return modelAndView;
}
}
除样式外,目前一切正常:它们不存在(这是合乎逻辑的)
如何将样式添加到生成的 ascii 文档中?
此控制器生成默认 Asciidoc 文档,其中 table 内容位于屏幕左侧,并具有默认样式
@Controller
@RequestMapping("/documentation")
class DocumentationController {
private final String content;
DocumentationController() throws IOException {
Attributes attributes = AttributesBuilder.attributes()
.tableOfContents2(Placement.LEFT)
.sectionNumbers(true)
.setAnchors(true)
.get();
Options options = OptionsBuilder.options()
.attributes(attributes)
.toFile(false)
.headerFooter(true)
.safe(SafeMode.SAFE)
.get();
Asciidoctor asciidoctor = Asciidoctor.Factory.create();
content = String.join("\n", asciidoctor.convertDirectory(
new AsciiDocDirectoryWalker(new ClassPathResource("/path/to/documentation")
.getURI()
.getPath()
.replaceFirst("/", "")),
options));
asciidoctor.close();
}
@ResponseBody
@GetMapping(produces = MediaType.TEXT_HTML_VALUE)
String documentation() {
return content;
}
}
目标非常简单:在 Spring 引导应用程序中公开文档,使用 Thymeleaf 模板通过单独的控制器
文档存储在多个文件中,将被组装成单个文档,屏幕左侧有 table 个内容
@Controller
@RequestMapping("/documentation")
class DocumentationController {
private final ModelAndView modelAndView;
DocumentationController(@Value("classpath*:path/to/documentation/*.adoc") Resource[] docs) {
Asciidoctor asciidoctor = Asciidoctor.Factory.create();
Options convertOptions = new Options();
convertOptions.setToFile(false);
convertOptions.setSafe(SafeMode.SAFE);
List<File> files = new ArrayList<>();
for (Resource doc : docs) {
try {
files.add(doc.getFile());
} catch (IOException e) {
e.printStackTrace();
}
}
String content = String.join("\n", asciidoctor.convertFiles(files, convertOptions));
modelAndView = new ModelAndView("documentation", "content", content);
}
@GetMapping
ModelAndView documentation() {
return modelAndView;
}
}
除样式外,目前一切正常:它们不存在(这是合乎逻辑的)
如何将样式添加到生成的 ascii 文档中?
此控制器生成默认 Asciidoc 文档,其中 table 内容位于屏幕左侧,并具有默认样式
@Controller
@RequestMapping("/documentation")
class DocumentationController {
private final String content;
DocumentationController() throws IOException {
Attributes attributes = AttributesBuilder.attributes()
.tableOfContents2(Placement.LEFT)
.sectionNumbers(true)
.setAnchors(true)
.get();
Options options = OptionsBuilder.options()
.attributes(attributes)
.toFile(false)
.headerFooter(true)
.safe(SafeMode.SAFE)
.get();
Asciidoctor asciidoctor = Asciidoctor.Factory.create();
content = String.join("\n", asciidoctor.convertDirectory(
new AsciiDocDirectoryWalker(new ClassPathResource("/path/to/documentation")
.getURI()
.getPath()
.replaceFirst("/", "")),
options));
asciidoctor.close();
}
@ResponseBody
@GetMapping(produces = MediaType.TEXT_HTML_VALUE)
String documentation() {
return content;
}
}