Skip to content

Commit

Permalink
Merge pull request #1 from abap34/first-release
Browse files Browse the repository at this point in the history
最初のリリース 🎉
  • Loading branch information
abap34 authored Aug 8, 2023
2 parents 56c018a + 8f4b3f4 commit 5f3c770
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 55 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Binary file added main
Binary file not shown.
6 changes: 0 additions & 6 deletions src/huga.cpp

This file was deleted.

10 changes: 10 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "makejson.hpp"
#include "parse.hpp"
#include "render.hpp"
#include <iostream>
#include <fstream>

int main(int argc, char *argv[]){
nlohmann::json json_ir = almo::make_json(almo::parse_md_file(argv[1]));
almo::render(json_ir);
}
6 changes: 3 additions & 3 deletions src/makejson.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ nlohmann::json dp_on_AST(AST::node_ptr ptr){
return cur_json;
}

void make_json(std::vector<AST::node_ptr> ast) {
nlohmann::json make_json(std::vector<AST::node_ptr> 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
91 changes: 46 additions & 45 deletions src/render.cpp → src/render.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include <glob.h>
#include "json.hpp"


namespace almo{

std::string read_file(const std::string& path) {
std::ifstream file(path);
if (!file.is_open()) {
Expand Down Expand Up @@ -37,7 +40,7 @@ std::vector<std::string> glob(const std::string& pattern) {
}

std::pair<std::string, std::string> 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("<!-- ___split___ -->"));
std::string tail = html_template.substr(html_template.find("<!-- ___split___ -->") + 20);
return std::make_pair(head, tail);
Expand Down Expand Up @@ -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 "<br>" + 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<std::string, std::function<std::string(nlohmann::json, std::string)>> render_map) {
Expand All @@ -235,56 +238,54 @@ std::string build_block(nlohmann::json j, std::map<std::string, std::function<st
render_str += from_render;
}
render_str = render_map[j["class"]](j, render_str);
} else {
}
else {
render_str = render_map[j["class"]](j, j["content"]);
}
return render_str;
}


int main() {

// クラス名とレンダリング関数の対応map
std::map<std::string, std::function<std::string(nlohmann::json, std::string)>> 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<std::string, std::function<std::string(nlohmann::json, std::string)>> 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<std::string, std::string> html_template = load_html_template();
}

std::string output_html = html_template.first + outputs + html_template.second;
std::pair<std::string, std::string> 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;
}

}

0 comments on commit 5f3c770

Please sign in to comment.