Skip to content

Instantly share code, notes, and snippets.

@entwanne
Last active August 2, 2022 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save entwanne/655200d86052bc18b85ee79935b4ca7e to your computer and use it in GitHub Desktop.
Save entwanne/655200d86052bc18b85ee79935b4ca7e to your computer and use it in GitHub Desktop.
ZdS clean gallery
#!/usr/bin/env python
import json
import re
import sys
from zipfile import ZipFile
img_regex = re.compile(rb'!\[[^\]]*\]\(([^)]+)\)')
def find_images_file(file):
for line in file:
if m := img_regex.search(line):
yield m.group(1).decode()
def find_images_archive(archive):
for path in archive.namelist():
if not path.endswith('.md'):
continue
with archive.open(path) as file:
yield from find_images_file(file)
def find_images(*archive_names):
for archive_name in archive_names:
with ZipFile(archive_name, 'r') as archive:
yield from find_images_archive(archive)
if __name__ == '__main__':
if len(sys.argv) < 2:
print(f'Usage: {sys.argv[0]} archive [archives]', file=sys.stderr)
sys.exit(1)
_, *archive_names = sys.argv
for img in find_images(*archive_names):
print(img)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment