提交 2f84db1a 编写于 作者: F feilong

如果markdown文本为空则初始化模版,修正新创建文件还没加入git管理时的username获取问题

上级 ae799754
......@@ -59,3 +59,6 @@ target/
.settings/**
.project
.pydevproject
.DS_Store
\ No newline at end of file
def simple_list_md_load(p):
with open(p, 'r', encoding='utf-8') as f:
lines = f.readlines()
result = []
for line in lines:
item = line.strip('\n')
result.append(item)
return result
def simple_list_md_dump(p, lines):
with open(p, 'w', encoding='utf-8') as f:
f.write('\n'.join(lines))
def emit_head(md):
title = '{在此填写标题}'
contents = ['{在此填写题目描述}']
md.append(f'# {title}')
md.append('')
for content in contents:
md.append(content)
md.append('')
def emit_answer(md, language):
md.append(f'## 答案')
md.append('')
if language:
md.append(f'```{language}')
md.append('')
md.append('```')
else:
md.append('{在此填写答案}')
md.append('')
def emit_options(md, language):
md.append(f'## 选项')
md.append('')
for tag in ['A', 'B', 'C']:
md.append(f'### {tag}')
md.append('')
if language:
md.append(f'```{language}')
md.append('')
md.append('```')
else:
md.append('{在此填写选项'+f'{tag}'+'}')
md.append('')
......@@ -9,6 +9,7 @@ import re
from parsec import BasicState, ParsecError
from .exercises.markdown import parse
from .exercises.init_exercises import emit_head, emit_answer, emit_options, simple_list_md_dump
id_set = set()
logger = logging.getLogger(__name__)
......@@ -36,6 +37,8 @@ def user_name(md_file, author_dict):
for line in lines:
if line.startswith('Author'):
author_lines.append(line.split(' ')[1])
if len(author_lines) == 0:
return None
author_nick_name = author_lines[-1]
return search_author(author_dict, author_nick_name)
......@@ -45,7 +48,8 @@ def load_json(p):
try:
return json.loads(f.read())
except UnicodeDecodeError:
logger.info("json 文件 [{p}] 编码错误,请确保其内容保存为 utf-8 或 base64 后的 ascii 格式。")
logger.info(
"json 文件 [{p}] 编码错误,请确保其内容保存为 utf-8 或 base64 后的 ascii 格式。")
def dump_json(p, j, exist_ok=False, override=False):
......@@ -374,19 +378,38 @@ class TreeWalker:
export.append(mfile)
flag = True
config["export"] = export
data = None
with open(md_file, "r", encoding="utf-8") as efile:
try:
data = efile.read()
except UnicodeDecodeError:
logger.error(f"习题 [{md_file}] 编码错误,请确保其保存为 utf-8 编码")
sys.exit(1)
state = BasicState(data)
if data.strip() == '':
md = []
emit_head(md)
emit_answer(md, None)
emit_options(md, None)
simple_list_md_dump(md_file, md)
data = None
with open(md_file, "r", encoding="utf-8") as efile:
try:
doc = parse(state)
except ParsecError as err:
index = state.index
context = state.data[index - 15:index + 15]
logger.error(f"习题 [{md_file}] 解析失败,在位置 {index} [{context}] 附近有格式: [{err}]")
data = efile.read()
except UnicodeDecodeError:
logger.error(f"习题 [{md_file}] 编码错误,请确保其保存为 utf-8 编码")
sys.exit(1)
state = BasicState(data)
try:
doc = parse(state)
except ParsecError as err:
index = state.index
context = state.data[index - 15:index + 15]
logger.error(
f"习题 [{md_file}] 解析失败,在位置 {index} [{context}] 附近有格式: [{err}]")
if flag:
dump_json(os.path.join(section_path, "config.json"),
......@@ -418,6 +441,8 @@ class TreeWalker:
meta["source"] = source
if "author" not in meta:
meta["author"] = user_name(md_file, self.authors)
elif meta['author'] is None:
meta["author"] = user_name(md_file, self.authors)
if "type" not in meta:
meta["type"] = "code_options"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册