Attachment 'SeeSaw-0.5.py'

Download

   1 # -*- coding: utf-8 -*-
   2 '''
   3 MoinMoin 1.6-1.8 - SeeSaw Macro
   4     @copyright: 2008,2009 Jim Wight
   5     @licence: GNU GPL, see COPYING for details
   6 
   7 PURPOSE
   8     SeeSaw enables sections of a page to be individually see-sawed
   9     (toggled) between hidden and shown, typically from near their
  10     locations.
  11 
  12 DESCRIPTION
  13     SeeSaw adds a link for see-sawing a named section of a page. It is
  14     created by a call of the following form:
  15 
  16         <<SeeSaw(section="section", toshow="toshow", tohide="tohide",
  17                  show=True|False, bg="background", inline="inline",
  18                  image="arrow|plumin", speed="slow|normal|fast"|integer,
  19                  seesaw=True|False)>>
  20     where
  21         section  gives the section a name;
  22                  it defaults to 'section'
  23          toshow  specifies the text to display for showing the section
  24                  it defaults to 'Show' (for block) and '»»' (for inline)
  25          tohide  specifies the text to display for hiding the section;
  26                  it defaults to 'Hide' (block) and '««' (inline)
  27                  if toshow is given, but not tohide, tohide is set to the
  28                  value of toshow; toshow is undefined if only tohide is
  29                  provided
  30            show  specifies whether the section is to be shown initially;
  31                  it defaults to False
  32              bg  specifies the background colour to use for the section;
  33                  it defaults to None
  34          inline  specifies the text of an inline section;
  35                  it defaults to None, and non-presence implies the call
  36                  relates to a block section
  37            image selects an image set to use for the links instead of text,
  38                  with toshow and tohide being ignored, but see just below;
  39                  it defaults to None;
  40                  the location of the images must be configured in the
  41                  Configuration section below, where additional image sets
  42                  can easily be added;
  43                  the images provided are embedded in the IMAGES section
  44                  of this file as a uuencoded .tgz file
  45            speed specifies the rate at which the section (not inline)
  46                  should appear or disappear, or provides a number of
  47                  milliseconds for the animation to last;
  48                  it defaults to 0
  49           seesaw SeeSaw will, by default, only toggle sections (i.e. divs)
  50                  created for it; this option, when set to False, enables
  51                  other divs to be toggled from a SeeSaw link
  52 
  53     (NB The string arguments don't need to be quoted unless they contain
  54         special characters)
  55                  
  56     'toshow' and 'tohide' can accommodate the text surrounding the link,
  57     to allow it to be different in the two cases. The three parts are
  58     distinguished by enclosing the text of the link between '<<' and '>>',
  59     e.g. toshow="<<Open>> me up", tohide="Now <<close>> me down". The 
  60     middle part is ignored if image is used, so can be empty.
  61 
  62     The (leading) arguments can also be given positionally, in the
  63     order of the keyword arguments above, e.g.
  64 
  65         <<SeeSaw(section1, "See-Saw")>>
  66         <<SeeSaw(section1, show=True)>>
  67 
  68     Block sections require to be set up as follows:
  69 
  70       {{{#!wiki seesaw/section   or   {{{#!wiki seesaw section
  71       }}}                             }}}
  72 
  73     where the word 'section' matches the value "section" in the
  74     corresponding SeeSaw call. This creates a div with the classes
  75     'seesaw' and 'section' applied.
  76 
  77     By default, block sections are hidden initially, but can be shown
  78     by adding 'show' to the '{{{#!wiki' line. 'show' should be set to
  79     True in the matching SeeSaw call if its 'tohide' and 'toshow'
  80     arguments are different (so that the correct one can be shown
  81     initially)
  82 
  83     If a background colour is specified for a block section,
  84     '"section"-bg' needs to be added to the corresponding '{{{#!wiki'
  85     line to have the colour applied to the section. If there are
  86     multiple sections with the same name, it is sufficient to use 'bg'
  87     in just one of the SeeSaw calls, with the first taking precedence
  88     if multiple, but different, values are given.
  89 
  90     The text of inline sections is embedded in SeeSaw calls. By
  91     default, inline sections are hidden initially.
  92 
  93     SeeSaw sections behave similarly to MoinMoin comments in that all
  94     sections with the same name are toggled together. In fact, if the
  95     section name contains 'comment', it can be toggled on and off by
  96     the 'Comments' link in the edit bar as well as by its own link,
  97     along with any others whose names contain 'comment'.
  98 
  99     If 'comment' is added to a '{{{#!wiki' line, the section becomes
 100     a highlighted MoinMoin comment. However, as the MoinMoin comments
 101     mechanism doesn't know anything about SeeSaw sections, the text of
 102     the link doesn't get toggled when 'Comments' is used, so it is
 103     best if the same text is used for both "tohide" and "toshow" in
 104     that situation to avoid them getting out of sync.
 105 
 106     By setting the option 'seesaw' to False it is possible to apply
 107     SeeSaw to non-SeeSaw sections, e.g. the div created by the
 108     TableOfContents macro. In such a case, the section name in the
 109     SeeSaw call should be a class already attached to the div. If the
 110     section is - more than likely - shown initially, 'show=True'
 111     should be set in order to get the link text right (or toshow and
 112     tohide can be reversed).
 113     NB MoinMoin's CSS for tables of content at versions prior to 1.8.1
 114     also affects the SeeSaw link, so it will be necessary to do some
 115     tweaking to counteract its effect.
 116     
 117 JQUERY
 118     Implementation requires the following jQuery code:
 119 
 120 function seeSaw(section,speed,seesaw) {
 121     if (seesaw) dotseesaw = ".seesaw"; else dotseesaw = "";
 122 
 123     spandimg = $("span.seesaw." + section + ",img.seesaw." + section);
 124     spandimg.toggle().not(":hidden").css("display","inline");
 125     $("div" + dotseesaw + "." + section).toggle(speed);
 126 };
 127 
 128 $(document).ready(function() {
 129     $("div.seesaw,span.seesawinline").not(".show").hide();
 130     $("input.seesaw:hidden").each(function() {
 131 	bg = $(this).attr("value").split(":");
 132 	$("div.seesaw." + bg[0]).css("background-color",bg[1]).removeClass(bg[0]);
 133     });
 134 });
 135 
 136     jQuery will be required until (if ever) I can be bothered to
 137     rewrite using pure JavaScript. It can be downloaded from
 138     http://jquery.com/
 139 
 140     One way to load the seesaw code and jQuery would be to place them
 141     in /moin_static18x/common/js as seesaw.js and jquery.js, say, and
 142     to add the following to the configuration variable html_head:
 143 
 144 <script type="text/javascript" src="/moin_static18x/common/js/jquery.js"></script>
 145 <script type="text/javascript" src="/moin_static18x/common/js/seesaw.js"></script>
 146 
 147 EXAMPLES
 148     See http://seesaw.ncl.ac.uk
 149 
 150 IMAGES
 151 begin 644 images.tgz
 152 M'XL(`&*B9$@``\O(3$E-+"K*+]<KR$MGH`TP``(S,P,P;6!BB$H#@1%0SM#0
 153 MP-C`U,C`S-B,P<#0P,3$@$'!@$;N00&EQ26)10H*]+!J,(+.`#]W7BXI+B"3
 154 MU]/#)0A(<X(P!QN0?#"130!(E7NZ.(9(3"T];\AW6(&']4+^R^J/DQHYQ=[-
 155 M4@WNN\Y<XOJ(9S]W9X:T3N6&RDT;G\[9^SUN8\>42](JW`UL#OPW/<0\F!J2
 156 M&_D\&U1V5-?.N'9G[ST/H3-OYDR<.??Z[5ZEI-F!+CGU&7]RY3/^OSI_=,<7
 157 MUTTZ%[=^V_Z=L?M1(X?OMU?5('=ZNOJYK'-*:!K`H!J6(`.8_PMR2G,S\VA7
 158 M`!#*_X8FQHC\;V8"RO]FAL:C^9\>@)C\KP7)_\DI"0D_SCNP*"QY82]LN$Q@
 159 M:4=KP]:>%UT2FIOZ-%Q[+.XT,0&S*:M`\;=#[T%&CV;9(0"*,_++![S^-S1!
 160 MY'\C(U#^-S4P',W_]`#$Y/]@2/Z?.T&1K\&!AT7]9DQPY]K]_@D/'G-4L,P_
 161 M*-+-X"7P6&WV756E63._=F3,/"GK\<J!:?62G9.$+!I<+WU1B[VITCMURH%W
 162 M[PZO7[+I\N==^O9SWP15MW+=[+\'<L-H03%P`)3_![S^-T5J_YL:@^M_4[/1
 163 M_$\/0$S^U\=6_\_:H'F(0<?"IV&C9*%D_T'U4U8Y71(;.05XU!F"#W"Q_[",
 164 MO`XR?S1OCX)1,`I&P2@8!:-@%(R"43`*1L$H&`6C8!2,@E$P"D;!*!@%HV`4
 165 ,T!<```N2X?$`*```
 166 `
 167 end
 168 
 169 AUTHOR
 170     Jim Wight <j.k.wight@ncl.ac.uk>
 171 
 172 HISTORY
 173     [v0.5] 2009-01-31
 174     Add seesaw option
 175     Remove unnecessary HTML if {pre|post}/{show|hide} not used
 176     Remove examples
 177 
 178     [v0.4] Not released
 179     Add speed option
 180 
 181     [v0.3] 2008-07-05
 182     Add image links
 183 
 184     [v0.2] 2008-05-12
 185     Accommodate different text surrounding the link
 186 
 187     [v0.1] 2008-05-01
 188     Initial version
 189 '''
 190 
 191 
 192 #--- Configuration ----------------------------------------
 193 # 
 194 # Change this to the relative location of the images
 195 #
 196 # img_prefix="/moin_static18x/common"
 197 img_prefix="/img"
 198 #
 199 # Optionally change this (to extend the list of image sets)
 200 #
 201 imageset = {
 202     "arrow" : ["showarrow.png", "hidearrow.png"],
 203     "plumin": ["showplumin.png", "hideplumin.png"]
 204     }
 205 #
 206 #--- End of Configuration ---------------------------------
 207 
 208 
 209 Dependencies = []
 210 def execute(macro, args):
 211     import re
 212     from MoinMoin import wikiutil
 213 
 214     def escape(x):
 215         if x is None:
 216             return x;
 217         else:
 218             return wikiutil.escape(x)
 219 
 220     parser = wikiutil.ParameterParser('%(section)s%(toshow)s%(tohide)s%(show)b%(bg)s%(inline)s%(image)s%(speed)s%(seesaw)b')
 221     (count,dict) = parser.parse_parameters(args)
 222     (section,toshow,tohide,show,bg,inline,image,speed,seesaw) = (dict[x] for x in ('section','toshow','tohide','show','bg','inline','image','speed', 'seesaw'))
 223 
 224     if section is None:
 225         section = 'section'
 226     if tohide is None and toshow is None:
 227         if inline is None:
 228             toshow = 'Show'; tohide = 'Hide'
 229         else:
 230             toshow = u'»»';  tohide = u'««'
 231     elif tohide is None and toshow is not None:
 232         tohide = toshow
 233 
 234     regex = re.compile(r'(.*)<<(.*)>>(.*)')
 235     preshow = postshow = prehide = posthide = ""
 236     matches = regex.match(toshow)
 237     if matches is not None:
 238         preshow,toshow,postshow = matches.groups()
 239     matches = regex.match(tohide)
 240     if matches is not None:
 241         prehide,tohide,posthide = matches.groups()
 242 
 243     showimage = hideimage = ""
 244     img = False
 245     if image is not None:
 246         imagepair = imageset.get(image)
 247         if imagepair is not None:
 248             (showimage,hideimage) = (img_prefix.rstrip("/") + "/" + x for x in imagepair)
 249             img = True
 250     
 251     if show is None:
 252         show = False
 253     if show:
 254         toshow,tohide, preshow,prehide, postshow,posthide, showimage,hideimage = tohide,toshow, prehide,preshow, posthide,postshow, hideimage,showimage
 255 
 256     section,preshow,toshow,postshow,prehide,tohide,posthide,bg,inline = (escape(x) for x in (section,preshow,toshow,postshow,prehide,tohide,posthide,bg,inline))
 257 
 258     comment = bgstyle = divstyle = shown = ""
 259     if inline is None:
 260         if bg is not None:
 261             divstyle = '''<input class="seesaw" type="hidden" value="%(section)s-bg:%(bg)s">''' % locals()
 262     else:
 263         if show:
 264             shown = "show"
 265         if section != "comment" and bg is not None:
 266             bgstyle = '''style="background-color:%(bg)s"''' % locals()
 267         comment = '''<span class="seesaw seesawinline %(section)s %(shown)s" %(bgstyle)s>%(inline)s</span>''' % locals()
 268 
 269     showit = '''<span class="seesaw %s" style="display:inline">%s</span>'''
 270     hideit = '''<span class="seesaw %s" style="display:none">%s</span>'''
 271     pres = preh = psts = psth = ''
 272     if preshow != '':
 273         pres = showit % (section, preshow)
 274     if prehide != '':
 275         preh = hideit % (section, prehide)
 276     if postshow != '':
 277         psts = showit % (section, postshow)
 278     if posthide != '':
 279         psth = hideit % (section, posthide)
 280 
 281     if speed is None:
 282         speed = 0
 283     try:
 284         speed = int(speed)
 285     except:
 286         speed = '''"%s"''' % speed
 287 
 288     if seesaw is None or seesaw:
 289         seesaw = 'true'
 290     else:
 291         seesaw = 'false'
 292 
 293     if img:
 294         html = '''%(pres)s%(preh)s<a href="">%(divstyle)s<img class="seesaw %(section)s" style="display:inline" src="%(showimage)s" onClick='seeSaw("%(section)s",%(speed)s, %(seesaw)s);return false;'></a><a href=""><img class="seesaw %(section)s" style="display:none" src="%(hideimage)s" onClick='seeSaw("%(section)s",%(speed)s,%(seesaw)s);return false;'></a>%(psts)s%(psth)s%(comment)s''' % locals()
 295     else:
 296         html = '''%(pres)s%(preh)s<a href="" onClick='seeSaw("%(section)s",%(speed)s,%(seesaw)s);return false;'>%(divstyle)s<span class="seesaw %(section)s" style="display:inline">%(toshow)s</span><span class="seesaw %(section)s" style="display:none">%(tohide)s</span></a>%(psts)s%(psth)s%(comment)s''' % locals()
 297 
 298     return macro.formatter.rawHTML(html)

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] (2008-05-09 14:59:21, 7.4 KB) [[attachment:SeeSaw-0.1.py]]
  • [get | view] (2008-05-16 11:14:42, 8.7 KB) [[attachment:SeeSaw-0.2.py]]
  • [get | view] (2008-07-05 09:39:52, 12.2 KB) [[attachment:SeeSaw-0.3.py]]
  • [get | view] (2009-01-31 11:31:32, 12.0 KB) [[attachment:SeeSaw-0.5.py]]
  • [get | view] (2009-04-11 11:10:41, 27.6 KB) [[attachment:SeeSaw-1.0.tgz]]
  • [get | view] (2011-01-23 12:32:08, 35.1 KB) [[attachment:SeeSaw-1.1.tgz]]
  • [get | view] (2013-03-31 13:52:00, 40.5 KB) [[attachment:SeeSaw-1.3.tgz]]
  • [get | view] (2013-04-28 13:34:58, 42.5 KB) [[attachment:SeeSaw-1.4.tgz]]
  • [get | view] (2013-06-09 08:57:04, 43.4 KB) [[attachment:SeeSaw-1.5.tgz]]
  • [get | view] (2008-07-05 09:38:54, 0.2 KB) [[attachment:hidearrow.png]]
  • [get | view] (2008-07-05 09:39:11, 0.1 KB) [[attachment:hideplumin.png]]
  • [get | view] (2013-06-09 08:56:44, 49.6 KB) [[attachment:seesawexamples.tgz]]
  • [get | view] (2008-07-05 09:39:25, 0.1 KB) [[attachment:showarrow.png]]
  • [get | view] (2008-07-05 09:39:40, 0.1 KB) [[attachment:showplumin.png]]
 All files | Selected Files: delete move to page copy to page

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