Skip to content

Commit

Permalink
processor rewrites image links
Browse files Browse the repository at this point in the history
  • Loading branch information
renerocksai committed Sep 15, 2024
1 parent f0884be commit cc3c729
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,16 @@ class IngoreFileAction:
source_file: str # the file being processed
reason: str

@dataclass
class TranslateImageAction:
source_file: str
original: str
destination: str


Action = Union[CreateDirAction, TranslateLinkAction, MergeIntoSmdAction,
SplitSmdAction, ProcessingFileAction, IngoreFileAction]
SplitSmdAction, ProcessingFileAction, IngoreFileAction,
TranslateImageAction]


# helper functions
Expand Down Expand Up @@ -223,13 +231,22 @@ def rewrite_link(self, relative_path: str, link_text: str, target: str):
destination=new_target))
return link_url

# TODO: this is temp until loris fixes zine
def rewrite_image_link(self, relative_path, img_text, img_target):
new_target = f"[{img_text}]($image.url('{img_target}'))"
self.actions.append(TranslateImageAction(source_file=relative_path,
original=img_target,
destination=new_target))
return new_target

def rewrite_content(self, markdown_content:str, relative_path:str) -> str:
"""
- rewrites markdown links
- but ignores image links ![imgtext](imglink)
- also handles newlines in links
"""
link_pattern = re.compile(r'(?<!\!)\[([^\]]*?)\]\(([^)]+)\)', re.DOTALL)
image_link_pattern = re.compile(r'!\[([^\]]*?)\]\(([^)]+)\)', re.DOTALL)

def handle_link(match: re.Match[str]) -> str:
# Replace newlines in link text with spaces
Expand All @@ -243,9 +260,19 @@ def handle_link(match: re.Match[str]) -> str:
# Rewrite the link if it's not an HTTP URL
rewritten_link = self.rewrite_link(relative_path, link_text, target)
return rewritten_link

def handle_image_link(match: re.Match[str]) -> str:
img_text = match.group(1).replace('\n', ' ') # Replace newlines in image text with spaces
img_target = match.group(2).strip() # Get the image target and strip any extra whitespace
# Rewrite the image link using the rewrite_image_link function
rewritten_image_link = self.rewrite_image_link(relative_path, img_text, img_target)
return rewritten_image_link

# Replace all links in the markdown content using the handle_link function
rewritten_content = link_pattern.sub(handle_link, markdown_content)

# now the images
rewritten_content = image_link_pattern.sub(handle_image_link, rewritten_content)
return rewritten_content


Expand Down

0 comments on commit cc3c729

Please sign in to comment.