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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129 | #!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.11"
# dependencies = []
# ///
import subprocess
import datetime
import os
import re
def extract_meta(filepath):
title = None
desc = None
body_lines = []
in_frontmatter = False
frontmatter_done = False
try:
with open(filepath, "r") as f:
for line in f:
stripped = line.strip()
if stripped == "---":
if not in_frontmatter:
in_frontmatter = True
continue
elif in_frontmatter and not frontmatter_done:
frontmatter_done = True
continue
if in_frontmatter and not frontmatter_done:
title_match = re.match(
r'^title:\s*["\']?(.*?)["\']?\s*$', stripped, re.IGNORECASE
)
desc_match = re.match(
r'^description:\s*["\']?(.*?)["\']?\s*$',
stripped,
re.IGNORECASE,
)
if title_match:
title = title_match.group(1)
if desc_match:
desc = desc_match.group(1)
elif frontmatter_done:
body_lines.append(stripped)
except Exception:
return None, None, None
# fallback to first sentence from body
if not desc:
for line in body_lines:
if line:
sentence = re.split(r"(?<=[.!?])\s", line)[0].strip()
if sentence:
desc = sentence
break
return title, desc, filepath
def is_draft(filepath):
try:
with open(filepath, "r") as f:
for _ in range(20):
line = f.readline()
if not line:
break
if "draft: true" in line.lower():
return True
except Exception:
return True
return False
def get_recent_md_files(repo_path: str, days: int = 7):
since_date = (datetime.datetime.now() - datetime.timedelta(days=days)).strftime(
"%Y-%m-%d"
)
os.chdir(repo_path)
cmd = [
"git",
"log",
f"--since={since_date}",
"--diff-filter=A",
"--name-only",
"--pretty=format:%ad",
"--date=short",
]
output = subprocess.check_output(cmd, text=True)
lines = [line.strip() for line in output.splitlines()]
entries = []
date = None
for line in lines:
if not line:
continue
if re.match(r"^\d{4}-\d{2}-\d{2}$", line):
date = line
continue
if not (line.endswith(".md") and line.startswith("content/")):
continue
if is_draft(line):
continue
title, desc, full_path = extract_meta(line)
if not title:
continue
relpath = line.removeprefix("content/").removesuffix(".md")
entries.append((date, title, relpath, desc or ""))
entries.sort(reverse=True)
return [f"- {d} [[{p}|{t}]] -- {desc}" for d, t, p, desc in entries]
def main():
repo = "."
items = get_recent_md_files(repo)
if not items:
print("No new non-draft markdown files with titles created in the last week.")
return
print("New pages and entries since last week:\n\n")
print("\n".join(items))
if __name__ == "__main__":
main()
|