in scripts/add_outdated_banner.py [0:0]
def main(warning_text: str, cutoff: timedelta):
now = datetime.utcnow().astimezone()
for md in Path("content/").rglob("*.md"):
last_changed = check_output(
["git", "log", "-1", "--pretty=format:%ci", md]
).decode("utf-8")
last_changed = datetime.strptime(last_changed, "%Y-%m-%d %H:%M:%S %z")
# If the docs are recent, don't add the header
if now - last_changed < cutoff:
continue
contents = md.read_text()
# If the doc already has an out of date warning, replace it with the new one
if re.search(WARNING_REGEX, contents, flags=re.MULTILINE | re.DOTALL):
md.write_text(
re.sub(
WARNING_REGEX,
warning_text,
contents,
flags=re.MULTILINE | re.DOTALL,
)
)
# Otherwise, check to see if there's a front matter section delimited by `+++`. If so,
# add the out of date warning directly afterwards.
else:
pluses = list(re.finditer(r"\+\+\+", contents))
if len(pluses) == 2:
p = pluses[1]
# If there's only a front matter section and no actual content, don't bother
# adding the warning.
if contents[p.end():].strip():
md.write_text(contents[:p.end()] + warning_text + contents[p.end():])