Gossamer Forum
Home : Products : Links 2.0 : Customization :

Quition for <%Description%>

Quote Reply
Quition for <%Description%>
Hi,

is there a mod which reduces <%Description%> to a given word count? Maybe with an extra Tag like
<%ShortDescription%> which shows only the first few words in the category template.

Then it whould be possible to link with "more..." to the detailed site, which shows the whole text.

I search the forum, but i found nothing.

Best,
Karl
Quote Reply
Re: [Karl] Quition for <%Description%> In reply to
You could a regex (regular expression) that would do the job, shouldn't be too hard to figure out. Do you know how to do that? If not, I can look into it...


Leonard
aka PerlFlunkie
Quote Reply
Re: [PerlFlunkie] Quition for <%Description%> In reply to
In Reply To:
You could a regex (regular expression) that would do the job, shouldn't be too hard to figure out. Do you know how to do that? If not, I can look into it...

Hi Leonard.

i have try´d it, but i dont get it. My perl kung fu is not very good. :-)

Best,
Karl
Quote Reply
Re: [Karl] Quition for <%Description%> In reply to
I have been choppin' on the code, and came up with something that works. I have called it the Abbreviated Description mod. Add this in nph-build.cgi, sub build_category_pages:

Code:

# Otherwise we either only have less then x number of links, or we are not
# splitting pages, so let's just build them all.
else {
for ($i = 0; $i < $numlinks; $i++) {
%tmp = &array_to_hash ($i, @{$links{$cat}});

# > Abbreviated Description mod
my $msg = $tmp{'Description'};
my $max = 6;
my @cuts = split / /, $msg;

if ($#cuts <= $max) {
$words = $msg;
}
else {
my $back;
for (my $i = 0; $i < $max; $i++) {
$back .= " ". $cuts[$i];
}

$back .= qq~...<a href="$build_detail_url/$tmp{'ID'}$build_extension">[more]</a>~;
$words = $back;
}

$tmp{'Description'} = $words;
# < Abbreviated Description mod

$links .= &site_html_link (%tmp);
}
}



You can adjust the length of the shortened description field by changing the number in this line:

my $max = 6;

You can change the part added onto the end, and the URL it jumps to, on this line:

$back .= qq~...<a href="$build_detail_url/$tmp{'ID'}$build_extension">[more]</a>~;

I did not test this on spanned pages, it may need an adjustment. Adding this red part above each instance of $links .= &site_html_link (%tmp); would fix it:

$tmp{'Description'} = $words; # < Abbreviated description mod

I also did not test it on any other pages, like Popular, Rated, Search, etc. If this needs to be done, let me know...

This code is modified from some I found posted by Andy in this post in the Gossamer Links forum.


Leonard
aka PerlFlunkie

Last edited by:

PerlFlunkie: Jan 22, 2006, 5:33 PM
Quote Reply
Re: [PerlFlunkie] Quition for <%Description%> In reply to
Well, I did some testing, and I'm having problems with spanning pages. I moved the code to just above...

# If we are spanning pages, we grab the first x number of links and build
# the main index page. We set $numlinks to the remaining links, and we remove
# the links from the list.


...and removed this line from it:

$tmp{'Description'} = $words;

I put that line between these:


%tmp = &array_to_hash ($i, @{$links{$cat}});

$tmp{'Description'} = $words;


$links .= &site_html_link (%tmp);

where it appears (three times) in the routine.

The problem? Depending on which bits I comment out (1, 2, or 3)...

It works on page one, but not on page two...
It works on page two, but not on page one...
It works on both pages, but uses the same description for both...

Anyone have any ideas on fixing this? Pirate


Leonard
aka PerlFlunkie
Quote Reply
Re: [PerlFlunkie] Quition for <%Description%> In reply to
OK, fixed it!

Instead of all the above, put this new sub in db_utils.pl:

Code:

# > Abbreviated Description mod
sub teaser {
# -----------------------------------------------
# This creates a shortened description.
my $msg = $tease;
my $max = 6; # You can set the word count here.
my @cuts = split / /, $msg;

if ($#cuts <= $max) {
return $msg;
}
else {
my $snip;
for (my $i = 0; $i < $max; $i++) {
$snip .= " ". $cuts[$i];
}

$back .= qq~...<a href="$build_detail_url/$tmp{'ID'}$build_extension">[more]</a>~;
return $snip;
}

}
# < Abbreviated Description mod


Then in nph-build.cgi, sub build_category_pages, add these:

Code:

