From 2f84db1a08d398fd58b90ca5800339b72b582f5c Mon Sep 17 00:00:00 2001 From: feilong Date: Wed, 9 Feb 2022 19:08:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A6=82=E6=9E=9Cmarkdown=E6=96=87=E6=9C=AC?= =?UTF-8?q?=E4=B8=BA=E7=A9=BA=E5=88=99=E5=88=9D=E5=A7=8B=E5=8C=96=E6=A8=A1?= =?UTF-8?q?=E7=89=88=EF=BC=8C=E4=BF=AE=E6=AD=A3=E6=96=B0=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E8=BF=98=E6=B2=A1=E5=8A=A0=E5=85=A5git?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E6=97=B6=E7=9A=84username=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++ src/skill_tree/exercises/init_exercises.py | 52 ++++++++++++++++++++++ src/skill_tree/tree.py | 39 +++++++++++++--- 3 files changed, 87 insertions(+), 7 deletions(-) create mode 100644 src/skill_tree/exercises/init_exercises.py diff --git a/.gitignore b/.gitignore index 8e258b3..3fa30d8 100644 --- a/.gitignore +++ b/.gitignore @@ -59,3 +59,6 @@ target/ .settings/** .project .pydevproject + + +.DS_Store \ No newline at end of file diff --git a/src/skill_tree/exercises/init_exercises.py b/src/skill_tree/exercises/init_exercises.py new file mode 100644 index 0000000..1e98ec1 --- /dev/null +++ b/src/skill_tree/exercises/init_exercises.py @@ -0,0 +1,52 @@ +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('') diff --git a/src/skill_tree/tree.py b/src/skill_tree/tree.py index a9386cc..1bcc423 100644 --- a/src/skill_tree/tree.py +++ b/src/skill_tree/tree.py @@ -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" -- GitLab