diff --git a/.github/workflows/dev_branch.yaml b/.github/workflows/dev_branch.yaml new file mode 100644 index 0000000..e8fd607 --- /dev/null +++ b/.github/workflows/dev_branch.yaml @@ -0,0 +1,30 @@ +name: Dev Branch + +on: + push: + branches: + - 'dev' + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + with: + submodules: 'recursive' + fetch-depth: 0 + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.x' + - name: Install necessary Python packages + run: | + pip install -r requirements.txt + - name: Update submodules + run: | + git submodule update --remote + + # Custom script + - name: Run custom prechecking + run: python prechecker.py \ No newline at end of file diff --git a/.github/workflows/pull_request.yaml b/.github/workflows/pull_request.yaml new file mode 100644 index 0000000..b00a665 --- /dev/null +++ b/.github/workflows/pull_request.yaml @@ -0,0 +1,29 @@ +name: Pull Request + +on: + pull_request: + types: [opened, reopened] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + with: + submodules: 'recursive' + fetch-depth: 0 + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.x' + - name: Install necessary Python packages + run: | + pip install -r requirements.txt + - name: Update submodules + run: | + git submodule update --remote + + # Custom script + - name: Run custom prechecking + run: python prechecker.py \ No newline at end of file diff --git a/.github/workflows/push.yaml b/.github/workflows/push.yaml index 6f03393..b8d88cc 100644 --- a/.github/workflows/push.yaml +++ b/.github/workflows/push.yaml @@ -2,6 +2,8 @@ name: Push on: push: + branches: + - 'main' workflow_dispatch: repository_dispatch: types: [ost-data-export-update-event] diff --git a/_main_config.py b/_main_config.py new file mode 100644 index 0000000..2a7f7cb --- /dev/null +++ b/_main_config.py @@ -0,0 +1,11 @@ +ALL_LANGS = ["en", "zh_cn"] +LANG_OTHER_PAGE = { + "page/{lang}/main_all.html": "main/index.html", + "page/{lang}/_index.html": "index.html", + "page/{lang}/_about.html": "about.html" +} +OTHER_STATIC_PAGE = { + "page/index.html": "index.html", + "page/404.html": "404.html", + "page/zh_cn/_zhcn_technical.html": "zh_cn/zhcn_technical.html" +} \ No newline at end of file diff --git a/main_prod.py b/main_prod.py index 4dcec73..ba5253e 100644 --- a/main_prod.py +++ b/main_prod.py @@ -1,16 +1,5 @@ from _main import * - -ALL_LANGS = ["en", "zh_cn"] -LANG_OTHER_PAGE = { - "page/{lang}/main_all.html": "main/index.html", - "page/{lang}/_index.html": "index.html", - "page/{lang}/_about.html": "about.html" -} -OTHER_STATIC_PAGE = { - "page/index.html": "index.html", - "page/404.html": "404.html", - "page/zh_cn/_zhcn_technical.html": "zh_cn/zhcn_technical.html" -} +from _main_config import * delete_old_page() copy_static_file(True) diff --git a/main_test.py b/main_test.py index d8dbbc5..991afd6 100644 --- a/main_test.py +++ b/main_test.py @@ -1,16 +1,6 @@ from _main import * +from _main_config import * -ALL_LANGS = ["zh_cn"] -LANG_OTHER_PAGE = { - "page/{lang}/main_all.html": "main/index.html", - "page/{lang}/_index.html": "index.html", - "page/{lang}/_about.html": "about.html" -} -OTHER_STATIC_PAGE = { - "page/index.html": "index.html", - "page/404.html": "404.html", - "page/zh_cn/_zhcn_technical.html": "zh_cn/zhcn_technical.html" -} ALL_FOLDERS = ["character"] delete_old_page() diff --git a/main_tool.py b/main_tool.py index 9c70e19..7a77abe 100644 --- a/main_tool.py +++ b/main_tool.py @@ -1,5 +1,5 @@ -import datetime -import minify_html +import time +import htmlmin import os import json import urllib.parse @@ -17,8 +17,8 @@ class JinjaTool: @staticmethod - def get_current_utc(fmt="%Y-%m-%d %H:%M:%S"): - return datetime.datetime.utcnow().strftime(fmt) + def get_current_utc(): + return time.time() @staticmethod def generate_tooltip_id(): @@ -31,7 +31,10 @@ def generate_tooltip_id(): @staticmethod def js_string_safe(value): - return value.replace("&", "&").replace("'", "\'") + return (value.replace("&", "&"). + replace("'", "\'"). + replace("[", "["). + replace("]", "]")) @staticmethod def js_html_string_safe(value): @@ -39,11 +42,10 @@ def js_html_string_safe(value): @staticmethod def page_minify_html(value): - return minify_html.minify(value, keep_closing_tags=True, - remove_processing_instructions=True). \ - replace("'", "'"). \ - replace(" ", ""). \ - replace("   ", " ") + return htmlmin.minify(value, + remove_empty_space=True, + remove_optional_attribute_quotes=False). \ + replace("'", "'") @staticmethod def get_outer_json(instance_id: str, instance_type: str): @@ -154,7 +156,6 @@ def py_tooltip_background(background, instance_id, instance_type, extra_data): # if GenerationCache.check_for_cache("all", cache_id): # return GenerationCache.get_from_cache("all", cache_id) - for story in extra_data.values(): story = story[0] story_parts = [] @@ -342,4 +343,3 @@ def py_output_usedby_story(data_story: dict, instance_uuid, instance_key): # GenerationCache.add_to_cache("all", cache_id, [story for (i, j) in all_story_with_filetype.items() for story in j]) return all_story_with_filetype - diff --git a/page/en/battle.html b/page/en/battle.html index 2d01a79..d94cdf3 100644 --- a/page/en/battle.html +++ b/page/en/battle.html @@ -2,7 +2,7 @@ {% block title %}Battle/{% if battle.filetype == 21 %}{{ battle.chapter }}-{{ battle.no[:2] }} {% endif %}{{ battle.name.en }}{% endblock title %} {% block article_content %} - {% from "template/en/_macro.html" import get_social_for_composer %} + {% from "template/en/_macro.html" import get_social_for_composer, generate_track_anchor %}

