# HG changeset patch # User Todd Larsen # Date 1223094856 0 # Node ID 56357a92c110e70fe75934578c7470e39ea3e261 # Parent 78fd8c2ed80a7f28505068b7a93e264d793d4c34 Move helpers/html_menu.py to helper/html_menu.py, to clear out soc/views/helpers for deletion. Patch by: Todd Larsen Review by: to-be-reviewed diff -r 78fd8c2ed80a -r 56357a92c110 app/soc/views/helper/html_menu.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/soc/views/helper/html_menu.py Sat Oct 04 04:34:16 2008 +0000 @@ -0,0 +1,252 @@ +#!/usr/bin/python2.5 +# +# Copyright 2008 the Melange authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Helpers for displaying arbitrarily nested menus as HTML lists. +""" + +__authors__ = [ + '"Todd Larsen" ', + ] + + +from soc.logic import menu + + +class HtmlMenu: + """Ordered collection of MenuItem objects as

...

paragraphs. + """ + ITEM_PREFIX_FMT = '%(indent)s

' + ITEM_SUFFIX_FMT = '%(indent)s

' + + def __init__(self, menu, item_class=None): + """Wraps an soc.logic.menu.Menu in order to render it as HTML. + + Args: + menu: an soc.logic.menu.Menu object + item_class: style used to render the MenuItems contained in menu; + default is None, which causes AHrefMenuItem to be used + """ + self._menu = menu + + # workaround for circular dependency between AHrefMenuItem and this class + if not item_class: + item_class = AHrefMenuItem + + self._item_class = item_class + + def getHtmlTags(self, indent): + """Returns list of HTML tags for arbitrarily nested items in the menu. + + Args: + indent: string prepended to the beginning of each line of output + (usually consists entirely of spaces) + + Returns: + a list of strings that can be joined with '\n' into a single string + to produce an entire list in HTML + """ + tags = [] + + if self._menu.items: + tags.append(self.ITEM_PREFIX_FMT % {'indent': indent}) + + for item in self._menu.items: + tags.extend(self._item_class( + item, menu_class=self.__class__).getHtmlTags(indent + ' ')) + + tags.append(self.ITEM_SUFFIX_FMT % {'indent': indent}) + + return tags + + def __str__(self): + return '\n'.join(self.getHtmlTags('')) + + +class UlMenu(HtmlMenu): + """Ordered collection of MenuItem objects as a