[kupfer] plugin.notes: Format text when creating new notes



commit 84b3a103dc9c91a0a159a029916e28e63ec9f344
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Sat Oct 24 20:13:28 2009 +0200

    plugin.notes: Format text when creating new notes
    
    Try to format text to make a sensible title. If the first line is a
    good title, pass the text on as it is, the first line will become the
    note's title and the following text the content.
    
    If the first line is too long, we break it off and take the first
    words as a title while keeping the full text as content to not break
    any sentences between title and content.
    
    This works well for latin languages. Doing smart things with other
    languages can be implemented later.

 kupfer/plugin/notes.py |   36 +++++++++++++++++++++++++++++++++++-
 1 files changed, 35 insertions(+), 1 deletions(-)
---
diff --git a/kupfer/plugin/notes.py b/kupfer/plugin/notes.py
index 66e3658..0020c6a 100644
--- a/kupfer/plugin/notes.py
+++ b/kupfer/plugin/notes.py
@@ -107,13 +107,47 @@ class AppendToNote (Action):
 	def get_icon_name(self):
 		return "gtk-add"
 
+def _prepare_note_text(text):
+	"""Prepare note text
+
+	A long text will have its first words on a separate line as title
+	"""
+	if not text.strip():
+		return text
+
+	def split_first_line(text):
+		"""Take first non-empty line of text"""
+		lines = iter(text.splitlines())
+		for l in lines:
+			l = l.strip()
+			if not l:
+				continue
+			rest = u"\n".join(lines)
+			return l, rest
+
+	def split_first_words(text, maxwords):
+		words = text.split(None, maxwords)
+		return u" ".join(words[:maxwords]), u" ".join(words[maxwords:])
+
+	# Take first line -- if it s a good title return text as is
+	# If first line is too long, take first words and *full* text follow
+	maxtitlewords = 7
+	firstline, rest = split_first_line(text)
+	if len(firstline.split(None, maxtitlewords)) > maxtitlewords:
+		firstline, rest = split_first_words(text, maxtitlewords)
+	else:
+		return text.lstrip()
+	if rest.strip():
+		text = u"%s\n%s" % (firstline, text)
+	return text
+
 class CreateNote (Action):
 	def __init__(self):
 		Action.__init__(self, _("Create Note"))
 
 	def activate(self, leaf):
 		notes = _get_notes_interface(activate=True)
-		text = leaf.object
+		text = _prepare_note_text(leaf.object)
 		# FIXME: For Gnote we have to call DisplayNote
 		# else we can't change its contents
 		noteuri = notes.CreateNote()



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]