Browse Source

add lexbor and get math vals

master
alistair 3 years ago
parent
commit
1995def9b0
  1. 10
      CMakeLists.txt
  2. 55
      main.cpp

10
CMakeLists.txt

@ -35,8 +35,13 @@ FetchContent_Declare(tinyxml2 @@ -35,8 +35,13 @@ FetchContent_Declare(tinyxml2
GIT_REPOSITORY "https://github.com/leethomason/tinyxml2"
)
FetchContent_Declare(lexbor
GIT_REPOSITORY "https://github.com/lexbor/lexbor"
)
FetchContent_MakeAvailable(spdlog)
FetchContent_MakeAvailable(tinyxml2)
FetchContent_MakeAvailable(lexbor)
if(NOT TARGET tinyxml2)
# Stand-alone build
@ -48,6 +53,10 @@ if(NOT TARGET spdlog) @@ -48,6 +53,10 @@ if(NOT TARGET spdlog)
find_package(spdlog REQUIRED)
endif()
if(NOT TARGET spdlog)
# Stand-alone build
find_package(lexbor REQUIRED)
endif()
ADD_EXECUTABLE(stgen3 util.cpp darkhttpd.c duktape/src/duktape.c markdown.cpp main.cpp)
@ -62,5 +71,6 @@ add_custom_command( @@ -62,5 +71,6 @@ add_custom_command(
target_link_libraries(stgen3 PRIVATE ${CMAKE_SOURCE_DIR}/MultiMarkdown/build/libMultiMarkdown.a)
target_link_libraries(stgen3 PRIVATE tinyxml2)
target_link_libraries(stgen3 PRIVATE lexbor)
install(TARGETS stgen3)

55
main.cpp

@ -32,6 +32,14 @@ @@ -32,6 +32,14 @@
#include "markdown.h"
#include "templater.hpp"
extern "C" {
namespace lex {
#include <lexbor/dom/dom.h>
#include "lexbor_base.h"
};
}
const std::string SITE_CONFIG_FNAME = "stgen.json";
const std::string DEFAULT_PUBLISH_ROOT = "public";
@ -321,6 +329,50 @@ class builder { @@ -321,6 +329,50 @@ class builder {
}
void postprocess_math(const std::string &html_text) {
using namespace lex;
lxb_html_document_t *document =
parse((const unsigned char *)html_text.c_str(), html_text.length());
lxb_dom_collection_t *collection =
lxb_dom_collection_make(&document->dom_document, 128);
if (collection == nullptr) {
spdlog::warn("failed to parse html?");
// messy
return;
}
auto err = lxb_dom_elements_by_class_name(
lxb_dom_interface_element(document->body),
collection, (const lxb_char_t *) "math", 4);
if (err != LXB_STATUS_OK) {
spdlog::warn("failed to parse html?");
return;
}
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);
spdlog::warn("math text: {}", mathtext);
}
lxb_dom_collection_destroy(collection, true);
lxb_html_document_destroy(document);
}
/** BRoken and ridiculous and wont work why did i write this. */
std::string replace_math(const std::string &text) {
@ -651,9 +703,8 @@ class builder { @@ -651,9 +703,8 @@ class builder {
auto job_t = job_type::TEMPLATE;
if (ext == "md" || ext == "markdown") {
replace_math(text);
text = parser.parse_to_html(text);
// text = postprocess_js(text);
postprocess_math(text);
}

Loading…
Cancel
Save