-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdown.py
39 lines (30 loc) · 1.2 KB
/
markdown.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import os
from rich.console import Console
from rich.markdown import Markdown
from atcdr.util.execute import execute_files
from atcdr.util.filetype import FILE_EXTENSIONS, Lang
from atcdr.util.parse import ProblemHTML
def save_markdown(html_path: str, lang: str) -> None:
console = Console()
with open(html_path, 'r', encoding='utf-8') as f:
html = ProblemHTML(f.read())
md = html.make_problem_markdown(lang)
file_without_ext = os.path.splitext(html_path)[0]
md_path = file_without_ext + FILE_EXTENSIONS[Lang.MARKDOWN]
with open(md_path, 'w', encoding='utf-8') as f:
f.write(md)
console.print('[green][+][/green] Markdownファイルを作成しました.')
def print_markdown(md_path: str) -> None:
console = Console()
with open(md_path, 'r', encoding='utf-8') as f:
md = f.read()
console.print(Markdown(md))
def markdown(*args: str, lang: str = 'ja', save: bool = False) -> None:
if save:
execute_files(
*args,
func=lambda html_path: save_markdown(html_path, lang),
target_filetypes=[Lang.HTML],
)
else:
execute_files(*args, func=print_markdown, target_filetypes=[Lang.MARKDOWN])