2021-05-28 23:05:03 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
2021-06-02 03:23:50 +02:00
|
|
|
Create image galleries
|
2021-05-28 23:05:03 +02:00
|
|
|
"""
|
|
|
|
import os
|
|
|
|
from typing import Dict, Any
|
|
|
|
from sphinx.application import Sphinx
|
|
|
|
from sphinx.environment import BuildEnvironment
|
|
|
|
from sphinx.util.osutil import ensuredir, copyfile
|
|
|
|
from PIL import Image
|
|
|
|
|
2021-07-25 15:16:10 +02:00
|
|
|
from sphinx_galleria import directive
|
|
|
|
from sphinx_galleria import collector
|
2021-05-28 23:05:03 +02:00
|
|
|
|
2021-07-25 01:54:37 +02:00
|
|
|
|
|
|
|
with open(os.path.join(
|
|
|
|
os.path.dirname(__file__),
|
|
|
|
'VERSION')) as version_file:
|
|
|
|
__version__ = version_file.read().strip()
|
|
|
|
__version_info__ = tuple(int(v) for v in __version__.split('.'))
|
2021-05-28 23:05:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
def copy_images_files(app: Sphinx, env: BuildEnvironment) -> None:
|
|
|
|
|
|
|
|
if hasattr(env, 'galleria') and env.galleria:
|
|
|
|
for source in env.galleria:
|
|
|
|
relpath = os.path.relpath(source, env.srcdir)
|
|
|
|
dest = os.path.join(app.outdir, relpath)
|
|
|
|
ensuredir(os.path.dirname(dest))
|
|
|
|
copyfile(source, dest)
|
|
|
|
|
|
|
|
if hasattr(env, 'galleriathumbs') and env.galleriathumbs:
|
|
|
|
for thumb in env.galleriathumbs:
|
|
|
|
relpath = os.path.relpath(thumb, env.srcdir)
|
|
|
|
dest = os.path.join(app.outdir, relpath)
|
|
|
|
basename, ext = os.path.splitext(dest)
|
2021-06-02 23:58:10 +02:00
|
|
|
|
|
|
|
if ext.lower() in ('.svg', '.svgz'):
|
|
|
|
continue
|
|
|
|
|
2021-05-28 23:05:03 +02:00
|
|
|
thumbsize = basename.split('-')[-1].split('x')
|
|
|
|
thumbsize = [int(size) for size in thumbsize]
|
|
|
|
original = '.'.join(basename.split('.')[:-1]) + ext
|
2021-07-25 16:29:09 +02:00
|
|
|
dest = basename + ext
|
2021-05-28 23:05:03 +02:00
|
|
|
ensuredir(os.path.dirname(dest))
|
|
|
|
|
|
|
|
with Image.open(original) as im:
|
2021-06-02 03:23:50 +02:00
|
|
|
if im.size[0]/im.size[1] > thumbsize[0]/thumbsize[1]:
|
|
|
|
out = im.resize((
|
|
|
|
thumbsize[0],
|
|
|
|
thumbsize[1]
|
|
|
|
), box=(
|
|
|
|
(im.size[0]-thumbsize[1]*im.size[0]/thumbsize[0])//2,
|
|
|
|
0,
|
|
|
|
(im.size[0]+thumbsize[1]*im.size[0]/thumbsize[0])//2,
|
|
|
|
im.size[1]
|
|
|
|
))
|
|
|
|
else:
|
|
|
|
out = im.resize((
|
|
|
|
thumbsize[0],
|
|
|
|
thumbsize[1]
|
|
|
|
), box=(
|
|
|
|
0,
|
|
|
|
(im.size[1]-thumbsize[0]*im.size[1]/thumbsize[0])//2,
|
|
|
|
im.size[0],
|
|
|
|
(im.size[1]+thumbsize[0]*im.size[1]/thumbsize[0])//2,
|
|
|
|
))
|
|
|
|
|
2021-07-25 16:29:09 +02:00
|
|
|
if ext.lower() in ("jpg", "jpeg"):
|
|
|
|
out.save(dest, "JPEG", quality=90)
|
|
|
|
else:
|
|
|
|
out.save(dest)
|
2021-05-28 23:05:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
def install_static_files(app: Sphinx, env: BuildEnvironment) -> None:
|
|
|
|
path = os.path.join(
|
|
|
|
app.builder.outdir,
|
|
|
|
'_static',
|
|
|
|
'sphinxgalleria'
|
|
|
|
)
|
|
|
|
source = os.path.join(
|
|
|
|
os.path.dirname(__file__),
|
|
|
|
'static',
|
|
|
|
'sphinxgalleria',
|
|
|
|
)
|
|
|
|
|
|
|
|
statics = []
|
|
|
|
for dirpath, dirs, files in os.walk(source):
|
|
|
|
source_path = os.path.relpath(dirpath, source)
|
|
|
|
for static in files:
|
|
|
|
statics.append(os.path.join(source_path, static))
|
|
|
|
|
|
|
|
for static in statics:
|
|
|
|
static_path = os.path.join(path, static)
|
|
|
|
source_path = os.path.join(source, static)
|
|
|
|
if not os.path.exists(os.path.dirname(static_path)):
|
|
|
|
ensuredir(os.path.dirname(static_path))
|
|
|
|
|
|
|
|
copyfile(source_path, static_path)
|
|
|
|
|
2021-06-02 03:23:50 +02:00
|
|
|
if static.endswith('.css'):
|
2021-05-28 23:05:03 +02:00
|
|
|
app.add_css_file(os.path.join('sphinxgalleria', static))
|
|
|
|
|
|
|
|
|
|
|
|
def setup(app: Sphinx) -> Dict[str, Any]:
|
|
|
|
app.add_node(
|
|
|
|
directive.galleria,
|
|
|
|
html=(directive.html_visit_galleria, None),
|
|
|
|
epub=(directive.html_visit_galleria, None),
|
|
|
|
latex=(directive.latex_visit_galleria, None),
|
|
|
|
texinfo=(directive.texinfo_visit_galleria, None),
|
|
|
|
text=(directive.text_visit_galleria, None),
|
|
|
|
man=(directive.man_visit_galleria, None),
|
|
|
|
gemini=(directive.gemini_visit_galleria, None),
|
|
|
|
)
|
|
|
|
app.add_directive('galleria', directive.GalleriaDirective)
|
|
|
|
app.add_env_collector(collector.GalleriaCollector)
|
|
|
|
app.connect('env-updated', install_static_files)
|
|
|
|
app.connect('env-updated', copy_images_files)
|
2021-06-03 19:17:21 +02:00
|
|
|
|
|
|
|
locale_path = os.path.join(
|
|
|
|
os.path.abspath(os.path.dirname(__file__)),
|
|
|
|
'locale'
|
|
|
|
)
|
|
|
|
app.add_message_catalog('sphinx', locale_path)
|
2021-05-28 23:05:03 +02:00
|
|
|
return {'version': __version__}
|