Browse Source

duktape

master
alistair 3 years ago
parent
commit
e62593b77e
  1. 5
      CMakeLists.txt
  2. 70
      main.cpp

5
CMakeLists.txt

@ -6,7 +6,6 @@ set(CMAKE_CXX_STANDARD_REQUIRED True) @@ -6,7 +6,6 @@ set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_EXTENSIONS OFF)
set(BUILD_SHARED_LIBS OFF)
set(CMAKE_BUILD_TYPE "Debug")
set(BUILD_SHARED_LIBS OFF)
@ -48,9 +47,9 @@ if(NOT TARGET spdlog) @@ -48,9 +47,9 @@ if(NOT TARGET spdlog)
endif()
ADD_EXECUTABLE(stgen3 util.cpp darkhttpd.c markdown.cpp main.cpp)
ADD_EXECUTABLE(stgen3 util.cpp darkhttpd.c duktape/src/duktape.c markdown.cpp main.cpp)
target_include_directories(stgen3 PRIVATE include)
target_include_directories(stgen3 PRIVATE include duktape/src)
add_custom_command(
OUTPUT default-templates.h

70
main.cpp

@ -31,6 +31,7 @@ @@ -31,6 +31,7 @@
#include "util.h"
#include "markdown.h"
const std::string SITE_CONFIG_FNAME = "stgen.json";
const std::string DEFAULT_PUBLISH_ROOT = "public";
const std::string DEFAULT_SOURCE_DIR = ".";
@ -58,6 +59,69 @@ struct runtime_config { @@ -58,6 +59,69 @@ struct runtime_config {
runtime_config settings;
namespace duk {
extern "C" {
#include <duk_config.h>
#include <duktape.h>
}
class duktape {
duk_context *ctx;
public:
duktape() {
ctx = duk_create_heap_default();
}
~duktape() {
duk_destroy_heap(ctx);
}
void compile_file(std::string fname)
{
std::string file = read_file(fname);
if (duk_pcompile_lstring(ctx, 0, file.c_str(), file.length()) != 0) {
std::cout << "Error :" << duk_safe_to_string(ctx, -1);
}
}
void compile_text(std::string file)
{
if (duk_pcompile_lstring(ctx, 0, file.c_str(), file.length()) != 0) {
std::cout << "Error :" << duk_safe_to_string(ctx, -1);
}
}
void run()
{
duk_call(ctx, 0);
std::cout << "program result: " << duk_safe_to_string(ctx, -1);
duk_pop(ctx);
}
void test() {
compile_file("katex.min.js");
duk_call(ctx, 0);
duk_pop(ctx);
std::string line = "katex.renderToString(\"c = \\pm\\sqrt{a^2 + b^2}\", {throwOnError: false });";
duk_push_lstring(ctx, line.c_str(), line.length());
if (duk_peval(ctx) != 0) {
std::cout << "Error (test):" << duk_safe_to_string(ctx, -1);
} else {
std::cout << "Result :" << duk_safe_to_string(ctx, -1);
}
duk_pop(ctx);
}
};
};
/* Fork and exec a command given in args taking input from in and sending
@ -858,6 +922,12 @@ int main(int argc, char **argv) { @@ -858,6 +922,12 @@ int main(int argc, char **argv) {
auto cmd_options = parse_options(argc, argv);
duk::duktape d {};
d.test();
exit(0);
stgen::builder b (cmd_options);
b.build();

Loading…
Cancel
Save