Battle/{% if battle.filetype == 21 %}{{ battle.chapter }}-{{ battle.no[:2] }} {% endif %}{{ battle.name.en }}

{% include "template/en/infobox/battle.html" %} @@ -18,14 +18,13 @@

BOSS Info

{% endif %}

Track Info

- {% if battle.filetype in [21, 22] %} - {% set track = battle.track.0 %} - {% else %} - {% set track = battle.track %} - {% endif %} {% endblock %} \ No newline at end of file diff --git a/page/en/character_all.html b/page/en/character_all.html index bf74d22..eb0e7ea 100644 --- a/page/en/character_all.html +++ b/page/en/character_all.html @@ -38,7 +38,7 @@

Students

{% set curr = i.1 %}
  • - {{ curr.student.name.name.en }} / {{ curr.student.name.name.jp }} + {{ curr.student.name.name.en }} / {{ curr.student.name.name.jp }}
    diff --git a/page/en/video.html b/page/en/video.html index d559518..9e97b8e 100644 --- a/page/en/video.html +++ b/page/en/video.html @@ -1,4 +1,5 @@ {% extends "template/en/base.html" %} +{% from 'template/en/_macro.html' import output_video_stat_badge %} {% from 'template/en/_macro_page.html' import output_usedby_track_list, track_version_video_player %} {% block title %}Video/{{ video.name.en }}{% endblock title %} @@ -6,7 +7,7 @@ {% endblock other_head %} {% block article_content %} -

    Video/{{ video.name.en }}

    +

    Video/{{ video.name.en }} {{ output_video_stat_badge(video.stats) }}

    {% include "template/en/infobox/video.html" %}

    {{ video.name.en }} is a video related to Blue Archive.

    Track Information

    diff --git a/page/en/video_all.html b/page/en/video_all.html index b409bd8..fbb87ee 100644 --- a/page/en/video_all.html +++ b/page/en/video_all.html @@ -1,4 +1,5 @@ {% extends "template/en/base.html" %} +{% from 'template/en/_macro.html' import output_video_stat_badge %} {% block title %}{{ video.name.en }}{% endblock title %} {% block article_content %} @@ -7,7 +8,9 @@

    {{ video.name.en }}

    {% endblock article_content %} \ No newline at end of file diff --git a/page/zh_cn/battle.html b/page/zh_cn/battle.html index 69372ba..243abb6 100644 --- a/page/zh_cn/battle.html +++ b/page/zh_cn/battle.html @@ -2,7 +2,7 @@ {% block title %}任务/{% if battle.filetype == 21 %}{{ battle.chapter }}-{{ battle.no[:2] }} {% endif %}{{ battle.name.zh_cn }}{% endblock title %} {% block article_content %} - {% from "template/zh_cn/_macro.html" import get_social_for_composer, auto_zhcn_gen %} + {% from "template/zh_cn/_macro.html" import get_social_for_composer, auto_zhcn_gen, generate_track_anchor %}

    战斗/{% if battle.filetype == 21 %}{{ battle.chapter }}-{{ battle.no[:2] }} {% endif %}{{ auto_zhcn_gen(battle.name) }}

    {% include "template/zh_cn/infobox/battle.html" %} @@ -18,14 +18,13 @@

    BOSS信息

    {% endif %}

    曲目信息

    - {% if battle.filetype in [21, 22] %} - {% set track = battle.track.0 %} - {% else %} - {% set track = battle.track %} - {% endif %} {% endblock article_content %} \ No newline at end of file diff --git a/page/zh_cn/video.html b/page/zh_cn/video.html index 7191f72..d150638 100644 --- a/page/zh_cn/video.html +++ b/page/zh_cn/video.html @@ -1,7 +1,8 @@ {% extends "template/zh_cn/base.html" %} +{% from 'template/zh_cn/_macro.html' import output_video_stat_badge %} {% from 'template/zh_cn/_macro_page.html' import output_usedby_track_list, track_version_video_player %} -{% block title %}视频/{{ video.name.zh_cn }}{% endblock title %} +{% block title %}视频/{{ video.name.zh_cn }} {{ output_video_stat_badge(video.stats) }}{% endblock title %} {% block other_head %} {% endblock other_head %} diff --git a/page/zh_cn/video_all.html b/page/zh_cn/video_all.html index f57b4f5..333e7e1 100644 --- a/page/zh_cn/video_all.html +++ b/page/zh_cn/video_all.html @@ -1,4 +1,5 @@ {% extends "template/zh_cn/base.html" %} +{% from 'template/zh_cn/_macro.html' import output_video_stat_badge %} {% block title %}{{ video.name.zh_cn }}{% endblock title %} {% block article_content %} @@ -7,7 +8,9 @@

    {{ video.name.zh_cn }}

    {% endblock article_content %} \ No newline at end of file diff --git a/precheck_script/check_static.py b/precheck_script/check_static.py new file mode 100644 index 0000000..bf46cc9 --- /dev/null +++ b/precheck_script/check_static.py @@ -0,0 +1,49 @@ +import time + +ALL_LANGS = ["en", "zh_cn"] + + +def render_template(env, path): + print(f"[INFO]\tTesting {path}...") + try: + template = env.get_template(path) + + start = time.time_ns() + template.render(is_static=True) + end = time.time_ns() + except Exception as e: + print(f"[FAIL]\tFailed with exception: {str(e)}") + raise e + else: + print(f"[OK]\tSuccessfully tested (runtime {(end - start) / 1e6} ms)") + + +def check_static_per_lang(env, page: dict): + total = len(page.keys()) + failed_count = 0 + + for i in ALL_LANGS: + for path, name in page.items(): + try: + render_template(env, path.format(lang=i)) + except Exception: + failed_count += 1 + + print(f"Total {total}, failed {failed_count} ({failed_count / total * 100:0.2f}%)") + if failed_count != 0: + raise AssertionError + + +def check_static_otehr(env, page: dict): + total = len(page.keys()) + failed_count = 0 + + for path, name in page.items(): + try: + render_template(env, path) + except Exception: + failed_count += 1 + + print(f"Total {total}, failed {failed_count} ({failed_count / total * 100:0.2f}%)") + if failed_count != 0: + raise AssertionError diff --git a/precheck_script/check_template.py b/precheck_script/check_template.py index 2a3b7d3..0337bd2 100644 --- a/precheck_script/check_template.py +++ b/precheck_script/check_template.py @@ -12,17 +12,39 @@ "track.html", "track_all.html", "ui.html", "ui_all.html", "video.html", "video_all.html"] -ALL_TEST_FILES = ["album/01.json", "album/_all.json", - "background/BG_CS_Abydos_04.jpg.json", "background/_all.json", - "main/battle/main/3/NA0.json", "main/battle/main/3/_all.json", - "character/_all.json", "character/npc/ArisKey/ArisKey.json", "character/student/yuuka/yuuka.json", - "event/_all.json", "event/810/battle/_all.json", "event/818/story/_all.json", - "event/812/ui/_all.json", - "main/story/main/2/2/25.json", "main/story/main/2/2/_all.json", - "tag/bright.json", "tag/_all.json", - "track/ost/7.json", "track/ost/_all.json", - "ui/1.json", "ui/_all.json", - "video/1.json", "video/_all.json"] +ALL_TEST_FILES = [["album/01.json"], # album.html + ["album/_all.json"], # album_all.html + + ["background/BG_CS_Abydos_04.jpg.json"], # background.html + ["background/_all.json"], # background_all.html + + ["main/battle/main/3/NA0.json", "main/battle/main/3/H10.json", "main/battle/main/3/N10.json"], # battle.html + ["main/battle/main/3/_all.json"], # battle_all.html + + ["character/_all.json", "character/npc/_all.json"], # character_all.html + ["character/npc/ArisKey/ArisKey.json"], # npc.html + ["character/student/yuuka/yuuka.json"], # student.html + + ["event/_all.json"], # event_all.html + ["event/810/battle/_all.json"], # event_battle_all.html + ["event/818/story/_all.json"], # event_story_all.html + ["event/812/ui/_all.json"], # event_ui_all.html + + ["main/story/main/2/2/23.json", "event/818/story/01.json"], # story.html + ["main/story/main/2/2/_all.json", "event/818/story/_all.json"], # story_all.html + + ["tag/bright.json"], # tag.html + ["tag/_all.json"], # tag_all.html + + ["track/ost/7.json", "track/ost/1.json"], # track.html + ["track/ost/_all.json"], # track_all.html + + ["ui/1.json", "event/812/ui/1.json"], # ui.html + ["ui/_all.json"], # ui_all.html + + ["video/1.json"], # video.html + ["video/_all.json"] # video_all.html +] TEST_FILE_BASEPATH = "exported_data/" ALL_LANGS = ["en", "zh_cn"] @@ -33,23 +55,24 @@ def _load_json(filepath): def check_template(jinja_env, return_template): - total = len(ALL_LANGS) * len(ALL_TEMPLATE_NAMES) + total = len(ALL_LANGS) * sum([len(i) for i in ALL_TEST_FILES]) failed_count = 0 for lang in ALL_LANGS: - for (template_name, json_filepath) in zip(ALL_TEMPLATE_NAMES, ALL_TEST_FILES): - template = jinja_env.get_template(f"page/{lang}/{template_name}") - - print(f"[INFO]\tTesting {lang}/{template_name} with {json_filepath}...") - try: - start = time.time_ns() - return_template(template_name, template, _load_json(json_filepath)) - end = time.time_ns() - except Exception as e: - print(f"[FAIL]\tFailed with exception: {str(e)}") - failed_count += 1 - else: - print(f"[OK]\tSuccessfully tested (runtime {(end - start) / 1e6} ms)") + for (template_name, json_filepaths) in zip(ALL_TEMPLATE_NAMES, ALL_TEST_FILES): + for json_filepath in json_filepaths: + template = jinja_env.get_template(f"page/{lang}/{template_name}") + + print(f"[INFO]\tTesting {lang}/{template_name} with {json_filepath}...") + try: + start = time.time_ns() + return_template(template_name, template, _load_json(json_filepath)) + end = time.time_ns() + except Exception as e: + print(f"[FAIL]\tFailed with exception: {str(e)}") + failed_count += 1 + else: + print(f"[OK]\tSuccessfully tested (runtime {(end - start) / 1e6} ms)") print(f"Total {total}, failed {failed_count} ({failed_count / total * 100:0.2f}%)") if failed_count != 0: diff --git a/prechecker.py b/prechecker.py index 272bc6b..45471b2 100644 --- a/prechecker.py +++ b/prechecker.py @@ -1,5 +1,25 @@ from _main import get_jinja_env, GeneratorTool +from _main_config import * from precheck_script.check_template import check_template +from precheck_script.check_static import check_static_per_lang, check_static_otehr env = get_jinja_env() -check_template(env, GeneratorTool.return_template) +encounter_exception = False + +try: + check_template(env, GeneratorTool.return_template) +except AssertionError: + encounter_exception = True + +try: + check_static_per_lang(env, LANG_OTHER_PAGE) +except AssertionError: + encounter_exception = True + +try: + check_static_otehr(env, OTHER_STATIC_PAGE) +except AssertionError: + encounter_exception = True + +if encounter_exception: + raise AssertionError diff --git a/requirements.txt b/requirements.txt index 14eb8f1..4a6f175 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ MarkupSafe==2.1.3 Jinja2==3.1.2 -minify-html==0.11.1 \ No newline at end of file +htmlmin==0.1.12 \ No newline at end of file diff --git a/template/base/_macro.html b/template/base/_macro.html index 298f103..7ad38aa 100644 --- a/template/base/_macro.html +++ b/template/base/_macro.html @@ -11,3 +11,7 @@ {% macro word_case(text) %}{{ text[0]|upper }}{{ text[1:] }}{% endmacro %} {% macro generate_tooltip_anchor(tooltip_id) %}onmouseover="showTooltip(this)" data-tooltip="{{ tooltip_id }}"{% endmacro %} + +{% macro generate_popup_for_tooltip_anchor(tooltip_id) %}{% endmacro %} + +{% macro generate_tag_badge(color_css, text) %}{{ text }}{% endmacro %} diff --git a/template/base/base.html b/template/base/base.html index 9b140a5..d01c1b7 100644 --- a/template/base/base.html +++ b/template/base/base.html @@ -11,11 +11,13 @@ + +