expiry.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. import re
  2. from datetime import datetime
  3. def on_page_context(context, page, config, nav):
  4. expiry_days = config.get("extra", {}).get("expiry_days", 365)
  5. def compute_expiry(meta):
  6. revision = (
  7. meta.get("git_revision_date_localized")
  8. or meta.get("git_creation_date_localized")
  9. or meta.get("revision_date")
  10. )
  11. is_expired = False
  12. last_update = None
  13. if revision:
  14. m = re.search(r"(\d{4}-\d{2}-\d{2})", str(revision))
  15. if m:
  16. last_update = m.group(1)
  17. try:
  18. dt = datetime.strptime(last_update, "%Y-%m-%d")
  19. if (datetime.now() - dt).days > expiry_days:
  20. is_expired = True
  21. except Exception:
  22. # 无法解析日期时,保持不显示过期提示
  23. pass
  24. return is_expired, last_update
  25. page.is_expired, page.last_update = compute_expiry(page.meta)
  26. context["is_expired"] = page.is_expired
  27. context["last_update"] = page.last_update
  28. context["expiry_days"] = expiry_days
  29. return context