Attachment 'SeeSaw-0.2.py'

Download

   1 # -*- coding: utf-8 -*-
   2 '''
   3 MoinMoin 1.6 - 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     where
  19         section  gives the section a name;
  20                  it defaults to 'section'
  21          toshow  specifies the text to display for showing the section
  22                  it defaults to 'Show' (for block) and '»»' (for inline)
  23          tohide  specifies the text to display for hiding the section;
  24                  it defaults to 'Hide' (block) and '««' (inline)
  25                  if toshow is given, but not tohide, tohide is set to the
  26                  value of toshow; toshow is undefined if only tohide is
  27                  provided
  28            show  specifies whether the section is to be shown initially;
  29                  it defaults to False
  30              bg  specifies the background colour to use for the section;
  31                  it defaults to None
  32          inline  specifies the text of an inline section;
  33                  it defaults to None, and non-presence implies the call
  34                  relates to a block section
  35                  
  36     'toshow' and 'tohide' can accommodate the text surrounding the link,
  37     to allow it to be different in the two cases. The three parts are
  38     distinguished by enclosing the text of the link between '<<' and '>>',
  39     e.g. toshow="<<Open>> me up", tohide="Now <<close>> me down"
  40 
  41     The (leading) arguments can also be given positionally, in the
  42     order of the keyword arguments above, e.g.
  43 
  44         <<SeeSaw("section1", "See-Saw")>>
  45         <<SeeSaw("section1", show=True)>>
  46 
  47     Block sections require to be set up as follows:
  48 
  49         {{{#!wiki seesaw/section
  50         }}}
  51 
  52     where the word 'section' after '/' matches the value "section" in
  53     the corresponding SeeSaw call.
  54 
  55     By default, block sections are hidden initially, but can be shown
  56     by adding '/show' to the '{{{#!wiki' line. 'show' should be set to
  57     True in the matching SeeSaw call if its 'tohide' and 'toshow'
  58     arguments are different (so that the correct one can be shown
  59     initially)
  60 
  61     If a background colour is specified for a block section,
  62     '/"section"-bg' needs to be added to the corresponding '{{{#!wiki'
  63     line to have the colour applied to the section. If there are
  64     multiple sections with the same name, it is sufficient to use 'bg'
  65     in just one of the SeeSaw calls, with the first taking precedence
  66     if multiple, but different, values are given.
  67 
  68     The text of inline sections is embedded in SeeSaw calls. By
  69     default, inline sections are hidden initially.
  70 
  71     SeeSaw sections behave similarly to MoinMoin comments in that all
  72     sections with the same name are toggled together. In fact, if the
  73     section name contains 'comment', it can be toggled on and off by
  74     the 'Comments' link in the edit bar as well as by its own link,
  75     along with any others whose names contain 'comment'.
  76 
  77     If '/comment' is added to a '{{{#!wiki' line, the section becomes
  78     a highlighted MoinMoin comment. However, as the MoinMoin comments
  79     mechanism doesn't know anything about SeeSaw sections, the text of
  80     the link doesn't get toggled when 'Comments' is used, so it is
  81     best if the same text is used for both "tohide" and "toshow" in
  82     that situation to avoid them getting out of sync.
  83 
  84     Implementation requires the following jQuery code:
  85 
  86 function seeSaw(section) {
  87     $("span.seesaw." + section).toggle().not(":hidden").css("display","inline");
  88     $("div.seesaw." + section).toggle();
  89 };
  90 
  91 $(document).ready(function() {
  92     $("div.seesaw,span.seesawinline").not(".show").hide();
  93     $("input.seesaw:hidden").each(function() {
  94 	bg = $(this).attr("value").split(":");
  95 	$("div.seesaw." + bg[0]).css("background-color",bg[1]).removeClass(bg[0]);
  96     });
  97 });
  98 
  99     jQuery will be required until (if ever) I can be bothered to
 100     rewrite using pure JavaScript. It can be downloaded from
 101     http://jquery.com/
 102 
 103     One way to load the local code and jQuery would be to place them
 104     in /moin_static16x/common/js as local.js and jquery.js, say, and to
 105     add the following to the configuration variable html_head:
 106 
 107 <script type="text/javascript" src="/moin_static16x/common/js/jquery.js"></script>
 108 <script type="text/javascript" src="/moin_static16x/common/js/local.js"></script>
 109 
 110 EXAMPLES
 111 
 112 <<SeeSaw(section="example1")>> example 1
 113 {{{#!wiki seesaw/example1
 114 This text was hidden initially and the link shown was Show;
 115 it should now be Hide
 116 }}}
 117 
 118 <<SeeSaw("example2", show=True)>> example 2
 119 {{{#!wiki seesaw/example2/show
 120 This text is displayed initially and the link shown is Hide
 121 }}}
 122 
 123 <<SeeSaw(section="example3")>> example 3
 124 {{{#!wiki seesaw/example3/comment
 125 This is a both a SeeSaw section and a MoinMoin comment.
 126 It (and any other MoinMoin comments) can be toggled on and off via the link
 127 above or by the Comments link in the edit bar, but note that if the Comments
 128 link is used, the text in the link gets out of sync.
 129 }}}
 130 
 131 <<SeeSaw(section="comment1")>> example 4
 132 {{{#!wiki seesaw/comment1
 133 This section can be toggled on and off via the link above or by the
 134 Comments link in the edit bar - but it is not subject to MoinMoin
 135 comment highlighting.
 136 }}}
 137 
 138 <<SeeSaw(section="example5", toshow="See-Saw", show=True, bg='#FFFFDD')>>
 139 example 5
 140 {{{#!wiki seesaw/example5/show
 141 This is part 1 of example 5; it is displayed initially.<<BR>>
 142 It is hidden and part 2 displayed when See-Saw is clicked
 143 }}}
 144 {{{#!wiki seesaw/example5/example5-bg
 145 This is part 2 of example 5; it has a different background colour and is
 146 displayed when part 1 is hidden.<<BR>>
 147 It is hidden and part 1 displayed again when See-Saw is clicked.
 148 }}}
 149 
 150 Example 6 contains some
 151 <<SeeSaw(section="inline1", inline=" hidden", bg='#FFFFDD')>> text.
 152 
 153 Example 7 demonstrates toggling a
 154 <<SeeSaw(section="comment", show=True, inline="MoinMoin")>> comment (plus
 155 any other MoinMoin comments)
 156 
 157 <<SeeSaw("example8", "<<Show>> example 8", "Use different text for <<hiding>> it")>>
 158 {{{#!wiki seesaw/example8
 159 ||The body||of example 8||is a table||
 160 }}}
 161 
 162 AUTHOR
 163     Jim Wight <j.k.wight@ncl.ac.uk>
 164 
 165 HISTORY
 166     [v0.2] 2008-05-12
 167     Accommodate different text surrounding the link
 168 
 169     [v0.1] 2008-05-01
 170     Initial version
 171 
 172 '''
 173 
 174 Dependencies = []
 175 def execute(macro, args):
 176     import re
 177     from MoinMoin import wikiutil
 178 
 179     parser = wikiutil.ParameterParser('%(section)s%(toshow)s%(tohide)s%(show)b%(bg)s%(inline)s')
 180     (count,dict) = parser.parse_parameters(args)
 181     (section,toshow,tohide,show,bg,inline) = dict['section'], dict['toshow'], dict['tohide'], dict['show'], dict['bg'], dict['inline']
 182     if section is None:
 183         section = 'section'
 184     if tohide is None and toshow is None:
 185         if inline is None:
 186             toshow = 'Show'; tohide = 'Hide'
 187         else:
 188             toshow = u'»»';  tohide = u'««'
 189     elif tohide is None and toshow is not None:
 190         tohide = toshow
 191 
 192     regex = re.compile(r'(.*)<<(.*)>>(.*)')
 193     preshow = postshow = prehide = posthide = ""
 194     matches = regex.match(toshow)
 195     if matches is not None:
 196         preshow,toshow,postshow = matches.groups()
 197     matches = regex.match(tohide)
 198     if matches is not None:
 199         prehide,tohide,posthide = matches.groups()
 200 
 201     if show is None:
 202         show = False
 203     if show:
 204         toshow,tohide = tohide,toshow
 205         preshow,prehide = prehide,preshow
 206         postshow,posthide = posthide,postshow 
 207 
 208     comment = bgstyle = divstyle = shown = ""
 209     if inline is None:
 210         if bg is not None:
 211             divstyle = '''<input class="seesaw" type="hidden" value="%(section)s-bg:%(bg)s">''' % locals()
 212     else:
 213         if show:
 214             shown = "show"
 215         if section != "comment" and bg is not None:
 216             bgstyle = '''style="background-color:%(bg)s"''' % locals()
 217         comment = '''<span class="seesaw seesawinline %(section)s %(shown)s" %(bgstyle)s>%(inline)s</span>''' % locals()
 218 
 219     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()
 220 
 221     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.