-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
36 lines (29 loc) · 917 Bytes
/
util.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
import os
import re
base_path = os.path.join(os.path.dirname(__file__), "entries")
def save_article(filename: str, content):
"""
Save content to a markdown file.
"""
if not filename.endswith(".md"):
filename += ".md"
file_path = os.path.join(base_path, filename)
with open(file_path, "w", encoding="utf-8") as file:
file.write(content)
def get_article(filename: str):
"""
Read content from a markdown file.
"""
try:
if not filename.endswith(".md"):
filename += ".md"
file_path = os.path.join(base_path, filename)
with open(file_path, "r", encoding="utf-8") as file:
return file.read()
except FileNotFoundError:
return None
def list_entries():
"""
List all markdown files in the directory.
"""
return [re.sub(r"\.md$", "", f) for f in os.listdir(base_path) if f.endswith(".md")]