Skip to content

Commit

Permalink
Server
Browse files Browse the repository at this point in the history
  • Loading branch information
Light-Beacon committed Feb 19, 2024
1 parent 7f669b4 commit a861d61
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 7 deletions.
4 changes: 4 additions & 0 deletions Core/Debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
1 change: 0 additions & 1 deletion Core/FileIO.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import json
import os
import re
import importlib.util
from typing import List,Tuple,Dict
from .Debug import LogInfo

Expand Down
4 changes: 2 additions & 2 deletions Core/Project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
return page_xaml
9 changes: 5 additions & 4 deletions Plugin/Markdown/Resources/Scripts/MarkdownPresenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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 = ' '
Expand All @@ -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 = '<LineBreak/>'
if tag.name == 'p':
content += FIRSTLINE_SPACES
# linebreak += FIRSTLINE_SPACES
content += replace_esc_char(child).replace('\n',linebreak)
else:
Expand Down Expand Up @@ -116,4 +116,5 @@ def script(card,args,res):
'½':'&frac12;',
'¾':'&frac34;',
'¿':'&iquest;',
'&':'&amp;'
}
39 changes: 39 additions & 0 deletions Server.py
Original file line number Diff line number Diff line change
@@ -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("/<path:name>")
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)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
beautifulsoup4==4.12.3
Markdown==3.5.2
PyYAML==6.0.1
flask==3.0.2

0 comments on commit a861d61

Please sign in to comment.