Common code to a utils file
authorIvan Frade <ivan.frade@gmail.com>
Thu, 20 Aug 2009 22:46:59 +0000 (01:46 +0300)
committerIvan Frade <ivan.frade@gmail.com>
Thu, 20 Aug 2009 22:46:59 +0000 (01:46 +0300)
escape_html function is needed when printing in markup

setup.py
src/utils.py [new file with mode: 0644]

index 7bb3298..6f05f3b 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -17,7 +17,8 @@ DATA = [('share/applications/hildon', ['data/mussorgsky.desktop']),
                             'src/mussorgsky.py',
                             'src/mutagen_backend.py',                            
                             'src/player_backend.py',
-                            'src/tracker_backend.py'])]
+                            'src/tracker_backend.py',
+                            'src/utils.py'])]
  
 setup(name         = 'mussorgsky',
       version      = '0.1',
diff --git a/src/utils.py b/src/utils.py
new file mode 100644 (file)
index 0000000..ed01048
--- /dev/null
@@ -0,0 +1,8 @@
+def escape_html (text, max_length=40):
+    if (len (text) > max_length):
+        cutpoint = text.find (' ', max_length-10)
+        if (cutpoint == -1 or cutpoint > max_length):
+            cutpoint = max_length
+        text = text [0:cutpoint] + "..."
+    return text.replace ("&","&amp;").replace ("<", "&lt;").replace (">", "&gt;").replace ("\"", "&quot;")
+