From b308890027564371bdb71b7e8d6eb8bf9fdfdafe Mon Sep 17 00:00:00 2001 From: Emil Axelsson Date: Tue, 27 Sep 2016 16:52:27 +0200 Subject: [PATCH] improve look and feel for lua scripting docs --- .../bootstrap.min.css | 0 .../handlebars-v4.0.5.js | 0 data/web/{documentation => common}/style.css | 8 +- data/web/luascripting/main.hbs | 37 ++++++++ data/web/luascripting/script.js | 28 ++++++ data/web/luascripting/scripting.hbs | 17 ++++ src/documentation/documentationengine.cpp | 6 +- src/scripting/scriptengine.cpp | 91 +++++++++++++++++-- 8 files changed, 170 insertions(+), 17 deletions(-) rename data/web/{documentation => common}/bootstrap.min.css (100%) rename data/web/{documentation => common}/handlebars-v4.0.5.js (100%) rename data/web/{documentation => common}/style.css (98%) create mode 100644 data/web/luascripting/main.hbs create mode 100644 data/web/luascripting/script.js create mode 100644 data/web/luascripting/scripting.hbs diff --git a/data/web/documentation/bootstrap.min.css b/data/web/common/bootstrap.min.css similarity index 100% rename from data/web/documentation/bootstrap.min.css rename to data/web/common/bootstrap.min.css diff --git a/data/web/documentation/handlebars-v4.0.5.js b/data/web/common/handlebars-v4.0.5.js similarity index 100% rename from data/web/documentation/handlebars-v4.0.5.js rename to data/web/common/handlebars-v4.0.5.js diff --git a/data/web/documentation/style.css b/data/web/common/style.css similarity index 98% rename from data/web/documentation/style.css rename to data/web/common/style.css index aa9e1fd69a..b005151147 100644 --- a/data/web/documentation/style.css +++ b/data/web/common/style.css @@ -1,8 +1,4 @@ - - -.container { - padding-top: 70px; -} +/* Documentation */ .documentation-name, .documentation-name:hover, .documentation-name:focus { text-decoration: none; @@ -34,7 +30,6 @@ background-color: #fff; } - .documentation-type { color: #888; font-size: 0.8em; @@ -55,7 +50,6 @@ } - /*! * Start Bootstrap - Simple Sidebar (http://startbootstrap.com/) * Copyright 2013-2016 Start Bootstrap diff --git a/data/web/luascripting/main.hbs b/data/web/luascripting/main.hbs new file mode 100644 index 0000000000..1b531bacdf --- /dev/null +++ b/data/web/luascripting/main.hbs @@ -0,0 +1,37 @@ +
+ + + + +
+
+

OpenSpace Lua Scripting

+

Version: {{version.[0]}}.{{version.[1]}}.{{version.[2]}}

+ {{#each scripting}} + + {{/each}} +
+
+
diff --git a/data/web/luascripting/script.js b/data/web/luascripting/script.js new file mode 100644 index 0000000000..b43af6f884 --- /dev/null +++ b/data/web/luascripting/script.js @@ -0,0 +1,28 @@ +window.onload = function () { + var mainTemplateElement = document.getElementById('mainTemplate'); + var mainTemplate = Handlebars.compile(mainTemplateElement.innerHTML); + + var scriptingTemplateElement = document.getElementById('scriptingTemplate'); + Handlebars.registerPartial('scripting', scriptingTemplateElement.innerHTML); + + Handlebars.registerHelper('urlify', function(options, context) { + var data = context.data; + var identifier = options.replace(" ", "-").toLowerCase(); + + while (data = data._parent) { + if (data.key !== undefined) { + identifier = data.key + "-" + identifier; + } + } + + return identifier; + }); + + var data = { + scripting: scripting, + version: version + } + + var contents = mainTemplate(data); + document.body.innerHTML = contents; +} \ No newline at end of file diff --git a/data/web/luascripting/scripting.hbs b/data/web/luascripting/scripting.hbs new file mode 100644 index 0000000000..00313e3372 --- /dev/null +++ b/data/web/luascripting/scripting.hbs @@ -0,0 +1,17 @@ +{{#each functions}} +
+ +
+{{/each}} diff --git a/src/documentation/documentationengine.cpp b/src/documentation/documentationengine.cpp index 24e3fa2297..0bcab0e7c3 100644 --- a/src/documentation/documentationengine.cpp +++ b/src/documentation/documentationengine.cpp @@ -38,10 +38,10 @@ namespace { const std::string MainTemplateFilename = "${OPENSPACE_DATA}/web/documentation/main.hbs"; const std::string DocumentationTemplateFilename = "${OPENSPACE_DATA}/web/documentation/documentation.hbs"; - const std::string HandlebarsFilename = "${OPENSPACE_DATA}/web/documentation/handlebars-v4.0.5.js"; + const std::string HandlebarsFilename = "${OPENSPACE_DATA}/web/common/handlebars-v4.0.5.js"; const std::string JsFilename = "${OPENSPACE_DATA}/web/documentation/script.js"; - const std::string BootstrapFilename = "${OPENSPACE_DATA}/web/documentation/bootstrap.min.css"; - const std::string CssFilename = "${OPENSPACE_DATA}/web/documentation/style.css"; + const std::string BootstrapFilename = "${OPENSPACE_DATA}/web/common/bootstrap.min.css"; + const std::string CssFilename = "${OPENSPACE_DATA}/web/common/style.css"; } namespace openspace { diff --git a/src/scripting/scriptengine.cpp b/src/scripting/scriptengine.cpp index 7ac37b5eb3..bfec6b6589 100644 --- a/src/scripting/scriptengine.cpp +++ b/src/scripting/scriptengine.cpp @@ -23,6 +23,7 @@ ****************************************************************************************/ #include +#include #include #include @@ -39,6 +40,15 @@ #include "scriptengine_lua.inl" +namespace { + const std::string MainTemplateFilename = "${OPENSPACE_DATA}/web/luascripting/main.hbs"; + const std::string DocumentationTemplateFilename = "${OPENSPACE_DATA}/web/luascripting/scripting.hbs"; + const std::string HandlebarsFilename = "${OPENSPACE_DATA}/web/common/handlebars-v4.0.5.js"; + const std::string JsFilename = "${OPENSPACE_DATA}/web/luascripting/script.js"; + const std::string BootstrapFilename = "${OPENSPACE_DATA}/web/common/bootstrap.min.css"; + const std::string CssFilename = "${OPENSPACE_DATA}/web/common/style.css"; +} + namespace openspace { namespace scripting { @@ -562,18 +572,48 @@ void ScriptEngine::writeDocumentation(const std::string& filename, const std::st } } else if (type == "html") { + std::ifstream handlebarsInput(absPath(HandlebarsFilename)); + std::ifstream jsInput(absPath(JsFilename)); + + std::string jsContent; + std::back_insert_iterator jsInserter(jsContent); + + std::copy(std::istreambuf_iterator{handlebarsInput}, std::istreambuf_iterator(), jsInserter); + std::copy(std::istreambuf_iterator{jsInput}, std::istreambuf_iterator(), jsInserter); + + std::ifstream bootstrapInput(absPath(BootstrapFilename)); + std::ifstream cssInput(absPath(CssFilename)); + + std::string cssContent; + std::back_insert_iterator cssInserter(cssContent); + + std::copy(std::istreambuf_iterator{bootstrapInput}, std::istreambuf_iterator(), cssInserter); + std::copy(std::istreambuf_iterator{cssInput}, std::istreambuf_iterator(), cssInserter); + + std::ifstream mainTemplateInput(absPath(MainTemplateFilename)); + std::string mainTemplateContent{ std::istreambuf_iterator{mainTemplateInput}, + std::istreambuf_iterator{} }; + + std::ifstream documentationTemplateInput(absPath(DocumentationTemplateFilename)); + std::string documentationTemplateContent{ std::istreambuf_iterator{documentationTemplateInput}, + std::istreambuf_iterator{} }; + std::ofstream file; file.exceptions(~std::ofstream::goodbit); file.open(filename); -#ifdef JSON // Create JSON std::stringstream json; json << "["; + bool first = true; for (const LuaLibrary& l : _registeredLibraries) { - json << "{"; + if (!first) { + json << ","; + } + first = false; + json << "{"; json << "\"library\": \"" << l.name << "\","; json << "\"functions\": ["; @@ -582,16 +622,53 @@ void ScriptEngine::writeDocumentation(const std::string& filename, const std::st json << "\"name\": \"" << f.name << "\", "; json << "\"arguments\": \"" << f.argumentText << "\", "; json << "\"help\": \"" << f.helpText << "\""; - json << "},"; + json << "}"; + if (&f != &l.functions.back()) { + json << ","; + } } - json << "]},"; + json << "]}"; + } json << "]"; + std::string jsonString = ""; + for (const char& c : json.str()) { + if (c == '\'') { + jsonString += "\\'"; + } + else { + jsonString += c; + } + } - std::string jsonText = json.str(); -#else std::stringstream html; + html << "\n" + << "\n" + << "\t\n" + << "\t\t\n" + << "\t\t\n" + << "\t\n" + << "\t\n" + << "\t\tDocumentation\n" + << "\t\n" + << "\t\n" + << "\t\n" + << "\n"; + + file << html.str(); + + /* html << "\n" << "\t\n" @@ -644,7 +721,7 @@ void ScriptEngine::writeDocumentation(const std::string& filename, const std::st << ""; file << html.str(); -#endif +*/ } else { throw ghoul::RuntimeError("Undefined type '" + type + "' for Lua documentation");