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

@@ -71,14 +71,6 @@ public:
*/
void addDocumentation(Documentation documentation);
/* Adds the \p templates to the list of templates that are written to the
* documentation html file.
* \param templates Vector of templates to add. Most of the time this list
* will just contain one item, but some modules may wish to provide
* multiple templates for subtypes, etc
*/
void addHandlebarTemplates(std::vector<HandlebarTemplate> templates);
/**
* Returns a list of all registered Documentation%s.
*
@@ -97,17 +89,6 @@ public:
*/
static DocumentationEngine& ref();
/**
* Generates the documentation html file. Generated file will have embeded
* in it: HandlebarJS Templates (from _handlebarTemplates) and json (from
* \p data) along with the base template and js/css files from the source
* directory ${WEB}/documentation
*
* \param path The path to add
* \param data The JSON data that is written to the documentation
*/
void writeDocumentationHtml(const std::string& path, std::string data);
std::string generateJson() const override;
nlohmann::json generateJsonJson() const;
@@ -116,12 +97,9 @@ private:
/// The list of all Documentation%s that are stored by the DocumentationEngine
std::vector<Documentation> _documentations;
/// The list of templates to render the documentation with.
std::vector<HandlebarTemplate> _handlebarTemplates;
static DocumentationEngine* _instance;
};
} // namespace openspace::documentation
#define DocEng (openspace::documentation::DocumentationEngine::ref())

View File

@@ -25,6 +25,7 @@
#ifndef __OPENSPACE_CORE___DOCUMENTATIONGENERATOR___H__
#define __OPENSPACE_CORE___DOCUMENTATIONGENERATOR___H__
#include <openspace/json.h>
#include <string>
#include <vector>
@@ -46,35 +47,24 @@ namespace openspace {
*/
class DocumentationGenerator {
public:
/// This struct contains a single Handlebar template, with the name and the filename
struct HandlebarTemplate {
std::string name; ///< The name of the Handlebar template defined in #filename
std::string filename; ///< The filename referenced in the #name
};
static const std::string NameTag;
static const std::string DataTag;
/**
* The constructor that is used to set the member variables later used in the
* writeDocumentation method.
*
* \param name The name of the written documentation
* \param jsonName The variable name of the value generated by the generateJson
* \param handlebarTemplates A list of Handlebar templates that is added to the
* documentation file
*
* \pre name must not be empty
* \pre jsonName must not be empty
* \pre Each handlebarTemplates' `name` must not be empty
* \pre javascriptFilename must not be empty
*/
DocumentationGenerator(std::string name, std::string jsonName,
std::vector<HandlebarTemplate> handlebarTemplates);
DocumentationGenerator(std::string name, std::string jsonName);
/// Default constructor
virtual ~DocumentationGenerator() = default;
//getter for handlebar templates
std::vector<HandlebarTemplate> templatesToRegister();
//getter for identifier
std::string jsonName();
@@ -89,10 +79,10 @@ public:
*/
virtual std::string generateJson() const = 0;
void sortJson(nlohmann::json& json) const;
private:
const std::string _name;
const std::string _jsonName;
const std::vector<HandlebarTemplate> _handlebarTemplates;
};
} // namespace openspace

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

View File

