2021-05-28 23:05:03 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
Galleria directive
|
|
|
|
"""
|
|
|
|
|
|
|
|
import uuid
|
|
|
|
import json
|
|
|
|
|
|
|
|
from docutils import nodes
|
|
|
|
from docutils.parsers.rst import Directive
|
|
|
|
import docutils.parsers.rst.directives as directives
|
|
|
|
|
|
|
|
from sphinx.writers.html import HTMLTranslator
|
|
|
|
from sphinx.writers.latex import LaTeXTranslator
|
|
|
|
from sphinx.writers.texinfo import TexinfoTranslator
|
|
|
|
from sphinx.writers.text import TextTranslator
|
|
|
|
from sphinx.writers.manpage import ManualPageTranslator
|
2021-06-02 03:23:50 +02:00
|
|
|
from sphinx.util.osutil import relative_uri
|
|
|
|
from sphinx.locale import __
|
2021-05-28 23:05:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
class galleria(nodes.General, nodes.Element):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def html_visit_galleria(self: HTMLTranslator, node: galleria) -> None:
|
|
|
|
galleria_id = 'galleria-%s' % uuid.uuid4()
|
|
|
|
|
|
|
|
self.body.append(
|
2021-06-02 03:23:50 +02:00
|
|
|
"<div id='%s' class='%s' width='%s' height='%s'>" % (
|
2021-05-28 23:05:03 +02:00
|
|
|
galleria_id,
|
2021-06-02 03:23:50 +02:00
|
|
|
node['class'] + ' align-%s' % node['options']['align'],
|
|
|
|
node['options']['width'],
|
|
|
|
node['options']['height'],
|
2021-05-28 23:05:03 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2021-06-02 03:23:50 +02:00
|
|
|
if len(node['images']) > 0:
|
|
|
|
self.body.append("<figure><div class='row'>")
|
|
|
|
self.body.append("<img src='%s' title='%s' alt='%s'>" % (
|
|
|
|
node['images'][0]['image'],
|
|
|
|
node['images'][0]['title'],
|
|
|
|
node['images'][0]['alt']
|
|
|
|
))
|
|
|
|
self.body.append('</div></figure>')
|
|
|
|
|
2021-05-28 23:05:03 +02:00
|
|
|
self.body.append(
|
2021-06-02 03:23:50 +02:00
|
|
|
"</div><script type='module'>" +
|
|
|
|
"import {SphinxGalleria} from './%s';\n" %
|
|
|
|
relative_uri(
|
|
|
|
self.builder.get_target_uri(self.builder.current_docname),
|
|
|
|
'_static/sphinxgalleria/sphinxgalleria.mjs') +
|
|
|
|
"new SphinxGalleria('%s', %s, %s).init();</script>" % (
|
2021-05-28 23:05:03 +02:00
|
|
|
galleria_id,
|
|
|
|
json.dumps(node['options']),
|
|
|
|
json.dumps(node['images'])
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
raise nodes.SkipNode
|
|
|
|
|
|
|
|
|
|
|
|
def latex_visit_galleria(self: LaTeXTranslator, node: galleria) -> None:
|
|
|
|
for image in node['images']:
|
2021-06-02 03:23:50 +02:00
|
|
|
self.body.append('[%s]' % image['alt'])
|
2021-05-28 23:05:03 +02:00
|
|
|
raise nodes.SkipNode
|
|
|
|
|
|
|
|
|
|
|
|
def texinfo_visit_galleria(self: TexinfoTranslator, node: galleria) -> None:
|
|
|
|
for image in node['images']:
|
2021-06-02 03:23:50 +02:00
|
|
|
self.body.append('[%s]' % image['alt'])
|
2021-05-28 23:05:03 +02:00
|
|
|
raise nodes.SkipNode
|
|
|
|
|
|
|
|
|
|
|
|
def text_visit_galleria(self: TextTranslator, node: galleria) -> None:
|
|
|
|
for image in node['images']:
|
2021-06-02 03:23:50 +02:00
|
|
|
self.body.append('[%s]' % image['alt'])
|
2021-05-28 23:05:03 +02:00
|
|
|
raise nodes.SkipNode
|
|
|
|
|
|
|
|
|
|
|
|
def gemini_visit_galleria(self, node: galleria) -> None:
|
|
|
|
for image in node['images']:
|
2021-06-02 03:23:50 +02:00
|
|
|
self.body.append('=> %s %s' % (image['path'], image['alt']))
|
2021-05-28 23:05:03 +02:00
|
|
|
raise nodes.SkipNode
|
|
|
|
|
|
|
|
|
|
|
|
def man_visit_galleria(self: ManualPageTranslator, node: galleria) -> None:
|
2021-06-02 03:23:50 +02:00
|
|
|
if 'alt' in node.attributes:
|
|
|
|
self.body.append('[%s]' % node['alt'])
|
2021-05-28 23:05:03 +02:00
|
|
|
raise nodes.SkipNode
|
|
|
|
|
|
|
|
|
2021-06-02 03:23:50 +02:00
|
|
|
def align_choices(argument):
|
|
|
|
return directives.choice(argument, ('left', 'right', 'center'))
|
|
|
|
|
|
|
|
|
2021-05-28 23:05:03 +02:00
|
|
|
class GalleriaDirective(Directive):
|
|
|
|
|
|
|
|
has_content = False
|
|
|
|
required_arguments = 1
|
|
|
|
final_argument_whitespace = True
|
2021-06-02 03:23:50 +02:00
|
|
|
|
2021-05-28 23:05:03 +02:00
|
|
|
option_spec = {
|
2021-06-02 03:23:50 +02:00
|
|
|
"class": directives.class_option,
|
2021-05-28 23:05:03 +02:00
|
|
|
"galleria": directives.unchanged,
|
2021-06-02 03:23:50 +02:00
|
|
|
"alt": directives.unchanged,
|
2021-05-28 23:05:03 +02:00
|
|
|
"title": directives.unchanged,
|
|
|
|
"thumbsize": directives.unchanged,
|
|
|
|
"transition": directives.unchanged,
|
2021-06-02 03:23:50 +02:00
|
|
|
'width': directives.length_or_percentage_or_unitless,
|
|
|
|
'height': directives.length_or_unitless,
|
|
|
|
'align': align_choices,
|
|
|
|
'hide_title': directives.flag,
|
|
|
|
'hide_alt': directives.flag,
|
2021-05-28 23:05:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
def run(self):
|
2021-06-02 03:23:50 +02:00
|
|
|
source = self.state.document.settings._source
|
|
|
|
document = self.state.document
|
|
|
|
if not document.hasattr('galleria_nodes'):
|
|
|
|
document.galleria_nodes = {}
|
|
|
|
if source not in document.galleria_nodes:
|
|
|
|
document.galleria_nodes[source] = {}
|
|
|
|
|
|
|
|
galleria_name = self.options.get('galleria') or uuid.uuid4()
|
2021-05-28 23:05:03 +02:00
|
|
|
created = False
|
|
|
|
|
2021-06-02 03:23:50 +02:00
|
|
|
if galleria_name in document.galleria_nodes[source]:
|
|
|
|
node = document.galleria_nodes[source][galleria_name]
|
2021-05-28 23:05:03 +02:00
|
|
|
|
|
|
|
else:
|
|
|
|
node = galleria()
|
|
|
|
node['class'] = 'galleria'
|
|
|
|
node['options'] = {
|
2021-06-02 03:23:50 +02:00
|
|
|
'transition': 'fade',
|
|
|
|
'label_prev': __('Previous'),
|
|
|
|
'label_next': __('Next'),
|
|
|
|
'label_close': __('Close'),
|
|
|
|
'label_thumbnail': __('Thumbnail, click to enlarge'),
|
2021-05-28 23:05:03 +02:00
|
|
|
}
|
|
|
|
node['images'] = []
|
2021-06-02 03:23:50 +02:00
|
|
|
document.galleria_nodes[source][galleria_name] = node
|
2021-05-28 23:05:03 +02:00
|
|
|
|
|
|
|
created = True
|
|
|
|
|
|
|
|
if self.options.get('class'):
|
|
|
|
node['class'] = self.options['class']
|
|
|
|
if self.options.get('transition'):
|
|
|
|
node['options']['transition'] = self.options['transition']
|
|
|
|
|
2021-06-02 03:23:50 +02:00
|
|
|
node['options']["width"] = self.options.get('width') or 'auto'
|
|
|
|
node['options']["height"] = self.options.get('height') or 'auto'
|
|
|
|
node['options']["align"] = self.options.get('align') or 'center'
|
|
|
|
|
2021-05-28 23:05:03 +02:00
|
|
|
images_path = self.arguments
|
|
|
|
for path in images_path:
|
|
|
|
image = {}
|
2021-06-02 03:23:50 +02:00
|
|
|
image["alt"] = self.options.get('alt')
|
2021-05-28 23:05:03 +02:00
|
|
|
image["title"] = self.options.get('title')
|
|
|
|
image["thumbsize"] = self.options.get('thumbsize') or '100x100'
|
2021-06-02 03:23:50 +02:00
|
|
|
image["hide_alt"] = bool(self.options.get('hide_alt'))
|
|
|
|
image["hide_title"] = bool(self.options.get('hide_title'))
|
2021-05-28 23:05:03 +02:00
|
|
|
image["image"] = path
|
|
|
|
node['images'].append(image)
|
|
|
|
|
|
|
|
if created:
|
|
|
|
return [node]
|
|
|
|
return []
|