|
|
|
@ -37,11 +37,6 @@ namespace lex {
@@ -37,11 +37,6 @@ namespace lex {
|
|
|
|
|
#include <lexbor/dom/dom.h> |
|
|
|
|
#include "lexbor_base.h" |
|
|
|
|
|
|
|
|
|
struct serializer_result { |
|
|
|
|
size_t len; |
|
|
|
|
const lxb_char_t *data; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -142,10 +137,26 @@ class duktape {
@@ -142,10 +137,26 @@ class duktape {
|
|
|
|
|
eval_file("katex.min.js"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
std::string eval_katex(std::string math_text) { |
|
|
|
|
|
|
|
|
|
std::string eval_katex(std::string text) { |
|
|
|
|
//math_text = math_text.substr(2, math_text.length() - 4);
|
|
|
|
|
//
|
|
|
|
|
if (text.length() > 4) { |
|
|
|
|
text = text.substr(2, text.length() - 4); |
|
|
|
|
} else { |
|
|
|
|
spdlog::error("Generate katex: short \n\t{}", text); |
|
|
|
|
return "math syntax error"; |
|
|
|
|
} |
|
|
|
|
std::string math_text {}; |
|
|
|
|
|
|
|
|
|
math_text = math_text.substr(2, math_text.length() - 4); |
|
|
|
|
for (char i:text) { |
|
|
|
|
if (i == '\'') { |
|
|
|
|
math_text += "\\'"; |
|
|
|
|
} else if (i == '\\') { |
|
|
|
|
math_text += "\\\\"; |
|
|
|
|
} else { |
|
|
|
|
math_text += i; |
|
|
|
|
}
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const char *matht = math_text.c_str(); |
|
|
|
@ -157,12 +168,12 @@ class duktape {
@@ -157,12 +168,12 @@ class duktape {
|
|
|
|
|
|
|
|
|
|
duk_push_lstring(ctx, line.c_str(), line.length()); |
|
|
|
|
if (duk_peval(ctx) != 0) { |
|
|
|
|
spdlog::error("Generate katex: {}", duk_safe_to_string(ctx, -1)); |
|
|
|
|
spdlog::error("Generate katex: {} \n\t{}", duk_safe_to_string(ctx, -1), line); |
|
|
|
|
} else { |
|
|
|
|
return std::string{duk_safe_to_string(ctx, -1)}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return ""; |
|
|
|
|
return "math syntax error"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void test() { |
|
|
|
@ -329,16 +340,6 @@ class builder {
@@ -329,16 +340,6 @@ class builder {
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
lxb_inline lex::lxb_status_t |
|
|
|
|
serializer_callback_s(const lex::lxb_char_t *data, size_t len, void *ctx) |
|
|
|
|
{ |
|
|
|
|
lex::serializer_result * res = (lex::serializer_result *)ctx; |
|
|
|
|
res->len = len; |
|
|
|
|
res->data = data; |
|
|
|
|
|
|
|
|
|
return lex::LXB_STATUS_OK; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::string postprocess_math(const std::string &html_text) { |
|
|
|
|
using namespace lex; |
|
|
|
@ -372,12 +373,43 @@ class builder {
@@ -372,12 +373,43 @@ class builder {
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < lxb_dom_collection_length(collection); i++) { |
|
|
|
|
lxb_dom_element_t *element = lxb_dom_collection_element(collection, i); |
|
|
|
|
//serialize_node(lxb_dom_interface_node(element));
|
|
|
|
|
|
|
|
|
|
size_t len; |
|
|
|
|
lxb_char_t *text = lxb_dom_node_text_content(&element->node, &len); |
|
|
|
|
const std::string tt {(const char *)text, (const char *)text + len}; |
|
|
|
|
|
|
|
|
|
std::string mathtext = dukengine.eval_katex(tt); |
|
|
|
|
auto hn = lxb_dom_node_text_content_set(&element->node, (const unsigned char *)mathtext.c_str(), mathtext.length()); |
|
|
|
|
|
|
|
|
|
const unsigned char local_name[] = "math"; |
|
|
|
|
|
|
|
|
|
lxb_html_element_t * ele = lxb_html_document_create_element(document, local_name,
|
|
|
|
|
(size_t)4, nullptr); |
|
|
|
|
|
|
|
|
|
auto elem = lxb_dom_interface_element(ele); |
|
|
|
|
|
|
|
|
|
lxb_dom_node_t *n = lxb_html_document_parse_fragment(document,
|
|
|
|
|
elem, (const unsigned char *)mathtext.c_str(), mathtext.length()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lxb_dom_node_insert_after(&element->node, n); |
|
|
|
|
lxb_dom_node_remove(&element->node); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
lxb_dom_element_create(lxb_dom_document_t *document, |
|
|
|
|
const lxb_char_t *local_name, size_t lname_len, |
|
|
|
|
const lxb_char_t *ns_name, size_t ns_len, |
|
|
|
|
const lxb_char_t *prefix, size_t prefix_len, |
|
|
|
|
const lxb_char_t *is, size_t is_len, |
|
|
|
|
bool sync_custom); |
|
|
|
|
lxb_html_document_parse_fragment(lxb_html_document_t *document, |
|
|
|
|
lxb_dom_element_t *element, |
|
|
|
|
const lxb_char_t *html, size_t size); |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//auto hn = lxb_dom_node_text_content_set(&element->node, (const unsigned char *)mathtext.c_str(), mathtext.length());
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -391,7 +423,6 @@ class builder {
@@ -391,7 +423,6 @@ class builder {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
serialize(lxb_dom_interface_node(document)); |
|
|
|
|
std::string result = {res.data, res.data + res.length}; |
|
|
|
|
|
|
|
|
|
lxb_dom_collection_destroy(collection, true); |
|
|
|
|