in reply to A module for creating "sane" URLs from "arbitrary" titles?

Converting "ß" to "ss" seems reasonable to me. "sz" is a possibility of course, but IIRC "ss" tends to be used when sorting "ß" alphabetically.

I say, go for it. Publish. I'd use a module like this in WWW::DataWiki if it existed. Right now I just do something along the lines of:

my $slug = lc $ctx->req->header('Slug'); $slug =~ s/[^a-z0-9]/-/; $slug =~ s/[-]{2,}/-/g; while (page_exists($slug)) { $slug++; } $slug = sprintf('uuid-%s', lc $self->uuid_generator->create_str) unless $slug =~ /^[a-z][a-z0-9-]*[a-z0-9]$/;

It would be nice if your module provided a way of passing in a "page already exists" function as a coderef.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Replies are listed 'Best First'.
Re^2: A module for creating "sane" URLs from "arbitrary" titles?
by Corion (Patriarch) on Apr 21, 2012 at 08:35 UTC

    I have the "URL was already created" thing on my to-do list as well, but I'm not sure that the module needs a callback. You can always do that check on the outside, and especially I'm unsure that incrementing the result string is what is desireable. For example I would like to append a counter such that img_1234 and img_1234 map to img_1234 (first) and img_1234_1 (second). Incrementing img_1234 to img_1235 will create an ugly and confusing rippling effect, as most of my images are sequentially numbered.

    On the other hand, almost every situation I come up with has to tackle this problem - at least the two use cases I can see, generating URL fragments and (re)naming files according to contained tags.

    So indeed, both functionalities would be useful, but I don't see how to do automatic counter (or whatever) generation through an API in a way that avoids duplicate_1_1_1 but still allows for img_1234 to become img_1234_1.

    Passing in and using the duplicate detection callback is easy, but I don't see how it can be useful except if the duplicate callback returns the result to use instead. Basically, the use case with the explicit default callback would be:

    my %fragment_exists; sanitize_name(sub {map { $_ . "_" . $fragment_exists{ $_ }++} @_ }, $title, ...);

    I think the following idiom will be going into the documentation, together with the hint of sorting the fragments, and potentially stripping of anything that looks like a counter, to eliminate the _1_1_1 effect:

    my %fragment_exists; my $fragment; my $duplicate = ''; do { $fragment = sanitize_name( "$title_" . $duplicate++ ); while( $fragment_exists{ $fragment }++ );

    As sanitize_name will strip trailing underscores, it's easy to add a counter to the end that way, even if that counter starts numbering the first duplicate with _1 instead of _2 ...