Attachment 'ThumbGallery-0.1.py'

Download

   1 # -*- coding: utf-8 -*-
   2 """
   3     MoinMoin - ThumbGallery Macro
   4     Version 0.1
   5 
   6     Show all the attached images in a table of thumbnails with links to the full-size images
   7 
   8     Usage:
   9     <<ThumbGallery()>>
  10     <<ThumbGallery(columns, imgwidth)>>
  11         columns: number of columns in a table (default 3)
  12         imgwidth: width of every thumbnail in pixels (default 300)
  13 
  14     Requires the Thumbnail macro/action by Kenneth Bull
  15 
  16     @copyright: 2010 by Alexander 'Loki' Agibalov
  17     Reused some pieces of code from the ImageBrowser Macro by Kenneth Bull
  18 
  19     @license: GNU GPL, see COPYING for details.
  20 
  21 """
  22 
  23 
  24 def getImageList(request, pagename):
  25     from MoinMoin.action.AttachFile import _get_files, getFilename, getAttachUrl
  26     from MoinMoin import wikiutil
  27     import os, mimetypes
  28     files = _get_files(request, pagename)
  29     fmt = request.formatter
  30     imgs = []
  31     for f in files:
  32         if os.path.isfile(getFilename(request, pagename, f)) and (str(mimetypes.guess_type(f)[0])[:5] == "image"):
  33             imgs.append(wikiutil.escape(f))
  34     return imgs
  35 
  36 
  37 def buildCell(request, page, cell, imgwidth):
  38     from MoinMoin.action.AttachFile import getAttachUrl
  39     from MoinMoin import config
  40     fmt = request.formatter
  41     ret = fmt.url(1, getAttachUrl(page.page_name, cell, request)) + \
  42           u'<img src="%s?action=Thumbnail&amp;target=%s&amp;w=%d">' % (page.url(request), cell, imgwidth) + \
  43           fmt.url(0)
  44     return ret
  45 
  46 
  47 def macro_ThumbGallery(macro, columns=3, imgwidth=300):
  48     from MoinMoin.action import AttachFile
  49     import math
  50     page = macro.formatter.page
  51     imglist = getImageList(macro.request, page.page_name)
  52     ret = u'<table>'
  53     # number of rows in html table for the given number of columns
  54     rowcnt = int(math.ceil(len(imglist) / (columns + 0.0)))
  55     for r in range(rowcnt):
  56         ret = ret + u'<tr>'
  57         for c in range(columns):
  58             pos = c + r * columns
  59             if pos > (len(imglist) - 1):
  60                 cell = u'&nbsp;'
  61             else:
  62                 cell = imglist[pos]
  63             ret = ret + u'<td>' + buildCell(macro.request, page, cell, imgwidth) + u'</td>'
  64         ret = ret + u'</tr>'
  65     ret = ret + u'</table>'
  66     return ret

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2010-06-24 06:24:03, 2.2 KB) [[attachment:ThumbGallery-0.1.py]]
  • [get | view] (2010-06-24 06:24:37, 214.7 KB) [[attachment:thumbgallery.png]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.