Skip to content

Commit 56d49b8

Browse files
committed
设置Markdown模板
1 parent e27e191 commit 56d49b8

File tree

6 files changed

+119
-71
lines changed

6 files changed

+119
-71
lines changed

Diff for: anima.py

+2-71
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,7 @@
11
#!/usr/bin/env python3
2-
3-
import re
4-
from dataclasses import dataclass
5-
from pathlib import Path
6-
72
import fire
83

9-
project_dir = Path('.')
10-
11-
12-
@dataclass
13-
class ProblemInfo:
14-
id: int
15-
title: str
16-
17-
def title_slug(self):
18-
title_parts = re.split(r'\s+', self.title)
19-
return f'{self.id:04d}-' + '-'.join(title_parts)
20-
21-
22-
@dataclass
23-
class Article:
24-
problem: ProblemInfo
25-
path: Path
26-
27-
@classmethod
28-
def create(cls, problem: ProblemInfo, path: Path):
29-
article = Article(problem, path)
30-
article._create_dirs()
31-
article._create_doc()
32-
return article
33-
34-
def _create_dirs(self):
35-
for d in ('Animation', 'Article', 'Code'):
36-
(self.path / d).mkdir()
37-
38-
def _create_doc(self):
39-
doc_file = self.path / 'Article' / (self.problem.title_slug() + '.md')
40-
with doc_file.open('w') as f:
41-
pass
42-
43-
44-
def create_article_r(directory: Path, paths) -> None:
45-
if isinstance(paths, str):
46-
(directory / paths).mkdir()
47-
elif isinstance(paths, list):
48-
for path in paths:
49-
create_article_r(directory, path)
50-
51-
52-
def create_article(problem_id: int, problem_title: str) -> None:
53-
problem = ProblemInfo(problem_id, problem_title)
54-
article_dir = project_dir / problem.title_slug()
55-
56-
if article_dir.exists():
57-
print(f'创建失败,文件夹 {article_dir} 已存在')
58-
exit(1)
59-
article_dir.mkdir()
60-
61-
article = Article.create(problem, article_dir)
62-
print(f'题解框架创建完毕,位于文件夹 {article_dir}')
63-
64-
65-
class Anima:
66-
"""
67-
LeetCode Animation Manager
68-
"""
69-
70-
def new(self, id: str, title: str):
71-
create_article(id, title)
72-
4+
import anima
735

746
if __name__ == '__main__':
75-
anima = Anima()
76-
fire.Fire(anima)
7+
fire.Fire(anima.Anima())

Diff for: anima/__init__.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from anima.create import create_solution
2+
3+
4+
class Anima:
5+
"""
6+
LeetCode Animation Manager
7+
"""
8+
9+
def new(self, id: str, title: str):
10+
create_solution(id, title)

Diff for: anima/base.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import os
2+
from pathlib import Path
3+
4+
5+
def get_project_path() -> Path:
6+
script_path = os.path.realpath(__file__)
7+
project_path = Path(script_path).parent.parent
8+
return project_path
9+
10+
11+
def get_md_template_path() -> Path:
12+
return get_project_path() / 'template' / 'template.md'

Diff for: anima/create.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import shutil
2+
3+
from anima.base import get_project_path, get_md_template_path
4+
from anima.model import ProblemInfo, Solution
5+
6+
7+
def create_solution(problem_id: int, problem_title: str) -> None:
8+
problem = ProblemInfo(problem_id, problem_title)
9+
solution_dir = get_project_path() / problem.title_slug()
10+
11+
if solution_dir.exists():
12+
print(f'创建失败,文件夹 {solution_dir} 已存在')
13+
exit(1)
14+
solution_dir.mkdir()
15+
16+
solution = Solution.create(problem, solution_dir)
17+
18+
template = get_md_template_path()
19+
shutil.copy(template, solution.doc_path())
20+
21+
print(f'题解框架创建完毕,位于文件夹 {solution.path}')
22+

Diff for: anima/model.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import re
2+
from dataclasses import dataclass
3+
from pathlib import Path
4+
5+
6+
@dataclass
7+
class ProblemInfo:
8+
id: int
9+
title: str
10+
11+
def title_slug(self):
12+
title_parts = re.split(r'\s+', self.title)
13+
return f'{self.id:04d}-' + '-'.join(title_parts)
14+
15+
16+
@dataclass
17+
class Solution:
18+
problem: ProblemInfo
19+
path: Path
20+
21+
@classmethod
22+
def create(cls, problem: ProblemInfo, path: Path):
23+
solution = Solution(problem, path)
24+
solution._create_dirs()
25+
return solution
26+
27+
def _create_dirs(self):
28+
self.animation_path().mkdir()
29+
self.article_path().mkdir()
30+
self.code_path().mkdir()
31+
(self.animation_path() / 'Animation.m4v').touch()
32+
(self.animation_path() / 'Animation.gif').touch()
33+
34+
def _path_to(self, s: str) -> Path:
35+
return self.path / s
36+
37+
def animation_path(self) -> Path:
38+
return self.path / 'Animation'
39+
40+
def article_path(self) -> Path:
41+
return self.path / 'Article'
42+
43+
def doc_path(self) -> Path:
44+
return self.article_path() / (self.problem.title_slug() + '.md')
45+
46+
def code_path(self) -> Path:
47+
return self.path / 'Code'

Diff for: template/template.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# LeetCode 图解 |
2+
3+
> 本文首发于公众号「图解面试算法」,是 [图解 LeetCode](<https://github.com/MisterBooo/LeetCodeAnimation>) 系列文章之一。
4+
>
5+
> 同步博客:https://www.algomooc.com
6+
7+
本题解作者:
8+
9+
## 题目描述
10+
11+
12+
13+
## 题目解析
14+
15+
16+
17+
## 动画理解
18+
19+
![](../Animation/Animation.gif)
20+
21+
## 参考代码
22+
23+
24+
25+
## 复杂度分析
26+

0 commit comments

Comments
 (0)