2019-07-11 03:03:25 +08:00
|
|
|
//===-- HTMLGenerator.cpp - HTML Generator ----------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Generators.h"
|
|
|
|
#include "Representation.h"
|
2019-07-26 04:14:45 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2019-07-11 03:03:25 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
2019-08-07 02:31:46 +08:00
|
|
|
#include "llvm/Support/JSON.h"
|
2019-07-11 03:03:25 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2019-08-07 02:31:46 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2019-07-11 03:03:25 +08:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace doc {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class HTMLTag {
|
|
|
|
public:
|
|
|
|
// Any other tag can be added if required
|
|
|
|
enum TagType {
|
2019-08-07 02:31:46 +08:00
|
|
|
TAG_A,
|
2019-07-11 03:03:25 +08:00
|
|
|
TAG_DIV,
|
|
|
|
TAG_H1,
|
|
|
|
TAG_H2,
|
|
|
|
TAG_H3,
|
|
|
|
TAG_LI,
|
2019-07-26 05:27:50 +08:00
|
|
|
TAG_LINK,
|
2019-08-07 02:31:46 +08:00
|
|
|
TAG_META,
|
|
|
|
TAG_P,
|
|
|
|
TAG_SCRIPT,
|
|
|
|
TAG_SPAN,
|
|
|
|
TAG_TITLE,
|
|
|
|
TAG_UL,
|
2019-07-11 03:03:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
HTMLTag() = default;
|
|
|
|
constexpr HTMLTag(TagType Value) : Value(Value) {}
|
|
|
|
|
|
|
|
operator TagType() const { return Value; }
|
|
|
|
operator bool() = delete;
|
|
|
|
|
|
|
|
bool IsSelfClosing() const;
|
|
|
|
llvm::SmallString<16> ToString() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
TagType Value;
|
|
|
|
};
|
|
|
|
|
2019-07-13 02:32:00 +08:00
|
|
|
enum NodeType {
|
|
|
|
NODE_TEXT,
|
|
|
|
NODE_TAG,
|
|
|
|
};
|
|
|
|
|
2019-07-11 03:03:25 +08:00
|
|
|
struct HTMLNode {
|
2019-07-13 02:32:00 +08:00
|
|
|
HTMLNode(NodeType Type) : Type(Type) {}
|
2019-07-11 03:03:25 +08:00
|
|
|
virtual ~HTMLNode() = default;
|
|
|
|
|
|
|
|
virtual void Render(llvm::raw_ostream &OS, int IndentationLevel) = 0;
|
2019-07-13 02:32:00 +08:00
|
|
|
NodeType Type; // Type of node
|
2019-07-11 03:03:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct TextNode : public HTMLNode {
|
2019-07-26 04:49:00 +08:00
|
|
|
TextNode(const Twine &Text)
|
|
|
|
: HTMLNode(NodeType::NODE_TEXT), Text(Text.str()) {}
|
2019-07-11 03:03:25 +08:00
|
|
|
|
|
|
|
std::string Text; // Content of node
|
|
|
|
void Render(llvm::raw_ostream &OS, int IndentationLevel) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TagNode : public HTMLNode {
|
2019-07-26 04:49:00 +08:00
|
|
|
TagNode(HTMLTag Tag) : HTMLNode(NodeType::NODE_TAG), Tag(Tag) {}
|
2019-07-11 03:03:25 +08:00
|
|
|
TagNode(HTMLTag Tag, const Twine &Text) : TagNode(Tag) {
|
2019-07-26 04:49:00 +08:00
|
|
|
Children.emplace_back(llvm::make_unique<TextNode>(Text.str()));
|
2019-07-11 03:03:25 +08:00
|
|
|
}
|
|
|
|
|
2019-07-26 04:49:00 +08:00
|
|
|
HTMLTag Tag; // Name of HTML Tag (p, div, h1)
|
2019-07-11 03:03:25 +08:00
|
|
|
std::vector<std::unique_ptr<HTMLNode>> Children; // List of child nodes
|
|
|
|
llvm::StringMap<llvm::SmallString<16>>
|
|
|
|
Attributes; // List of key-value attributes for tag
|
|
|
|
|
|
|
|
void Render(llvm::raw_ostream &OS, int IndentationLevel) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
constexpr const char *kDoctypeDecl = "<!DOCTYPE html>";
|
|
|
|
|
|
|
|
struct HTMLFile {
|
|
|
|
std::vector<std::unique_ptr<HTMLNode>> Children; // List of child nodes
|
|
|
|
void Render(llvm::raw_ostream &OS) {
|
|
|
|
OS << kDoctypeDecl << "\n";
|
|
|
|
for (const auto &C : Children) {
|
|
|
|
C->Render(OS, 0);
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
bool HTMLTag::IsSelfClosing() const {
|
|
|
|
switch (Value) {
|
|
|
|
case HTMLTag::TAG_META:
|
2019-07-26 05:27:50 +08:00
|
|
|
case HTMLTag::TAG_LINK:
|
2019-07-11 03:03:25 +08:00
|
|
|
return true;
|
2019-08-07 02:31:46 +08:00
|
|
|
case HTMLTag::TAG_A:
|
2019-07-11 03:03:25 +08:00
|
|
|
case HTMLTag::TAG_DIV:
|
|
|
|
case HTMLTag::TAG_H1:
|
|
|
|
case HTMLTag::TAG_H2:
|
|
|
|
case HTMLTag::TAG_H3:
|
2019-08-07 02:31:46 +08:00
|
|
|
case HTMLTag::TAG_LI:
|
2019-07-11 03:03:25 +08:00
|
|
|
case HTMLTag::TAG_P:
|
2019-08-07 02:31:46 +08:00
|
|
|
case HTMLTag::TAG_SCRIPT:
|
|
|
|
case HTMLTag::TAG_SPAN:
|
|
|
|
case HTMLTag::TAG_TITLE:
|
2019-07-11 03:03:25 +08:00
|
|
|
case HTMLTag::TAG_UL:
|
|
|
|
return false;
|
|
|
|
}
|
2019-07-11 14:50:10 +08:00
|
|
|
llvm_unreachable("Unhandled HTMLTag::TagType");
|
2019-07-11 03:03:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::SmallString<16> HTMLTag::ToString() const {
|
|
|
|
switch (Value) {
|
2019-08-07 02:31:46 +08:00
|
|
|
case HTMLTag::TAG_A:
|
|
|
|
return llvm::SmallString<16>("a");
|
2019-07-11 03:03:25 +08:00
|
|
|
case HTMLTag::TAG_DIV:
|
|
|
|
return llvm::SmallString<16>("div");
|
|
|
|
case HTMLTag::TAG_H1:
|
|
|
|
return llvm::SmallString<16>("h1");
|
|
|
|
case HTMLTag::TAG_H2:
|
|
|
|
return llvm::SmallString<16>("h2");
|
|
|
|
case HTMLTag::TAG_H3:
|
|
|
|
return llvm::SmallString<16>("h3");
|
|
|
|
case HTMLTag::TAG_LI:
|
|
|
|
return llvm::SmallString<16>("li");
|
2019-07-26 05:27:50 +08:00
|
|
|
case HTMLTag::TAG_LINK:
|
|
|
|
return llvm::SmallString<16>("link");
|
2019-08-07 02:31:46 +08:00
|
|
|
case HTMLTag::TAG_META:
|
|
|
|
return llvm::SmallString<16>("meta");
|
|
|
|
case HTMLTag::TAG_P:
|
|
|
|
return llvm::SmallString<16>("p");
|
|
|
|
case HTMLTag::TAG_SCRIPT:
|
|
|
|
return llvm::SmallString<16>("script");
|
|
|
|
case HTMLTag::TAG_SPAN:
|
|
|
|
return llvm::SmallString<16>("span");
|
|
|
|
case HTMLTag::TAG_TITLE:
|
|
|
|
return llvm::SmallString<16>("title");
|
|
|
|
case HTMLTag::TAG_UL:
|
|
|
|
return llvm::SmallString<16>("ul");
|
2019-07-11 03:03:25 +08:00
|
|
|
}
|
2019-07-11 14:50:10 +08:00
|
|
|
llvm_unreachable("Unhandled HTMLTag::TagType");
|
2019-07-11 03:03:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextNode::Render(llvm::raw_ostream &OS, int IndentationLevel) {
|
2019-07-26 04:49:00 +08:00
|
|
|
OS.indent(IndentationLevel * 2);
|
2019-07-26 04:14:45 +08:00
|
|
|
printHTMLEscaped(Text, OS);
|
2019-07-11 03:03:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TagNode::Render(llvm::raw_ostream &OS, int IndentationLevel) {
|
2019-07-26 04:49:00 +08:00
|
|
|
// Children nodes are rendered in the same line if all of them are text nodes
|
|
|
|
bool InlineChildren = true;
|
|
|
|
for (const auto &C : Children)
|
|
|
|
if (C->Type == NodeType::NODE_TAG) {
|
|
|
|
InlineChildren = false;
|
|
|
|
break;
|
|
|
|
}
|
2019-07-11 03:03:25 +08:00
|
|
|
OS.indent(IndentationLevel * 2);
|
|
|
|
OS << "<" << Tag.ToString();
|
|
|
|
for (const auto &A : Attributes)
|
|
|
|
OS << " " << A.getKey() << "=\"" << A.getValue() << "\"";
|
2019-07-26 04:49:00 +08:00
|
|
|
if (Tag.IsSelfClosing()) {
|
2019-07-11 03:03:25 +08:00
|
|
|
OS << "/>";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
OS << ">";
|
|
|
|
if (!InlineChildren)
|
|
|
|
OS << "\n";
|
2019-07-13 02:32:00 +08:00
|
|
|
bool NewLineRendered = true;
|
2019-07-11 03:03:25 +08:00
|
|
|
for (const auto &C : Children) {
|
2019-07-13 02:32:00 +08:00
|
|
|
int ChildrenIndentation =
|
|
|
|
InlineChildren || !NewLineRendered ? 0 : IndentationLevel + 1;
|
2019-07-11 03:03:25 +08:00
|
|
|
C->Render(OS, ChildrenIndentation);
|
2019-07-13 02:32:00 +08:00
|
|
|
if (!InlineChildren && (C == Children.back() ||
|
|
|
|
(C->Type != NodeType::NODE_TEXT ||
|
|
|
|
(&C + 1)->get()->Type != NodeType::NODE_TEXT))) {
|
2019-07-11 03:03:25 +08:00
|
|
|
OS << "\n";
|
2019-07-13 02:32:00 +08:00
|
|
|
NewLineRendered = true;
|
|
|
|
} else
|
|
|
|
NewLineRendered = false;
|
2019-07-11 03:03:25 +08:00
|
|
|
}
|
|
|
|
if (!InlineChildren)
|
|
|
|
OS.indent(IndentationLevel * 2);
|
|
|
|
OS << "</" << Tag.ToString() << ">";
|
|
|
|
}
|
|
|
|
|
2019-07-13 02:32:00 +08:00
|
|
|
template <typename Derived, typename Base,
|
|
|
|
typename = std::enable_if<std::is_base_of<Derived, Base>::value>>
|
|
|
|
static void AppendVector(std::vector<Derived> &&New,
|
|
|
|
std::vector<Base> &Original) {
|
|
|
|
std::move(New.begin(), New.end(), std::back_inserter(Original));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the relative path that names the file path relative to the given
|
|
|
|
// directory.
|
|
|
|
static SmallString<128> computeRelativePath(StringRef FilePath,
|
|
|
|
StringRef Directory) {
|
|
|
|
StringRef Path = FilePath;
|
|
|
|
while (!Path.empty()) {
|
|
|
|
if (Directory == Path)
|
|
|
|
return FilePath.substr(Path.size());
|
|
|
|
Path = llvm::sys::path::parent_path(Path);
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef Dir = Directory;
|
|
|
|
SmallString<128> Result;
|
|
|
|
while (!Dir.empty()) {
|
|
|
|
if (Dir == FilePath)
|
|
|
|
break;
|
|
|
|
Dir = llvm::sys::path::parent_path(Dir);
|
|
|
|
llvm::sys::path::append(Result, "..");
|
|
|
|
}
|
|
|
|
llvm::sys::path::append(Result, FilePath.substr(Dir.size()));
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2019-07-11 03:03:25 +08:00
|
|
|
// HTML generation
|
|
|
|
|
2019-08-07 02:31:46 +08:00
|
|
|
static std::vector<std::unique_ptr<TagNode>>
|
2019-07-26 06:46:40 +08:00
|
|
|
genStylesheetsHTML(StringRef InfoPath, const ClangDocContext &CDCtx) {
|
|
|
|
std::vector<std::unique_ptr<TagNode>> Out;
|
|
|
|
for (const auto &FilePath : CDCtx.UserStylesheets) {
|
|
|
|
auto LinkNode = llvm::make_unique<TagNode>(HTMLTag::TAG_LINK);
|
|
|
|
LinkNode->Attributes.try_emplace("rel", "stylesheet");
|
|
|
|
SmallString<128> StylesheetPath = computeRelativePath("", InfoPath);
|
|
|
|
llvm::sys::path::append(StylesheetPath,
|
|
|
|
llvm::sys::path::filename(FilePath));
|
2019-07-27 02:02:42 +08:00
|
|
|
// Paths in HTML must be in posix-style
|
|
|
|
llvm::sys::path::native(StylesheetPath, llvm::sys::path::Style::posix);
|
2019-07-26 06:46:40 +08:00
|
|
|
LinkNode->Attributes.try_emplace("href", StylesheetPath);
|
|
|
|
Out.emplace_back(std::move(LinkNode));
|
|
|
|
}
|
|
|
|
return Out;
|
|
|
|
}
|
|
|
|
|
2019-08-07 02:31:46 +08:00
|
|
|
static std::vector<std::unique_ptr<TagNode>>
|
|
|
|
genJsScriptsHTML(StringRef InfoPath, const ClangDocContext &CDCtx) {
|
|
|
|
std::vector<std::unique_ptr<TagNode>> Out;
|
|
|
|
for (const auto &FilePath : CDCtx.JsScripts) {
|
|
|
|
auto ScriptNode = llvm::make_unique<TagNode>(HTMLTag::TAG_SCRIPT);
|
|
|
|
SmallString<128> ScriptPath = computeRelativePath("", InfoPath);
|
|
|
|
llvm::sys::path::append(ScriptPath, llvm::sys::path::filename(FilePath));
|
2019-08-07 04:59:14 +08:00
|
|
|
// Paths in HTML must be in posix-style
|
|
|
|
llvm::sys::path::native(ScriptPath, llvm::sys::path::Style::posix);
|
2019-08-07 02:31:46 +08:00
|
|
|
ScriptNode->Attributes.try_emplace("src", ScriptPath);
|
|
|
|
Out.emplace_back(std::move(ScriptNode));
|
|
|
|
}
|
|
|
|
return Out;
|
|
|
|
}
|
|
|
|
|
2019-07-13 02:32:00 +08:00
|
|
|
static std::unique_ptr<TagNode> genLink(const Twine &Text, const Twine &Link) {
|
|
|
|
auto LinkNode = llvm::make_unique<TagNode>(HTMLTag::TAG_A, Text);
|
|
|
|
LinkNode->Attributes.try_emplace("href", Link.str());
|
|
|
|
return LinkNode;
|
|
|
|
}
|
|
|
|
|
2019-08-08 05:04:52 +08:00
|
|
|
static std::unique_ptr<HTMLNode>
|
|
|
|
genTypeReference(const Reference &Type, StringRef CurrentDirectory,
|
|
|
|
llvm::Optional<StringRef> JumpToSection = None) {
|
|
|
|
if (Type.Path.empty() && !Type.IsInGlobalNamespace) {
|
|
|
|
if (!JumpToSection)
|
|
|
|
return llvm::make_unique<TextNode>(Type.Name);
|
|
|
|
else
|
|
|
|
return genLink(Type.Name, "#" + JumpToSection.getValue());
|
|
|
|
}
|
2019-07-13 02:32:00 +08:00
|
|
|
llvm::SmallString<128> Path =
|
|
|
|
computeRelativePath(Type.Path, CurrentDirectory);
|
|
|
|
llvm::sys::path::append(Path, Type.Name + ".html");
|
2019-07-30 06:03:39 +08:00
|
|
|
// Paths in HTML must be in posix-style
|
|
|
|
llvm::sys::path::native(Path, llvm::sys::path::Style::posix);
|
2019-08-08 05:04:52 +08:00
|
|
|
if (JumpToSection)
|
|
|
|
Path += ("#" + JumpToSection.getValue()).str();
|
2019-07-13 02:32:00 +08:00
|
|
|
return genLink(Type.Name, Path);
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::vector<std::unique_ptr<HTMLNode>>
|
|
|
|
genReferenceList(const llvm::SmallVectorImpl<Reference> &Refs,
|
|
|
|
const StringRef &CurrentDirectory) {
|
|
|
|
std::vector<std::unique_ptr<HTMLNode>> Out;
|
|
|
|
for (const auto &R : Refs) {
|
|
|
|
if (&R != Refs.begin())
|
|
|
|
Out.emplace_back(llvm::make_unique<TextNode>(", "));
|
|
|
|
Out.emplace_back(genTypeReference(R, CurrentDirectory));
|
|
|
|
}
|
|
|
|
return Out;
|
|
|
|
}
|
|
|
|
|
2019-08-10 01:49:41 +08:00
|
|
|
static std::vector<std::unique_ptr<TagNode>>
|
|
|
|
genHTML(const EnumInfo &I, const ClangDocContext &CDCtx);
|
|
|
|
static std::vector<std::unique_ptr<TagNode>>
|
|
|
|
genHTML(const FunctionInfo &I, const ClangDocContext &CDCtx,
|
|
|
|
StringRef ParentInfoDir);
|
2019-07-11 03:03:25 +08:00
|
|
|
|
|
|
|
static std::vector<std::unique_ptr<TagNode>>
|
2019-08-10 01:49:41 +08:00
|
|
|
genEnumsBlock(const std::vector<EnumInfo> &Enums,
|
|
|
|
const ClangDocContext &CDCtx) {
|
2019-07-11 03:03:25 +08:00
|
|
|
if (Enums.empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
std::vector<std::unique_ptr<TagNode>> Out;
|
|
|
|
Out.emplace_back(llvm::make_unique<TagNode>(HTMLTag::TAG_H2, "Enums"));
|
2019-08-08 05:04:52 +08:00
|
|
|
Out.back()->Attributes.try_emplace("id", "Enums");
|
2019-07-11 03:03:25 +08:00
|
|
|
Out.emplace_back(llvm::make_unique<TagNode>(HTMLTag::TAG_DIV));
|
|
|
|
auto &DivBody = Out.back();
|
|
|
|
for (const auto &E : Enums) {
|
2019-08-10 01:49:41 +08:00
|
|
|
std::vector<std::unique_ptr<TagNode>> Nodes = genHTML(E, CDCtx);
|
2019-07-11 03:03:25 +08:00
|
|
|
AppendVector(std::move(Nodes), DivBody->Children);
|
|
|
|
}
|
|
|
|
return Out;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::unique_ptr<TagNode>
|
|
|
|
genEnumMembersBlock(const llvm::SmallVector<SmallString<16>, 4> &Members) {
|
|
|
|
if (Members.empty())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
auto List = llvm::make_unique<TagNode>(HTMLTag::TAG_UL);
|
|
|
|
for (const auto &M : Members)
|
|
|
|
List->Children.emplace_back(llvm::make_unique<TagNode>(HTMLTag::TAG_LI, M));
|
|
|
|
return List;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::vector<std::unique_ptr<TagNode>>
|
2019-07-13 02:32:00 +08:00
|
|
|
genFunctionsBlock(const std::vector<FunctionInfo> &Functions,
|
2019-08-10 01:49:41 +08:00
|
|
|
const ClangDocContext &CDCtx, StringRef ParentInfoDir) {
|
2019-07-11 03:03:25 +08:00
|
|
|
if (Functions.empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
std::vector<std::unique_ptr<TagNode>> Out;
|
|
|
|
Out.emplace_back(llvm::make_unique<TagNode>(HTMLTag::TAG_H2, "Functions"));
|
2019-08-08 05:04:52 +08:00
|
|
|
Out.back()->Attributes.try_emplace("id", "Functions");
|
2019-07-11 03:03:25 +08:00
|
|
|
Out.emplace_back(llvm::make_unique<TagNode>(HTMLTag::TAG_DIV));
|
|
|
|
auto &DivBody = Out.back();
|
|
|
|
for (const auto &F : Functions) {
|
2019-08-10 01:49:41 +08:00
|
|
|
std::vector<std::unique_ptr<TagNode>> Nodes =
|
|
|
|
genHTML(F, CDCtx, ParentInfoDir);
|
2019-07-11 03:03:25 +08:00
|
|
|
AppendVector(std::move(Nodes), DivBody->Children);
|
|
|
|
}
|
|
|
|
return Out;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::vector<std::unique_ptr<TagNode>>
|
2019-07-13 02:32:00 +08:00
|
|
|
genRecordMembersBlock(const llvm::SmallVector<MemberTypeInfo, 4> &Members,
|
|
|
|
StringRef ParentInfoDir) {
|
2019-07-11 03:03:25 +08:00
|
|
|
if (Members.empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
std::vector<std::unique_ptr<TagNode>> Out;
|
|
|
|
Out.emplace_back(llvm::make_unique<TagNode>(HTMLTag::TAG_H2, "Members"));
|
2019-08-08 05:04:52 +08:00
|
|
|
Out.back()->Attributes.try_emplace("id", "Members");
|
2019-07-11 03:03:25 +08:00
|
|
|
Out.emplace_back(llvm::make_unique<TagNode>(HTMLTag::TAG_UL));
|
|
|
|
auto &ULBody = Out.back();
|
|
|
|
for (const auto &M : Members) {
|
|
|
|
std::string Access = getAccess(M.Access);
|
|
|
|
if (Access != "")
|
|
|
|
Access = Access + " ";
|
2019-07-13 02:32:00 +08:00
|
|
|
auto LIBody = llvm::make_unique<TagNode>(HTMLTag::TAG_LI);
|
|
|
|
LIBody->Children.emplace_back(llvm::make_unique<TextNode>(Access));
|
|
|
|
LIBody->Children.emplace_back(genTypeReference(M.Type, ParentInfoDir));
|
|
|
|
LIBody->Children.emplace_back(llvm::make_unique<TextNode>(" " + M.Name));
|
|
|
|
ULBody->Children.emplace_back(std::move(LIBody));
|
2019-07-11 03:03:25 +08:00
|
|
|
}
|
|
|
|
return Out;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::vector<std::unique_ptr<TagNode>>
|
|
|
|
genReferencesBlock(const std::vector<Reference> &References,
|
|
|
|
llvm::StringRef Title) {
|
|
|
|
if (References.empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
std::vector<std::unique_ptr<TagNode>> Out;
|
|
|
|
Out.emplace_back(llvm::make_unique<TagNode>(HTMLTag::TAG_H2, Title));
|
2019-08-08 05:04:52 +08:00
|
|
|
Out.back()->Attributes.try_emplace("id", Title);
|
2019-07-11 03:03:25 +08:00
|
|
|
Out.emplace_back(llvm::make_unique<TagNode>(HTMLTag::TAG_UL));
|
|
|
|
auto &ULBody = Out.back();
|
|
|
|
for (const auto &R : References)
|
|
|
|
ULBody->Children.emplace_back(
|
|
|
|
llvm::make_unique<TagNode>(HTMLTag::TAG_LI, R.Name));
|
|
|
|
return Out;
|
|
|
|
}
|
|
|
|
|
2019-08-10 01:49:41 +08:00
|
|
|
static std::unique_ptr<TagNode>
|
|
|
|
writeFileDefinition(const Location &L,
|
|
|
|
llvm::Optional<StringRef> RepositoryUrl = None) {
|
|
|
|
if (!L.IsFileInRootDir || !RepositoryUrl)
|
|
|
|
return llvm::make_unique<TagNode>(
|
|
|
|
HTMLTag::TAG_P, "Defined at line " + std::to_string(L.LineNumber) +
|
|
|
|
" of file " + L.Filename);
|
|
|
|
SmallString<128> FileURL(RepositoryUrl.getValue());
|
|
|
|
llvm::sys::path::append(FileURL, llvm::sys::path::Style::posix, L.Filename);
|
|
|
|
auto Node = llvm::make_unique<TagNode>(HTMLTag::TAG_P);
|
|
|
|
Node->Children.emplace_back(llvm::make_unique<TextNode>("Defined at line "));
|
|
|
|
auto LocNumberNode =
|
|
|
|
llvm::make_unique<TagNode>(HTMLTag::TAG_A, std::to_string(L.LineNumber));
|
|
|
|
// The links to a specific line in the source code use the github /
|
|
|
|
// googlesource notation so it won't work for all hosting pages.
|
|
|
|
LocNumberNode->Attributes.try_emplace(
|
|
|
|
"href", (FileURL + "#" + std::to_string(L.LineNumber)).str());
|
|
|
|
Node->Children.emplace_back(std::move(LocNumberNode));
|
|
|
|
Node->Children.emplace_back(llvm::make_unique<TextNode>(" of file "));
|
|
|
|
auto LocFileNode = llvm::make_unique<TagNode>(
|
|
|
|
HTMLTag::TAG_A, llvm::sys::path::filename(FileURL));
|
|
|
|
LocFileNode->Attributes.try_emplace("href", FileURL);
|
|
|
|
Node->Children.emplace_back(std::move(LocFileNode));
|
|
|
|
return Node;
|
2019-07-11 03:03:25 +08:00
|
|
|
}
|
|
|
|
|
2019-08-07 02:31:46 +08:00
|
|
|
static std::vector<std::unique_ptr<TagNode>>
|
|
|
|
genCommonFileNodes(StringRef Title, StringRef InfoPath,
|
|
|
|
const ClangDocContext &CDCtx) {
|
|
|
|
std::vector<std::unique_ptr<TagNode>> Out;
|
|
|
|
auto MetaNode = llvm::make_unique<TagNode>(HTMLTag::TAG_META);
|
|
|
|
MetaNode->Attributes.try_emplace("charset", "utf-8");
|
|
|
|
Out.emplace_back(std::move(MetaNode));
|
|
|
|
Out.emplace_back(llvm::make_unique<TagNode>(HTMLTag::TAG_TITLE, Title));
|
|
|
|
std::vector<std::unique_ptr<TagNode>> StylesheetsNodes =
|
|
|
|
genStylesheetsHTML(InfoPath, CDCtx);
|
|
|
|
AppendVector(std::move(StylesheetsNodes), Out);
|
|
|
|
std::vector<std::unique_ptr<TagNode>> JsNodes =
|
|
|
|
genJsScriptsHTML(InfoPath, CDCtx);
|
|
|
|
AppendVector(std::move(JsNodes), Out);
|
|
|
|
// An empty <div> is generated but the index will be then rendered here
|
|
|
|
auto IndexNode = llvm::make_unique<TagNode>(HTMLTag::TAG_DIV);
|
|
|
|
IndexNode->Attributes.try_emplace("id", "index");
|
|
|
|
IndexNode->Attributes.try_emplace("path", InfoPath);
|
|
|
|
Out.emplace_back(std::move(IndexNode));
|
|
|
|
return Out;
|
|
|
|
}
|
|
|
|
|
2019-08-08 05:04:52 +08:00
|
|
|
template <typename T,
|
|
|
|
typename = std::enable_if<std::is_base_of<T, Info>::value>>
|
|
|
|
static Index genInfoIndexItem(const std::vector<T> &Infos, StringRef Title) {
|
|
|
|
Index Idx(Title, Title);
|
|
|
|
for (const auto &C : Infos)
|
|
|
|
Idx.Children.emplace_back(C.extractName(),
|
|
|
|
llvm::toHex(llvm::toStringRef(C.USR)));
|
|
|
|
return Idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::vector<std::unique_ptr<TagNode>> genHTML(const Index &Index,
|
|
|
|
StringRef InfoPath) {
|
|
|
|
std::vector<std::unique_ptr<TagNode>> Out;
|
|
|
|
if (!Index.Name.empty()) {
|
|
|
|
Out.emplace_back(llvm::make_unique<TagNode>(HTMLTag::TAG_SPAN));
|
|
|
|
auto &SpanBody = Out.back();
|
|
|
|
if (!Index.JumpToSection)
|
|
|
|
SpanBody->Children.emplace_back(genTypeReference(Index, InfoPath));
|
|
|
|
else
|
|
|
|
SpanBody->Children.emplace_back(genTypeReference(
|
|
|
|
Index, InfoPath, StringRef{Index.JumpToSection.getValue()}));
|
|
|
|
}
|
|
|
|
if (Index.Children.empty())
|
|
|
|
return Out;
|
|
|
|
Out.emplace_back(llvm::make_unique<TagNode>(HTMLTag::TAG_UL));
|
|
|
|
const auto &UlBody = Out.back();
|
|
|
|
for (const auto &C : Index.Children) {
|
|
|
|
auto LiBody = llvm::make_unique<TagNode>(HTMLTag::TAG_LI);
|
|
|
|
std::vector<std::unique_ptr<TagNode>> Nodes = genHTML(C, InfoPath);
|
|
|
|
AppendVector(std::move(Nodes), LiBody->Children);
|
|
|
|
UlBody->Children.emplace_back(std::move(LiBody));
|
|
|
|
}
|
|
|
|
return Out;
|
|
|
|
}
|
|
|
|
|
2019-07-11 03:03:25 +08:00
|
|
|
static std::unique_ptr<HTMLNode> genHTML(const CommentInfo &I) {
|
|
|
|
if (I.Kind == "FullComment") {
|
|
|
|
auto FullComment = llvm::make_unique<TagNode>(HTMLTag::TAG_DIV);
|
|
|
|
for (const auto &Child : I.Children) {
|
|
|
|
std::unique_ptr<HTMLNode> Node = genHTML(*Child);
|
|
|
|
if (Node)
|
|
|
|
FullComment->Children.emplace_back(std::move(Node));
|
|
|
|
}
|
|
|
|
return std::move(FullComment);
|
|
|
|
} else if (I.Kind == "ParagraphComment") {
|
|
|
|
auto ParagraphComment = llvm::make_unique<TagNode>(HTMLTag::TAG_P);
|
|
|
|
for (const auto &Child : I.Children) {
|
|
|
|
std::unique_ptr<HTMLNode> Node = genHTML(*Child);
|
|
|
|
if (Node)
|
|
|
|
ParagraphComment->Children.emplace_back(std::move(Node));
|
|
|
|
}
|
|
|
|
if (ParagraphComment->Children.empty())
|
|
|
|
return nullptr;
|
|
|
|
return std::move(ParagraphComment);
|
|
|
|
} else if (I.Kind == "TextComment") {
|
|
|
|
if (I.Text == "")
|
|
|
|
return nullptr;
|
2019-07-26 04:49:00 +08:00
|
|
|
return llvm::make_unique<TextNode>(I.Text);
|
2019-07-11 03:03:25 +08:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::unique_ptr<TagNode> genHTML(const std::vector<CommentInfo> &C) {
|
|
|
|
auto CommentBlock = llvm::make_unique<TagNode>(HTMLTag::TAG_DIV);
|
|
|
|
for (const auto &Child : C) {
|
|
|
|
if (std::unique_ptr<HTMLNode> Node = genHTML(Child))
|
|
|
|
CommentBlock->Children.emplace_back(std::move(Node));
|
|
|
|
}
|
|
|
|
return CommentBlock;
|
|
|
|
}
|
|
|
|
|
2019-08-10 01:49:41 +08:00
|
|
|
static std::vector<std::unique_ptr<TagNode>>
|
|
|
|
genHTML(const EnumInfo &I, const ClangDocContext &CDCtx) {
|
2019-07-11 03:03:25 +08:00
|
|
|
std::vector<std::unique_ptr<TagNode>> Out;
|
|
|
|
std::string EnumType;
|
|
|
|
if (I.Scoped)
|
|
|
|
EnumType = "enum class ";
|
|
|
|
else
|
|
|
|
EnumType = "enum ";
|
|
|
|
|
|
|
|
Out.emplace_back(
|
|
|
|
llvm::make_unique<TagNode>(HTMLTag::TAG_H3, EnumType + I.Name));
|
2019-08-08 05:04:52 +08:00
|
|
|
Out.back()->Attributes.try_emplace("id",
|
|
|
|
llvm::toHex(llvm::toStringRef(I.USR)));
|
2019-07-11 03:03:25 +08:00
|
|
|
|
|
|
|
std::unique_ptr<TagNode> Node = genEnumMembersBlock(I.Members);
|
|
|
|
if (Node)
|
|
|
|
Out.emplace_back(std::move(Node));
|
|
|
|
|
2019-08-10 01:49:41 +08:00
|
|
|
if (I.DefLoc) {
|
|
|
|
if (!CDCtx.RepositoryUrl)
|
|
|
|
Out.emplace_back(writeFileDefinition(I.DefLoc.getValue()));
|
|
|
|
else
|
|
|
|
Out.emplace_back(writeFileDefinition(
|
|
|
|
I.DefLoc.getValue(), StringRef{CDCtx.RepositoryUrl.getValue()}));
|
|
|
|
}
|
2019-07-11 03:03:25 +08:00
|
|
|
|
|
|
|
std::string Description;
|
|
|
|
if (!I.Description.empty())
|
|
|
|
Out.emplace_back(genHTML(I.Description));
|
|
|
|
|
|
|
|
return Out;
|
|
|
|
}
|
|
|
|
|
2019-08-10 01:49:41 +08:00
|
|
|
static std::vector<std::unique_ptr<TagNode>>
|
|
|
|
genHTML(const FunctionInfo &I, const ClangDocContext &CDCtx,
|
|
|
|
StringRef ParentInfoDir) {
|
2019-07-11 03:03:25 +08:00
|
|
|
std::vector<std::unique_ptr<TagNode>> Out;
|
|
|
|
Out.emplace_back(llvm::make_unique<TagNode>(HTMLTag::TAG_H3, I.Name));
|
2019-08-08 05:04:52 +08:00
|
|
|
// USR is used as id for functions instead of name to disambiguate function
|
|
|
|
// overloads.
|
|
|
|
Out.back()->Attributes.try_emplace("id",
|
|
|
|
llvm::toHex(llvm::toStringRef(I.USR)));
|
2019-07-11 03:03:25 +08:00
|
|
|
|
2019-07-13 02:32:00 +08:00
|
|
|
Out.emplace_back(llvm::make_unique<TagNode>(HTMLTag::TAG_P));
|
|
|
|
auto &FunctionHeader = Out.back();
|
2019-07-11 03:03:25 +08:00
|
|
|
|
|
|
|
std::string Access = getAccess(I.Access);
|
|
|
|
if (Access != "")
|
2019-07-13 02:32:00 +08:00
|
|
|
FunctionHeader->Children.emplace_back(
|
|
|
|
llvm::make_unique<TextNode>(Access + " "));
|
|
|
|
if (I.ReturnType.Type.Name != "") {
|
|
|
|
FunctionHeader->Children.emplace_back(
|
|
|
|
genTypeReference(I.ReturnType.Type, ParentInfoDir));
|
|
|
|
FunctionHeader->Children.emplace_back(llvm::make_unique<TextNode>(" "));
|
|
|
|
}
|
|
|
|
FunctionHeader->Children.emplace_back(
|
|
|
|
llvm::make_unique<TextNode>(I.Name + "("));
|
2019-07-11 03:03:25 +08:00
|
|
|
|
2019-07-13 02:32:00 +08:00
|
|
|
for (const auto &P : I.Params) {
|
|
|
|
if (&P != I.Params.begin())
|
|
|
|
FunctionHeader->Children.emplace_back(llvm::make_unique<TextNode>(", "));
|
|
|
|
FunctionHeader->Children.emplace_back(
|
|
|
|
genTypeReference(P.Type, ParentInfoDir));
|
|
|
|
FunctionHeader->Children.emplace_back(
|
|
|
|
llvm::make_unique<TextNode>(" " + P.Name));
|
|
|
|
}
|
|
|
|
FunctionHeader->Children.emplace_back(llvm::make_unique<TextNode>(")"));
|
2019-07-11 03:03:25 +08:00
|
|
|
|
2019-08-10 01:49:41 +08:00
|
|
|
if (I.DefLoc) {
|
|
|
|
if (!CDCtx.RepositoryUrl)
|
|
|
|
Out.emplace_back(writeFileDefinition(I.DefLoc.getValue()));
|
|
|
|
else
|
|
|
|
Out.emplace_back(writeFileDefinition(
|
|
|
|
I.DefLoc.getValue(), StringRef{CDCtx.RepositoryUrl.getValue()}));
|
|
|
|
}
|
2019-07-11 03:03:25 +08:00
|
|
|
|
|
|
|
std::string Description;
|
|
|
|
if (!I.Description.empty())
|
|
|
|
Out.emplace_back(genHTML(I.Description));
|
|
|
|
|
|
|
|
return Out;
|
|
|
|
}
|
|
|
|
|
2019-08-08 05:04:52 +08:00
|
|
|
static std::vector<std::unique_ptr<TagNode>>
|
2019-08-10 01:49:41 +08:00
|
|
|
genHTML(const NamespaceInfo &I, Index &InfoIndex, const ClangDocContext &CDCtx,
|
|
|
|
std::string &InfoTitle) {
|
2019-07-11 03:03:25 +08:00
|
|
|
std::vector<std::unique_ptr<TagNode>> Out;
|
|
|
|
if (I.Name.str() == "")
|
|
|
|
InfoTitle = "Global Namespace";
|
|
|
|
else
|
|
|
|
InfoTitle = ("namespace " + I.Name).str();
|
|
|
|
|
|
|
|
Out.emplace_back(llvm::make_unique<TagNode>(HTMLTag::TAG_H1, InfoTitle));
|
|
|
|
|
|
|
|
std::string Description;
|
|
|
|
if (!I.Description.empty())
|
|
|
|
Out.emplace_back(genHTML(I.Description));
|
|
|
|
|
|
|
|
std::vector<std::unique_ptr<TagNode>> ChildNamespaces =
|
|
|
|
genReferencesBlock(I.ChildNamespaces, "Namespaces");
|
|
|
|
AppendVector(std::move(ChildNamespaces), Out);
|
|
|
|
std::vector<std::unique_ptr<TagNode>> ChildRecords =
|
|
|
|
genReferencesBlock(I.ChildRecords, "Records");
|
|
|
|
AppendVector(std::move(ChildRecords), Out);
|
|
|
|
|
|
|
|
std::vector<std::unique_ptr<TagNode>> ChildFunctions =
|
2019-08-10 01:49:41 +08:00
|
|
|
genFunctionsBlock(I.ChildFunctions, CDCtx, I.Path);
|
2019-07-11 03:03:25 +08:00
|
|
|
AppendVector(std::move(ChildFunctions), Out);
|
|
|
|
std::vector<std::unique_ptr<TagNode>> ChildEnums =
|
2019-08-10 01:49:41 +08:00
|
|
|
genEnumsBlock(I.ChildEnums, CDCtx);
|
2019-07-11 03:03:25 +08:00
|
|
|
AppendVector(std::move(ChildEnums), Out);
|
|
|
|
|
2019-08-08 05:04:52 +08:00
|
|
|
if (!I.ChildNamespaces.empty())
|
|
|
|
InfoIndex.Children.emplace_back("Namespaces", "Namespaces");
|
|
|
|
if (!I.ChildRecords.empty())
|
|
|
|
InfoIndex.Children.emplace_back("Records", "Records");
|
|
|
|
if (!I.ChildFunctions.empty())
|
|
|
|
InfoIndex.Children.emplace_back(
|
|
|
|
genInfoIndexItem(I.ChildFunctions, "Functions"));
|
|
|
|
if (!I.ChildEnums.empty())
|
|
|
|
InfoIndex.Children.emplace_back(genInfoIndexItem(I.ChildEnums, "Enums"));
|
|
|
|
|
2019-07-11 03:03:25 +08:00
|
|
|
return Out;
|
|
|
|
}
|
|
|
|
|
2019-08-08 05:04:52 +08:00
|
|
|
static std::vector<std::unique_ptr<TagNode>>
|
2019-08-10 01:49:41 +08:00
|
|
|
genHTML(const RecordInfo &I, Index &InfoIndex, const ClangDocContext &CDCtx,
|
|
|
|
std::string &InfoTitle) {
|
2019-07-11 03:03:25 +08:00
|
|
|
std::vector<std::unique_ptr<TagNode>> Out;
|
|
|
|
InfoTitle = (getTagType(I.TagType) + " " + I.Name).str();
|
|
|
|
Out.emplace_back(llvm::make_unique<TagNode>(HTMLTag::TAG_H1, InfoTitle));
|
|
|
|
|
2019-08-10 01:49:41 +08:00
|
|
|
if (I.DefLoc) {
|
|
|
|
if (!CDCtx.RepositoryUrl)
|
|
|
|
Out.emplace_back(writeFileDefinition(I.DefLoc.getValue()));
|
|
|
|
else
|
|
|
|
Out.emplace_back(writeFileDefinition(
|
|
|
|
I.DefLoc.getValue(), StringRef{CDCtx.RepositoryUrl.getValue()}));
|
|
|
|
}
|
2019-07-11 03:03:25 +08:00
|
|
|
|
|
|
|
std::string Description;
|
|
|
|
if (!I.Description.empty())
|
|
|
|
Out.emplace_back(genHTML(I.Description));
|
|
|
|
|
2019-07-13 02:32:00 +08:00
|
|
|
std::vector<std::unique_ptr<HTMLNode>> Parents =
|
|
|
|
genReferenceList(I.Parents, I.Path);
|
|
|
|
std::vector<std::unique_ptr<HTMLNode>> VParents =
|
|
|
|
genReferenceList(I.VirtualParents, I.Path);
|
2019-07-11 03:03:25 +08:00
|
|
|
if (!Parents.empty() || !VParents.empty()) {
|
2019-07-13 02:32:00 +08:00
|
|
|
Out.emplace_back(llvm::make_unique<TagNode>(HTMLTag::TAG_P));
|
|
|
|
auto &PBody = Out.back();
|
|
|
|
PBody->Children.emplace_back(llvm::make_unique<TextNode>("Inherits from "));
|
2019-07-11 03:03:25 +08:00
|
|
|
if (Parents.empty())
|
2019-07-13 02:32:00 +08:00
|
|
|
AppendVector(std::move(VParents), PBody->Children);
|
2019-07-11 03:03:25 +08:00
|
|
|
else if (VParents.empty())
|
2019-07-13 02:32:00 +08:00
|
|
|
AppendVector(std::move(Parents), PBody->Children);
|
|
|
|
else {
|
|
|
|
AppendVector(std::move(Parents), PBody->Children);
|
|
|
|
PBody->Children.emplace_back(llvm::make_unique<TextNode>(", "));
|
|
|
|
AppendVector(std::move(VParents), PBody->Children);
|
|
|
|
}
|
2019-07-11 03:03:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::unique_ptr<TagNode>> Members =
|
2019-07-13 02:32:00 +08:00
|
|
|
genRecordMembersBlock(I.Members, I.Path);
|
2019-07-11 03:03:25 +08:00
|
|
|
AppendVector(std::move(Members), Out);
|
|
|
|
std::vector<std::unique_ptr<TagNode>> ChildRecords =
|
|
|
|
genReferencesBlock(I.ChildRecords, "Records");
|
|
|
|
AppendVector(std::move(ChildRecords), Out);
|
|
|
|
|
|
|
|
std::vector<std::unique_ptr<TagNode>> ChildFunctions =
|
2019-08-10 01:49:41 +08:00
|
|
|
genFunctionsBlock(I.ChildFunctions, CDCtx, I.Path);
|
2019-07-11 03:03:25 +08:00
|
|
|
AppendVector(std::move(ChildFunctions), Out);
|
|
|
|
std::vector<std::unique_ptr<TagNode>> ChildEnums =
|
2019-08-10 01:49:41 +08:00
|
|
|
genEnumsBlock(I.ChildEnums, CDCtx);
|
2019-07-11 03:03:25 +08:00
|
|
|
AppendVector(std::move(ChildEnums), Out);
|
|
|
|
|
2019-08-08 05:04:52 +08:00
|
|
|
if (!I.Members.empty())
|
|
|
|
InfoIndex.Children.emplace_back("Members", "Members");
|
|
|
|
if (!I.ChildRecords.empty())
|
|
|
|
InfoIndex.Children.emplace_back("Records", "Records");
|
|
|
|
if (!I.ChildFunctions.empty())
|
|
|
|
InfoIndex.Children.emplace_back(
|
|
|
|
genInfoIndexItem(I.ChildFunctions, "Functions"));
|
|
|
|
if (!I.ChildEnums.empty())
|
|
|
|
InfoIndex.Children.emplace_back(genInfoIndexItem(I.ChildEnums, "Enums"));
|
|
|
|
|
2019-07-11 03:03:25 +08:00
|
|
|
return Out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generator for HTML documentation.
|
|
|
|
class HTMLGenerator : public Generator {
|
|
|
|
public:
|
|
|
|
static const char *Format;
|
|
|
|
|
2019-07-26 06:46:40 +08:00
|
|
|
llvm::Error generateDocForInfo(Info *I, llvm::raw_ostream &OS,
|
|
|
|
const ClangDocContext &CDCtx) override;
|
2019-08-07 02:31:46 +08:00
|
|
|
bool createResources(ClangDocContext &CDCtx) override;
|
2019-07-11 03:03:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const char *HTMLGenerator::Format = "html";
|
|
|
|
|
2019-07-26 06:46:40 +08:00
|
|
|
llvm::Error HTMLGenerator::generateDocForInfo(Info *I, llvm::raw_ostream &OS,
|
|
|
|
const ClangDocContext &CDCtx) {
|
2019-07-11 03:03:25 +08:00
|
|
|
HTMLFile F;
|
|
|
|
std::string InfoTitle;
|
|
|
|
auto MainContentNode = llvm::make_unique<TagNode>(HTMLTag::TAG_DIV);
|
2019-08-08 05:04:52 +08:00
|
|
|
Index InfoIndex;
|
2019-07-11 03:03:25 +08:00
|
|
|
switch (I->IT) {
|
|
|
|
case InfoType::IT_namespace: {
|
2019-08-10 01:49:41 +08:00
|
|
|
std::vector<std::unique_ptr<TagNode>> Nodes =
|
|
|
|
genHTML(*static_cast<clang::doc::NamespaceInfo *>(I), InfoIndex, CDCtx,
|
|
|
|
InfoTitle);
|
2019-07-11 03:03:25 +08:00
|
|
|
AppendVector(std::move(Nodes), MainContentNode->Children);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case InfoType::IT_record: {
|
2019-08-08 05:04:52 +08:00
|
|
|
std::vector<std::unique_ptr<TagNode>> Nodes = genHTML(
|
2019-08-10 01:49:41 +08:00
|
|
|
*static_cast<clang::doc::RecordInfo *>(I), InfoIndex, CDCtx, InfoTitle);
|
2019-07-11 03:03:25 +08:00
|
|
|
AppendVector(std::move(Nodes), MainContentNode->Children);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case InfoType::IT_enum: {
|
|
|
|
std::vector<std::unique_ptr<TagNode>> Nodes =
|
2019-08-10 01:49:41 +08:00
|
|
|
genHTML(*static_cast<clang::doc::EnumInfo *>(I), CDCtx);
|
2019-07-11 03:03:25 +08:00
|
|
|
AppendVector(std::move(Nodes), MainContentNode->Children);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case InfoType::IT_function: {
|
|
|
|
std::vector<std::unique_ptr<TagNode>> Nodes =
|
2019-08-10 01:49:41 +08:00
|
|
|
genHTML(*static_cast<clang::doc::FunctionInfo *>(I), CDCtx, "");
|
2019-07-11 03:03:25 +08:00
|
|
|
AppendVector(std::move(Nodes), MainContentNode->Children);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case InfoType::IT_default:
|
|
|
|
return llvm::make_error<llvm::StringError>("Unexpected info type.\n",
|
|
|
|
llvm::inconvertibleErrorCode());
|
|
|
|
}
|
|
|
|
|
2019-08-07 02:31:46 +08:00
|
|
|
std::vector<std::unique_ptr<TagNode>> BasicNodes =
|
|
|
|
genCommonFileNodes(InfoTitle, I->Path, CDCtx);
|
|
|
|
AppendVector(std::move(BasicNodes), F.Children);
|
2019-08-08 05:04:52 +08:00
|
|
|
std::vector<std::unique_ptr<TagNode>> InfoIndexHTML =
|
|
|
|
genHTML(InfoIndex, I->Path);
|
|
|
|
AppendVector(std::move(InfoIndexHTML), F.Children);
|
2019-07-11 03:03:25 +08:00
|
|
|
F.Children.emplace_back(std::move(MainContentNode));
|
|
|
|
F.Render(OS);
|
|
|
|
|
|
|
|
return llvm::Error::success();
|
|
|
|
}
|
|
|
|
|
2019-08-07 02:31:46 +08:00
|
|
|
static std::string getRefType(InfoType IT) {
|
|
|
|
switch (IT) {
|
|
|
|
case InfoType::IT_default:
|
|
|
|
return "default";
|
|
|
|
case InfoType::IT_namespace:
|
|
|
|
return "namespace";
|
|
|
|
case InfoType::IT_record:
|
|
|
|
return "record";
|
|
|
|
case InfoType::IT_function:
|
|
|
|
return "function";
|
|
|
|
case InfoType::IT_enum:
|
|
|
|
return "enum";
|
|
|
|
}
|
|
|
|
llvm_unreachable("Unknown InfoType");
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool SerializeIndex(ClangDocContext &CDCtx) {
|
|
|
|
std::error_code OK;
|
|
|
|
std::error_code FileErr;
|
|
|
|
llvm::SmallString<128> FilePath;
|
|
|
|
llvm::sys::path::native(CDCtx.OutDirectory, FilePath);
|
|
|
|
llvm::sys::path::append(FilePath, "index_json.js");
|
|
|
|
llvm::raw_fd_ostream OS(FilePath, FileErr, llvm::sys::fs::F_None);
|
|
|
|
if (FileErr != OK) {
|
|
|
|
llvm::errs() << "Error creating index file: " << FileErr.message() << "\n";
|
|
|
|
return false;
|
2019-07-26 05:27:50 +08:00
|
|
|
}
|
2019-08-07 02:31:46 +08:00
|
|
|
CDCtx.Idx.sort();
|
|
|
|
llvm::json::OStream J(OS, 2);
|
|
|
|
std::function<void(Index)> IndexToJSON = [&](Index I) {
|
|
|
|
J.object([&] {
|
|
|
|
J.attribute("USR", toHex(llvm::toStringRef(I.USR)));
|
|
|
|
J.attribute("Name", I.Name);
|
|
|
|
J.attribute("RefType", getRefType(I.RefType));
|
|
|
|
J.attribute("Path", I.Path);
|
|
|
|
J.attributeArray("Children", [&] {
|
|
|
|
for (const Index &C : I.Children)
|
|
|
|
IndexToJSON(C);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
OS << "var JsonIndex = `\n";
|
|
|
|
IndexToJSON(CDCtx.Idx);
|
|
|
|
OS << "`;\n";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-10 04:52:28 +08:00
|
|
|
static bool GenIndex(const ClangDocContext &CDCtx) {
|
|
|
|
std::error_code FileErr, OK;
|
|
|
|
llvm::SmallString<128> IndexPath;
|
|
|
|
llvm::sys::path::native(CDCtx.OutDirectory, IndexPath);
|
|
|
|
llvm::sys::path::append(IndexPath, "index.html");
|
|
|
|
llvm::raw_fd_ostream IndexOS(IndexPath, FileErr, llvm::sys::fs::F_None);
|
|
|
|
if (FileErr != OK) {
|
|
|
|
llvm::errs() << "Error creating main index: " << FileErr.message() << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
HTMLFile F;
|
|
|
|
std::vector<std::unique_ptr<TagNode>> BasicNodes =
|
|
|
|
genCommonFileNodes("Index", "", CDCtx);
|
|
|
|
AppendVector(std::move(BasicNodes), F.Children);
|
|
|
|
F.Render(IndexOS);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-07 02:31:46 +08:00
|
|
|
static bool CopyFile(StringRef FilePath, StringRef OutDirectory) {
|
|
|
|
llvm::SmallString<128> PathWrite;
|
|
|
|
llvm::sys::path::native(OutDirectory, PathWrite);
|
|
|
|
llvm::sys::path::append(PathWrite, llvm::sys::path::filename(FilePath));
|
|
|
|
llvm::SmallString<128> PathRead;
|
|
|
|
llvm::sys::path::native(FilePath, PathRead);
|
|
|
|
std::error_code OK;
|
|
|
|
std::error_code FileErr = llvm::sys::fs::copy_file(PathRead, PathWrite);
|
|
|
|
if (FileErr != OK) {
|
|
|
|
llvm::errs() << "Error creating file "
|
|
|
|
<< llvm::sys::path::filename(FilePath) << ": "
|
|
|
|
<< FileErr.message() << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HTMLGenerator::createResources(ClangDocContext &CDCtx) {
|
2019-08-10 04:52:28 +08:00
|
|
|
if (!SerializeIndex(CDCtx) || !GenIndex(CDCtx))
|
2019-08-07 02:31:46 +08:00
|
|
|
return false;
|
|
|
|
for (const auto &FilePath : CDCtx.UserStylesheets)
|
|
|
|
if (!CopyFile(FilePath, CDCtx.OutDirectory))
|
|
|
|
return false;
|
|
|
|
for (const auto &FilePath : CDCtx.FilesToCopy)
|
|
|
|
if (!CopyFile(FilePath, CDCtx.OutDirectory))
|
|
|
|
return false;
|
2019-07-26 05:27:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-11 03:03:25 +08:00
|
|
|
static GeneratorRegistry::Add<HTMLGenerator> HTML(HTMLGenerator::Format,
|
|
|
|
"Generator for HTML output.");
|
|
|
|
|
|
|
|
// This anchor is used to force the linker to link in the generated object
|
|
|
|
// file and thus register the generator.
|
|
|
|
volatile int HTMLGeneratorAnchorSource = 0;
|
|
|
|
|
|
|
|
} // namespace doc
|
|
|
|
} // namespace clang
|