Gossamer Forum
Skip to Content



Home : Products : Links 2.0 : Customization :

Rename all directories to end with "suffix"

(Page 1 of 2)
> >
Quote Reply
Rename all directories to end with "suffix"
Has anyone done this?

I would like links to rename all directories with small cap letters and add a suffix (of my choice) at the end of the directory name. This will help in search engine results.

e.g. If my category.db is as follows:

California
California/San_Diego
California/San_Francisco

Say my suffix is directory, I want the links to create directories like this:

california
california/san_diego_directory
california/san_francisco_directory

I know in order to create categories with small caps, I could rename the categories in categories.db with all small caps (without using first letter capital) but then title linked, etc., will all show up with small caps in the category pages and will not look right.

Thanks
Quote Reply
Re: [socrates] Rename all directories to end with "suffix" In reply to
To make the URLs all lowercase, but keep the category names Capitalized, do this in nph-build.cgi:

sub build_dir {
# --------------------------------------------------------
# Verifies that all neccessary directories have been created
# before we create the category file. Takes as input, the category
# to verify, and returns the full directory path.
my $input = shift;
my ($dir, $path) = '';
my @dirs = split /\//, $input;
my $lowercase =~ tr/[A-Z]/[a-z]/; #change all letters to lowercase
foreach $dir (@dirs) {
$path .= "/$dir";
$path = &lowercase ($path);
&build_check_dir ($build_root_path, $path);
if (! (-e "$build_root_path$path")) {
print "\tMaking Directory ($build_dir_per): '$build_root_path$path' ...";
if (mkdir ("$build_root_path$path", "$build_dir_per")) {;
print "Made. CHMOD ($build_dir_per) ...";
if (chmod ($build_dir_per, "$build_root_path$path")) {;
print "Done.";
}
else { print "CHMOD ($build_dir_per) failed! Reason: $!."; }
}
else { print "mkdir failed! Reason: $!."; }
print "\n";
}
}
return "$build_root_path$path";
}

And in db_utils.pl:

sub urlencode {
# --------------------------------------------------------
# Escapes a string to make it suitable for printing as a URL.
#
my($toencode) = shift;
my $chrs = quotemeta ("/_-");
my $lowercase =~ tr/[A-Z]/[a-z]/; #change all letters to lowercase
$toencode =~ s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/eg;
$toencode =~ s/\%2F/\//g;
$toencode = &lowercase ($toencode);
return $toencode;
}

That should take of it.

Not sure why you want to add a suffix. Do you want different ones for different categories, or one for all? A name that is not included on the page, only in the URL? Adding a suffix that is the same for all pages is easy enough; making it a variable is a bit more involved.


Leonard
The One True Church
(Links-based site using all-CSS, and many mods to the basic script.)
Quote Reply
Re: [PerlFlunkie] Rename all directories to end with "suffix" In reply to
Oops, minor correction... the &'s should be $'s:

This:

$lowercase ($path);
-
$lowercase ($toencode);



Not this:

&lowercase ($path);
-
&lowercase ($toencode);


Blush


Leonard
The One True Church
(Links-based site using all-CSS, and many mods to the basic script.)

Last edited by:

PerlFlunkie: Nov 6, 2005, 1:29 AM
Quote Reply
Re: [PerlFlunkie] Rename all directories to end with "suffix" In reply to
Thanks, it doesn't work.

Error including libraries: syntax error at /home/www/cgi-bin/links/admin/db_utils.pl line 487, near "$lowercase ("
Compilation failed in require at admin.cgi line 29.

Also, are you sure those are the only changes? I didn't think it was as simple as just renaming and thought is much more complicated but if we can get this to work, I will try and see.

With regards to suffix:
Quote:
Do you want different ones for different categories, or one for all?


Let's say my category name in categories.db is "California/San_Francisco"

I just want the same ending apended for all directories and subdirectories. e.g. if the directory name in categories.db is California - I want the directory name as "california_directory" for California/San_Francisco, I want it renamed "california/san_francisco_directory.

Quote:
A name that is not included on the page, only in the URL?

I am not sure what you mean. URL's will of-course show as http://www.site.com/directory/callifornia/san_francisco_directory

However, I want all the category names in category pages to show properly as first letter capital e.g. California, San_Francisco and California > San_Francisco with the Title linked tag, and Links under San Franciso in the category page headings, etc.,

I other words, it will only affect URL's (and directory names) but all printing of name in the pages will not be affected.

Thanks

Last edited by:

socrates: Nov 6, 2005, 10:54 PM
Quote Reply
Re: [socrates] Rename all directories to end with "suffix" In reply to
You're right, there is a bit more involved, but it's not too hard. I was bed-bound when I replied before; this is a bit more thought-out. I had done this for another mod I posted here. The changes in red in the code below are adapted from that, but to only do what you want (lowercase). The changes in blue will add a suffix to the category URLs. [First, undo what was changed in the other post.]

In nph-build.cgi:

