From f07cd2dc418b10776dc2725043615aa591285d2d Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Tue, 26 Aug 2008 19:35:50 -0700 Subject: [PATCH] Notebooks.summarize_note() can now optionally highlight some particular text. --- controller/Notebooks.py | 13 +++++++++++-- controller/test/Test_notebooks.py | 5 +++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/controller/Notebooks.py b/controller/Notebooks.py index 58b18d0..423d4c5 100644 --- a/controller/Notebooks.py +++ b/controller/Notebooks.py @@ -426,7 +426,7 @@ class Notebooks( object ): note = summarize and self.summarize_note( note ) or note, ) - def summarize_note( self, note, max_summary_length = None, word_count = None ): + def summarize_note( self, note, max_summary_length = None, word_count = None, highlight_text = None ): """ Create a truncated, HTML-free note summary for the given note, and then return the note with its summary set. @@ -439,6 +439,8 @@ class Notebooks( object ): @type word_count: int or NoneType @param word_count: the number of words to which the summary is truncated (optional, defaults to a reasonable number of words) + @type highlight_text: unicode or NoneType + @param highlight_text: text to emphasize within the summary (optional, defaults to no emphasis) @rtype: model.Note or NoneType @return: note with its summary member set, or None if no note was provided """ @@ -485,6 +487,9 @@ class Notebooks( object ): if truncated or word_count < len( words ): summary += " ..." + if highlight_text: + summary = summary.replace( highlight_text, "%s" % highlight_text ) + note.summary = summary return note @@ -1124,7 +1129,11 @@ class Notebooks( object ): notes = self.__database.select_many( Note, Notebook.sql_search_notes( user_id, notebook_id, search_text, self.__database.backend ) ) # make a summary for each note that doesn't have one - notes = [ note.summary and note or self.summarize_note( note, max_summary_length = 80, word_count = 30 ) for note in notes ] + notes = [ + note.summary and note or + self.summarize_note( note, max_summary_length = 80, word_count = 30, highlight_text = search_text ) + for note in notes + ] return dict( notes = notes, diff --git a/controller/test/Test_notebooks.py b/controller/test/Test_notebooks.py index 344dc40..0449bd2 100644 --- a/controller/test/Test_notebooks.py +++ b/controller/test/Test_notebooks.py @@ -1308,6 +1308,11 @@ class Test_notebooks( Test_controller ): assert note == None + def test_summarize_note_with_highligh_text( self ): + note = cherrypy.root.notebooks.summarize_note( self.note, highlight_text = "la" ) + + assert note.summary == u"blah" + def test_lookup_note_id( self ): self.login()