首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何向Spring Boot控制器返回的Asciidoc HTML添加样式?

如何向Spring Boot控制器返回的Asciidoc HTML添加样式?
EN

Stack Overflow用户
提问于 2020-05-11 02:31:51
回答 1查看 116关注 0票数 0

目标非常简单:通过使用Thymeleaf模板的单独控制器公开Spring Boot应用程序中的文档

文档存储在多个文件中,并将汇编成单个文档,目录位于屏幕左侧

代码语言:javascript
复制
@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文档中?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-11 19:29:58

此控制器生成默认Asciidoc文档,内容列表位于屏幕左侧,并且具有默认样式

代码语言:javascript
复制
@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;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61716906

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档