Attachment 'SeeSaw-0.3.py'

Download

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