sub build_category_pages {
# --------------------------------------------------------
# This routine builds all the category pages. Each category uses
# the same template which is defined in &site_html_category.

my $build_single = shift;
my ($cat, $url, $dir, @related, $relation, $page_num, $next_page, $numlinks);
local ($category, $links, $title_linked, $title, $total, $category_name, $category_name_escaped);
local ($description, $related, $meta_name, $meta_keywords, $header, $footer, $next, $prev);
# Go through each category and build the appropriate page.
CATEGORY: foreach $cat (sort keys %category) {
next CATEGORY if ($cat =~ /^\s*$/); # How'd that get in here? =)
next CATEGORY if ($build_single and ($build_single ne $cat));
my $suffix = '_directory';
$url = "$build_root_url/" . &urlencode($cat) . "$suffix/";
$url =~ tr/[A-Z]/[a-z]/;
$use_html ?
print qq|Building Category: <A HREF="$url" TARGET="_blank">$cat</A>\n| :
print qq|Building Category: $cat\n|;
print "\tSubcategories: " . ($#{$subcategories{$cat}} + 1) . "\n";
print "\tLinks: " . (($#{$links{$cat}}+1) / ($#db_cols+1)) . "\n";
# Let's make sure the directory exists, build it if it doesn't.
$dir = &build_dir ($cat);
print "\tDirectory: $dir\n";
print "\tFilename : $dir/$build_index\n";
# We set up all the variables people can use in &site_html.pl.
($description, $related, $meta_name, $meta_keywords, $header, $footer) = @{$category{$cat}}[2..7];
# Calculate the related entries and put in a <LI> list.
@related = split(/\Q$db_delim\E/, $related); $related = "";
foreach $relation (@related) {
$related .= qq|<li><a href="$build_root_url/|;
$related .= &urlencode($relation);
$related =~ tr/[A-Z]/[a-z]/;
$related .= qq|$suffix/$build_index">|;
$related .= &build_clean($relation);
$related .= "</a></li>";
}
....


sub build_new_page {
# --------------------------------------------------------
# Creates a "What's New" page. Set $build_span_pages to 1 in links.cfg
# and it will create a seperate page for each date.

... edited for space ...

CATEGORY: foreach $category (sort keys %{$link_output{$date}}) {
$category_clean = &build_clean ($category);
my $category2;
$category2 = ($rec{'Category'});

$category2 =~ tr/[A-Z]/[a-z]/;
$link_results .= qq|<P><A HREF="$build_root_url/$category2/$build_index">$category_clean</A>\n|;
$link_results .= ${$link_output{$date}}{$category};
}


sub build_cool_page {
# --------------------------------------------------------
# Creates a "What's Cool" page.
local ($total, $percent, $link_results, $title_linked);
my (%link_output, $category_clean);

if ($build_cool_path =~ m,^$build_root_path/(.*)$,) {
&build_dir ($1);
}

$total = 0;
CATEGORY: foreach $category (sort keys %cool_links) {
LINK: for ($i = 0; $i < ($#{$cool_links{$category}}+1) / ($#db_cols + 1); $i++) {
$total++;
%tmp = &array_to_hash ($i, @{$cool_links{$category}});
$link_output{$category} .= &site_html_link (%tmp) . "\n";
}
}
foreach $category (sort keys %cool_links) {
$category_clean = &build_clean ($category);
my $category2;
$category2 = ($rec{'Category'});
$category2 =~ tr/[A-Z]/[a-z]/;

$link_results .= qq|<P><A HREF="$build_root_url/$category2/$build_index">$category_clean</A>\n|;
$link_results .= $link_output{$category};
}
$title_linked = &build_linked_title ("Cool");
open (COOL, ">$build_cool_path/$build_index") or cgierr ("unable to open what's cool page: $build_cool_path/$build_index. Reason: $!");
print "\tCool Links: $total\n";
($db_popular_cutoff < 1) ?
($percent = $db_popular_cutoff * 100 . "%") :
($percent = $db_popular_cutoff);
print COOL &site_html_cool(@cool_links);
close COOL;
}

sub build_linked_title {
# --------------------------------------------------------
# Returns a string of the current category broken up
# by section, with each part linked to the respective section.
my $input = shift;
my (@dirs, $dir, $output, $path, $last);
my $suffix = '_directory';
@dirs = split (/\//, $input);
$last = &build_clean(pop @dirs);

$output = qq| <A HREF="$build_root_url/">Top</A> :|;
foreach $dir (@dirs) {
$path .= "/$dir$suffix";
$path =~ tr/[A-Z]/[a-z]/;

$dir = &build_clean ($dir);
$output .= qq| <A HREF="$build_root_url$path/">$dir</A> :|;
}
$output .= " $last";

return $output;
}

=======

In search.cgi, change:

sub build_linked_title {
# --------------------------------------------------------
# A little different then the one found in nph-build.cgi as it also
# links up the last field as well.
my ($input) = shift;
my ($dir, $output, $path, $last);
foreach $dir ((split m!/!, $input)) {
$path .= "/$dir$suffix";
$path =~ tr/[A-Z]/[a-z]/;

$dir = &build_clean ($dir);
$output .= qq|<A HREF="$build_root_url$path/">$dir</A>:|;
}


=======

>> Try it without these next parts, see if it works OK. I changes the mod so hopefully it will not affect as much.

In db_utils.pl:

sub urlencode {
# --------------------------------------------------------
# Escapes a string to make it suitable for printing as a URL.
#
my($toencode) = shift;
$toencode =~ s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/eg;
$toencode =~ s/\%2F/\//g;
$toencode =~ tr/[A-Z]/[a-z]/; #May not be required
return $toencode;
}

========

This mod makes the New, Cool , and Rate pages build with lowercase letters, so a few more changes are in order. All templates need to be changed so that they have lowercase letters. Here is the original code:

<a class="menulink" href="<%build_root_url%>/New">What's New</a> |
<a class="menulink" href="<%build_root_url%>/Cool">What's Cool</a> |
<a class="menulink" href="<%build_root_url%>/Ratings">Top Rated</a> |

and the updated code:

<a class="menulink" href="<%build_root_url%>/new">What's New</a> |
<a class="menulink" href="<%build_root_url%>/cool">What's Cool</a> |
<a class="menulink" href="<%build_root_url%>/ratings">Top Rated</a> |


Then, in links.cfg, change the uppercase N, C, and R to lowercase:

# PATH and URL of What's New page. No Trailing slash.
# $build_new_path = "$build_root_path/new";
$build_new_path = "$build_root_path/new";
$build_new_url = "$build_root_url/new";
# PATH and URL of What's Cool page. No Trailing slash.
$build_cool_path = "$build_root_path/cool";
$build_cool_url = "$build_root_url/cool";
# PATH and URL of What's Rating page. No Trailing slash.
$build_ratings_path = "$build_root_path/ratings";
$build_ratings_url = "$build_root_url/ratings";

<< End of try without...

======

I may have missed something, especially for the suffix mod, as I did not really test it. You could probably not put the my $suffix = '_directory'; part into each routine, and instead just put that line in links.cfg. Just seems easier to troubleshoot things when you don't have to hunt around for bits of code. (Now where did I put that "$suffix =" ???) Sly

Hope this works!


Leonard
The One True Church
(Links-based site using all-CSS, and many mods to the basic script.)

Last edited by:

PerlFlunkie: Nov 7, 2005, 12:01 PM
Quote Reply
Re: [PerlFlunkie] Rename all directories to end with "suffix" In reply to
I decided to test this, and here are the changes:

In nph-build.cgi:


To have the $suffix added to the page name (very top of browser window), change the suffix to a capitalized word, and change the $category_name = part...

sub build_category_pages {
...

my $suffix = '_Directory';

...

$cat2 = $cat . $suffix;
$title_linked = &build_linked_title ($cat);
$title = &build_unlinked_title ($cat);
$total = ($#{$links{$cat}} + 1) / ($#db_cols + 1);
$category_name = $cat2;
$category_name_escaped = &urlencode ($cat);
$category_clean = &build_clean ($cat);
...

======

These changes are needed so the New, Cool, and Popular links will still work (and not have the $suffix added to them).

sub build_new_page {
# --------------------------------------------------------
# Creates a "What's New" page. Set $build_span_pages to 1 in links.cfg
# and it will create a seperate page for each date.
#
my (%link_output, $category_clean, $long_date, $category, $date,
$number_links, $main_link_results, $main_total, %span_totals);
local ($total, $link_results, $title_linked);
# Let's build the What's New directory.
if ($build_new_path =~ m,^$build_root_path/(.*)$,) {
&build_dir_2 ($1);
}
...

sub build_cool_page {
# --------------------------------------------------------
# Creates a "What's Cool" page.
local ($total, $percent, $link_results, $title_linked);
my (%link_output, $category_clean);

if ($build_cool_path =~ m,^$build_root_path/(.*)$,) {
&build_dir_2 ($1);
}
...


sub build_rate_page {
# --------------------------------------------------------
# Creates a Top 10 ratings page.
my (@values, $id, $votes, $rate, @top_votes, %top_votes, @top_rate, %top_rate);
local ($top_rated, $top_votes);

if ($build_ratings_path =~ m,^$build_root_path/(.*)$,) {
&build_dir_2 ($1);
}
...


...This makes sure the categories build like we want...


sub build_dir {
# --------------------------------------------------------
# Verifies that all neccessary directories have been created
# before we create the category file. Takes as input, the category
# to verify, and returns the full directory path.
my $input = shift;
my ($dir, $path) = '';
my @dirs = split /\//, $input;
my $suffix = '_directory';
foreach $dir (@dirs) {
$path .= "/$dir$suffix";
$path =~ tr/[A-Z]/[a-z]/;
&build_check_dir ($build_root_path, $path);
...

sub build_dir_2 {
# --------------------------------------------------------
# Verifies that all neccessary directories have been created
# before we create the category file. Takes as input, the category
# to verify, and returns the full directory path.

my $input = shift;
my ($dir, $path) = '';
my @dirs = split /\//, $input;

foreach $dir (@dirs) {
$path .= "/$dir";
&build_check_dir ($build_root_path, $path);
if (! (-e "$build_root_path$path")) {
print "\tMaking Directory ($build_dir_per): '$build_root_path$path' ...";
if (mkdir ("$build_root_path$path", "$build_dir_per")) {;
print "Made. CHMOD ($build_dir_per) ...";
if (chmod ($build_dir_per, "$build_root_path$path")) {;
print "Done.";
}
else { print "CHMOD ($build_dir_per) failed! Reason: $!."; }
}
else { print "mkdir failed! Reason: $!."; }
print "\n";
}
}
return "$build_root_path$path";
}


======

And this makes sure we can link to our new categories.

In db_utils.pl:

sub urlencode {
# --------------------------------------------------------
# Escapes a string to make it suitable for printing as a URL.
#
my($toencode) = shift;
my $suffix = _directory;
$toencode =~ s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/eg;
$toencode =~ s/\%2F/\//g;
$toencode =~ tr/[A-Z]/[a-z]/;
$toencode = $toencode . $suffix;
return $toencode;
}


It works for me, let me know if there are any problems... Wink


Leonard
The One True Church
(Links-based site using all-CSS, and many mods to the basic script.)

Last edited by:

PerlFlunkie: Nov 7, 2005, 4:21 PM
Quote Reply
Re: [PerlFlunkie] Rename all directories to end with "suffix" In reply to
Thanks - close but no cigar!

It builds the directories fine but the url's on homepage and subcategory pages are not printing/pointing properly. I have yahoo subcats mod installed.

My Categories on the homepage look like this:

Arizona
Phoenix, Tuscon ...

and the URL's are
http://site.com/pages/arizona_directory (correct)
http://site.com/pages/arizona/phoenix_directory(incorrect)
http://site.com/pages/arizona/tuscon_directory(incorrect)

California
San Diego, San Francisco ....

http://site.com/pages/california_directory (correct)
http://site.com/pages/california/san_diego_directory(incorrect)
http://site.com/pages/california/san_francisco_directory(incorrect)


NOTICE the suffix missing from URL's in subcategories after arizona and california.

The URL's should be :

http://site.com/pages/california_directory
http://site.com/pages/california_directory/san_diego_directory
http://site.com/pages/california_directory/san_francisco_directory

Thanks a lot.

Last edited by:

socrates: Nov 8, 2005, 1:02 AM
Quote Reply
Re: [socrates] Rename all directories to end with "suffix" In reply to
OK, I think I fixed that... My test site did not have subcategories, so I did not see the problem; it does now.

In db_utils.pl, add a new line to this routine, and put 'single quotes' around _directory (just to be doin'):

sub urlencode {
# --------------------------------------------------------
# Escapes a string to make it suitable for printing as a URL.
#
my($toencode) = shift;
my $suffix = '_directory';
$toencode =~ s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/eg;
$toencode =~ s/\%2F/\//g;
$toencode =~ tr/[A-Z]/[a-z]/;
$toencode =~ s/\//$suffix\//s;
$toencode = $toencode . $suffix;
return $toencode;
}

Now anything that goes through this routine (&urlencode) will be converted to lowercase, and will have $suffix appended to it. If it is a subcategory, $suffix will also be added before each slash (/).

======

Another correction is in nph-build.cgi; in sub build_category_pages, remove this:

# Go through each category and build the appropriate page.
CATEGORY: foreach $cat (sort keys %category) {
next CATEGORY if ($cat =~ /^\s*$/); # How'd that get in here? =)
next CATEGORY if ($build_single and ($build_single ne $cat));
my $suffix = '_Directory';
$url = "$build_root_url/" . &urlencode($cat) . "$suffix/";
$url =~ tr/[A-Z]/[a-z]/;
$use_html ?

and

$related .= qq|<li><a href="$build_root_url/|;
$related .= &urlencode($relation);
$related =~ tr/[A-Z]/[a-z]/;
$related .= qq|$suffix/$build_index">|;
$related .= &build_clean($relation);
$related .= "</a></li>";

Then change this by putting in the line removed above (to keep changes grouped together):

my $suffix = '_Directory';
$cat2 = $cat . $suffix;
$title_linked = &build_linked_title ($cat);
$title = &build_unlinked_title ($cat);
$total = ($#{$links{$cat}} + 1) / ($#db_cols + 1);
$category_name = $cat2;
$category_name_escaped = &urlencode ($cat);
$category_clean = &build_clean ($cat);

======

In the sub build_new_page and sub build_new_page routines, add this:

my $category2;
$category2 = ($rec{'Category'}) . $suffix;
$category2 =~ tr/[A-Z]/[a-z]/;

(That's space-dot-space-$suffix.)

======

Confused yet? Crazy
That's all I see right now, let me know how it
goes...


Leonard
The One True Church
(Links-based site using all-CSS, and many mods to the basic script.)

Last edited by:

PerlFlunkie: Nov 8, 2005, 11:17 AM
Quote Reply
Re: [PerlFlunkie] Rename all directories to end with "suffix" In reply to
Another line to add, in nph-build.cgi, sub build_category_pages:

my $suffix = '_Directory';
$cat2 = $cat . $suffix;
$cat2 =~ s/\//$suffix\//s;
$title_linked = &build_linked_title ($cat);
$title = &build_unlinked_title ($cat);
$total = ($#{$links{$cat}} + 1) / ($#db_cols + 1);
$category_name = $cat2;
$category_name_escaped = &urlencode ($cat);
$category_clean = &build_clean ($cat);

This makes sure the page title (top of browser) maintains the use of $suffix for subcategory pages.


Leonard
The One True Church
(Links-based site using all-CSS, and many mods to the basic script.)
Quote Reply
Re: [PerlFlunkie] Rename all directories to end with "suffix" In reply to
Seems to work fine without the changes in the last post. I am still testing and I sure will post if there are any problems but thanks so far for the help.
Quote Reply
Re: [socrates] Rename all directories to end with "suffix" In reply to
Glad it's workin! Smile

See if your subcategory pages have titles like this:

site-name:california:san_diego_directory

The last bit I posted would make it like so:

site-name:california_directory:san_diego_directory

But I guess it works fine either way, and is not so redundant the first way.


Leonard
The One True Church
(Links-based site using all-CSS, and many mods to the basic script.)
Quote Reply
Re: [PerlFlunkie] Rename all directories to end with "suffix" In reply to
Perlflunkie,

I have a question for you - let's say I want the suffix to show only at the end of subdirectories and not at the end of main directories. e.g. if I have

California
California/San_Diego

as my category and subcategory in categories.db

I want it to build directories (and point to proper URL's) as follows:

http://www.site.com/california
http://www.site.com/california/san_diego_directory

To make it that way, what changes do I need to remove from what I have done so far? I am not worried about the New, Cool and rate pages, just the main directory.

Thanks again.
Quote Reply
Re: [socrates] Rename all directories to end with "suffix" In reply to
Ok, if I have multiple levels of subcategoires, I have the same problem with URL's not pointing properly (as posted in #7) - even after I made the last changes you posted for build_category_pages sub.

If I have only 2 levels it works fine but if I have 3 lievels - e.g.

California
California/Southern_California
California/Southern_California/San_Diego

The URL's in home/category pages show as:

california
california_directory/southern_california_directory/san_diego_directory

The text in red ink is missing.

Thanks

Last edited by:

socrates: Nov 10, 2005, 7:13 PM
Quote Reply
Re: [socrates] Rename all directories to end with "suffix" In reply to
Change:

Code:
$cat2 =~ s/\//$suffix\//s;

to....

Code:
$cat2 =~ s[/][$suffix/]sg;
Quote Reply
Re: [Hargreaves] Rename all directories to end with "suffix" In reply to
That was a typo on my part, that should be the g, you don't need the s. I'm working on your other question...


Leonard
The One True Church
(Links-based site using all-CSS, and many mods to the basic script.)
Quote Reply
Re: [PerlFlunkie] Rename all directories to end with "suffix" In reply to
Hargreaves - thanks $cat2 =~s[/][$suffix/]g; works

Pealflukie - let me know if you have answer for post #10 (add suffix only at the end of the last directory).

Thanks to both!
Quote Reply
Re: [socrates] Rename all directories to end with "suffix" In reply to
Yowza, we're havin' fun now! Cool

You're makin' me study up on my Perl. That's a good thing...

Do this in nph-build.cgi:

sub build_dir {
# --------------------------------------------------------
# Verifies that all neccessary directories have been created
# before we create the category file. Takes as input, the category
# to verify, and returns the full directory path.
my $input = shift; #input is $cat
my ($dir, $path) = '';
my @dirs = split /\//, $input;
my $suffix = '_directory';
$quant = @dirs;
if ($quant > 1) {
$end = $dirs[-1];
$dirs[-1] = $end . $suffix;
}
foreach $dir (@dirs) {
$path .= "/$dir";
$path =~ tr/[A-Z]/[a-z]/;

&build_check_dir ($build_root_path, $path);


sub build_linked_title {
# --------------------------------------------------------
# Returns a string of the current category broken up
# by section, with each part linked to the respective section.
my $input = shift;
my (@dirs, $dir, $output, $path, $last);
my $suffix = '_directory';
@dirs = split (/\//, $input);
$last = &build_clean(pop @dirs);

$output = qq| <A HREF="$build_root_url/">Top</A> :|;
foreach $dir (@dirs) {
$path .= "/$dir$suffix";
$path =~ s/$suffix//; # removes suffix if top-level category
$path =~ tr/[A-Z]/[a-z]/;
$dir = &build_clean ($dir);
$output .= qq| <A HREF="$build_root_url$path/">$dir</A> :|;
}
$output .= " $last";

return $output;
}

========

In db_utils.pl:

sub urlencode {
# --------------------------------------------------------
# Escapes a string to make it suitable for printing as a URL.
#
my($toencode) = shift;
my $suffix = '_directory';
$toencode =~ s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/eg;
$toencode =~ s/\%2F/\//g; # replaces a space with an underscore
$toencode =~ tr/[A-Z]/[a-z]/; # make lowercase
$toencode = $toencode . $suffix; # adds $suffix to end
$toencode =~ s/\//$suffix\//g; # adds $suffix before all /
$toencode =~ s/$suffix//; # removes suffix if top-level category
return $toencode;
}

========

Do NOT do the step in post #9.

Let me know how it goes...


Leonard
The One True Church
(Links-based site using all-CSS, and many mods to the basic script.)
Quote Reply
Re: [PerlFlunkie] Rename all directories to end with "suffix" In reply to
Works fine for upto 2 levels. If we have more tha 2 levels then

1) it adds the suffix to anything after the first level.
2) it builds the subdirectories twice (one with suffix and one without suffix)

e.g. California/Southern_California/San_Diego

Directories built
california
southern_california_directory
san_diego_directory

URL's
http://www.site.com/california
http://www.site.com/california/northern_california_directory
http://www.site.com/california/northern_california_directory/san_diego_diretory

Directories built
california
northern_california
northern_california_directory

It also builds subsubdirectories in the first one (northern_california) and only the pages such as index.html in the other.

I am sure this may be getting confusing and tiring for you now and if so, we will just stop here and if not we will go further

Thanks
Quote Reply
Re: [socrates] Rename all directories to end with "suffix" In reply to
The URL's are building right, the directories need an adjustment. To do what you want will require that Links create multiple directories with similar names. See if you follow...

--

site.com/california

...requires one directory:

Code:
'california'


site.com/california/northern_california_directory

...requires two directories:

Code:
'california'
|
|_ 'northern_california_directory'



site.com/california/northern_california_directory/san_diego_directory

...requires four directories:

Code:
'california'
| |_'northern_california_directory'
|
|_ 'northern_california'
|
|_ 'san_diego_directory'



Do you see what I mean? This can be done easy enough, but keep in mind that whatever is in 'northern_california_directory' will also be in 'northern_california' which will eat up more disk space. It will also require careful attention to how the category links are built.

I'm not sure why your directories are building like...

california
northern_california
northern_california_directory

Mine does not do that, but rather builds as it should...

california
northern_california_directory

Verify the code changes. You can post the changed subroutines here (not the whole file) if you want me to look 'em over, or send me a PM.


Leonard
The One True Church
(Links-based site using all-CSS, and many mods to the basic script.)

Last edited by:

PerlFlunkie: Nov 15, 2005, 6:13 PM
Quote Reply
Re: [socrates] Rename all directories to end with "suffix" In reply to
Well, I added a sub-sub category, and got the same results you did. Here's the fix in nph-build.cgi:

sub build_dir {
# --------------------------------------------------------
# Verifies that all neccessary directories have been created
# before we create the category file. Takes as input, the category
# to verify, and returns the full directory path.
my $input = shift; #input is $cat
my ($dir, $path) = '';
my $suffix = '_directory';
my @dirs = split /\//, $input;
$first = $dirs[0];
foreach $dir(@dirs) {
$dir = $dir . $suffix;
}

$dirs[0] = $first;
foreach $dir (@dirs) {
$path .= "/$dir";
$path =~ tr/[A-Z]/[a-z]/;
&build_check_dir ($build_root_path, $path);



sub build_linked_title {
# --------------------------------------------------------
# Returns a string of the current category broken up
# by section, with each part linked to the respective section.
my $input = shift;
my (@dirs, $dir, $output, $path, $last);
my $suffix = '_directory';
@dirs = split (/\//, $input);
my $quant = @dirs;
$last = &build_clean(pop @dirs);
if ($quant > 1) {
$last = $last . " Directory";

$first = $dirs[0];
foreach $dir(@dirs) {
$dir = $dir . $suffix;
}
$dirs[0] = $first;
}


$output = qq| <A HREF="$build_root_url/">Top</A> :|;
foreach $dir (@dirs) {
$path .= "/$dir";
$path =~ tr/[A-Z]/[a-z]/;
$dir =~ s/$suffix//g;
$dir = &build_clean ($dir);
$output .= qq| <A HREF="$build_root_url$path/">$dir</A> :|;
}
$output .= " $last";

return $output;
}


Leonard
The One True Church
(Links-based site using all-CSS, and many mods to the basic script.)
Quote Reply
Re: [PerlFlunkie] Rename all directories to end with "suffix" In reply to
Thanks - builds directories ok but problems with the URL's again on home and category pages (doesn't add suffix to the end of the URL).
Quote Reply
Re: [socrates] Rename all directories to end with "suffix" In reply to
Let's see what you have for these altered subs:

In db_utils.pl: sub urlencode

In nph-build.cgi: sub build_dir and sub build_linked_title


Leonard
The One True Church
(Links-based site using all-CSS, and many mods to the basic script.)
Quote Reply
Re: [PerlFlunkie] Rename all directories to end with "suffix" In reply to
Code:
sub build_dir {
# --------------------------------------------------------
# Verifies that all neccessary directories have been created
# before we create the category file. Takes as input, the category
# to verify, and returns the full directory path.

my $input = shift;
my ($dir, $path) = '';
my $suffix = '_directory';
my @dirs = split /\//, $input;

##### changed to build directories with suffix
$first = $dirs[0];
foreach $dir(@dirs) {
$dir = $dir . $suffix;
}
$dirs[0] = $first;
##### end change

foreach $dir (@dirs) {
$path .= "/$dir";
##### changed to build directories with suffix
$path =~ tr/[A-Z]/[a-z]/;
##### end change
&build_check_dir ($build_root_path, $path);
if (! (-e "$build_root_path$path")) {
print "\tMaking Directory ($build_dir_per): '$build_root_path$path' ...";
if (mkdir ("$build_root_path$path", "$build_dir_per")) {;
print "Made. CHMOD ($build_dir_per) ...";
if (chmod ($build_dir_per, "$build_root_path$path")) {;
print "Done.";
}
else { print "CHMOD ($build_dir_per) failed! Reason: $!."; }
}
else { print "mkdir failed! Reason: $!."; }
print "\n";
}
}
return "$build_root_path$path";
}

sub build_linked_title {
# --------------------------------------------------------
# Returns a string of the current category broken up
# by section, with each part linked to the respective section.
my $input = shift;
my (@dirs, $dir, $output, $path, $last);
my $suffix = '_directory';
@dirs = split (/\//, $input);
my $quant = @dirs;
$last = &build_clean(pop @dirs);
if ($quant > 1) {
$last = $last . " Directory";
$first = $dirs[0];
foreach $dir(@dirs) {
$dir = $dir . $suffix;
}
$dirs[0] = $first;
}

$output = qq|<A HREF="$build_root_url/">Home</A> |;
foreach $dir (@dirs) {
$path .= "/$dir";
$path =~ tr/[A-Z]/[a-z]/;
$dir =~ s/$suffix//g;
$dir = &build_clean ($dir);
$output .= qq| <A HREF="$build_root_url$path/">$dir</A> :|;
}
$output .= " $last";

return $output;
}

sub urlencode {
# --------------------------------------------------------
# Escapes a string to make it suitable for printing as a URL.
#
my($toencode) = shift;
##### changed to convert to lowercase directories (need only if building all levels)
#my $suffix = '_directory';
$toencode =~ s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/eg;
$toencode =~ s/\%2F/\//g;
$toencode = $toencode . $suffix;
#$toencode =~s[/][$suffix/]g;
#$toencode =~ tr/[A-Z]/[a-z]/;
return $toencode;
}

Last edited by:

socrates: Nov 22, 2005, 6:16 PM
Quote Reply
Re: [socrates] Rename all directories to end with "suffix" In reply to
First we need to clarify just what you're aiming for. As pointed out above, having $suffix attached ONLY to the last category in a system over two-levels deep (cat-level-1, sub-level-2, sub-sub-level-3-$suffix) will require redundant directories, and is not how I have coded this mod. It can be done, of course.

The way this is set up, anything below cat-level-1 will have the $suffix attached to it, which will show up in the URL. It works on my test site, using the code below:

Code:

sub urlencode {
# --------------------------------------------------------
# Escapes a string to make it suitable for printing as a URL.

my($toencode) = shift;
my $suffix = '_directory';


$toencode =~ s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/eg;
$toencode =~ s/\%2F/\//g; # replaces / for a space

$toencode =~ tr/[A-Z]/[a-z]/; # converts to lowercase
$toencode = $toencode . $suffix; # attaches suffix to end
$toencode =~ s/\//$suffix\//g; # attaches suffix ahead of all /
$toencode =~ s/$suffix//; # removes first occurance of $suffix

return $toencode;
}


This line:

$toencode =~ s/$suffix//; # removes first occurance of $suffix

is important, as it removes the suffix from the top-level category. Your other code looks OK; use the above in place of what you have, rebuild, and see if all the links work right. If yes, then we are on the same page!




The code below might work to do what you actually want; add $suffix ONLY to the end, no matter how many levels deep. It should build the redundant categories I mentioned. I DID NOT TEST THIS CODE!

Code:

sub build_dir {
# --------------------------------------------------------
# Verifies that all neccessary directories have been created
# before we create the category file. Takes as input, the category
# to verify, and returns the full directory path.

my $input = shift;
my ($dir, $path) = '';
my $suffix = '_directory';
my @dirs = split /\//, $input;

##### changed to build directories with suffix
$first = $dirs[0];
foreach $dir(@dirs) {
$dir = $dir . $suffix;
}
$dirs[0] = $first;
##### end change

foreach $dir (@dirs) {
$path .= "/$dir";
##### changed to build directories with suffix
$path =~ tr/[A-Z]/[a-z]/;
##### end change
&build_check_dir ($build_root_path, $path);
if (! (-e "$build_root_path$path")) {
print "\tMaking Directory ($build_dir_per): '$build_root_path$path' ...";
if (mkdir ("$build_root_path$path", "$build_dir_per")) {;
print "Made. CHMOD ($build_dir_per) ...";
if (chmod ($build_dir_per, "$build_root_path$path")) {;
print "Done.";
}
else { print "CHMOD ($build_dir_per) failed! Reason: $!."; }
}
else { print "mkdir failed! Reason: $!."; }
print "\n";
}
}

##### build second directory #####
$last = $dirs[-1];
$last = $last . $suffix;
$dirs[-1] = $last;

foreach $dir (@dirs) {
$path .= "/$dir";
##### changed to build directories with suffix
$path =~ tr/[A-Z]/[a-z]/;
##### end change
&build_check_dir ($build_root_path, $path);
if (! (-e "$build_root_path$path")) {
print "\tMaking Directory ($build_dir_per): '$build_root_path$path' ...";
if (mkdir ("$build_root_path$path", "$build_dir_per")) {;
print "Made. CHMOD ($build_dir_per) ...";
if (chmod ($build_dir_per, "$build_root_path$path")) {;
print "Done.";
}
else { print "CHMOD ($build_dir_per) failed! Reason: $!."; }
}
else { print "mkdir failed! Reason: $!."; }
print "\n";
}
}

##### end build second directory #####

return "$build_root_path$path";
}


#################

sub build_linked_title {
# --------------------------------------------------------
# Returns a string of the current category broken up
# by section, with each part linked to the respective section.
my $input = shift;
my (@dirs, $dir, $output, $path);
my $suffix = '_directory';

$output = qq|<A HREF="$build_root_url/">Home</A> |;
foreach $dir (@dirs) {
$path .= "/$dir";
$path =~ tr/[A-Z]/[a-z]/;
$dir = &build_clean ($dir);
$output .= qq| <A HREF="$build_root_url$path/">$dir</A> :|;
}
$output .= " $last . $suffix";

return $output;
}

################


sub urlencode {
# --------------------------------------------------------
# Escapes a string to make it suitable for printing as a URL.

my($toencode) = shift;
my $suffix = '_directory';


$toencode =~ s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/eg;
$toencode =~ s/\%2F/\//g; # replaces / for a space

$toencode =~ tr/[A-Z]/[a-z]/; # converts to lowercase
$toencode = $toencode . $suffix; # attaches suffix to end



return $toencode;
}


Leonard
The One True Church
(Links-based site using all-CSS, and many mods to the basic script.)
Quote Reply
Re: [PerlFlunkie] Rename all directories to end with "suffix" In reply to
I don't know why, but I tested some more, and I find that the code below will build build what you want:


Code:

California
| | |
| | |_California/Southern_California
| | |
| | |_California/Southern_California/San_Diego directory
| | |
| | |_index.html
| |
| |_California/Southern_California directory
| |
| |_index.html
|
|_index.html



The trick is how you link to the categories, but I'm sure what is already in this thread will work.


Code:

sub build_dir {
# --------------------------------------------------------
# Verifies that all neccessary directories have been created
# before we create the category file. Takes as input, the category
# to verify, and returns the full directory path.

my $input = shift;
my ($dir, $path) = '';
my $suffix = '_directory';

my @dirs = split /\//, $input;






my $quant = @dirs;
my $end = $dirs[-1];
foreach $dir(@dirs) {

if ($quant > 1) {
$dirs[-1] = $end . $suffix;
}
}



foreach $dir (@dirs) {
$path .= "/$dir";
$path =~ tr/[A-Z]/[a-z]/;

&build_check_dir ($build_root_path, $path);
if (! (-e "$build_root_path$path")) {
print "\tMaking Directory ($build_dir_per): '$build_root_path$path' ...";
if (mkdir ("$build_root_path$path", "$build_dir_per")) {;
print "Made. CHMOD ($build_dir_per) ...";
if (chmod ($build_dir_per, "$build_root_path$path")) {;
print "Done.";
}
else { print "CHMOD ($build_dir_per) failed! Reason: $!."; }
}
else { print "mkdir failed! Reason: $!."; }
print "\n";
}
}
return "$build_root_path$path";

}


Leonard
The One True Church
(Links-based site using all-CSS, and many mods to the basic script.)
> >