aboutsummaryrefslogtreecommitdiffstats
path: root/Bugs-Everywhere-Web/beweb/formatting.py
diff options
context:
space:
mode:
authorAaron Bentley <aaron.bentley@utoronto.ca>2006-04-10 22:07:54 -0400
committerAaron Bentley <aaron.bentley@utoronto.ca>2006-04-10 22:07:54 -0400
commit68df9cb4413c2642bac15a940c26e68f9d1391cb (patch)
treecb429c57d740d679a467db3034f93f96ea4e2e33 /Bugs-Everywhere-Web/beweb/formatting.py
parent16ed63b5c8dc2e211a29ba3636cd00b547ceb9aa (diff)
downloadbugseverywhere-68df9cb4413c2642bac15a940c26e68f9d1391cb.tar.gz
Updated beweb to use TurboGears 0.9
Diffstat (limited to 'Bugs-Everywhere-Web/beweb/formatting.py')
-rw-r--r--Bugs-Everywhere-Web/beweb/formatting.py73
1 files changed, 73 insertions, 0 deletions
diff --git a/Bugs-Everywhere-Web/beweb/formatting.py b/Bugs-Everywhere-Web/beweb/formatting.py
new file mode 100644
index 0000000..44ed849
--- /dev/null
+++ b/Bugs-Everywhere-Web/beweb/formatting.py
@@ -0,0 +1,73 @@
+from StringIO import StringIO
+
+from elementtree.ElementTree import XML
+from libbe.restconvert import rest_xml
+
+def to_unix(text):
+ skip_newline = False
+ for ch in text:
+ if ch not in ('\r', '\n'):
+ yield ch
+ else:
+ if ch == '\n':
+ if skip_newline:
+ continue
+ else:
+ skip_newline = True
+ yield '\n'
+
+
+def soft_text(text):
+ first_space = False
+ translations = {'\n': '<br />\n', '&': '&amp;', '\x3c': '&lt;',
+ '\x3e': '&gt;'}
+ for ch in to_unix(text):
+ if ch == ' ' and first_space is True:
+ yield '&#160;'
+ first_space = ch in (' ')
+ try:
+ yield translations[ch]
+ except KeyError:
+ yield ch
+
+
+def soft_pre(text):
+ return XML('<div style="font-family: monospace">'+
+ ''.join(soft_text(text))+'</div>')
+
+
+def get_rest_body(rest):
+ xml, warnings = rest_xml(StringIO(rest))
+ return xml.find('{http://www.w3.org/1999/xhtml}body'), warnings
+
+
+def comment_body_xhtml(comment):
+ if comment.content_type == "text/restructured":
+ return get_rest_body(comment.body)[0]
+ else:
+ return soft_pre(comment.body)
+
+
+def select_among(name, options, default, display_names=None):
+ output = ['<select name="%s">' % name]
+ for option in options:
+ if option == default:
+ selected = ' selected="selected"'
+ else:
+ selected = ""
+ if display_names is None:
+ display_name = None
+ else:
+ display_name = display_names.get(option)
+
+ if option is None:
+ option = ""
+ if display_name is None:
+ display_name = option
+ value = ""
+ else:
+ value = ' value="%s"' % option
+ output.append("<option%s%s>%s</option>" % (selected, value,
+ display_name))
+ output.append("</select>")
+ return XML("".join(output))