Skip to content

Commit

Permalink
Increase level of headers in regular files
Browse files Browse the repository at this point in the history
  • Loading branch information
bastantoine committed Nov 25, 2023
1 parent 81539ef commit 3558c21
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def process(self):

if self.format:
logger.info("Processing markdown of files")
self.process_markdown_files(self.target_dir, self.target_dir)
self.process_markdown_files(self.target_dir)

def clean_filenames(self, dir: Path):
for child in dir.iterdir():
Expand Down Expand Up @@ -309,15 +309,30 @@ def process_citations(self, content: str, *_: t.Any) -> str:
offset += 1
return "\n".join(lines)

def process_markdown_file(self, file: Path, base_dir: Path):
def process_headers(self, content: str, filepath: Path | str) -> str:
if content.find("#") == -1:
# No headers in the file, no need to go any further
return content

if filepath.name == "index.md":
return content

lines = content.split("\n")
for index, line in enumerate(lines):
vals = line.split()
if vals and (val := vals[0]) and val.startswith("#"):
lines[index] = f"#{line}"
return "\n".join(lines)

def process_markdown_file(self, file: Path):
with open(file) as f:
post = frontmatter.load(f)
content = post.content

# file = str(file.relative_to(base_dir))
for f in [
self.process_links,
self.process_citations,
self.process_headers,
]:
content = f(content, file)

Expand All @@ -331,12 +346,12 @@ def process_markdown_file(self, file: Path, base_dir: Path):
with open(file, "w") as f:
f.write(frontmatter.dumps(post))

def process_markdown_files(self, dir: Path, base_dir: Path):
def process_markdown_files(self, dir: Path):
for child in dir.iterdir():
if child.is_file() and child.suffix == ".md":
self.process_markdown_file(child, base_dir)
self.process_markdown_file(child)
elif child.is_dir():
self.process_markdown_files(child, base_dir)
self.process_markdown_files(child)


def main(
Expand Down

0 comments on commit 3558c21

Please sign in to comment.