#!/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 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( "
" % ( galleria_id, node['class'] ) ) self.body.append( "" % ( 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']: self.body.append('[%s]' % image['description']) raise nodes.SkipNode def texinfo_visit_galleria(self: TexinfoTranslator, node: galleria) -> None: for image in node['images']: self.body.append('[%s]' % image['description']) raise nodes.SkipNode def text_visit_galleria(self: TextTranslator, node: galleria) -> None: for image in node['images']: self.body.append('[%s]' % image['description']) raise nodes.SkipNode def gemini_visit_galleria(self, node: galleria) -> None: for image in node['images']: self.body.append('=> %s %s' % (image['path'], image['description'])) raise nodes.SkipNode def man_visit_galleria(self: ManualPageTranslator, node: galleria) -> None: if 'description' in node.attributes: self.body.append('[%s]' % node['description']) raise nodes.SkipNode class GalleriaDirective(Directive): has_content = False required_arguments = 1 final_argument_whitespace = True option_spec = { "class": directives.unchanged, "galleria": directives.unchanged, "description": directives.unchanged, "title": directives.unchanged, "thumbsize": directives.unchanged, "transition": directives.unchanged, } def run(self): if not self.state.document.hasattr('galleria_nodes'): self.state.document.galleria_nodes = {} galleria_name = self.options.get('galleria') created = False if galleria_name and \ galleria_name in self.state.document.galleria_nodes: node = self.state.document.galleria_nodes[galleria_name] else: node = galleria() node['class'] = 'galleria' node['options'] = { 'transition': 'fade' } node['images'] = [] created = True if self.options.get('class'): node['class'] = self.options['class'] if self.options.get('transition'): node['options']['transition'] = self.options['transition'] images_path = self.arguments for path in images_path: image = {} image["description"] = self.options.get('description') image["title"] = self.options.get('title') image["thumbsize"] = self.options.get('thumbsize') or '100x100' image["image"] = path node['images'].append(image) if created: return [node] return []