Skip to content

Commit

Permalink
fix markdown issue
Browse files Browse the repository at this point in the history
  • Loading branch information
SlapDrone committed Nov 17, 2023
1 parent fbfbcd8 commit 87d869a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
26 changes: 23 additions & 3 deletions slopify/applier.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,46 @@ def apply_markdown(markdown_content: str, base_path: ty.Optional[Path] = None):
tokens = md.parse(markdown_content)

code_blocks = {}
markdown_sections = {}
current_file_path = None
in_markdown_file = False
markdown_accumulator = []

for token in tokens:
if token.type == "heading_open" and token.tag == "h1":
# The next token is the text of the heading, which contains the file path
current_file_path = tokens[tokens.index(token) + 1].content.strip("`")
in_markdown_file = current_file_path.endswith(".md")
if in_markdown_file:
# Start accumulating Markdown content for .md files
markdown_accumulator = [current_file_path]
elif in_markdown_file:
# Accumulate all content for Markdown files
markdown_accumulator.append(token.content)
elif token.type == "fence" and current_file_path:
# This token contains the content of the code block
code_blocks[current_file_path] = token.content
else:
print(f"skipping token {token.content}")

# Apply the code blocks to the file system
for file_path, code in code_blocks.items():
if file_path.endswith(".md"):
# Unescape code blocks within Markdown files
code = unescape_code_blocks(code)
print(f"raw {code=}")
full_path = (base_path or Path.cwd()) / file_path
full_path.parent.mkdir(parents=True, exist_ok=True)
with full_path.open("w", encoding="utf-8") as f:
print(f"processed {code=}")
f.write(code)

# Apply the Markdown sections to the file system
for markdown_accumulator in markdown_sections.values():
file_path = markdown_accumulator[0]
markdown_content = "\n".join(markdown_accumulator[1:])
markdown_content = unescape_code_blocks(markdown_content)
full_path = (base_path or Path.cwd()) / file_path
full_path.parent.mkdir(parents=True, exist_ok=True)
with full_path.open("w", encoding="utf-8") as f:
f.write(markdown_content)

if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Apply Markdown file to directory.")
Expand Down
12 changes: 4 additions & 8 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ def setup_test_files(tmp_path) -> List[Path]:
"""
)
)
print("file 5 content:")
print(file5.read_text())
print(f"write {file5=}")
return [file1, file2, init_file, file3, file4, file5]
return [file5]#[file1, file2, init_file, file3, file4, file5]


def test_slop_apply_idempotent(setup_test_files, tmp_path):
Expand All @@ -57,17 +54,16 @@ def test_slop_apply_idempotent(setup_test_files, tmp_path):

# Capture the original content of the files before applying markdown
original_contents = {file: file.read_text() for file in setup_test_files}

# print(f"{original_contents=}")
# Generate markdown content
markdown_content = dump_files_to_markdown(setup_test_files, None, base_path=base_path)
print(f"{markdown_content=}")
apply_markdown(markdown_content, base_path=base_path)

# Verify that the files' content remains unchanged except for a trailing newline
for file in setup_test_files:
file_content = file.read_text()
print(f"{file}")
print("FILE CONTENTS")
print(file_content)
print(f"{file_content=}")
original_content = original_contents[file]
assert file_content.strip() == original_content.strip(), f"Mismatch in file: {file}"

Expand Down

0 comments on commit 87d869a

Please sign in to comment.