#-- # WontoMedia - a wontology web application # Copyright (C) 2011 - Glen E. Ivey # www.wontology.com # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License version # 3 as published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program in the file COPYING and/or LICENSE. If not, # see . #++ # This module contains a collection of page- and model-independent # formatting methods. All return a string suitable for including # directly in a page. module FormatHelper # This method takes in a string, and returns a version of it # excluding any sub-strings enclosed in parentheticals. Nested and # multiple parentheticals are supported. This method is intended to # support the WontoMedia behavior where sometimes an Item.title # string is displayed in its entirety, and sometimes it is # abbreviated (for space) by stripping any parentheticals it contains. def filter_parenthetical(title_in) local_copy = title_in begin title_out = local_copy local_copy = local_copy.sub(/\s*\([^()]*\)/, "") end until local_copy == title_out title_out end def last_word(text) text =~ /([^\s]+)$/ $1 end # This method is used to wrap long strings that don't contain spaces # (which would allow the browser to wrap them), such as an # Item.name string. The +len+ parameter is a length in # _characters_, and the default is tuned to produce reasonably good # results in the table in the items/index page. The return value is # an HTML snippet including a marker to indicate that wrapping has # occurred. #-- # TODO: It would be better if wrapping were done # dynamically in JavaScript as the page is resized, as sometimes # wrapping the string statically doesn't produce a really great # result. Of course, to really do it right, we'd rewrap any time # the page width is adjusted, and use the actual width of the text # (rather than character count) as the basis for wrapping. def wrap_item_name(name, len = 30) wrapped = '' [ len, len*2, len*3, len*4 ].each do |wrap_at| wrapped += name[wrap_at - len, len].to_s if name.length > wrap_at wrapped += ' >>>
' end end wrapped end # This method generates an HTML snipet for an icon that will cause a # pop-up help box to be displayed when clicked. This helper is tied # very tightly to WontoMedia's style sheet, included JavaScript # libraries, and image assets. The parameters are used as follows: # # [alt] This string is used as the 'alt' attribute for the tag that is generated, and allows each help image/link generated by this helper to have unique alt text that describes the kind of help the link points to. # [target] This string specifies the source of the help page that should be rendered into the popup