You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

145 lines
3.9 KiB

import os
from glob import glob
from pprint import pprint
from random import choice
import pypandoc
import base64
# --------------------------------------------------
# Parameters
# number of items on one page
nr_of_items = 10
# number of different pages it will generate for each partyline
nr_of_versions = 10
# you can download the materials with download-materials.sh
materials_folder = "materials"
# this is the folder where the bricolages will be saved to
output_folder = "generated-bricolages"
# for uploading the pages with images somehwere
encode_imgs_as_base64 = True
# --------------------------------------------------
# First make an index of all the files
index = {}
dir = f"./{ materials_folder }/*"
folders = glob(dir, recursive=True)
for folder in folders:
name = folder.replace("./materials/", "")
dir = folder + "/**"
materials = glob(dir, recursive=True)
index[name] = materials
# pprint(index)
# ---------------------------------------------------
# Functions
def parsePad(pad):
"""Parses pad into a list of <p>'s"""
html = open(pad, "r").read()
paragraphs = html.split("<br>")
paragraphs = [p for p in paragraphs if p]
paragraphs = [p for p in paragraphs if not "body>" in p]
paragraphs = [p for p in paragraphs if not "html>" in p]
return paragraphs
def parseAnnouncement(page):
"""Parses an announcement page from the website into a list of <p>'s"""
html = open(page, "r").read()
paragraphs = html.split("\n")
paragraphs = [p for p in paragraphs if p]
paragraphs = [p for p in paragraphs if "<p>" in p]
paragraphs = [p for p in paragraphs if not "body>" in p]
paragraphs = [p for p in paragraphs if not "html>" in p]
paragraphs = [p.replace("\t", "") for p in paragraphs]
return paragraphs
def parseAllItems(partyline):
"""Makes a list of all items of one partyline"""
all_items = []
for item in index[partyline]:
el = ""
for ext in ["jpg","png","jpeg"]:
if item.endswith(ext):
if encode_imgs_as_base64 == True:
img = open(item, "rb").read()
img_in_base64 = base64.b64encode(img)
el = f"<img src='data:image/jpeg;base64,{ img_in_base64.decode('utf-8') }'>"
else:
el = f"<img src='{ item }'>"
all_items.append(el)
if "pad.html" in item:
paragraphs = parsePad(item)
for paragraph in paragraphs:
# do not wrap <ul>'s in <p>'s'
if "<ul" in paragraph:
el = f"<div class='item pad'>{ paragraph }</div>"
else:
el = f"<p class='pad'>{ paragraph }</p>"
all_items.append(el)
if "announcement" in item:
paragraphs = parseAnnouncement(item)
for paragraph in paragraphs:
el = f"<div class='item announcement'>{ paragraph }</div>"
all_items.append(el)
return all_items
def generateHTML(partyline, all_items, x):
"""Generates an HTML page with $nr of items."""
html = ""
for i in range(nr_of_items):
random_choice = choice(all_items) + "\n\n"
# print(random_choice)
# print("------")
html += random_choice
template = f"""<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body>{ html }</body>
</html>
"""
# check if output folder exists
if not os.path.exists(output_folder):
os.system(f"mkdir { output_folder }")
out = open(f"{ output_folder }/archive-bricolage-{ partyline }-{ str(x) }.html", "w")
out.write(template)
out.close()
return html
def generateMD(partyline, html, x):
md = pypandoc.convert_text(html, 'md', format='html')
out = open(f"{ output_folder }/archive-bricolage-{ partyline }-{ str(x) }.md", "w")
out.write(md)
out.close()
# ---------------------------------------------------
# Make a list of all items for each partyline that includes:
# - paragraphs from pads
# - images
# - paragraphs from announcement text
#
# Generate a HTML page with $nr of items
# Also save the HTML page as MD file
for x in range(nr_of_versions):
for partyline in index.keys():
all_items = parseAllItems(partyline)
html = generateHTML(partyline, all_items, x)
generateMD(partyline, html, x)