Skip to content

Commit

Permalink
Merge pull request #13 from obeone:fix-unecessary-blank-lines
Browse files Browse the repository at this point in the history
fix(export): fix excessive newline characters in Markdown content
  • Loading branch information
obeone authored Jun 14, 2024
2 parents 49ef4a7 + 2a42293 commit feb4a58
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions export_manager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
from database_manager import DatabaseManager
import logging # Add log messages
import re


class ExportManager:
Expand Down Expand Up @@ -32,10 +33,29 @@ def _adjust_headers(self, content, level_increment=1):
if line.startswith("#"):
hashes = len(line.split(" ")[0])
new_hashes = min(hashes + level_increment, 6) # Limit to ######
line = "#" * new_hashes + line[hashes:]
line = "\n" + "#" * new_hashes + line[hashes:] + "\n"
new_content += line + "\n"
return new_content

def _cleanup_markdown(self, content):
"""
Remove excessive newline characters from Markdown content.
This method replaces sequences of three or more consecutive newline characters
with exactly two newline characters, ensuring that there are no unnecessary
blank lines in the output.
Args:
content (str): The Markdown content to be cleaned up.
Returns:
str: The cleaned-up Markdown content with reduced newline characters.
"""
while "\n\n\n" in content:
content = content.replace("\n\n\n", "\n\n")
return content


def _concatenate_markdown(self, pages):
"""
Concatenate a list of Markdown files into one, with header adjustments.
Expand All @@ -46,7 +66,7 @@ def _concatenate_markdown(self, pages):
Returns:
str: The concatenated Markdown content.
"""
final_content = f"# {self.title}\n\n"
final_content = f"# {self.title}\n"
for url, content, metadata in pages:
if content is None:
continue # Skip empty pages
Expand All @@ -56,7 +76,7 @@ def _concatenate_markdown(self, pages):
}

# Prepare metadata as an HTML comment
metadata_content = f"<!--\n"
metadata_content = "<!--\n"
metadata_content += f"URL: {url}\n"
for key, value in filtered_metadata.items():
metadata_content += f"{key}: {value}\n"
Expand All @@ -66,8 +86,10 @@ def _concatenate_markdown(self, pages):
adjusted_content = self._adjust_headers(content)

final_content += (
"\n\n" + metadata_content + "\n\n" + adjusted_content + "\n\n---"
"\n" + metadata_content + "\n\n" + adjusted_content + "\n---"
) # Add a separator and metadata

final_content = self._cleanup_markdown(final_content)

return final_content

Expand Down Expand Up @@ -100,6 +122,8 @@ def export_to_json(self, output_path):
if content is None:
continue # Skip empty pages

content = self._cleanup_markdown(content)

filtered_metadata = {
k: v for k, v in json.loads(metadata).items() if v is not None
}
Expand Down

0 comments on commit feb4a58

Please sign in to comment.