Notebooks.summarize_note() can now optionally highlight some particular text.
This commit is contained in:
parent
8cad14ca34
commit
f07cd2dc41
|
@ -426,7 +426,7 @@ class Notebooks( object ):
|
||||||
note = summarize and self.summarize_note( note ) or note,
|
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
|
Create a truncated, HTML-free note summary for the given note, and then return the note with
|
||||||
its summary set.
|
its summary set.
|
||||||
|
@ -439,6 +439,8 @@ class Notebooks( object ):
|
||||||
@type word_count: int or NoneType
|
@type word_count: int or NoneType
|
||||||
@param word_count: the number of words to which the summary is truncated (optional, defaults
|
@param word_count: the number of words to which the summary is truncated (optional, defaults
|
||||||
to a reasonable number of words)
|
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
|
@rtype: model.Note or NoneType
|
||||||
@return: note with its summary member set, or None if no note was provided
|
@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 ):
|
if truncated or word_count < len( words ):
|
||||||
summary += " ..."
|
summary += " ..."
|
||||||
|
|
||||||
|
if highlight_text:
|
||||||
|
summary = summary.replace( highlight_text, "<b>%s</b>" % highlight_text )
|
||||||
|
|
||||||
note.summary = summary
|
note.summary = summary
|
||||||
return note
|
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 ) )
|
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
|
# 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(
|
return dict(
|
||||||
notes = notes,
|
notes = notes,
|
||||||
|
|
|
@ -1308,6 +1308,11 @@ class Test_notebooks( Test_controller ):
|
||||||
|
|
||||||
assert note == None
|
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"b<b>la</b>h"
|
||||||
|
|
||||||
def test_lookup_note_id( self ):
|
def test_lookup_note_id( self ):
|
||||||
self.login()
|
self.login()
|
||||||
|
|
||||||
|
|
Reference in New Issue