# If we are spanning pages, we grab the first x number of links and build
# the main index page. We set $numlinks to the remaining links, and we remove
# the links from the list.
$numlinks = ($#{$links{$cat}} + 1) / ($#db_cols + 1);
$next = $prev = $links = "";
if (($numlinks > $build_links_per_page) && $build_span_pages) {
$page_num = 2;
$next = $url . "more$page_num$build_extension";
for ($i = 0; $i < $build_links_per_page; $i++) {
%tmp = &array_to_hash ($i, @{$links{$cat}});


# > Abbreviated Description mod
$tease = $tmp{Description};
$tmp{Description} = &teaser;

# < Abbreviated Description mod

$links .= &site_html_link (%tmp);
}
@{$links{$cat}} = @{$links{$cat}}[(($#db_cols+1)*$build_links_per_page) .. $#{$links{$cat}}];
$numlinks = ($#{$links{$cat}}+1) / ($#db_cols + 1);
}
# Otherwise we either only have less then x number of links, or we are not
# splitting pages, so let's just build them all.
else {
for ($i = 0; $i < $numlinks; $i++) {
%tmp = &array_to_hash ($i, @{$links{$cat}});
# > Abbreviated Description mod
$tease = $tmp{Description};
$tmp{Description} = &teaser;

# < Abbreviated Description mod
$links .= &site_html_link (%tmp);
}
}
# Create the main page.
open (CAT, ">$dir/$build_index") or &cgierr ("unable to open category page: $dir/$build_index. Reason: $!");
print CAT &site_html_category;
close CAT;
# Then we go through the list of links and build on the remaining pages.
$prev = $url if ($build_span_pages);
while ($next && $build_span_pages) {
if ($numlinks > $build_links_per_page) {
$next_page = $page_num+1;
$next = $url . "more$next_page$build_extension";
}
else {
$next = "";
}
$links = "";
LINK: for ($i = 0; $i < $build_links_per_page; $i++) {
%tmp = &array_to_hash ($i, @{$links{$cat}});
# > Abbreviated Description mod

$tease = $tmp{Description};
$tmp{Description} = &teaser;

# < Abbreviated Description mod
last LINK if ($tmp{$db_key} eq "");
$links .= &site_html_link (%tmp);
}



That will take care of the category pages. For New and Cool, do this (Rated does not use the description field)...
In sub_build_new_page:


Code:
# Now we go through all the new_links (which are organized by category), and
# build the html in array indexed by date then category.
$total = 0;
CATEGORY: foreach $category (sort keys %new_links) {
LINK: for ($i = 0; $i < ($#{$new_links{$category}}+1) / ($#db_cols + 1); $i++) {
$total++;
%tmp = &array_to_hash ($i, @{$new_links{$category}});
# > Abbreviated Description mod
$tease = $tmp{Description};
$tmp{Description} = &teaser;
# < Abbreviated Description mod
${$link_output{$tmp{'Date'}}}{$category} .= &site_html_link (%tmp) . "\n";
$span_totals{$tmp{'Date'}}++;
}
}




In sub build_cool_page:

Code:

LINK: for ($i = 0; $i < ($#{$cool_links{$category}}+1) / ($#db_cols + 1); $i++) {
$total++;
%tmp = &array_to_hash ($i, @{$cool_links{$category}});
# > Abbreviated Description mod
$tease = $tmp{Description};
$tmp{Description} = &teaser;
# < Abbreviated Description mod

$link_output{$category} .= &site_html_link (%tmp) . "\n";
}



In search.cgi, sub main:

Code:
# Go through each category of links returned, and build the HTML. Store in hash %link_output.
SETOFLINKS: foreach $setoflinks (sort keys %link_results) {
my $hits = ($#{$link_results{$setoflinks}} + 1) / ($#db_cols+1);
LINK: for ($i = 0; $i < $hits; $i++) {
$link_hits++;
if (($link_hits <= $highrange) && ($link_hits >= $lowrange)) {
%tmp = &array_to_hash ($i, @{$link_results{$setoflinks}});
# > Abbreviated Description mod
$tease = $tmp{Description};
$tmp{Description} = &teaser;
# < Abbreviated Description mod


$link_output{$setoflinks} .= &site_html_link (%tmp) . "\n";
}
}
}




If you have any trouble, let me know!


Leonard
aka PerlFlunkie

Last edited by:

PerlFlunkie: Jan 24, 2006, 12:21 PM
Quote Reply
Re: [PerlFlunkie] Quition for <%Description%> In reply to
Hi Leonard !

i will try it today. Can you please give me a hint which tag i should use to put it into the template?

Best,
Karl
Quote Reply
Re: [Karl] Quition for <%Description%> In reply to
You would still use the <%Description%> tag. In the routines above, the description will be shortened to whatever number of words you set, but on your detail page, the full description will appear.


Leonard
aka PerlFlunkie
Quote Reply
Re: [PerlFlunkie] Quition for <%Description%> In reply to
Hi Leonard,

the cutting works fine, but this line does not worl The "more" does no apear.

$back .= qq~...<a href="$build_detail_url/$tmp{'ID'}$build_extension">[more]</a>~;

Best,
Karl
Quote Reply
Re: [Karl] Quition for <%Description%> In reply to
Hi Leonard,

i change the variable $back to $snip, now it works.

I think, this was the error?

Best,
Karl
Quote Reply
Re: [Karl] Quition for <%Description%> In reply to
Yes, that should have been changed too, my bad... Blush


Leonard
aka PerlFlunkie
Quote Reply
Re: [PerlFlunkie] Quition for <%Description%> In reply to
Hi PerlFlunkie,

is there an easy way, not to shorten the description of Priority links?

Best,
Karl
Quote Reply
Re: [Karl] Quition for <%Description%> In reply to
Not tested, but it should look something like this...


Code:

# > Abbreviated Description mod
sub teaser {
# -----------------------------------------------
# This creates a shortened description.
my $msg = $tease;
my $max = 6; # You can set the word count here.
my @cuts = split / /, $msg;
if ($#cuts <= $max) {
return $msg;
}
else {
my $snip;
for (my $i = 0; $i < $max; $i++) {
$snip .= " ". $cuts[$i];
}

$snip .= qq~...<a href="$build_detail_url/$tmp{'ID'}$build_extension">[more]</a>~;

#
return $snip;

if ($tmp{Priority} == Yes) { # Provides full description for Priority links
$snip = $tmp{Description};
return $snip;
}
else {
return $snip;
}


}

}
# < Abbreviated Description mod


Leonard
aka PerlFlunkie
Quote Reply
Re: [PerlFlunkie] Quition for <%Description%> In reply to
Hi PerlFlunkie,

first, thank you for your fast response! I try´d the mod, but it doesend work. With this mod all the descriptions are in full lenght, not only the Priority links.

Best, Karl

Last edited by:

Karl: May 2, 2006, 1:39 PM
Quote Reply
Re: [Karl] Quition for <%Description%> In reply to
Try this...

Code:

if ($tmp{Priority} = 'Yes') { # Provides full description for Priority links
$snip .= $tmp{Description};
return $snip;
}
else {
return $snip;
}


Leonard
aka PerlFlunkie
Quote Reply
Re: [PerlFlunkie] Quition for <%Description%> In reply to
Hi PerlFlunkie,

now everything which has more words than my $max = 6; becomes an priority link with full Description and the [more] apears in the middle of the description after the first 6 words followed by the rest of the text.

Best, Karl

Last edited by:

Karl: May 2, 2006, 11:45 PM
Quote Reply
Re: [Karl] Quition for <%Description%> In reply to
Sorry, I've not been working with code much lately, and my mind is on another project... Try this one:

Code:

# > Abbreviated Description mod
sub teaser {
# -----------------------------------------------
# This creates a shortened description.
my $msg = $tease;
my $max = 6; # You can set the word count here.
my @cuts = split / /, $msg;

if ($tmp{Priority} = Yes) { # Provides full description for Priority links
return $msg;
}

elsif ($#cuts <= $max) {
return $msg;
}
else {
my $snip;
for (my $i = 0; $i < $max; $i++) {
$snip .= " ". $cuts[$i];
}

$snip .= qq~...<a href="$build_detail_url/$tmp{'ID'}$build_extension">[more]</a>~;
return $snip;


}

}
# < Abbreviated Description mod


Should be getting closer...


Leonard
aka PerlFlunkie
Quote Reply
Re: [PerlFlunkie] Quition for <%Description%> In reply to
Hi Leonard,

now all links become Priority links with full description. Smile

Best,
Karl

Last edited by:

Karl: May 3, 2006, 11:54 PM
Quote Reply
Re: [Karl] Quition for <%Description%> In reply to
Refer to post #6 above, and each time you see this:

$tease = $tmp{Description};

add this under it:

$priority = $tmp{Priority};

Now refer to #17, and make i5t look like this:

Code:

my @cuts = split / /, $msg;

if $priority == 'Yes') { # Provides full description for Priority links
return $msg;
}

elsif ($#cuts <= $max) {
return $msg;
}




Leonard
aka PerlFlunkie
Quote Reply
Re: [PerlFlunkie] Quition for <%Description%> In reply to
Hi Leonard, there was a missing ( in if ($priority == 'Yes')

Now all descriptions are full lengt. No more cutting. Strange...

Best, Karl

Last edited by:

Karl: May 4, 2006, 12:09 PM
Quote Reply
Re: [Karl] Quition for <%Description%> In reply to
Good catch of bad copy.
Add this to the top of sub build_category_pages:

local ($description, $related, $meta_name, $meta_keywords, $header, $footer, $category_title, $category_sort, $subcats, $next, $prev, $priority);

Eventually, this WILL work... Pirate


Leonard
aka PerlFlunkie
Quote Reply
Re: [PerlFlunkie] Quition for <%Description%> In reply to
Hi Leonard,

sorry for all the trouble.

Putting the var $priority there does not change the result. Still the same problem. All descriptions are full lenght.

Best,
Karl
Quote Reply
Re: [Karl] Quition for <%Description%> In reply to
Hi Leonard, ill tryd all the code you postet and some of my own variations. Nothing works. I give up now. Many thanks for your help. Best, Karl
Quote Reply
Re: [Karl] Quition for <%Description%> In reply to
Karl,

I will work on this when I get more time, and I will test before posting... I know this can be done, my mind is just not into code at the moment.


Leonard
aka PerlFlunkie