Skip to content

Commit

Permalink
Merge pull request #60 from abap34/dev
Browse files Browse the repository at this point in the history
レスポンス改善のために Pyodide の読み込みが不要な時スキップされるように
  • Loading branch information
abap34 authored Mar 2, 2024
2 parents e3cf508 + c293472 commit a675034
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
9 changes: 9 additions & 0 deletions src/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ namespace almo {
// - syntax_theme: Highlight.jsのテーマ
std::map<std::string, std::string> meta_data;

// レスポンス時間短縮のため、pyodideが不要なら読み込みをスキップするためのフラグ.
bool loaded_pyodide = false;

std::string pyodide_loader = "<script src=\"https://cdn.jsdelivr.net/pyodide/v0.24.0/full/pyodide.js\"></script>";

// 各ノードに対して一意なIDを生成する用の関数オブジェクト. 呼ぶと前に呼ばれた時の値 + 1 が返る.
struct UUID_gen {
std::string operator()() {
Expand Down Expand Up @@ -552,6 +557,8 @@ namespace almo {

std::string output = title_h3 + editor_div + ace_editor + sample_in_area + sample_out_area + expect_out_area + define_data + test_run_button + submit_button + judge_code;

loaded_pyodide = true;

return output;
}

Expand Down Expand Up @@ -613,6 +620,8 @@ namespace almo {

std::string output = editor_div + ace_editor + out_area + plot_area + run_button;

loaded_pyodide = true;

return output;
}

Expand Down
15 changes: 11 additions & 4 deletions src/render.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ namespace almo {
}

std::string runner = "<script>" + RUNNER + "</script>";

if (loaded_pyodide) {
// runnner の先頭に  pyodide を挿入
runner = pyodide_loader + runner;
} else {
runner = "<!-- Runner is not required. Skip this. -->";
}

std::string sidebar_builder = "<script>" + SIDEBAR_BULDER + "</script>";

// runner を挿入
Expand Down Expand Up @@ -88,11 +96,10 @@ namespace almo {
}

std::string render(Block ast, std::map<std::string, std::string> meta_data) {

std::string html_template = load_html_template(meta_data["template_file"], meta_data["css_setting"]);

std::string content = ast.render();


std::string html_template = load_html_template(meta_data["template_file"], meta_data["css_setting"]);

std::string output_html = replace_template(html_template, meta_data, content);

return output_html;
Expand Down
4 changes: 1 addition & 3 deletions src/runner.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@




all_sample_input = {}
all_sample_output = {}
Expand All @@ -11,7 +10,6 @@ use_libs = []

judge_types = {}
problem_status = {}
page_contents = []

const pyodidePromise = loadPyodide({
stdin: stdin_func,
Expand Down
7 changes: 4 additions & 3 deletions src/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@

<script>hljs.highlightAll();</script>

<script src="https://cdn.jsdelivr.net/pyodide/v0.24.0/full/pyodide.js"></script>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/confetti.browser.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.0/ace.js"></script>
Expand All @@ -40,11 +38,14 @@
<script type="text/javascript" id="MathJax-script" async
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.11.0/gsap.min.js"></script>

{{style}}


<script>
page_contents = []
</script>

{{runner}}


Expand Down

0 comments on commit a675034

Please sign in to comment.