diff options
Diffstat (limited to 'thumbnail/macro')
-rw-r--r-- | thumbnail/macro/Thumbnail.py | 40 | ||||
-rw-r--r-- | thumbnail/macro/ThumbnailGallery.py | 47 | ||||
-rw-r--r-- | thumbnail/macro/__init__.py | 5 |
3 files changed, 92 insertions, 0 deletions
diff --git a/thumbnail/macro/Thumbnail.py b/thumbnail/macro/Thumbnail.py new file mode 100644 index 0000000..0603435 --- /dev/null +++ b/thumbnail/macro/Thumbnail.py | |||
@@ -0,0 +1,40 @@ | |||
1 | from MoinMoin import wikiutil | ||
2 | from MoinMoin.action import AttachFile | ||
3 | |||
4 | |||
5 | def macro_Thumbnail(macro, attachment, height=200, width=None, link=True): | ||
6 | _ = macro.request.getText | ||
7 | formatter = macro.formatter | ||
8 | |||
9 | pagename, filename = AttachFile.absoluteName(attachment, formatter.page.page_name) | ||
10 | fname = wikiutil.taintfilename(filename) | ||
11 | |||
12 | if not macro.request.user.may.read(pagename): | ||
13 | return _('You are not allowed to view attachments of this page.') | ||
14 | |||
15 | if width: | ||
16 | size, dimension = width, "w" | ||
17 | else: | ||
18 | size, dimension = height, "h" | ||
19 | |||
20 | if AttachFile.exists(macro.request, pagename, fname): | ||
21 | url = AttachFile.getAttachUrl(pagename, fname, macro.request, do='get') | ||
22 | |||
23 | output = [ | ||
24 | formatter.url(True, url) if link else "", | ||
25 | formatter.image( | ||
26 | macro.request.href(pagename, { | ||
27 | "target": fname, | ||
28 | "action": "Thumbnail", | ||
29 | "do": "tc:{},{}".format(size, dimension) | ||
30 | })), | ||
31 | formatter.url(False) if link else "", | ||
32 | ] | ||
33 | else: | ||
34 | output = [ | ||
35 | formatter.span(True, style="color: red"), | ||
36 | "No Image: {}".format(attachment), | ||
37 | formatter.span(False), | ||
38 | ] | ||
39 | |||
40 | return "".join(output) | ||
diff --git a/thumbnail/macro/ThumbnailGallery.py b/thumbnail/macro/ThumbnailGallery.py new file mode 100644 index 0000000..038c3bc --- /dev/null +++ b/thumbnail/macro/ThumbnailGallery.py | |||
@@ -0,0 +1,47 @@ | |||
1 | import re | ||
2 | from MoinMoin import wikiutil | ||
3 | from MoinMoin.action import AttachFile | ||
4 | |||
5 | |||
6 | def macro_ThumbnailGallery(macro, page=None, regex=None, height=200, width=None, link=True): | ||
7 | _ = macro.request.getText | ||
8 | formatter = macro.formatter | ||
9 | pagename = page or macro.formatter.page.page_name | ||
10 | output = [] | ||
11 | |||
12 | if not regex: | ||
13 | regex = re.compile(".*\.(jpg|jpeg|png|gif)$") | ||
14 | else: | ||
15 | regex = re.compile(regex) | ||
16 | |||
17 | if not macro.request.user.may.read(pagename): | ||
18 | return _('You are not allowed to view attachments of this page.') | ||
19 | |||
20 | for attachment in AttachFile._get_files(macro.request, pagename): | ||
21 | if regex.match(attachment) is None: | ||
22 | continue | ||
23 | |||
24 | pagename, filename = AttachFile.absoluteName(attachment, pagename) | ||
25 | fname = wikiutil.taintfilename(filename) | ||
26 | |||
27 | if width: | ||
28 | size, dimension = width, "w" | ||
29 | else: | ||
30 | size, dimension = height, "h" | ||
31 | |||
32 | if AttachFile.exists(macro.request, pagename, fname): | ||
33 | url = AttachFile.getAttachUrl(pagename, fname, macro.request, do='get') | ||
34 | |||
35 | output.extend([ | ||
36 | macro.formatter.url(True, url) if link else "", | ||
37 | macro.formatter.image( | ||
38 | macro.request.href(pagename, { | ||
39 | "target": fname, | ||
40 | "action": "Thumbnail", | ||
41 | "do": "tc:{},{}".format(size, dimension) | ||
42 | })), | ||
43 | macro.formatter.url(False) if link else "", | ||
44 | " ", | ||
45 | ]) | ||
46 | |||
47 | return "".join(output) | ||
diff --git a/thumbnail/macro/__init__.py b/thumbnail/macro/__init__.py new file mode 100644 index 0000000..e4ed3b6 --- /dev/null +++ b/thumbnail/macro/__init__.py | |||
@@ -0,0 +1,5 @@ | |||
1 | # -*- coding: iso-8859-1 -*- | ||
2 | |||
3 | from MoinMoin.util import pysupport | ||
4 | |||
5 | modules = pysupport.getPackageModules(__file__) | ||