From a861d61858edbf31cc3d5f4b78cd9ab973ba195a Mon Sep 17 00:00:00 2001 From: Light Beacon <2682645990@qq.com> Date: Tue, 20 Feb 2024 01:03:46 +0800 Subject: [PATCH] Server --- Core/Debug.py | 4 ++ Core/FileIO.py | 1 - Core/Project.py | 4 +- .../Resources/Scripts/MarkdownPresenter.py | 9 +++-- Server.py | 39 +++++++++++++++++++ requirements.txt | 1 + 6 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 Server.py diff --git a/Core/Debug.py b/Core/Debug.py index b2c19c3..63f276e 100644 --- a/Core/Debug.py +++ b/Core/Debug.py @@ -11,6 +11,10 @@ def LogError(infomation:str): print(f'{red}[ERROR]{infomation}') return infomation +def LogFatal(infomation:str): + print(f'{red}[FATAL]{infomation}') + return infomation + tabtext = ' ' def FormatXaml(code:str): diff --git a/Core/FileIO.py b/Core/FileIO.py index bbe9f57..f0de823 100644 --- a/Core/FileIO.py +++ b/Core/FileIO.py @@ -2,7 +2,6 @@ import json import os import re -import importlib.util from typing import List,Tuple,Dict from .Debug import LogInfo diff --git a/Core/Project.py b/Core/Project.py index 2c9c00d..2cdbfb7 100644 --- a/Core/Project.py +++ b/Core/Project.py @@ -3,7 +3,7 @@ from .Resource import Resource from .Styles import getStyleCode from .Templates_Manager import TemplateManager -from .Debug import LogInfo, LogError, LogWarning +from .Debug import LogInfo, LogError, LogWarning, FormatXaml import os sep = os.path.sep @@ -80,4 +80,4 @@ def get_page_xaml(self,page_alias): page_xaml = page_xaml.replace('${animations}','') # TODO page_xaml = page_xaml.replace('${styles}',getStyleCode(self.resources.styles)) page_xaml = page_xaml.replace('${content}',content_xaml) - return page_xaml \ No newline at end of file + return page_xaml \ No newline at end of file diff --git a/Plugin/Markdown/Resources/Scripts/MarkdownPresenter.py b/Plugin/Markdown/Resources/Scripts/MarkdownPresenter.py index 632584b..7f37d41 100644 --- a/Plugin/Markdown/Resources/Scripts/MarkdownPresenter.py +++ b/Plugin/Markdown/Resources/Scripts/MarkdownPresenter.py @@ -12,7 +12,7 @@ def get_replacement(name:str,attrs:dict): component_name = 'h' replace_list.append(('level',name[1:])) elif name == 'a': - replace_list.append(('link',attrs['href'])) + replace_list.append(('link',attrs['href'])) return (component_name, replace_list) def replace(name,attrs,content,res): @@ -29,7 +29,7 @@ def replace(name,attrs,content,res): #print(f'{replace_str} : {content}') for k,v in replace_list: #print(k+v+' ') - replace_str = replace_str.replace(f'${{{k}}}',v) + replace_str = replace_str.replace(f'${{{k}}}',replace_esc_char(v)) return replace_str FIRSTLINE_SPACES = ' ' @@ -38,11 +38,11 @@ def tag2xaml(tag,res): attrs = tag.attrs content = '' if tag.contents: + if tag.name == 'p': + content += FIRSTLINE_SPACES for child in tag.contents: if isinstance(child,str): linebreak = '' - if tag.name == 'p': - content += FIRSTLINE_SPACES # linebreak += FIRSTLINE_SPACES content += replace_esc_char(child).replace('\n',linebreak) else: @@ -116,4 +116,5 @@ def script(card,args,res): '½':'½', '¾':'¾', '¿':'¿', + '&':'&' } \ No newline at end of file diff --git a/Server.py b/Server.py new file mode 100644 index 0000000..5abda8e --- /dev/null +++ b/Server.py @@ -0,0 +1,39 @@ +from flask import Flask +from Core.Project import Project +from Core.FileIO import readYaml +from Core.Debug import LogFatal +import os +import subprocess +app = Flask(__name__) + +@app.route("/") +def getpage(name:str): + if name.endswith('/version'): + return server.getPage('version') + return server.getPage(name) + +class Server: + def __init__(self): + envpath = os.path.dirname(os.path.dirname(__file__)) + self.config_path = f"{envpath}{os.path.sep}server_config.yml" + self.cache = {} + try: + self.config = readYaml(self.config_path) + self.project_path = self.config['project_path'] + self.project = Project(self.project_path) + project_dir = os.path.dirname(self.project_path) + githash = subprocess.check_output('git rev-parse HEAD',cwd = project_dir, shell=True) + githash = githash.decode("utf-8") + self.cache['version'] = githash + except Exception as e: + LogFatal(e.args) + exit() + + def getPage(self,name): + if name not in self.cache: + self.cache[name] = self.project.get_page_xaml(name) + return self.cache[name] + +server = Server() +if __name__ == "__main__": + app.run(port=6608) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 1ff2f0a..eb78d82 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ beautifulsoup4==4.12.3 Markdown==3.5.2 PyYAML==6.0.1 +flask==3.0.2 \ No newline at end of file