Remove handlebars and refactor

This commit is contained in:
Ylva Selling
2023-04-21 15:48:09 -04:00
parent 88fcf3ff78
commit b8b2867a20
10 changed files with 81 additions and 368 deletions

View File

@@ -58,10 +58,7 @@ DocumentationEngine::DuplicateDocumentationException::DuplicateDocumentationExce
DocumentationEngine::DocumentationEngine()
: DocumentationGenerator(
"Top Level",
"toplevel",
{
{ "toplevelTemplate", "${WEB}/documentation/toplevel.hbs" },
}
"toplevel"
)
{}
@@ -180,142 +177,7 @@ void DocumentationEngine::addDocumentation(Documentation documentation) {
}
}
void DocumentationEngine::addHandlebarTemplates(std::vector<HandlebarTemplate> templates)
{
_handlebarTemplates.insert(
_handlebarTemplates.end(),
templates.begin(), templates.end()
);
}
std::vector<Documentation> DocumentationEngine::documentations() const {
return _documentations;
}
void DocumentationEngine::writeDocumentationHtml(const std::string& path,
std::string data)
{
ZoneScoped;
std::ifstream handlebarsInput;
handlebarsInput.exceptions(~std::ofstream::goodbit);
handlebarsInput.open(absPath(HandlebarsFilename));
const std::string handlebarsContent = std::string(
std::istreambuf_iterator<char>(handlebarsInput),
std::istreambuf_iterator<char>()
);
std::ifstream jsInput;
jsInput.exceptions(~std::ofstream::goodbit);
jsInput.open(absPath(JsFilename));
const std::string jsContent = std::string(
std::istreambuf_iterator<char>(jsInput),
std::istreambuf_iterator<char>()
);
std::ifstream bootstrapInput;
bootstrapInput.exceptions(~std::ofstream::goodbit);
bootstrapInput.open(absPath(BootstrapFilename));
const std::string bootstrapContent = std::string(
std::istreambuf_iterator<char>(bootstrapInput),
std::istreambuf_iterator<char>()
);
std::ifstream cssInput;
cssInput.exceptions(~std::ofstream::goodbit);
cssInput.open(absPath(CssFilename));
const std::string cssContent = std::string(
std::istreambuf_iterator<char>(cssInput),
std::istreambuf_iterator<char>()
);
std::string filename = path + ("index.html");
std::ofstream file;
file.exceptions(~std::ofstream::goodbit);
file.open(filename);
// We probably should escape backslashes here?
file << "<!DOCTYPE html>" << '\n'
<< "<html>" << '\n'
<< " " << "<head>" << '\n';
//write handlebar templates to htmlpage as script elements (as per hb)
for (const HandlebarTemplate& t : _handlebarTemplates) {
const char* Type = "text/x-handlebars-template";
file << " <script id=\"" << t.name << "\" type=\"" << Type << "\">";
file << '\n';
std::ifstream templateFilename(absPath(t.filename));
std::string templateContent(
std::istreambuf_iterator<char>{templateFilename},
std::istreambuf_iterator<char>{}
);
file << templateContent << "\n </script>" << '\n';
}
//write main template
file << " <script id=\"mainTemplate\" type=\"text/x-handlebars-template\">";
file << '\n';
std::ifstream templateFilename(absPath("${WEB}/documentation/main.hbs"));
std::string templateContent(
std::istreambuf_iterator<char>{templateFilename},
std::istreambuf_iterator<char>{}
);
file << templateContent << " </script>" << '\n';
//write scripte to register templates dynamically
file << " <script type=\"text/javascript\">" << '\n';
file << " templates = [];" << '\n';
file << " registerTemplates = function() {" << '\n';
for (const HandlebarTemplate& t : _handlebarTemplates) {
std::string nameOnly = t.name.substr(0, t.name.length() - 8); //-8 for Template
file << "\t\t\t\tvar " << t.name;
file << "Element = document.getElementById('" << t.name << "');" << '\n';
file << "\t\t\t\tHandlebars.registerPartial('" << nameOnly << "', ";
file << t.name << "Element.innerHTML);" << '\n';
file << "\t\t\t\ttemplates['" << nameOnly << "'] = Handlebars.compile(";
file << t.name << "Element.innerHTML);" << '\n';
}
file << "\t\t\t}" << '\n';
file << "\t\t</script>" << '\n';
const std::string DataId = "data";
const std::string Version =
"[" +
std::to_string(OPENSPACE_VERSION_MAJOR) + "," +
std::to_string(OPENSPACE_VERSION_MINOR) + "," +
std::to_string(OPENSPACE_VERSION_PATCH) +
"]";
file
<< " " << "<script id=\"" << DataId
<< "\" type=\"text/application/json\">" << '\n'
<< " " << std::move(data) << '\n'
<< " " << "</script>" << '\n';
file
<< " " << "<script>" << '\n'
<< " " << jsContent << '\n'
<< " " << "var documentation = parseJson('" << DataId << "').documentation;\n"
<< " " << "var version = " << Version << ";" << '\n'
<< " " << "var currentDocumentation = documentation[0];" << '\n'
<< " " << handlebarsContent << '\n'
<< " " << "</script>" << '\n'
<< " " << "<style type=\"text/css\">" << '\n'
<< " " << cssContent << '\n'
<< " " << bootstrapContent << '\n'
<< " " << "</style>" << '\n'
<< " " << "<title>OpenSpace Documentation</title>" << '\n'
<< " " << "</head>" << '\n'
<< " " << "<body>" << '\n'
<< " " << "</body>" << '\n'
<< "</html>" << '\n';
}
} // namespace openspace::documentation

View File

@@ -32,30 +32,45 @@
namespace openspace {
const std::string DocumentationGenerator::DataTag = "Data";
const std::string DocumentationGenerator::NameTag = "Name";
DocumentationGenerator::DocumentationGenerator(std::string name,
std::string jsonName,
std::vector<HandlebarTemplate> handlebarTemplates)
std::string jsonName)
: _name(std::move(name))
, _jsonName(std::move(jsonName))
, _handlebarTemplates(std::move(handlebarTemplates))
{
ghoul_precondition(!_name.empty(), "name must not be empty");
ghoul_precondition(!_jsonName.empty(), "jsonName must not be empty");
for (const HandlebarTemplate& t : _handlebarTemplates) {
(void)t; // Unused variable in Release mode
ghoul_precondition(!t.name.empty(), "name must not be empty");
ghoul_precondition(!t.filename.empty(), "filename must not be empty");
}
}
std::vector<DocumentationGenerator::HandlebarTemplate>
DocumentationGenerator::templatesToRegister()
{
return _handlebarTemplates;
}
std::string DocumentationGenerator::jsonName() {
return _jsonName;
}
void DocumentationGenerator::sortJson(nlohmann::json& json) const {
std::sort(
json.begin(),
json.end(),
[](const nlohmann::json& lhs, const nlohmann::json& rhs) {
std::string lhsString = lhs["Name"];
std::string rhsString = rhs["Name"];
std::transform(
lhsString.begin(),
lhsString.end(),
lhsString.begin(),
[](unsigned char c) { return std::tolower(c); }
);
std::transform(
rhsString.begin(),
rhsString.end(),
rhsString.begin(),
[](unsigned char c) { return std::tolower(c); }
);
return rhsString > lhsString;
});
}
} // namespace openspace