Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Light-Beacon committed Jan 14, 2025
2 parents f4a4263 + 4b808c4 commit 345a435
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
12 changes: 9 additions & 3 deletions src/homepagebuilder/command/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
from os.path import sep, exists
from .proc import CommandProcesser
from ..core.utils.property import PropertySetter
from ..core.logger import Logger
from ..core.i18n import locale as t

logger = Logger('Command|Build')

class BuildCommand(CommandProcesser):
name = 'build'
help = 'Build Homepage'
Expand All @@ -28,9 +33,9 @@ def process(self,args) -> None:
self.__gen_allpage(args,builder,args.output_path)
else:
page = args.page
self.__gen_onepage(args,page,builder,args.output_path)
self.__gen_single_page(args,page,builder,args.output_path)

def __gen_onepage(self,args,page,builder,path):
def __gen_single_page(self,args,page,builder,path):
if not path:
path = os.getcwd() + os.path.sep + 'output.xaml'
if not page:
Expand All @@ -40,6 +45,7 @@ def __gen_onepage(self,args,page,builder,path):
else:
page_output_path = path
self.__build_and_output(builder.current_project, page, page_output_path, args)
logger.info(t('command.build.done', path=page_output_path))

def __gen_allpage(self,args,builder,path):
if not path:
Expand All @@ -50,7 +56,7 @@ def __gen_allpage(self,args,builder,path):
makedirs(path, exist_ok=True)
for page in builder.current_project.get_all_pagename():
page_output_path = f"{path}{page}.xaml"
self.__gen_onepage(args,page,builder,page_output_path)
self.__gen_single_page(args,page,builder,page_output_path)

@classmethod
def __build_and_output(cls,project, page, output_path, args):
Expand Down
2 changes: 1 addition & 1 deletion src/homepagebuilder/core/utils/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def trigger_event(event_name:str,*args,**kwargs):
except ResultOverride as ro:
raise ro
except Exception as ex:
logger.error(locale('event.error',eventname=event_name, ex=ex))
logger.error(locale('event.error',event_name=event_name, ex=ex))
raise ex

class ResultOverride(Exception):
Expand Down
21 changes: 16 additions & 5 deletions src/homepagebuilder/plugins/markdown/modules/MarkdownPresenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@
from bs4 import BeautifulSoup
from homepagebuilder.interfaces import script, require
from homepagebuilder.core.utils.encode import encode_escape
from homepagebuilder.core.utils.event import set_triggers, listen_event

parsers_module = require('markdown_parsers')
create_node= parsers_module.create_node
markdown_process_pipeline = []

DEL_PATTERN = re.compile(r'~~(.*)~~')

def markdown_processor(func):
'''markdown 额外处理函数装饰器'''
markdown_process_pipeline.append(func)
return func

def html2xaml(html,context):
'''html转为xaml代码'''
Expand All @@ -18,27 +27,29 @@ def html2xaml(html,context):
DELETE_PATTEN = re.compile(r'~~(.*)~~')
BLOCK_CODE_PATTEN = re.compile(r'`{3,}\s*(\S*)\s*\n(.*?)\n`{3,}', flags=re.RegexFlag.DOTALL)

@markdown_processor
def md_del_replace(md:str):
'''转译删除线'''
return re.sub(DELETE_PATTEN, r'<del>\1</del>',md)

@markdown_processor
def block_code_replace(md:str):
'''转译块状代码'''
def repl(matchobj:re.Match) -> str:
# 注意:bs4 会将转义字符先反转义一次
return f'<blockcode lang="{matchobj.group(1).upper()}" code="{encode_escape(matchobj.group(2), with_special=True)}"/>'
return re.sub(BLOCK_CODE_PATTEN, repl, md)

def convert(card,context):
def convert(md,context):
'''生成xaml代码'''
md = card['markdown']
md = md_del_replace(md)
md = block_code_replace(md)
for processor in markdown_process_pipeline:
md = processor(md)
html = markdown.markdown(md)
xaml = html2xaml(html,context)
return xaml

@script('MarkdownPresenter')
def markdown_presenter(card,context,**_):
'''从markdown生成xaml代码脚本'''
return convert(card,context)
md = card['markdown']
return convert(md,context)
1 change: 1 addition & 0 deletions src/homepagebuilder/resources/i18n/en_US.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
builder.load.resources: 'Loading builder resources'
command.build.done: 'Build done, files saved to ${path}'
debug.timer.start: '${func}(${count}) Timer Started'
debug.timer.stop: '${func}(${count}) Timer Stopped, Time used: ${time}s'
event.subscribe: '${func} subcribed event ${name}'
Expand Down
1 change: 1 addition & 0 deletions src/homepagebuilder/resources/i18n/zh_CN.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
builder.load.resources: '加载构建器资源'
command.build.done: '构建完成,文件保存至 ${path}'
debug.timer.start: '${func}(${count}) 计时器启动'
debug.timer.stop: '${func}(${count}) 计时器停止,该函数运行时间: ${time}s'
event.subscribe: '${func} 订阅事件 ${name}'
Expand Down

0 comments on commit 345a435

Please sign in to comment.