//===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// /// /// \file /// This file contains the implementation of the MDMustacheGenerator, which /// generates documentation in Markdown format using Mustache templates. It /// defines how the structured data in Info objects is mapped to template /// tags to produce readable markdown documents. /// //===----------------------------------------------------------------------===// #include "Generators.h" namespace clang { using namespace llvm; namespace doc { static std::unique_ptr RecordTemplate = nullptr; static std::unique_ptr NamespaceTemplate = nullptr; static std::unique_ptr AllFilesTemplate = nullptr; static std::unique_ptr IndexTemplate = nullptr; struct MDMustacheGenerator : public MustacheGenerator { static const char *Format; Error generateDocumentation(StringRef RootDir, StringMap Infos, const ClangDocContext &CDCtx, std::string DirName) override; Error setupTemplateFiles(const ClangDocContext &CDCtx) override; Error generateDocForJSON(json::Value &JSON, raw_fd_ostream &OS, const ClangDocContext &CDCtx, StringRef ObjectTypeStr, StringRef RelativeRootPath) override; // This generator doesn't need this function, but it inherits from the // original generator interface. Error generateDocForInfo(Info *I, llvm::raw_ostream &OS, const ClangDocContext &CDCtx) override; }; Error MDMustacheGenerator::setupTemplateFiles(const ClangDocContext &CDCtx) { std::string ClassFilePath = CDCtx.MustacheTemplates.lookup("class-template"); std::string NamespaceFilePath = CDCtx.MustacheTemplates.lookup("namespace-template"); std::string AllFilesPath = CDCtx.MustacheTemplates.lookup("all-files"); std::string IndexFilePath = CDCtx.MustacheTemplates.lookup("index"); std::string CommentsFilePath = CDCtx.MustacheTemplates.lookup("comments"); std::vector> Partials = { {"Comments", CommentsFilePath}}; if (Error Err = setupTemplate(RecordTemplate, ClassFilePath, Partials)) return Err; if (Error Err = setupTemplate(NamespaceTemplate, NamespaceFilePath, Partials)) return Err; if (Error Err = setupTemplate(AllFilesTemplate, AllFilesPath, Partials)) return Err; if (Error Err = setupTemplate(IndexTemplate, IndexFilePath, Partials)) return Err; // Override the default HTML Mustache escape characters. We don't need to // override `<` here. static const DenseMap EscapeChars; RecordTemplate->setEscapeCharacters(EscapeChars); NamespaceTemplate->setEscapeCharacters(EscapeChars); AllFilesTemplate->setEscapeCharacters(EscapeChars); IndexTemplate->setEscapeCharacters(EscapeChars); return Error::success(); } Error MDMustacheGenerator::generateDocumentation( StringRef RootDir, StringMap Infos, const clang::doc::ClangDocContext &CDCtx, std::string Dirname) { return MustacheGenerator::generateDocumentation(RootDir, std::move(Infos), CDCtx, "md"); } Error MDMustacheGenerator::generateDocForJSON(json::Value &JSON, raw_fd_ostream &OS, const ClangDocContext &CDCtx, StringRef ObjTypeStr, StringRef RelativeRootPath) { if (ObjTypeStr == "record") { assert(RecordTemplate && "RecordTemplate is nullptr."); RecordTemplate->render(JSON, OS); } else if (ObjTypeStr == "namespace") { assert(NamespaceTemplate && "NamespaceTemplate is nullptr."); NamespaceTemplate->render(JSON, OS); } else if (ObjTypeStr == "all_files") { assert(AllFilesTemplate && "AllFilesTemplate is nullptr."); AllFilesTemplate->render(JSON, OS); } else if (ObjTypeStr == "index") { assert(IndexTemplate && "IndexTemplate is nullptr"); IndexTemplate->render(JSON, OS); } return Error::success(); } Error MDMustacheGenerator::generateDocForInfo(Info *I, raw_ostream &OS, const ClangDocContext &CDCtx) { return Error::success(); } const char *MDMustacheGenerator::Format = "md_mustache"; static GeneratorRegistry::Add MDMustache(MDMustacheGenerator::Format, "Generator for mustache Markdown output."); volatile int MDMustacheGeneratorAnchorSource = 0; } // namespace doc } // namespace clang