设置Markdown模板

This commit is contained in:
nettee 2020-04-17 23:26:19 +08:00
parent e27e1914f1
commit 56d49b874c
6 changed files with 119 additions and 71 deletions

View File

@ -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())

10
anima/__init__.py Normal file
View File

@ -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)

12
anima/base.py Normal file
View File

@ -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'

22
anima/create.py Normal file
View File

@ -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}')

47
anima/model.py Normal file
View File

@ -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'

26
template/template.md Normal file
View File

@ -0,0 +1,26 @@
# LeetCode 图解 |
> 本文首发于公众号图解面试算法 [图解 LeetCode](<https://github.com/MisterBooo/LeetCodeAnimation>) 系列文章之一
>
> 同步博客https://www.algomooc.com
本题解作者
## 题目描述
## 题目解析
## 动画理解
![](../Animation/Animation.gif)
## 参考代码
## 复杂度分析