From 56d49b874ca5b4da1660a063d4e71ec1028920f5 Mon Sep 17 00:00:00 2001 From: nettee Date: Fri, 17 Apr 2020 23:26:19 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E7=BD=AEMarkdown=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- anima.py | 73 ++------------------------------------------ anima/__init__.py | 10 ++++++ anima/base.py | 12 ++++++++ anima/create.py | 22 +++++++++++++ anima/model.py | 47 ++++++++++++++++++++++++++++ template/template.md | 26 ++++++++++++++++ 6 files changed, 119 insertions(+), 71 deletions(-) create mode 100644 anima/__init__.py create mode 100644 anima/base.py create mode 100644 anima/create.py create mode 100644 anima/model.py create mode 100644 template/template.md diff --git a/anima.py b/anima.py index 9dbdf75..a500e64 100755 --- a/anima.py +++ b/anima.py @@ -1,76 +1,7 @@ #!/usr/bin/env python3 - -import re -from dataclasses import dataclass -from pathlib import Path - import fire -project_dir = Path('.') - - -@dataclass -class ProblemInfo: - id: int - title: str - - def title_slug(self): - title_parts = re.split(r'\s+', self.title) - return f'{self.id:04d}-' + '-'.join(title_parts) - - -@dataclass -class Article: - problem: ProblemInfo - path: Path - - @classmethod - def create(cls, problem: ProblemInfo, path: Path): - article = Article(problem, path) - article._create_dirs() - article._create_doc() - return article - - def _create_dirs(self): - for d in ('Animation', 'Article', 'Code'): - (self.path / d).mkdir() - - def _create_doc(self): - doc_file = self.path / 'Article' / (self.problem.title_slug() + '.md') - with doc_file.open('w') as f: - pass - - -def create_article_r(directory: Path, paths) -> None: - if isinstance(paths, str): - (directory / paths).mkdir() - elif isinstance(paths, list): - for path in paths: - create_article_r(directory, path) - - -def create_article(problem_id: int, problem_title: str) -> None: - problem = ProblemInfo(problem_id, problem_title) - article_dir = project_dir / problem.title_slug() - - if article_dir.exists(): - print(f'创建失败,文件夹 {article_dir} 已存在') - exit(1) - article_dir.mkdir() - - article = Article.create(problem, article_dir) - print(f'题解框架创建完毕,位于文件夹 {article_dir}') - - -class Anima: - """ - LeetCode Animation Manager - """ - - def new(self, id: str, title: str): - create_article(id, title) - +import anima if __name__ == '__main__': - anima = Anima() - fire.Fire(anima) + fire.Fire(anima.Anima()) diff --git a/anima/__init__.py b/anima/__init__.py new file mode 100644 index 0000000..108d14e --- /dev/null +++ b/anima/__init__.py @@ -0,0 +1,10 @@ +from anima.create import create_solution + + +class Anima: + """ + LeetCode Animation Manager + """ + + def new(self, id: str, title: str): + create_solution(id, title) diff --git a/anima/base.py b/anima/base.py new file mode 100644 index 0000000..7f5ad75 --- /dev/null +++ b/anima/base.py @@ -0,0 +1,12 @@ +import os +from pathlib import Path + + +def get_project_path() -> Path: + script_path = os.path.realpath(__file__) + project_path = Path(script_path).parent.parent + return project_path + + +def get_md_template_path() -> Path: + return get_project_path() / 'template' / 'template.md' diff --git a/anima/create.py b/anima/create.py new file mode 100644 index 0000000..e82ab9b --- /dev/null +++ b/anima/create.py @@ -0,0 +1,22 @@ +import shutil + +from anima.base import get_project_path, get_md_template_path +from anima.model import ProblemInfo, Solution + + +def create_solution(problem_id: int, problem_title: str) -> None: + problem = ProblemInfo(problem_id, problem_title) + solution_dir = get_project_path() / problem.title_slug() + + if solution_dir.exists(): + print(f'创建失败,文件夹 {solution_dir} 已存在') + exit(1) + solution_dir.mkdir() + + solution = Solution.create(problem, solution_dir) + + template = get_md_template_path() + shutil.copy(template, solution.doc_path()) + + print(f'题解框架创建完毕,位于文件夹 {solution.path}') + diff --git a/anima/model.py b/anima/model.py new file mode 100644 index 0000000..9f386b6 --- /dev/null +++ b/anima/model.py @@ -0,0 +1,47 @@ +import re +from dataclasses import dataclass +from pathlib import Path + + +@dataclass +class ProblemInfo: + id: int + title: str + + def title_slug(self): + title_parts = re.split(r'\s+', self.title) + return f'{self.id:04d}-' + '-'.join(title_parts) + + +@dataclass +class Solution: + problem: ProblemInfo + path: Path + + @classmethod + def create(cls, problem: ProblemInfo, path: Path): + solution = Solution(problem, path) + solution._create_dirs() + return solution + + def _create_dirs(self): + self.animation_path().mkdir() + self.article_path().mkdir() + self.code_path().mkdir() + (self.animation_path() / 'Animation.m4v').touch() + (self.animation_path() / 'Animation.gif').touch() + + def _path_to(self, s: str) -> Path: + return self.path / s + + def animation_path(self) -> Path: + return self.path / 'Animation' + + def article_path(self) -> Path: + return self.path / 'Article' + + def doc_path(self) -> Path: + return self.article_path() / (self.problem.title_slug() + '.md') + + def code_path(self) -> Path: + return self.path / 'Code' diff --git a/template/template.md b/template/template.md new file mode 100644 index 0000000..63c5f99 --- /dev/null +++ b/template/template.md @@ -0,0 +1,26 @@ +# LeetCode 图解 | + +> 本文首发于公众号「图解面试算法」,是 [图解 LeetCode]() 系列文章之一。 +> +> 同步博客:https://www.algomooc.com + +本题解作者: + +## 题目描述 + + + +## 题目解析 + + + +## 动画理解 + +![](../Animation/Animation.gif) + +## 参考代码 + + + +## 复杂度分析 +