# Copyright (C) 2005, Stefan Schwarzer # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # # - Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # # - Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # - Neither the name of the above author nor the names of the # contributors to the software may be used to endorse or promote # products derived from this software without specific prior written # permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ###################################################################### # Make tables sortable by using the JavaScript tool sorttable.js from # http://www.kryogenix.org/code/browser/sorttable/ . # # Usage: # # 1. Put this file into $TRAC_ENV/wiki-macros. # 2a. Put http://www.kryogenix.org/code/browser/sorttable/sorttable.js # into the same directory, OR # 2b. In trac.ini, define sorttable_js_url in a section [sortedtable]. # Usually, this should be string to a file on your server, e. g. # "/js/sorttable.js" . DON'T USE THE QUOTES IN trac.ini! # 3. Use the wiki processor. Example: # # {{{ # #!sortedtable # || Strings || Numbers || # || abc || 2 || # || def || 10 || # || abd || 9 || # }}} ###################################################################### import os import random import re from trac import WikiFormatter __version__ = "0.7.1" regex1 = re.compile(r'') def script_element(js_url): """ Return HTML code for the "script" element/tag. If the module-global variable "js_url" is not empty, use a "script" tag with "src" attribute. Else, try to include the sorttable.js script literally from the directory this file is in. """ if js_url: return '' % js_url # get JavaScript code for sorttable.js js_name = os.path.join(os.path.dirname(__file__), "sorttable.js") try: js_file = open(js_name) except IOError: return '' \ "Warning: can't find the sorttable.js script!" \ '' js_text = js_file.read() js_file.close() return """ """ % js_text def table_id(): """ Return an arbitrary id for an HTML table. The value doesn't matter as long as it's unique and contains only valid characters. """ return "table%s" % str(random.randint(0, 1000000)) def with_added_css_class(html): """ Return the HTML code for the table where the table tag contains the "sortable" CSS class. The argument "html" is the HTML code as received from "WikiFormatter.wiki_to_html". This function assumes that the only attribute (if any) of the "table" element is "class". """ id = table_id() # try to add the "sortable" CSS class to other classes, and add the id match = regex1.search(html) if match: replacement = '' % \ (match.group(1), id) return regex1.sub(replacement, html) # no CSS class at all: add it and the id replacement = '
' % id return html.replace('
', replacement) def execute(hdf, text, env): # convert passed-in text to HTML html = WikiFormatter.wiki_to_html(text, hdf, env, env.get_db_cnx()) # add the sortable class to the table tag html = with_added_css_class(html) # get from trac.ini js_url = env.get_config('sortedtable', 'sorttable_js_url') # include the JavaScript code only once if hdf.getValue('sortedtable.js_is_included', "false") == "false": hdf.setValue('sortedtable.js_is_included', "true") return "%s\n%s" % (script_element(js_url), html) else: return html