Attachment 'Columns-1.8.4.py'

Download

   1 # -*- coding: utf-8 -*-
   2 """
   3     MoinMoin - macro to specify columns
   4 
   5     As far as the author is concerned, this code is released to the public
   6     domain, but some restrictions in the MoinMoin COPYING file may apply. Ask a
   7     lawyer.
   8     ***********
   9     This code has not been tested exhaustively. Use at your own risk.
  10     ***********
  11 
  12     This macro generates containers, that can be used with CSS to create
  13     columns.
  14 
  15     Usage:
  16         <<Columns(columnCount, [start|end])>>
  17 
  18         columnCount    2-10 total number of columns (used primarily to calculate column width)
  19 
  20         start          pass "start" as the second argument to define the first column
  21         
  22         end            pass "end" as the second argument to define the last column
  23 
  24     Examples:
  25 
  26         <<Columns(3, start)>>
  27                 This text is in the left column.
  28         <<Columns(3)>>
  29                 This text is in the middle column.
  30         <<Columns(3)>>
  31                 This text is in the right column.
  32         <<Columns(3, end)>>
  33 
  34 
  35     Demo. Try pasting the above examples into a MoinMoin sandbox page.
  36 
  37     @copyright: Antti Kuntsi <antti@kuntsi.com>
  38     @license: GNU GPL, see COPYING for details.
  39 
  40     changes:
  41         12.2007 - conversion to new syntax by Bolesław Kulbabiński
  42         03.2010 - Moved CSS to inline for easier installation. Autocompute styles and widths to handle arbirtary numbers of columns without manual CSS editing. Updates by Peter Lyons
  43 """
  44 import string
  45 import sys
  46 import types
  47 
  48 from MoinMoin.wikiutil import required_arg
  49 
  50 #Feel free to increase this if you really can use more than 10 columns.
  51 #It's just an arbitrary reasonable limit
  52 MAX_COLUMNS = 10
  53 rangeStrings = [str(x) for x in range(1, MAX_COLUMNS + 1)]
  54 
  55 #Feel free to adjust this if needed. Could be added as a wiki markup parameter
  56 MARGIN_WIDTH = 1
  57 COLUMN_TEMPLATE = string.Template("""
  58 <!-- output generated by the Columns macro Version 1.8.4 -->
  59 <style>
  60 div.column_${columnCount} {
  61     float: left;
  62     width: ${columnWidth}%;
  63     margin-left: ${marginWidth}%;
  64 }
  65 </style>
  66 <div class="column_${columnCount}">
  67 """)
  68         
  69 def macro_Columns(macro, columnCount=required_arg(rangeStrings),
  70         startOrEnd=("", "start", "end")):
  71     tags = []
  72     if startOrEnd != "start":
  73         tags.append("\n</div><!--end column -->\n")
  74     if startOrEnd != "end":
  75         columnWidth = int(100 / int(columnCount)) - MARGIN_WIDTH
  76         tags.append(COLUMN_TEMPLATE.safe_substitute({
  77             "columnWidth": columnWidth,
  78             "columnCount": columnCount,
  79             "marginWidth": MARGIN_WIDTH
  80             }))
  81     if startOrEnd == "end":
  82         tags.append('\n<br clear="left"/>')
  83     if macro:
  84         result = [macro.formatter.rawHTML("".join(tags))]
  85     else:
  86         result = tags
  87     return "".join(result)
  88 
  89 #Facilititates quick testing on the command line        
  90 if __name__ == "__main__":
  91     print macro_Columns(None, *sys.argv[1:])

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] (2007-12-22 18:49:23, 3.0 KB) [[attachment:Columns-1.6.py]]
  • [get | view] (2010-03-07 17:08:44, 2.9 KB) [[attachment:Columns-1.8.4.py]]
  • [get | view] (2011-03-07 09:02:25, 2.9 KB) [[attachment:Columns-1.9.x.py]]
  • [get | view] (2004-06-14 13:14:13, 4.8 KB) [[attachment:Columns.py]]
  • [get | view] (2006-03-22 21:53:27, 0.4 KB) [[attachment:columns-macro.diff]]
  • [get | view] (2011-09-30 16:51:02, 2.3 KB) [[attachment:variable-columns.patch]]
 All files | Selected Files: delete move to page copy to page

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