witten
/
luminotes
Archived
1
0
Fork 0

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:
Dan Helfman 2008-11-21 14:07:23 -08:00
parent 54744dd3a9
commit fc3849b8be
4 changed files with 41 additions and 3 deletions

View File

@ -360,11 +360,15 @@ class Notebook( Persistent ):
self.__name = name self.__name = name
self.update_revision() self.update_revision()
HTML_REFERENCE_PATTERN = re.compile( "&[a-zA-Z]+;|&#\d+;" )
FRIENDLY_ID_STRIP_PATTERN = re.compile( "[^a-zA-Z0-9\-]+" ) FRIENDLY_ID_STRIP_PATTERN = re.compile( "[^a-zA-Z0-9\-]+" )
def __friendly_id( self ): def __friendly_id( self ):
friendly_id = self.WHITESPACE_PATTERN.sub( u"-", self.__name.lower() ) # convert to lowercase, remove HTML character/entity refs, collapse whitespace to dashes, strip
return self.FRIENDLY_ID_STRIP_PATTERN.sub( u"", friendly_id ) # 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 ): 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 # The read_write member isn't actually saved to the database, so setting it doesn't need to

15
model/delta/1.5.8.sql Normal file
View File

@ -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;

View File

@ -26,7 +26,18 @@ create function log_note_revision() returns trigger as $_$
$_$ language plpgsql; $_$ language plpgsql;
ALTER FUNCTION public.log_note_revision() OWNER TO luminotes; ALTER FUNCTION public.log_note_revision() OWNER TO luminotes;
CREATE FUNCTION friendly_id(text) RETURNS text 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; LANGUAGE sql IMMUTABLE;
ALTER FUNCTION public.friendly_id(text) OWNER TO luminotes; ALTER FUNCTION public.friendly_id(text) OWNER TO luminotes;
CREATE TABLE file ( CREATE TABLE file (

View File

@ -177,6 +177,14 @@ class Test_notebook( object ):
self.notebook.name = u"This is Bob's notebook!" self.notebook.name = u"This is Bob's notebook!"
assert self.notebook.friendly_id == u"this-is-bobs-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 ): def test_set_read_write( self ):
original_revision = self.notebook.revision original_revision = self.notebook.revision
self.notebook.read_write = Notebook.READ_WRITE_FOR_OWN_NOTES self.notebook.read_write = Notebook.READ_WRITE_FOR_OWN_NOTES