diff --git a/LICENSE b/LICENSE index c7452d0..543658e 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2023 abap34 +Copyright (c) 2023 abap34, ebi-fly13, noya2 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/main b/main new file mode 100755 index 0000000..0923476 Binary files /dev/null and b/main differ diff --git a/src/huga.cpp b/src/huga.cpp deleted file mode 100644 index e37451c..0000000 --- a/src/huga.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include "makejson.hpp" -#include "parse.hpp" - -int main(){ - almo::make_json(almo::parse_md_file("example.md")); -} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..56e491c --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,10 @@ +#include "makejson.hpp" +#include "parse.hpp" +#include "render.hpp" +#include +#include + +int main(int argc, char *argv[]){ + nlohmann::json json_ir = almo::make_json(almo::parse_md_file(argv[1])); + almo::render(json_ir); +} \ No newline at end of file diff --git a/src/makejson.hpp b/src/makejson.hpp index bc9d817..b68df52 100644 --- a/src/makejson.hpp +++ b/src/makejson.hpp @@ -76,12 +76,12 @@ nlohmann::json dp_on_AST(AST::node_ptr ptr){ return cur_json; } -void make_json(std::vector ast) { +nlohmann::json make_json(std::vector ast) { nlohmann::json output_json; for (AST::node_ptr ptr : ast){ output_json.push_back(dp_on_AST(ptr)); } - // output - std::cout << output_json.dump(4) << std::endl; + + return output_json; } } // namespace almo diff --git a/src/render.cpp b/src/render.hpp similarity index 81% rename from src/render.cpp rename to src/render.hpp index 733c62a..68473c5 100644 --- a/src/render.cpp +++ b/src/render.hpp @@ -7,6 +7,9 @@ #include #include "json.hpp" + +namespace almo{ + std::string read_file(const std::string& path) { std::ifstream file(path); if (!file.is_open()) { @@ -37,7 +40,7 @@ std::vector glob(const std::string& pattern) { } std::pair load_html_template() { - std::string html_template = read_file("template.html"); + std::string html_template = read_file("src/template.html"); std::string head = html_template.substr(0, html_template.find("")); std::string tail = html_template.substr(html_template.find("") + 20); return std::make_pair(head, tail); @@ -219,12 +222,12 @@ std::string render_block(nlohmann::json j, std::string content) { return content; } -std::string render_newline(nlohmann::json j, std::string content){ +std::string render_newline(nlohmann::json j, std::string content) { return "
" + content; } bool haschild(nlohmann::json j) { - return !(j["class"] == "PlainText" || j["class"] == "NewLine"); + return !(j["class"] == "PlainText" || j["class"] == "NewLine"); } std::string build_block(nlohmann::json j, std::map> render_map) { @@ -235,56 +238,54 @@ std::string build_block(nlohmann::json j, std::map> render_map; - render_map["H1"] = render_h1; - render_map["H2"] = render_h2; - render_map["H3"] = render_h3; - render_map["H4"] = render_h4; - render_map["H5"] = render_h5; - render_map["H6"] = render_h6; - render_map["InlineStrong"] = render_strong; - render_map["InlineItalic"] = render_italic; - render_map["InlineOverline"] = render_over_line; - render_map["InlineMath"] = render_inline_math; - render_map["MathBlock"] = render_math_block; - render_map["CodeBlock"] = render_code_block; - render_map["CodeRunner"] = render_code_runner; - render_map["PlainText"] = render_plain_text; - render_map["Block"] = render_block; - render_map["NewLine"] = render_newline; - - - nlohmann::json json_ir; - std::ifstream expect_file("noya2.json"); - expect_file >> json_ir; - - std::string outputs; - - for (nlohmann::json block : json_ir) { - if (block["class"] == "CodeRunner"){ - outputs += render_code_runner(block, ""); - } else { - std::string render_str; - render_str = build_block(block, render_map); - outputs += render_str + "\n"; - } - +void render(nlohmann::json json_ir) { + + // クラス名とレンダリング関数の対応map + std::map> render_map; + render_map["H1"] = render_h1; + render_map["H2"] = render_h2; + render_map["H3"] = render_h3; + render_map["H4"] = render_h4; + render_map["H5"] = render_h5; + render_map["H6"] = render_h6; + render_map["InlineStrong"] = render_strong; + render_map["InlineItalic"] = render_italic; + render_map["InlineOverline"] = render_over_line; + render_map["InlineMath"] = render_inline_math; + render_map["MathBlock"] = render_math_block; + render_map["CodeBlock"] = render_code_block; + render_map["CodeRunner"] = render_code_runner; + render_map["PlainText"] = render_plain_text; + render_map["Block"] = render_block; + render_map["NewLine"] = render_newline; + + std::string outputs; + + for (nlohmann::json block : json_ir) { + if (block["class"] == "CodeRunner") { + outputs += render_code_runner(block, ""); + } + else { + std::string render_str; + render_str = build_block(block, render_map); + outputs += render_str + "\n"; } - std::pair html_template = load_html_template(); + } - std::string output_html = html_template.first + outputs + html_template.second; + std::pair html_template = load_html_template(); - std::cout << output_html << std::endl; - } + std::string output_html = html_template.first + outputs + html_template.second; + + std::cout << output_html << std::endl; +} +} \ No newline at end of file