目标非常简单:通过使用Thymeleaf模板的单独控制器公开Spring Boot应用程序中的文档
文档存储在多个文件中,并将汇编成单个文档,目录位于屏幕左侧
@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文档中?
发布于 2020-05-11 19:29:58
此控制器生成默认Asciidoc文档,内容列表位于屏幕左侧,并且具有默认样式
@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;
}
}https://stackoverflow.com/questions/61716906
复制相似问题