HTML entities/characters like """ are now stripped from notebook names before turning them into friendly ids.
This means that blog posts now have better URLs if they the post titles contain HTML entities/characters.
This commit is contained in:
parent
54744dd3a9
commit
fc3849b8be
|
@ -360,11 +360,15 @@ class Notebook( Persistent ):
|
|||
self.__name = name
|
||||
self.update_revision()
|
||||
|
||||
HTML_REFERENCE_PATTERN = re.compile( "&[a-zA-Z]+;|&#\d+;" )
|
||||
FRIENDLY_ID_STRIP_PATTERN = re.compile( "[^a-zA-Z0-9\-]+" )
|
||||
|
||||
def __friendly_id( self ):
|
||||
friendly_id = self.WHITESPACE_PATTERN.sub( u"-", self.__name.lower() )
|
||||
return self.FRIENDLY_ID_STRIP_PATTERN.sub( u"", friendly_id )
|
||||
# convert to lowercase, remove HTML character/entity refs, collapse whitespace to dashes, strip
|
||||
# other punctuation. strip leading/trailing dashes
|
||||
friendly_id = self.HTML_REFERENCE_PATTERN.sub( u" ", self.__name.lower() )
|
||||
friendly_id = self.WHITESPACE_PATTERN.sub( u"-", friendly_id )
|
||||
return self.FRIENDLY_ID_STRIP_PATTERN.sub( u"", friendly_id ).strip( "-" )
|
||||
|
||||
def __set_read_write( self, read_write ):
|
||||
# The read_write member isn't actually saved to the database, so setting it doesn't need to
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
CREATE OR REPLACE FUNCTION friendly_id(text) RETURNS text
|
||||
AS $_$select trim( both '-' from
|
||||
regexp_replace(
|
||||
regexp_replace(
|
||||
regexp_replace(
|
||||
lower( $1 ),
|
||||
'&[a-zA-Z]+;|&#\\d+;', ' ', 'g'
|
||||
),
|
||||
'\\s+', '-', 'g'
|
||||
),
|
||||
'[^a-zA-Z0-9\\-]', '', 'g'
|
||||
)
|
||||
);$_$
|
||||
LANGUAGE sql IMMUTABLE;
|
||||
reindex index notebook_friendly_id_index;
|
|
@ -26,7 +26,18 @@ create function log_note_revision() returns trigger as $_$
|
|||
$_$ language plpgsql;
|
||||
ALTER FUNCTION public.log_note_revision() OWNER TO luminotes;
|
||||
CREATE FUNCTION friendly_id(text) RETURNS text
|
||||
AS $_$select regexp_replace( regexp_replace( lower( $1 ), '\\s+', '-', 'g' ), '[^a-zA-Z0-9\\-]', '', 'g' );$_$
|
||||
AS $_$select trim( both '-' from
|
||||
regexp_replace(
|
||||
regexp_replace(
|
||||
regexp_replace(
|
||||
lower( $1 ),
|
||||
'&[a-zA-Z]+;|&#\\d+;', ' ', 'g'
|
||||
),
|
||||
'\\s+', '-', 'g'
|
||||
),
|
||||
'[^a-zA-Z0-9\\-]', '', 'g'
|
||||
)
|
||||
);$_$
|
||||
LANGUAGE sql IMMUTABLE;
|
||||
ALTER FUNCTION public.friendly_id(text) OWNER TO luminotes;
|
||||
CREATE TABLE file (
|
||||
|
|
|
@ -177,6 +177,14 @@ class Test_notebook( object ):
|
|||
self.notebook.name = u"This is Bob's notebook!"
|
||||
assert self.notebook.friendly_id == u"this-is-bobs-notebook"
|
||||
|
||||
def test_friendly_id_with_html_entity_reference( self ):
|
||||
self.notebook.name = u"This is Bob's "notebook"!"
|
||||
assert self.notebook.friendly_id == u"this-is-bobs-notebook"
|
||||
|
||||
def test_friendly_id_with_html_character_reference( self ):
|
||||
self.notebook.name = u"This is Bob's ¥ notebook!"
|
||||
assert self.notebook.friendly_id == u"this-is-bobs-notebook"
|
||||
|
||||
def test_set_read_write( self ):
|
||||
original_revision = self.notebook.revision
|
||||
self.notebook.read_write = Notebook.READ_WRITE_FOR_OWN_NOTES
|
||||
|
|
Reference in New Issue