-
Notifications
You must be signed in to change notification settings - Fork 3
/
__init__.py
106 lines (85 loc) · 3.55 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# -*- coding: utf-8 -*-
# Copyright (c) 2016 Kura
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from __future__ import unicode_literals
from uuid import uuid4
from docutils import nodes
from docutils.parsers.rst import directives, Directive
def align(argument):
"""Conversion function for the "align" option."""
return directives.choice(argument, ("left", "center", "right"))
class Lightbox(Directive):
"""
Create a pure CSS lightbox for images.
Usage:
.. lightbox::
:thumb: /images/test-thumb.png
:large: /images/test.png
:alt: This is a test image
:caption: A test caption
:align: center
"""
required_arguments = 0
optional_arguments = 3
option_spec = {
"thumb": str,
"large": str,
"alt": str,
"caption": str,
"align": align,
}
final_argument_whitespace = False
has_content = False
def run(self):
"""Run the directive."""
if "thumb" not in self.options:
raise self.error("Thumb argument is required.")
thumb = self.options["thumb"]
if "large" not in self.options:
raise self.error("Large argument is required.")
large = self.options["large"]
uuid = str(uuid4())
alt = self.options.get("alt", None)
align = self.options.get("align", "left")
if alt is not None:
alt_text = "{} (click to view large image)".format(alt)
else:
alt_text = "Click to view large image"
caption = self.options.get("caption", None)
if caption is not None:
caption_block = (
"""<p class="align-{}">{} (click to view large """
"""image)</p>"""
).format(align, caption)
else:
caption_block = (
"""<p class="align-{}">Click to view large image</p>"""
).format(align)
block = (
"""<div class="lightbox-block align-{4}">"""
"""<a href="#{0}" title="{3}">"""
"""<img src="{1}" alt="{3}" class="align-{4}" /></a>"""
"""<a href="#_" class="lightbox" id="{0}" title="Click to """
"""close">"""
"""<img alt="Click to close" src="{2}" /></a>{5}</div>"""
"""<div class="lightbox-divider">"""
"""</div>"""
).format(uuid, thumb, large, alt_text, align, caption_block)
return [nodes.raw("", block, format="html")]
def register():
"""Register the directive."""
directives.register_directive("lightbox", Lightbox)