Skip to content

Commit

Permalink
Add gallery template and photo processing
Browse files Browse the repository at this point in the history
  • Loading branch information
naspeh committed Feb 9, 2020
1 parent d1528bb commit 2b9ec32
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 10 deletions.
10 changes: 10 additions & 0 deletions data/_theme/photos-index.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends '_theme/photos.tpl' %}

{% block body %}
{{ super() }}
<div id="gallery">
{% for i in p.photos %}
<a data-fancybox="photo" href="{{ i.src }}"><img src="{{ i.thumb }}"/></a>
{% endfor %}
</div>
{% endblock %}
30 changes: 30 additions & 0 deletions data/_theme/photos.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{% extends '_theme/base.tpl' %}

{% block head %}
{{ super() }}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css"/>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/css/justifiedGallery.css"/>
{% endblock %}

{% block js %}
{{ super() }}
<script src="https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>
<script>
$("#gallery").justifiedGallery({
rowHeight : 150,
// margins : 3,
// lastRow : 'nojustify',
});
</script>
{% endblock %}

{% block body %}
<div class="title">
<ul class="meta">
<li><a href="{{ p.parent.parent.url }}">{{ p.parent.parent.title|striptags }}</a></li>
<li><a href="{{ p.parent.url }}">{{ p.parent.title|striptags }}</a></li>
</ul>
</div>
{% endblock %}
22 changes: 20 additions & 2 deletions data/_theme/styles.css
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
body {
html, body {
font: 1em/1.4 "PT Sans", sans-serif;
font-weight: 400;
margin: 0 auto;
padding: 0 10px;
width: 780px;
width: 800px;
height: 100%;
}
a {
color: #008000;
Expand Down Expand Up @@ -277,3 +278,20 @@ img.align-left {
display: table;
clear: both;
}
/* Gallery related */
#gallery {
margin: 0 auto;
width: 100%;
height: 100%;
}
#gallery a:hover, #gallery a:focus, #gallery a:active {
color: inherit;
border-bottom: none;
}
.gallery-cover img {
width: 100%;
}
.gallery-cover:hover, .gallery-cover:focus, .gallery-cover:active {
color: inherit;
border-bottom: none;
}
74 changes: 66 additions & 8 deletions pusto.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import http.server
import json
import os
import pathlib
import pickle
import re
import shutil
Expand All @@ -15,7 +16,7 @@
from urllib.error import HTTPError
from urllib.parse import urljoin

from jinja2 import Environment, FileSystemLoader
from jinja2 import Environment, ChoiceLoader, FileSystemLoader
from lxml import etree
from pytz import timezone, utc

Expand All @@ -33,7 +34,7 @@ class Page:
__slots__ = ('_data', '_xml')
meta_fields = (
'template params aliases published modified sort archive author title '
'terms_file'.split()
'terms_file photos'.split()
)
fields = meta_fields + (
'pages url src_dir index_file meta_file summary body text kind'.split()
Expand Down Expand Up @@ -105,6 +106,17 @@ def path(self):
return path
return path + ('index.html' if self.index_file else '')

@property
def dir(self):
if os.path.isdir(self.path):
return self.path
else:
return os.path.dirname(self.path)

@property
def fname(self):
return os.path.basename(self.index_file)

@property
def ftype(self):
ftype = self.path.rsplit('.', 1)
Expand Down Expand Up @@ -291,7 +303,10 @@ def load(match):

def get_jinja(src_dir):
env = Environment(
loader=FileSystemLoader(src_dir),
loader=ChoiceLoader([
FileSystemLoader('.'),
FileSystemLoader(src_dir),
]),
lstrip_blocks=True, trim_blocks=True
)
env.filters.update({
Expand Down Expand Up @@ -329,11 +344,7 @@ def fill_page(page):
text = f.read().decode()

if page.kind == 'py':
subprocess.call(
'cd {} && python index.py'
.format(page.src_dir + page.url),
shell=True
)
subprocess.call(f'python {page.fname}', cwd=page.dir, shell=True)
with open(page.path, 'br') as f:
text = f.read().decode()
page.update(text=text)
Expand Down Expand Up @@ -515,6 +526,7 @@ def build(src_dir, build_dir, nginx_file=None, use_cache=False):
else:
clean_dir(build_dir)
copy_dir(src_dir, build_dir)
process_photos(build_dir)

with open(cache_file, 'bw') as f:
f.write(pickle.dumps(all_files))
Expand Down Expand Up @@ -570,6 +582,49 @@ def save_urls(pages, filename):
f.write(urlmap.encode())


def process_photos(build_dir):
root = pathlib.Path(build_dir)
page_tpl = (root / '_theme/photos-index.tpl').read_text()

print('Processing photos:')
for path in root.glob('**/photos/'):
if not path.is_dir():
continue

images = sorted(f for f in path.glob('*.jpg'))
if not images:
continue

print(f' - {path}')
files_for_cover = images[:6]
thumbs = ' '.join(f'"thumbs/{f.name}"' for f in files_for_cover)

subprocess.call('''
mkdir -p thumbs
gm mogrify -output-directory . -auto-orient -strip *.jpg
gm mogrify -output-directory thumbs -thumbnail x300 *.jpg
gm convert %s -border 2x2 +append thumbs/cover.jpg
''' % thumbs, cwd=path, shell=True)

items = []
for image_path in images:
url = image_path.as_posix()[len(root.as_posix()):]
url = pathlib.Path(url)
item = {
'thumb': f'{url.parent}/thumbs/{url.name}',
'src': f'{url}',
}
items.append(item)

meta = {
'title': 'Photos',
'photos': items,
}
meta_json = json.dumps(meta, indent=4, sort_keys=True)
(path / 'meta.json').write_text(meta_json)
(path / 'index.tpl').write_text(page_tpl)


def run(src_dir, build_dir, port=5000, no_build=False, no_cache=False):
if not no_build:
build(src_dir, build_dir, use_cache=not no_cache)
Expand Down Expand Up @@ -723,6 +778,9 @@ def cmd(name, **kw):
.arg('-c', '--use-cache', action='store_true')\
.exe(lambda a: build(SRC_DIR, a.bdir, a.nginx_file, a.use_cache))

cmd('photos', help='process photos')\
.exe(lambda a: process_photos(BUILD_DIR))

cmd('test-urls', help='test url responses')\
.arg('-v', '--verbose', action='store_true')\
.arg('-u', '--base-url', help='base url for test')\
Expand Down

0 comments on commit 2b9ec32

Please sign in to comment.