Skip to content

Commit

Permalink
Add documentation
Browse files Browse the repository at this point in the history
Change some names, add some coments.
  • Loading branch information
PSJ-Sokolov committed Mar 18, 2024
1 parent 58b0cf5 commit 87fc9fb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
14 changes: 13 additions & 1 deletion extract-thumbnails.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
#!/usr/bin/env python3
"""
"""
import os
from pdf2image import convert_from_path

import highest
# Generate the highest numebred pdf and take its number.
MAX : int = highest.find_highest_numbered_pdf()

# Generate and save the images.
imgs = []
for i in range(MAX + 1):
# Build the paths
pdf_path = f'./pdf/{i}.pdf'
img_path = f'./img/{i}.png'
# Remove an image file if it has no corresponding pdf,
# Assume it is a leftover artefact.
# The images should strictly supervene on the pdf's.
# This uses shortcircuiting.
# This uses a technique know as "shortcircuiting".
os.path.isfile(img_path) and not os.path.isfile(pdf_path) and os.remove(img_path)
# Generate the images, but look if they already exist, because this proces is costly.
if not os.path.isfile(img_path):
# We look if the pdf actually exiss and don't assume this.
# So we can skip editions.
if os.path.isfile(pdf_path):
print("extracting from pdf: " + str(i))
# Actually extract the image, well get an array back with one image in it.
images = convert_from_path(pdf_path, first_page=1, last_page=1)
# Loop through the array.
for image in images:
# Save the image.
image.save(img_path, "PNG")
else:
# Report that we skipped the image.
print(f"Thumbnail voor {img_path} bestaat al, skipping...")
16 changes: 12 additions & 4 deletions generate-index.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
#!/usr/bin/env python3
"""
"""
import os
import re
import highest
# Calculate the highest numbered pdf and take its humber.
MAX :int = highest.find_highest_numbered_pdf()



# The string for a gallery item.
gallery_item = '<a href="{pdf}" target="_blank"><img src="./img/{i}.png" alt="Title page of edition {i}"></a>'

def generate_gallery():
"""
"""
editions_html = []
for i in range(MAX + 1,0,-1):
pdf_file = f'./pdf/{i}.pdf'
if os.path.isfile(pdf_file):
editions_html.append(gallery_item.format(pdf=pdf_file, i=i))
return '\n'.join(editions_html)

with open("template.html") as template, open("index.html", "w") as index:
index.write(re.sub(r"<!--GALLERY-->",generate_gallery(),template.read()))
with open("index.template.html") as template, open("index.html", "w") as index:
replacement_string = r"""
<p>YOU HAVE LANDED ON THE TEMPLATE PAGE OF <a href="./index.html">INDEX.HTML</a> THIS PARAGRAPH WILL BE REPLACED WITH THE GENERATED GALLERY!</p>
""".strip()
index.write(re.sub(replacement_string, generate_gallery(), template.read()))
2 changes: 1 addition & 1 deletion template.html → index.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ <h1>De Filosoof</h1>
</nav>
<div class="container">
<!-- 88 images wrapped in links -->
<!--GALLERY-->
<p>YOU HAVE LANDED ON THE TEMPLATE PAGE OF <a href="./index.html">INDEX.HTML</a> THIS PARAGRAPH WILL BE REPLACED WITH THE GENERATED GALLERY!</p>
</div>
<footer id="footer">
<p><a href="mailto:redactie.filosoof@gmail.com">redactie.filosoof@gmail.com</a></p>
Expand Down

0 comments on commit 87fc9fb

Please sign in to comment.