mirror of
https://github.com/anotherhadi/sec-notes.git
synced 2026-10-05 15:48:25 +02:00
init
This commit is contained in:
Executable
+39
@@ -0,0 +1,39 @@
|
||||
#!/usr/bin/env python3
|
||||
import re
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
REPO_ROOT = Path(__file__).resolve().parents[2]
|
||||
SKIP_DIRS = {".git", ".direnv", ".github"}
|
||||
|
||||
TOC_PLACEHOLDER_RE = re.compile(r"\[toc\]", re.IGNORECASE)
|
||||
TOC_START = "<!-- START doctoc generated TOC please keep comment here to allow auto update -->"
|
||||
TOC_END = "<!-- END doctoc generated TOC please keep comment here to allow auto update -->"
|
||||
|
||||
|
||||
def find_markdown_files():
|
||||
for path in sorted(REPO_ROOT.rglob("*.md")):
|
||||
if not SKIP_DIRS.isdisjoint(path.relative_to(REPO_ROOT).parts):
|
||||
continue
|
||||
yield path
|
||||
|
||||
|
||||
def has_toc_markers(path):
|
||||
text = path.read_text()
|
||||
if TOC_START in text:
|
||||
return True
|
||||
if TOC_PLACEHOLDER_RE.search(text):
|
||||
text = TOC_PLACEHOLDER_RE.sub(f"{TOC_START}\n{TOC_END}", text, count=1)
|
||||
path.write_text(text)
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
for md_file in find_markdown_files():
|
||||
if has_toc_markers(md_file):
|
||||
subprocess.run(["doctoc", "--notitle", str(md_file)], check=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Executable
+75
@@ -0,0 +1,75 @@
|
||||
#!/usr/bin/env python3
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
REPO_ROOT = Path(__file__).resolve().parents[2]
|
||||
README = REPO_ROOT / "README.md"
|
||||
SKIP_DIRS = {".git", ".github", ".direnv"}
|
||||
SECTION_TITLE_OVERRIDES = {"osint": "OSINT"}
|
||||
|
||||
FRONTMATTER_RE = re.compile(r"^---\n(.*?\n)---\n", re.DOTALL)
|
||||
FIELD_RE = re.compile(r'^(\w+):\s*"?(.*?)"?\s*$')
|
||||
TOC_START = "<!-- START doctoc generated TOC please keep comment here to allow auto update -->"
|
||||
TOC_END = "<!-- END doctoc generated TOC please keep comment here to allow auto update -->"
|
||||
|
||||
|
||||
def existing_toc_block():
|
||||
if README.exists():
|
||||
text = README.read_text()
|
||||
if TOC_START in text and TOC_END in text:
|
||||
start = text.index(TOC_START)
|
||||
end = text.index(TOC_END) + len(TOC_END)
|
||||
return text[start:end]
|
||||
return "[toc]"
|
||||
|
||||
|
||||
def parse_frontmatter(path):
|
||||
text = path.read_text()
|
||||
match = FRONTMATTER_RE.match(text)
|
||||
if not match:
|
||||
return {}
|
||||
fields = {}
|
||||
for line in match.group(1).splitlines():
|
||||
field_match = FIELD_RE.match(line)
|
||||
if field_match:
|
||||
fields[field_match.group(1)] = field_match.group(2)
|
||||
return fields
|
||||
|
||||
|
||||
def section_title(dirname):
|
||||
return SECTION_TITLE_OVERRIDES.get(dirname, dirname.capitalize())
|
||||
|
||||
|
||||
def build_section(directory):
|
||||
lines = [f"## {section_title(directory.name)}", ""]
|
||||
for md_file in sorted(directory.glob("*.md")):
|
||||
fields = parse_frontmatter(md_file)
|
||||
title = fields.get("title", md_file.stem)
|
||||
description = fields.get("description", "")
|
||||
rel_path = f"./{directory.name}/{md_file.name}"
|
||||
lines.append(f"- [{title}]({rel_path}): {description}")
|
||||
lines.append("")
|
||||
return lines
|
||||
|
||||
|
||||
def main():
|
||||
directories = sorted(
|
||||
d
|
||||
for d in REPO_ROOT.iterdir()
|
||||
if d.is_dir() and d.name not in SKIP_DIRS and any(d.glob("*.md"))
|
||||
)
|
||||
|
||||
lines = [
|
||||
"# Sec Notes",
|
||||
"",
|
||||
existing_toc_block(),
|
||||
"",
|
||||
]
|
||||
for directory in directories:
|
||||
lines.extend(build_section(directory))
|
||||
|
||||
README.write_text("\n".join(lines).rstrip("\n") + "\n")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user