Add site generation script.

This commit is contained in:
Niels G. W. Serup 2016-08-25 01:26:29 +02:00
parent 207b0e5a18
commit cba5b8d2ae
3 changed files with 165 additions and 0 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
/extern
/thumbnails-small
/site

158
scripts/generate-pages.py Executable file
View File

@ -0,0 +1,158 @@
#!/usr/bin/env python3
import sys
import os
import shutil
import html
import yaml
def read(path):
with open(path) as f:
return f.read()
def write(path, text):
with open(path, 'w') as f:
f.write(text)
def load_settings(settings_file):
with open(settings_file) as f:
settings = {name: value
for name, value
in (line.split('=', 1) for line in
f.read().strip().split('\n'))}
return settings
def load_media(media_file):
with open(media_file) as f:
c = yaml.load(f)
c['year'] = int(c['year'])
c['ident'] = ident(media_file)
return c
def ident(media_file):
return os.path.splitext(os.path.split(media_file)[1])[0]
def thumbnail_path(name):
return os.path.join(name, 'thumbnail.jpg')
def thumbnail_url(link):
return link + 'thumbnail.jpg'
def setup_directory_structure(settings, base_dir, template_dir):
site_new_dir = os.path.join(base_dir, 'site-new')
os.mkdir(site_new_dir)
extern_dir = os.path.join(base_dir, 'extern')
extern_dir_site = os.path.join(site_new_dir, 'extern')
os.symlink(extern_dir, extern_dir_site)
style = read(os.path.join(template_dir, 'style.css'))
style_out = style.replace('{', '{{').replace('}', '}}') \
.replace('[', '{').replace(']', '}').format(**settings)
style_new_file = os.path.join(site_new_dir, 'style.css')
write(style_new_file, style_out)
return site_new_dir
def publish_new_site(site_dir, site_new_dir):
shutil.rmtree(site_dir, ignore_errors=True)
shutil.move(site_new_dir, site_dir)
def generate_index_page(out_dir, base_html, section_html, medias):
sections = [generate_video_section_html(section_html, media)
for media in medias]
all_sections = '\n'.join(sections)
html_out = base_html.format(content=all_sections,
style_url='style.css')
out_file = os.path.join(out_dir, 'index.html')
write(out_file, html_out)
def generate_video_section_html(section_html, media):
title = media['title']
link = media['ident'] + '/'
thumbnail = thumbnail_url(link)
html_out = section_html.format(
title=html.escape(title),
thumbnail=thumbnail, link=link)
return html_out
def extern_video(file_src):
url = '../extern/' + file_src
return '''\
<video src="{file_src}" controls height="427">
Your browser cannot play this video. You can download it here instead:
<a href="{file_src}">{file_src}</a>
</video>
'''.format(file_src=file_src)
def youtube_video(file_src):
return '''\
<iframe type="text/html" width="760" height="427"
src="https://www.youtube.com/embed/{file_src}?controls=2"
allowfullscreen frameborder="0">
</iframe>
'''.format(file_src=file_src)
def generate_video_page(base_dir, out_dir, base_html, showing_html, media):
title = media['title']
year = media['year']
description = media['description']
name = media['ident']
video_dir = os.path.join(out_dir, name)
os.mkdir(video_dir)
thumbnail_file = thumbnail_path(video_dir)
thumbnail_file_local = os.path.join(
base_dir, 'thumbnails-small', name + '.jpg')
shutil.copyfile(thumbnail_file_local, thumbnail_file)
file_type, file_src = media['file'].split(':', 1)
actions = {
'extern': extern_video,
'youtube': youtube_video
}
video_html = actions[file_type](file_src)
html_inner = showing_html.format(
title=html.escape(title),
year=str(year),
description=html.escape(description),
video=video_html)
html_out = base_html.format(content=html_inner,
style_url='../style.css')
out_file = os.path.join(video_dir, 'index.html')
write(out_file, html_out)
def main():
script_dir = os.path.dirname(__file__)
settings_file = os.path.join(script_dir, 'settings')
settings = load_settings(settings_file)
base_dir = os.path.split(script_dir)[0]
template_dir = os.path.join(base_dir, 'template')
base_html = read(os.path.join(template_dir, 'base.html'))
section_html = read(os.path.join(template_dir, 'section.html'))
showing_html = read(os.path.join(template_dir, 'showing.html'))
media_dir = os.path.join(base_dir, 'media')
media_files = [os.path.join(media_dir, media_file)
for media_file in os.listdir(media_dir)]
medias = [load_media(media_file) for media_file in media_files]
medias.sort(key=lambda m: (m['year'], m['ident']), reverse=True)
site_new_dir = setup_directory_structure(settings, base_dir, template_dir)
generate_index_page(site_new_dir, base_html, section_html, medias)
for media in medias:
generate_video_page(base_dir, site_new_dir, base_html, showing_html,
media)
site_dir = os.path.join(base_dir, 'site')
publish_new_site(site_dir, site_new_dir)
return 0
if __name__ == '__main__':
sys.exit(main())

6
scripts/generate-site.sh Executable file
View File

@ -0,0 +1,6 @@
#!/bin/sh
base_dir="$(dirname "$0")"
"$base_dir"/resize-thumbnails.sh
"$base_dir"/generate-pages.py