File: gallery.py

package info (click to toggle)
morse-simulator 1.4-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 187,116 kB
  • sloc: ansic: 108,311; python: 25,694; cpp: 786; makefile: 126; xml: 34; sh: 7
file content (76 lines) | stat: -rw-r--r-- 2,397 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import glob
from docutils import nodes
from docutils.parsers.rst import directives
from sphinx.util.compat import Directive

IMAGE_PATH="_images/"
HTML_PATH="user/"
RST_PATH="morse/" + HTML_PATH

def css(d):
    return "; ".join(sorted("%s: %s" % kv for kv in d.items()))

class gallery(nodes.General, nodes.Element): pass
class widegallery(nodes.General, nodes.Element): pass

def visit_widegallery_node(self, node):
    visit_gallery_node(self, node, wide = True)

def visit_gallery_node(self, node, wide = False):

    MEDIA_PATH=os.path.join(node.document.settings.env.config.html_theme_path[0], "media")

    if wide:
        images_per_row = 2
        width = "400px"
    else:
        images_per_row = 4
        width = "200px"

    style = {
        "position": "relative",
    }
    self.body.append(self.starttag(node, "table", style=css(style)))
    img_path = os.path.join(MEDIA_PATH, node["directory"])
    images =  os.listdir(img_path)
    images = sorted([img for img in images if os.path.isfile(os.path.join(img_path, img))])
    for i in range(len(images))[::images_per_row]:
        self.body.append("<tr>\n")
        for j in range(images_per_row):
            if i + j < len(images):
                obj = images[i + j][:-4] #remove extension
                self.body.append("\t<td style='text-align:center'><a href='" + HTML_PATH + node["directory"] + "/" + obj + ".html'><img style='width: " + width + ";' src='" + IMAGE_PATH + images[i+j] + "' /><br/><pre>" + obj + "</pre></a></td>\n")
        self.body.append("</tr>\n")
    self.body.append("</table>\n")

def depart_gallery_node(self, node):
    pass

class Gallery(Directive):
    has_content = True
    required_arguments = 1
    optional_arguments = 0
    final_argument_whitespace = False

    def run(self):
        return [gallery(directory=self.arguments[0])]

class WideGallery(Directive):
    has_content = True
    required_arguments = 1
    optional_arguments = 0
    final_argument_whitespace = False

    def run(self):
        return [widegallery(directory=self.arguments[0])]

def setup(app):
    app.add_node(widegallery, html=(visit_widegallery_node, depart_gallery_node))
    app.add_directive("widegallery", WideGallery)
    
    app.add_node(gallery, html=(visit_gallery_node, depart_gallery_node))
    app.add_directive("gallery", Gallery)