from flask import Flask, render_template_string import json import os import re app = Flask(__name__) # Получаем абсолютный путь к папке, где лежит app.py BASE_DIR = os.path.dirname(os.path.abspath(__file__)) def read_json(filename): # Строим путь через BASE_DIR path = os.path.join(BASE_DIR, 'static', 'json', filename) if os.path.exists(path): try: with open(path, 'r', encoding='utf-8') as f: return json.load(f) except Exception as e: print(f"Ошибка чтения JSON {filename}: {e}") return [] return [] def read_txt(filename): path = os.path.join(BASE_DIR, 'static', 'txt', filename) if os.path.exists(path): with open(path, 'r', encoding='utf-8') as f: content = f.read() return process_custom_tags(content) return f"Файл {filename} не найден" def process_custom_tags(text): # УБРАЛИ ведущий слэш перед static: 'static/images/...' вместо '/static/images/...' img_pattern = r'\[IMG:(.*?)\]' text = re.sub(img_pattern, r'', text) table_pattern = r'\[TABLE:(.*?):(.*?)\]' def build_table(match): filename = match.group(1) columns = match.group(2).split(',') data = read_json(filename) if not data: return "" html = '' for row in data: html += '' for col in columns: val = row.get(col, '') if col == 'Icon': # Тоже убрали слэш здесь content = f'' elif col == 'Order': content = f'{val}' else: content = val html += f'' html += '' html += '
{content}
' return html return re.sub(table_pattern, build_table, text) HTML_TEMPLATE = """ Game Academy

🛡️ Game Academy

1.1 Стандартная лестница
{{ t1_1 | safe }} {% for row in l_json %} {% endfor %}
{{ row.TXT }}
1.2 Боевые порядки
{{ t1_2 | safe }}
1.3 Формации
{{ t1_3 | safe }}
1.4 Механика боя
{{ t1_4 | safe }}
1.4.1 Сплоченная атака (СА)
{{ t1_4_1 | safe }}
1.4.2 Замок на замок
{{ t1_4_2 | safe }}
1.4.3 Саура
{{ t1_4_3 | safe }}
1.4.4 Арена, тропы
{{ t1_4_4 | safe }}
""" @app.route('/') def index(): return render_template_string(HTML_TEMPLATE, t1_1=read_txt('chapter1_1.txt'), l_json=read_json('ladder.json'), t1_2=read_txt('chapter1_2.txt'), t1_3=read_txt('chapter1_3.txt'), t1_4=read_txt('chapter1_4.txt'), t1_4_1=read_txt('chapter1_4_1.txt'), t1_4_2=read_txt('chapter1_4_2.txt'), t1_4_3=read_txt('chapter1_4_3.txt'), t1_4_4=read_txt('chapter1_4_4.txt')) if __name__ == '__main__': app.run(host='0.0.0.0', port=7860)