Author: kake Date: 2012-05-02 12:18:32 +0100 (Wed, 02 May 2012) New Revision: 1372
Modified: trunk/Changes trunk/lib/OpenGuides/Config.pm trunk/lib/OpenGuides/Template.pm trunk/t/68_bug_website_displayed.t Log: Made website truncation a bit more intelligent.
Modified: trunk/Changes =================================================================== --- trunk/Changes 2012-04-28 15:31:20 UTC (rev 1371) +++ trunk/Changes 2012-05-02 11:18:32 UTC (rev 1372) @@ -27,6 +27,9 @@ Added JSON output for the preferences page (#293). IP addresses no longer show on recent changes for people without the admin preference set. + Website truncation on node display is now a little more intelligent + (you can still tweak this by setting website_link_max_chars in + your wiki.conf).
0.66 12 April 2012 All templates now have access to the "username" TT variable.
Modified: trunk/lib/OpenGuides/Config.pm =================================================================== --- trunk/lib/OpenGuides/Config.pm 2012-04-28 15:31:20 UTC (rev 1371) +++ trunk/lib/OpenGuides/Config.pm 2012-05-02 11:18:32 UTC (rev 1372) @@ -118,7 +118,7 @@ host_checker_module => "", static_path => "/usr/local/share/openguides/static", send_moderation_notifications => 1, - website_link_max_chars => 20, + website_link_max_chars => 25, read_only => 0, );
@@ -357,7 +357,7 @@
=item * moderate_whitelist
-=item * website_link_max_chars (default: C<20>) +=item * website_link_max_chars (default: C<25>)
=item * read_only
Modified: trunk/lib/OpenGuides/Template.pm =================================================================== --- trunk/lib/OpenGuides/Template.pm 2012-04-28 15:31:20 UTC (rev 1371) +++ trunk/lib/OpenGuides/Template.pm 2012-05-02 11:18:32 UTC (rev 1372) @@ -368,10 +368,10 @@ : $q->param("website"); my $formatted_website_text = ""; if ( $website && $website ne "http://" && is_web_uri( $website ) ) { - my $trunc_website = substr( $website, 0, - $config->website_link_max_chars ); - unless ($website eq $trunc_website ) { - $trunc_website .= '...'; + my $maxlen = $config->website_link_max_chars; + my $trunc_website = $website; + if ( length( $website ) > $maxlen ) { + $trunc_website = substr( $website, 0, $maxlen - 3 ) . "..."; } $formatted_website_text = '<a href="' . $website . '">' . $trunc_website . '</a>';
Modified: trunk/t/68_bug_website_displayed.t =================================================================== --- trunk/t/68_bug_website_displayed.t 2012-04-28 15:31:20 UTC (rev 1371) +++ trunk/t/68_bug_website_displayed.t 2012-05-02 11:18:32 UTC (rev 1372) @@ -11,12 +11,13 @@ plan skip_all => "DBD::SQLite could not be used - no database to test with ($error)"; }
-plan tests => 2; +plan tests => 5;
- OpenGuides::Test::refresh_db(); +OpenGuides::Test::refresh_db();
my $config = OpenGuides::Test->make_basic_config; my $guide = OpenGuides->new( config => $config ); +$config->website_link_max_chars( 23 );
$guide->wiki->write_node( "South Croydon Station", "A sleepy main-line station in what is arguably the nicest part of Croydon.", undef, { website => "http://example.com/" } ) or die "Couldn't write node"; $guide->wiki->write_node( "North Croydon Station", "A busy main-line station in what is arguably the furthest North part of Croydon.", undef, { website => "http://longer.example.com/asdfasdf" } ) or die "Couldn't write node"; @@ -33,3 +34,28 @@ );
like( $output, qr#Website:</span> <span class="url"><a href="http://longer.example.com/asdfasdf">http://longer.exampl...</a>#, "website correctly truncated" ); + +# Make sure website isn't truncated unnecessarily, e.g. that we don't end up +# just replacing the final three characters with the ellipsis. Our full URL +# has 34 characters. +$config->website_link_max_chars( 33 ); +my %tt_vars = $guide->display_node( id => "North Croydon Station", + return_tt_vars => 1 ); +is( $tt_vars{formatted_website_text}, + '<a href="http://longer.example.com/asdfasdf">http://longer.example.com/asdf...</a>', + "Website truncated correctly when 1 character longer than allowed." ); + +$config->website_link_max_chars( 34 ); +%tt_vars = $guide->display_node( id => "North Croydon Station", + return_tt_vars => 1 ); +is( $tt_vars{formatted_website_text}, + '<a href="http://longer.example.com/asdfasdf">http://longer.example.com/asdfasdf</a>', + "Website not truncated when exact length allowed." ); + +$config->website_link_max_chars( 35 ); +%tt_vars = $guide->display_node( id => "North Croydon Station", + return_tt_vars => 1 ); +is( $tt_vars{formatted_website_text}, + '<a href="http://longer.example.com/asdfasdf">http://longer.example.com/asdfasdf</a>', + "Website not truncated when 1 character shorter than allowed." ); +