Skip to content

Commit

Permalink
[clang-doc] Add stylesheet to generated html docs
Browse files Browse the repository at this point in the history
A default css stylesheet is included for docs generated in html format.

Differential Revision: https://reviews.llvm.org/D64539

llvm-svn: 367056
  • Loading branch information
DiegoAstiazaran committed Jul 25, 2019
1 parent 3bdb7a9 commit db5d8e3
Show file tree
Hide file tree
Showing 10 changed files with 267 additions and 4 deletions.
1 change: 1 addition & 0 deletions clang-tools-extra/clang-doc/Generators.h
Expand Up @@ -27,6 +27,7 @@ class Generator {

// Write out the decl info in the specified format.
virtual llvm::Error generateDocForInfo(Info *I, llvm::raw_ostream &OS) = 0;
virtual bool createResources(ClangDocContext CDCtx) = 0;
};

typedef llvm::Registry<Generator> GeneratorRegistry;
Expand Down
33 changes: 33 additions & 0 deletions clang-tools-extra/clang-doc/HTMLGenerator.cpp
Expand Up @@ -35,6 +35,7 @@ class HTMLTag {
TAG_UL,
TAG_LI,
TAG_A,
TAG_LINK,
};

HTMLTag() = default;
Expand Down Expand Up @@ -103,6 +104,7 @@ struct HTMLFile {
bool HTMLTag::IsSelfClosing() const {
switch (Value) {
case HTMLTag::TAG_META:
case HTMLTag::TAG_LINK:
return true;
case HTMLTag::TAG_TITLE:
case HTMLTag::TAG_DIV:
Expand Down Expand Up @@ -140,6 +142,8 @@ llvm::SmallString<16> HTMLTag::ToString() const {
return llvm::SmallString<16>("li");
case HTMLTag::TAG_A:
return llvm::SmallString<16>("a");
case HTMLTag::TAG_LINK:
return llvm::SmallString<16>("link");
}
llvm_unreachable("Unhandled HTMLTag::TagType");
}
Expand Down Expand Up @@ -526,6 +530,7 @@ class HTMLGenerator : public Generator {
static const char *Format;

llvm::Error generateDocForInfo(Info *I, llvm::raw_ostream &OS) override;
bool createResources(ClangDocContext CDCtx) override;
};

const char *HTMLGenerator::Format = "html";
Expand Down Expand Up @@ -572,12 +577,40 @@ llvm::Error HTMLGenerator::generateDocForInfo(Info *I, llvm::raw_ostream &OS) {

F.Children.emplace_back(
llvm::make_unique<TagNode>(HTMLTag::TAG_TITLE, InfoTitle));
auto LinkNode = llvm::make_unique<TagNode>(HTMLTag::TAG_LINK);
LinkNode->Attributes.try_emplace("rel", "stylesheet");
SmallString<128> StylesheetPath = computeRelativePath("", I->Path);
llvm::sys::path::append(StylesheetPath, "clang-doc-default-stylesheet.css");
LinkNode->Attributes.try_emplace("href", StylesheetPath);
F.Children.emplace_back(std::move(LinkNode));
F.Children.emplace_back(std::move(MainContentNode));
F.Render(OS);

return llvm::Error::success();
}

bool HTMLGenerator::createResources(ClangDocContext CDCtx) {
llvm::outs() << "Generating stylesheet for docs...\n";
llvm::SmallString<128> StylesheetPathWrite;
llvm::sys::path::native(CDCtx.OutDirectory, StylesheetPathWrite);
llvm::sys::path::append(StylesheetPathWrite,
"clang-doc-default-stylesheet.css");
llvm::SmallString<128> StylesheetPathRead;
llvm::sys::path::native(CDCtx.ClangDocPath, StylesheetPathRead);
StylesheetPathRead = llvm::sys::path::parent_path(StylesheetPathRead);
llvm::sys::path::append(StylesheetPathRead, "..", "share", "clang",
"clang-doc-default-stylesheet.css");
std::error_code OK;
std::error_code FileErr =
llvm::sys::fs::copy_file(StylesheetPathRead, StylesheetPathWrite);
if (FileErr != OK) {
llvm::errs() << "Error creating stylesheet file: " << FileErr.message()
<< "\n";
return false;
}
return true;
}

static GeneratorRegistry::Add<HTMLGenerator> HTML(HTMLGenerator::Format,
"Generator for HTML output.");

Expand Down
1 change: 1 addition & 0 deletions clang-tools-extra/clang-doc/MDGenerator.cpp
Expand Up @@ -251,6 +251,7 @@ class MDGenerator : public Generator {
static const char *Format;

llvm::Error generateDocForInfo(Info *I, llvm::raw_ostream &OS) override;
bool createResources(ClangDocContext CDCtx) override { return true; }
};

const char *MDGenerator::Format = "md";
Expand Down
2 changes: 2 additions & 0 deletions clang-tools-extra/clang-doc/Representation.h
Expand Up @@ -349,6 +349,8 @@ mergeInfos(std::vector<std::unique_ptr<Info>> &Values);
struct ClangDocContext {
tooling::ExecutionContext *ECtx;
bool PublicOnly;
std::string OutDirectory;
std::string ClangDocPath;
};

} // namespace doc
Expand Down
1 change: 0 additions & 1 deletion clang-tools-extra/clang-doc/Serialize.cpp
Expand Up @@ -44,7 +44,6 @@ populateParentNamespaces(llvm::SmallVector<Reference, 4> &Namespaces,
// }
llvm::SmallString<128>
getInfoRelativePath(const llvm::SmallVectorImpl<doc::Reference> &Namespaces) {
std::error_code OK;
llvm::SmallString<128> Path;
for (auto R = Namespaces.rbegin(), E = Namespaces.rend(); R != E; ++R)
llvm::sys::path::append(Path, R->Name);
Expand Down
1 change: 1 addition & 0 deletions clang-tools-extra/clang-doc/YAMLGenerator.cpp
Expand Up @@ -244,6 +244,7 @@ class YAMLGenerator : public Generator {
static const char *Format;

llvm::Error generateDocForInfo(Info *I, llvm::raw_ostream &OS) override;
bool createResources(ClangDocContext CDCtx) override { return true; }
};

const char *YAMLGenerator::Format = "yaml";
Expand Down
@@ -0,0 +1,205 @@
body,div {
margin: 0;
padding: 0;
}

body[no-overflow] {
overflow: hidden;
}

li>p:first-child {
margin-top: 0;
}

li>p:last-child {
margin-bottom: 0;
}

html {
-webkit-box-sizing: border-box;
box-sizing: border-box;
}

*,*::before,*::after {
-webkit-box-sizing: inherit;
box-sizing: inherit;
}

body,html {
color: #202124;
font: 400 16px/24px Roboto,sans-serif;
-moz-osx-font-smoothing: grayscale;
-webkit-font-smoothing: antialiased;
height: 100%;
margin: 36px;
-webkit-text-size-adjust: 100%;
-moz-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
text-size-adjust: 100%;
}

body[devsite-framebox] {
overflow: hidden;
padding: 20px;
}

body[sitemask--active] {
overflow: hidden;
}

p {
margin: 16px 0;
padding: 0;
}

:link,:visited {
color: #039be5;
outline: 0;
text-decoration: none;
}

ul {
margin: 0;
padding-left: 40px;
}

ul {
list-style: disc outside;
}

li,li p {
margin: 12px 0;
padding: 0;
}

*[visually-hidden] {
opacity: 0 !important;
pointer-events: none !important;
visibility: hidden !important;
}

*[hidden] {
display: none !important;
}

[render-hidden] {
display: inline !important;
position: absolute !important;
visibility: hidden !important;
}

*[no-scroll] {
overflow: hidden;
}

@supports (display: flex) {
body[ready] .devsite-wrapper {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
}
}

@media screen and (max-width: 840px) {
body[devsite-book-nav--open] {
overflow: hidden;
}
}

h1,h2,h3,h4,h5,h6 {
overflow: hidden;
padding: 0;
text-overflow: ellipsis;
}

h1 {
color: #80868b;
font: 300 34px/40px Roboto,sans-serif;
letter-spacing: -0.01em;
margin: 40px 0 20px;
}

[layout=docs] h2 {
border-bottom: 1px solid #e8eaed;
padding-bottom: 3px;
}

h2 {
font: 300 24px/32px Roboto,sans-serif;
letter-spacing: -0.01em;
margin: 40px 0 20px;
}

h3 {
font: 400 20px/32px Roboto,sans-serif;
margin: 32px 0 16px;
}

h4,h5,h6 {
margin: 32px 0 16px;
}

h4 {
font: 500 16px/24px Roboto,sans-serif;
}

h5 {
font: 700 14px/24px Roboto,sans-serif;
}

h6 {
font: 500 14px/24px Roboto,sans-serif;
}

h1+h1,h1+h2,h1+h3,h1+h4,h1+h5,h1+h6,h2+h1,h2+h2,h2+h3,h2+h4,h2+h5,h2+h6,h3+h1,h3+h2,h3+h3,h3+h4,h3+h5,h3+h6,h4+h1,h4+h2,h4+h3,h4+h4,h4+h5,h4+h6,h5+h1,h5+h2,h5+h3,h5+h4,h5+h5,h5+h6,h6+h1,h6+h2,h6+h3,h6+h4,h6+h5,h6+h6 {
margin-top: 0;
}

@media screen and (max-width: 600px) {
h1 {
font: 300 24px/32px Roboto,sans-serif;
}
}

[scrollbars]::-webkit-scrollbar {
height: 8px;
width: 8px;
}

[scrollbars]::-webkit-scrollbar-thumb {
background: rgba(128,134,139,.26);
border-radius: 8px;
}

[no-horizontal-scrollbars]::-webkit-scrollbar {
height: 0;
width: 0;
}

[scrollbars]::-webkit-scrollbar-corner {
background: 0;
}

[background] h2 {
color: #fff;
}

@media print {
body, html, :link, :visited, h1, h2, h3, h4, h5, h6 {
color: #000 !important;
padding-left: 0 !important;
padding-right: 0 !important;
}

:link, :visited {
text-decoration: underline;
}
}

@page {
margin: .75in;
}
5 changes: 4 additions & 1 deletion clang-tools-extra/clang-doc/tool/CMakeLists.txt
Expand Up @@ -14,4 +14,7 @@ target_link_libraries(clang-doc
clangTooling
clangToolingCore
)


install(FILES ../stylesheets/clang-doc-default-stylesheet.css
DESTINATION share/clang
COMPONENT clang-doc)
17 changes: 15 additions & 2 deletions clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
Expand Up @@ -91,6 +91,15 @@ std::string getFormatString() {
llvm_unreachable("Unknown OutputFormatTy");
}

// This function isn't referenced outside its translation unit, but it
// can't use the "static" keyword because its address is used for
// GetMainExecutable (since some platforms don't support taking the
// address of main, and some platforms can't implement GetMainExecutable
// without being given the address of a function in the main executable).
std::string GetExecutablePath(const char *Argv0, void *MainAddr) {
return llvm::sys::fs::getMainExecutable(Argv0, MainAddr);
}

bool CreateDirectory(const Twine &DirName, bool ClearDirectory = false) {
std::error_code OK;
llvm::SmallString<128> DocsRootPath;
Expand Down Expand Up @@ -129,7 +138,6 @@ llvm::Expected<llvm::SmallString<128>> getInfoOutputFile(StringRef Root,
StringRef RelativePath,
StringRef Name,
StringRef Ext) {
std::error_code OK;
llvm::SmallString<128> Path;
llvm::sys::path::native(Root, Path);
llvm::sys::path::append(Path, RelativePath);
Expand Down Expand Up @@ -195,8 +203,10 @@ int main(int argc, const char **argv) {

// Mapping phase
llvm::outs() << "Mapping decls...\n";
void *MainAddr = (void *)(intptr_t)GetExecutablePath;
std::string ClangDocPath = GetExecutablePath(argv[0], MainAddr);
clang::doc::ClangDocContext CDCtx = {Exec->get()->getExecutionContext(),
PublicOnly};
PublicOnly, OutDirectory, ClangDocPath};
auto Err =
Exec->get()->execute(doc::newMapperActionFactory(CDCtx), ArgAdjuster);
if (Err) {
Expand Down Expand Up @@ -239,5 +249,8 @@ int main(int argc, const char **argv) {
llvm::errs() << toString(std::move(Err)) << "\n";
}

if (!G->get()->createResources(CDCtx))
return 1;

return 0;
}

0 comments on commit db5d8e3

Please sign in to comment.