clang-tools 23.0.0git
MDMustacheGenerator.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file contains the implementation of the MDMustacheGenerator, which
11/// generates documentation in Markdown format using Mustache templates. It
12/// defines how the structured data in Info objects is mapped to template
13/// tags to produce readable markdown documents.
14///
15//===----------------------------------------------------------------------===//
16
17#include "Generators.h"
18
19namespace clang {
20using namespace llvm;
21namespace doc {
22static std::unique_ptr<MustacheTemplateFile> RecordTemplate = nullptr;
23
24static std::unique_ptr<MustacheTemplateFile> NamespaceTemplate = nullptr;
25
26static std::unique_ptr<MustacheTemplateFile> AllFilesTemplate = nullptr;
27
28static std::unique_ptr<MustacheTemplateFile> IndexTemplate = nullptr;
29
31 static const char *Format;
32 Error generateDocumentation(StringRef RootDir, StringMap<doc::Info *> Infos,
33 const ClangDocContext &CDCtx,
34 std::string DirName) override;
35 Error setupTemplateFiles(const ClangDocContext &CDCtx) override;
36 Error generateDocForJSON(json::Value &JSON, raw_fd_ostream &OS,
37 const ClangDocContext &CDCtx,
38 StringRef ObjectTypeStr,
39 StringRef RelativeRootPath) override;
40 // This generator doesn't need this function, but it inherits from the
41 // original generator interface.
42 Error generateDocForInfo(Info *I, llvm::raw_ostream &OS,
43 const ClangDocContext &CDCtx) override;
44};
45
47 std::string ClassFilePath = CDCtx.MustacheTemplates.lookup("class-template");
48 std::string NamespaceFilePath =
49 CDCtx.MustacheTemplates.lookup("namespace-template");
50 std::string AllFilesPath = CDCtx.MustacheTemplates.lookup("all-files");
51 std::string IndexFilePath = CDCtx.MustacheTemplates.lookup("index");
52 std::string CommentsFilePath = CDCtx.MustacheTemplates.lookup("comments");
53 std::vector<std::pair<StringRef, StringRef>> Partials = {
54 {"Comments", CommentsFilePath}};
55
56 if (Error Err = setupTemplate(RecordTemplate, ClassFilePath, Partials))
57 return Err;
58 if (Error Err = setupTemplate(NamespaceTemplate, NamespaceFilePath, Partials))
59 return Err;
60 if (Error Err = setupTemplate(AllFilesTemplate, AllFilesPath, Partials))
61 return Err;
62 if (Error Err = setupTemplate(IndexTemplate, IndexFilePath, Partials))
63 return Err;
64
65 // Override the default HTML Mustache escape characters. We don't need to
66 // override `<` here.
67 static const DenseMap<char, std::string> EscapeChars;
68 RecordTemplate->setEscapeCharacters(EscapeChars);
69 NamespaceTemplate->setEscapeCharacters(EscapeChars);
70 AllFilesTemplate->setEscapeCharacters(EscapeChars);
71 IndexTemplate->setEscapeCharacters(EscapeChars);
72
73 return Error::success();
74}
75
77 StringRef RootDir, StringMap<doc::Info *> Infos,
78 const clang::doc::ClangDocContext &CDCtx, std::string Dirname) {
79 return MustacheGenerator::generateDocumentation(RootDir, std::move(Infos),
80 CDCtx, "md");
81}
82
84 raw_fd_ostream &OS,
85 const ClangDocContext &CDCtx,
86 StringRef ObjTypeStr,
87 StringRef RelativeRootPath) {
88 if (ObjTypeStr == "record") {
89 assert(RecordTemplate && "RecordTemplate is nullptr.");
90 RecordTemplate->render(JSON, OS);
91 } else if (ObjTypeStr == "namespace") {
92 assert(NamespaceTemplate && "NamespaceTemplate is nullptr.");
93 NamespaceTemplate->render(JSON, OS);
94 } else if (ObjTypeStr == "all_files") {
95 assert(AllFilesTemplate && "AllFilesTemplate is nullptr.");
96 AllFilesTemplate->render(JSON, OS);
97 } else if (ObjTypeStr == "index") {
98 assert(IndexTemplate && "IndexTemplate is nullptr");
99 IndexTemplate->render(JSON, OS);
100 }
101 return Error::success();
102}
103
105 const ClangDocContext &CDCtx) {
106 return Error::success();
107}
108
109const char *MDMustacheGenerator::Format = "md_mustache";
110
111static GeneratorRegistry::Add<MDMustacheGenerator>
113 "Generator for mustache Markdown output.");
114
116} // namespace doc
117} // namespace clang
static GeneratorRegistry::Add< MDMustacheGenerator > MDMustache(MDMustacheGenerator::Format, "Generator for mustache Markdown output.")
static std::unique_ptr< MustacheTemplateFile > RecordTemplate
static GeneratorRegistry::Add< JSONGenerator > JSON(JSONGenerator::Format, "Generator for JSON output.")
static std::unique_ptr< MustacheTemplateFile > IndexTemplate
static std::unique_ptr< MustacheTemplateFile > NamespaceTemplate
volatile int MDMustacheGeneratorAnchorSource
static std::unique_ptr< MustacheTemplateFile > AllFilesTemplate
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::StringMap< std::string > MustacheTemplates
A base struct for Infos.
Error setupTemplateFiles(const ClangDocContext &CDCtx) override
Initializes the template files from disk and calls setupTemplate to register partials.
Error generateDocumentation(StringRef RootDir, StringMap< doc::Info * > Infos, const ClangDocContext &CDCtx, std::string DirName) override
Error generateDocForJSON(json::Value &JSON, raw_fd_ostream &OS, const ClangDocContext &CDCtx, StringRef ObjectTypeStr, StringRef RelativeRootPath) override
Error generateDocForInfo(Info *I, llvm::raw_ostream &OS, const ClangDocContext &CDCtx) override
llvm::Error generateDocumentation(StringRef RootDir, llvm::StringMap< doc::Info * > Infos, const clang::doc::ClangDocContext &CDCtx, std::string DirName) override
The main orchestrator for Mustache-based documentation.
llvm::Error setupTemplate(std::unique_ptr< MustacheTemplateFile > &Template, StringRef TemplatePath, std::vector< std::pair< StringRef, StringRef > > Partials)
Registers partials to templates.