-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[clang-doc][NFC] Lift Mustache template generation from HTML #167815
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,8 @@ | |
|
|
||
| #include "Representation.h" | ||
| #include "llvm/Support/Error.h" | ||
| #include "llvm/Support/JSON.h" | ||
| #include "llvm/Support/Mustache.h" | ||
| #include "llvm/Support/Registry.h" | ||
|
|
||
| namespace clang { | ||
|
|
@@ -27,10 +29,9 @@ class Generator { | |
|
|
||
| // Write out the decl info for the objects in the given map in the specified | ||
| // format. | ||
| virtual llvm::Error | ||
| generateDocs(StringRef RootDir, | ||
| llvm::StringMap<std::unique_ptr<doc::Info>> Infos, | ||
| const ClangDocContext &CDCtx) = 0; | ||
| virtual llvm::Error generateDocumentation( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not just leave the name in place and update the mustache implementation to call this API?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess they're kind of equivalent, but the other way sounds easier.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah this ended up being a bit more invasive than I had planned. I didn't want to touch any of the older generators, but had to for the default arg and thought might as well use the nicer name. I can revert back to the old name if you want, it ends up being functionally the same thing.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don’t think it matters much in the end. The diff would have been a bit less, I think, but it’s a rather minor difference imo. |
||
| StringRef RootDir, llvm::StringMap<std::unique_ptr<doc::Info>> Infos, | ||
| const ClangDocContext &CDCtx, std::string DirName = "") = 0; | ||
|
|
||
| // This function writes a file with the index previously constructed. | ||
| // It can be overwritten by any of the inherited generators. | ||
|
|
@@ -52,6 +53,85 @@ findGeneratorByName(llvm::StringRef Format); | |
|
|
||
| std::string getTagType(TagTypeKind AS); | ||
|
|
||
| llvm::Error createFileOpenError(StringRef FileName, std::error_code EC); | ||
|
|
||
| class MustacheTemplateFile { | ||
| llvm::BumpPtrAllocator Allocator; | ||
| llvm::StringSaver Saver; | ||
| llvm::mustache::MustacheContext Ctx; | ||
| llvm::mustache::Template T; | ||
| std::unique_ptr<llvm::MemoryBuffer> Buffer; | ||
|
|
||
| public: | ||
| static Expected<std::unique_ptr<MustacheTemplateFile>> | ||
| createMustacheFile(StringRef FileName) { | ||
| llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> BufferOrError = | ||
| llvm::MemoryBuffer::getFile(FileName); | ||
| if (auto EC = BufferOrError.getError()) | ||
| return createFileOpenError(FileName, EC); | ||
| return std::make_unique<MustacheTemplateFile>( | ||
| std::move(BufferOrError.get())); | ||
| } | ||
|
|
||
| llvm::Error registerPartialFile(StringRef Name, StringRef FileName) { | ||
| llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> BufferOrError = | ||
| llvm::MemoryBuffer::getFile(FileName); | ||
| if (auto EC = BufferOrError.getError()) | ||
| return createFileOpenError(FileName, EC); | ||
|
|
||
| std::unique_ptr<llvm::MemoryBuffer> Buffer = std::move(BufferOrError.get()); | ||
| StringRef FileContent = Buffer->getBuffer(); | ||
| T.registerPartial(Name.str(), FileContent.str()); | ||
| return llvm::Error::success(); | ||
| } | ||
|
|
||
| void render(llvm::json::Value &V, raw_ostream &OS) { T.render(V, OS); } | ||
|
|
||
| MustacheTemplateFile(std::unique_ptr<llvm::MemoryBuffer> &&B) | ||
| : Saver(Allocator), Ctx(Allocator, Saver), T(B->getBuffer(), Ctx), | ||
| Buffer(std::move(B)) {} | ||
| }; | ||
|
|
||
| struct MustacheGenerator : public Generator { | ||
| Expected<std::string> getInfoTypeStr(llvm::json::Object *Info, | ||
| StringRef Filename); | ||
|
|
||
| /// Used to find the relative path from the file to the format's docs root. | ||
| /// Mainly used for the HTML resource paths. | ||
| SmallString<128> getRelativePathToRoot(StringRef PathToFile, | ||
| StringRef DocsRootPath); | ||
| virtual ~MustacheGenerator() = default; | ||
|
|
||
| /// Initializes the template files from disk and calls setupTemplate to | ||
| /// register partials | ||
| virtual llvm::Error setupTemplateFiles(const ClangDocContext &CDCtx) = 0; | ||
|
|
||
| /// Populates templates with data from JSON and calls any specifics for the | ||
| /// format. For example, for HTML it will render the paths for CSS and JS. | ||
| virtual llvm::Error generateDocForJSON(llvm::json::Value &JSON, | ||
| llvm::raw_fd_ostream &OS, | ||
| const ClangDocContext &CDCtx, | ||
| StringRef ObjectTypeStr, | ||
| StringRef RelativeRootPath) = 0; | ||
|
|
||
| /// Registers partials to templates. | ||
| llvm::Error | ||
| setupTemplate(std::unique_ptr<MustacheTemplateFile> &Template, | ||
| StringRef TemplatePath, | ||
| std::vector<std::pair<StringRef, StringRef>> Partials); | ||
|
|
||
| /// \brief The main orchestrator for Mustache-based documentation. | ||
| /// | ||
| /// 1. Initializes templates files from disk by calling setupTemplateFiles. | ||
| /// 2. Calls the JSON generator to write JSON to disk. | ||
| /// 3. Iterates over the JSON files, recreates the directory structure from | ||
| /// JSON, and calls generateDocForJSON for each file. | ||
| /// 4. A file of the desired format is created. | ||
| llvm::Error generateDocumentation( | ||
| StringRef RootDir, llvm::StringMap<std::unique_ptr<doc::Info>> Infos, | ||
| const clang::doc::ClangDocContext &CDCtx, std::string DirName) override; | ||
| }; | ||
|
|
||
| // This anchor is used to force the linker to link in the generated object file | ||
| // and thus register the generators. | ||
| extern volatile int YAMLGeneratorAnchorSource; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.