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/// Contains the Markdown generator using Mustache template files.
11///
12//===----------------------------------------------------------------------===//
13
14#include "Generators.h"
15
16namespace clang {
17using namespace llvm;
18namespace doc {
20
22
24
26
28 static const char *Format;
29 Error generateDocumentation(StringRef RootDir,
30 StringMap<doc::OwnedPtr<doc::Info>> Infos,
31 const ClangDocContext &CDCtx,
32 std::string DirName) override;
33 Error setupTemplateFiles(const ClangDocContext &CDCtx) override;
34 Error generateDocForJSON(json::Value &JSON, raw_fd_ostream &OS,
35 const ClangDocContext &CDCtx,
36 StringRef ObjectTypeStr,
37 StringRef RelativeRootPath) override;
38 // This generator doesn't need this function, but it inherits from the
39 // original generator interface.
40 Error generateDocForInfo(Info *I, llvm::raw_ostream &OS,
41 const ClangDocContext &CDCtx) override;
42};
43
45 std::string ClassFilePath = CDCtx.MustacheTemplates.lookup("class-template");
46 std::string NamespaceFilePath =
47 CDCtx.MustacheTemplates.lookup("namespace-template");
48 std::string AllFilesPath = CDCtx.MustacheTemplates.lookup("all-files");
49 std::string IndexFilePath = CDCtx.MustacheTemplates.lookup("index");
50 std::string CommentsFilePath = CDCtx.MustacheTemplates.lookup("comments");
51 std::vector<std::pair<StringRef, StringRef>> Partials = {
52 {"Comments", CommentsFilePath}};
53
54 if (Error Err = setupTemplate(RecordTemplate, ClassFilePath, Partials))
55 return Err;
56 if (Error Err = setupTemplate(NamespaceTemplate, NamespaceFilePath, Partials))
57 return Err;
58 if (Error Err = setupTemplate(AllFilesTemplate, AllFilesPath, Partials))
59 return Err;
60 if (Error Err = setupTemplate(IndexTemplate, IndexFilePath, Partials))
61 return Err;
62
63 // Override the default HTML Mustache escape characters. We don't need to
64 // override `<` here.
65 static const DenseMap<char, std::string> EscapeChars;
66 RecordTemplate->setEscapeCharacters(EscapeChars);
67 NamespaceTemplate->setEscapeCharacters(EscapeChars);
68 AllFilesTemplate->setEscapeCharacters(EscapeChars);
69 IndexTemplate->setEscapeCharacters(EscapeChars);
70
71 return Error::success();
72}
73
75 StringRef RootDir, StringMap<doc::OwnedPtr<doc::Info>> Infos,
76 const clang::doc::ClangDocContext &CDCtx, std::string Dirname) {
77 return MustacheGenerator::generateDocumentation(RootDir, std::move(Infos),
78 CDCtx, "md");
79}
80
82 raw_fd_ostream &OS,
83 const ClangDocContext &CDCtx,
84 StringRef ObjTypeStr,
85 StringRef RelativeRootPath) {
86 if (ObjTypeStr == "record") {
87 assert(RecordTemplate && "RecordTemplate is nullptr.");
88 RecordTemplate->render(JSON, OS);
89 } else if (ObjTypeStr == "namespace") {
90 assert(NamespaceTemplate && "NamespaceTemplate is nullptr.");
91 NamespaceTemplate->render(JSON, OS);
92 } else if (ObjTypeStr == "all_files") {
93 assert(AllFilesTemplate && "AllFilesTemplate is nullptr.");
94 AllFilesTemplate->render(JSON, OS);
95 } else if (ObjTypeStr == "index") {
96 assert(IndexTemplate && "IndexTemplate is nullptr");
97 IndexTemplate->render(JSON, OS);
98 }
99 return Error::success();
100}
101
103 const ClangDocContext &CDCtx) {
104 return Error::success();
105}
106
107const char *MDMustacheGenerator::Format = "md_mustache";
108
109static GeneratorRegistry::Add<MDMustacheGenerator>
111 "Generator for mustache Markdown output.");
112
114} // namespace doc
115} // namespace clang
std::unique_ptr< T > OwnedPtr
static GeneratorRegistry::Add< MDMustacheGenerator > MDMustache(MDMustacheGenerator::Format, "Generator for mustache Markdown output.")
static OwnedPtr< MustacheTemplateFile > AllFilesTemplate
static GeneratorRegistry::Add< JSONGenerator > JSON(JSONGenerator::Format, "Generator for JSON output.")
static OwnedPtr< MustacheTemplateFile > IndexTemplate
volatile int MDMustacheGeneratorAnchorSource
static OwnedPtr< MustacheTemplateFile > RecordTemplate
static OwnedPtr< MustacheTemplateFile > NamespaceTemplate
===– 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 generateDocForJSON(json::Value &JSON, raw_fd_ostream &OS, const ClangDocContext &CDCtx, StringRef ObjectTypeStr, StringRef RelativeRootPath) override
Error generateDocumentation(StringRef RootDir, StringMap< doc::OwnedPtr< doc::Info > > Infos, const ClangDocContext &CDCtx, std::string DirName) override
Error generateDocForInfo(Info *I, llvm::raw_ostream &OS, const ClangDocContext &CDCtx) override
llvm::Error setupTemplate(OwnedPtr< MustacheTemplateFile > &Template, StringRef TemplatePath, std::vector< std::pair< StringRef, StringRef > > Partials)
Registers partials to templates.
llvm::Error generateDocumentation(StringRef RootDir, llvm::StringMap< doc::OwnedPtr< doc::Info > > Infos, const clang::doc::ClangDocContext &CDCtx, std::string DirName) override
The main orchestrator for Mustache-based documentation.