@@ -1047,36 +1047,17 @@ void OpenSpaceEngine::writeDocumentation() {
&properties::PropertyOwner::generateJsonJson,
_scene.get()
);
DocEng.addHandlebarTemplates(global::scriptEngine->templatesToRegister());
DocEng.addHandlebarTemplates(FactoryManager::ref().templatesToRegister());
DocEng.addHandlebarTemplates(DocEng.templatesToRegister());
nlohmann::json scripting;
scripting["Name"] = "Scripting API";
scripting["Data"] = global::scriptEngine->generateJsonJson();
nlohmann::json factory;
factory["Name"] = "Asset Types";
factory["Data"] = FactoryManager::ref().generateJsonJson();
nlohmann::json keybindings;
keybindings["Name"] = "Keybindings";
keybindings["Keybindings"] = global::keybindingManager->generateJsonJson();
SceneLicenseWriter writer;
nlohmann::json license;
license["Name"] = "Licenses";
license["Data"] = writer.generateJsonJson();
nlohmann::json sceneProperties;
sceneProperties["Name"] = "Settings";
sceneProperties["Data"] = settings.get();
nlohmann::json sceneGraph;
sceneGraph["Name"] = "Scene";
sceneGraph["Data"] = scene.get();
nlohmann::json scripting = global::scriptEngine->generateJsonJson();
nlohmann::json factory = FactoryManager::ref().generateJsonJson();
nlohmann::json keybindings = global::keybindingManager->generateJsonJson();
nlohmann::json license = writer.generateJsonJson();
nlohmann::json sceneProperties = settings.get();
nlohmann::json sceneGraph = scene.get();
sceneProperties[DocumentationGenerator::NameTag] = "Settings";
sceneGraph[DocumentationGenerator::NameTag] = "Scene";
nlohmann::json documentation = {
sceneGraph, sceneProperties, keybindings, license, scripting, factory

View File

@@ -38,37 +38,10 @@
namespace openspace::interaction {
void sortJson(nlohmann::json& json) {
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;
});
}
KeybindingManager::KeybindingManager()
: DocumentationGenerator(
"Keybindings",
"keybinding",
{
{ "keybindingTemplate", "${WEB}/documentation/keybinding.hbs" }
}
"keybinding"
)
{}
@@ -177,7 +150,10 @@ nlohmann::json KeybindingManager::generateJsonJson() const {
}
sortJson(json);
return json;
nlohmann::json result;
result[NameTag] = "Keybindings";
result["Keybindings"] = json;
return result;
}
scripting::LuaLibrary KeybindingManager::luaLibrary() {

View File

@@ -41,30 +41,6 @@
namespace {
constexpr std::string_view _loggerCat = "PropertyOwner";
void sortJson(nlohmann::json& json) {
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;
});
}
nlohmann::json createJson(openspace::properties::PropertyOwner* owner) {
ZoneScoped
@@ -89,14 +65,12 @@ namespace {
json["Properties"].push_back(propertyJson);
}
sortJson(json["Properties"]);
auto propertyOwners = owner->propertySubOwners();
for (properties::PropertyOwner* o : propertyOwners) {
nlohmann::json propertyOwner;
json["PropertyOwners"].push_back(createJson(o));
}
sortJson(json["PropertyOwners"]);
return json;
}
@@ -107,12 +81,7 @@ namespace openspace::properties {
PropertyOwner::PropertyOwner(PropertyOwnerInfo info)
: DocumentationGenerator(
"Property Owners",
"propertyOwners",
{
{ "propertyOwnersTemplate","${WEB}/documentation/propertyowners.hbs" },
{ "propertyTemplate","${WEB}/documentation/property.hbs" },
{ "propertylistTemplate","${WEB}/documentation/propertylist.hbs" }
}
"propertyOwners"
)
, _identifier(std::move(info.identifier))
, _guiName(std::move(info.guiName))
@@ -437,12 +406,20 @@ nlohmann::json PropertyOwner::generateJsonJson() const {
std::vector<PropertyOwner*> subOwners = propertySubOwners();
for (PropertyOwner* owner : subOwners) {
if (owner->identifier() != "Scene") {
json.push_back(createJson(owner));
nlohmann::json jsonOwner = createJson(owner);
sortJson(jsonOwner["Properties"]);
sortJson(jsonOwner["PropertyOwners"]);
json.push_back(jsonOwner);
}
}
sortJson(json);
return json;
nlohmann::json result;
result[NameTag] = "PropertyOwner";
result[DataTag] = json;
return result;
}
} // namespace openspace::properties

View File

@@ -39,37 +39,10 @@ namespace openspace {
SceneLicenseWriter::SceneLicenseWriter()
: DocumentationGenerator(
"Scene Licenses",
"sceneLicense",
{
{ "sceneLicenseTemplate", "${WEB}/documentation/scenelicense.hbs" }
}
"sceneLicense"
)
{}
void sortJson(nlohmann::json& json) {
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;
});
}
nlohmann::json SceneLicenseWriter::generateJsonJson() const {
nlohmann::json json;
@@ -144,7 +117,12 @@ nlohmann::json SceneLicenseWriter::generateJsonJson() const {
assetsJson["Licenses"].push_back(entry);
}
json.push_back(assetsJson);
return json;
nlohmann::json result;
result[NameTag] = "Licenses";
result[DataTag] = json;
return result;
}
std::string SceneLicenseWriter::generateJson() const {

View File

@@ -81,29 +81,7 @@ namespace {
return result;
}
void sortJson(nlohmann::json& json) {
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;
});
}
nlohmann::json toJson(const openspace::scripting::LuaLibrary::Function& f) {
using namespace openspace;
@@ -216,10 +194,7 @@ namespace openspace::scripting {
ScriptEngine::ScriptEngine()
: DocumentationGenerator(
"Script Documentation",
"scripting",
{
{ "scriptingTemplate","${WEB}/documentation/scripting.hbs" },
}
"scripting"
)
{
//tracy::LuaRegister(_state);
@@ -600,7 +575,12 @@ nlohmann::json ScriptEngine::generateJsonJson() const {
sortJson(json);
}
return json;
nlohmann::json result;
result[NameTag] = "Scripting API";
result[DataTag] = json;
return result;
}
void ScriptEngine::writeLog(const std::string& script) {

View File

@@ -42,30 +42,6 @@ namespace {
using namespace openspace;
using namespace openspace::documentation;
void sortJson(nlohmann::json& json) {
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;
});
}
nlohmann::json generateJsonDocumentation(const Documentation& d) {
nlohmann::json json;
@@ -113,7 +89,6 @@ nlohmann::json generateJsonDocumentation(const Documentation& d) {
}
json["Members"].push_back(entry);
}
sortJson(json["Members"]);
return json;
}
@@ -133,10 +108,7 @@ FactoryManager::FactoryNotFoundError::FactoryNotFoundError(std::string t)
FactoryManager::FactoryManager()
: DocumentationGenerator(
"Factory Documentation",
"factory",
{
{ "factoryTemplate", "${WEB}/documentation/factory.hbs" }
}
"factory"
)
{}
@@ -252,11 +224,15 @@ nlohmann::json FactoryManager::generateJsonJson() const {
for (const Documentation& doc : docs) {
leftovers["Classes"].push_back(generateJsonDocumentation(doc));
}
sortJson(leftovers["Classes"]);
sortJson(json["Classes"]["Members"]);
json.push_back(leftovers);
sortJson(json);
// I did not check the output of this for correctness ---abock
return json;
nlohmann::json result;
result[NameTag] = "Asset Types";
result[DataTag] = json;
return result;
}
} // namespace openspace