# HG changeset patch # User paulb # Date 1133924607 0 # Node ID 5706a0da1e641f60a2be3623f820c1c11af75487 # Parent d1533bb2d4d766b6f3897368eb000b0bfe65d30e [project @ 2005-12-07 03:03:27 by paulb] Added an experimental range function, although memory usage issues may not be fully addressed. diff -r d1533bb2d4d7 -r 5706a0da1e64 XSLForms/Output.py --- a/XSLForms/Output.py Wed Dec 07 03:03:02 2005 +0000 +++ b/XSLForms/Output.py Wed Dec 07 03:03:27 2005 +0000 @@ -318,6 +318,53 @@ # Utility functions. +def xslforms_range(context, range_spec): + + """ + Exposed as {template:range(range_spec)}. + + The 'range_spec' is split up into 'start', 'finish' and 'step' according to + the following format: + + start...finish...step + + Provides the Python range function by producing a list of numbers, starting + at 'start', ending one step before 'finish', and employing the optional + 'step' to indicate the magnitude of the difference between successive + elements in the list as well as the "direction" of the sequence. By default, + 'step' is set to 1. + + NOTE: This uses a single string because template:element and other + NOTE: annotations use commas to separate fields, thus making the usage of + NOTE: this function impossible if each range parameter is exposed as a + NOTE: function parameter. + NOTE: The returning of values from this function is not fully verified, and + NOTE: it is probably better to use other extension functions instead of this + NOTE: one to achieve simple results (such as str:split from EXSLT). + """ + + parts = range_spec.split("...") + start, finish = parts[:2] + if len(parts) > 2: + step = parts[2] + else: + step = None + + start = int(start) + finish = int(finish) + if step is not None: + step = int(step) + else: + step = 1 + + # Create a list of elements. + # NOTE: libxslt complains: "Got a CObject" + + range_elements = libxml2mod.xmlXPathNewNodeSet(None) + for i in range(start, finish, step): + range_elements.append(libxml2mod.xmlNewText(str(i))) + return range_elements + def i18n(context, value): """ @@ -459,6 +506,7 @@ # Utility functions. +libxsltmod.xsltRegisterExtModuleFunction("range", "http://www.boddie.org.uk/ns/xmltools/template", xslforms_range) libxsltmod.xsltRegisterExtModuleFunction("i18n", "http://www.boddie.org.uk/ns/xmltools/template", i18n) libxsltmod.xsltRegisterExtModuleFunction("choice", "http://www.boddie.org.uk/ns/xmltools/template", choice) libxsltmod.xsltRegisterExtModuleFunction("url-encode", "http://www.boddie.org.uk/ns/xmltools/template", url_encode)