Gossamer Forum
Home : Products : DBMan : Customization :

Automatically convert to URL

Quote Reply
Automatically convert to URL
Heya. I've been working on a MOD similar in function to the naughty word MOD, in where each word is checked to see if it matches the format of a URL (starts with http:/ or www). If that is true, to convert the "word" to HTML URL code (<a href="$word">$word</A>. So, when an URL is listed in the middle of a description, the person entering it doesn't need to know HTML code at all; the system will do it for them.

Now, I've hacked up an attempt at doing this, but have failed miserably. I've even tried to modify the naughty word MOD, to no avail. I know it's a simple comparitor used in the validate_record subroutine, but the solution eludes me. Any help?

--Lee
Quote Reply
Re: Automatically convert to URL In reply to
Nice idea! I am sure others will use it when you come up with a solution...

One suggestion...rather than focusing on the validate_record sub in the db.cgi (which with your approach, the HTML codes will be put in the database file, which will fill up your database quickly with extraneous data), you should consider focusing in the html.pl file in the sub html_record and/or sub html_record_long routine.

Where you would change plain text in record fields to links.

Just a suggestion.

Regards,

------------------
Eliot Lee....
Former Handle: Eliot
* Check Resource Center
* Search Forums
Quote Reply
Re: Automatically convert to URL In reply to
<G> yeah it's a great idea, and your ideas are helpful. But the problem overall is making me feel stupid. I am better at programming in C/C++; PERL is OK, but I'm not so hot with the syntax.

Let me throw a little snippet here, let me know what you think of it. (This is where I kinda get lost...)

@urls = ('http*', 'https*', '*@*');
$header = '<A HREF="';
$middle = '">';
$closer = '</A>';

foreach $URL_string(@urls) {
if (lc($in{$col}) =~ /$URL_string/) {
$in{$col} =~ s/$header/$URL_string/$middle/$URL_string/$closer/ig;
}
}


I am quite certain that code won't work. I'm not exactly sure why it won't. I know it won't work in the way you suggested, Eliot, but this is what I had been toying with. I imagine instead of this, which would do as you mentioned and clutter up the DB, it would instead be a routine that reads each record from the database and compares against the criteria before sending to the output. But that seems like it would slow down the system, especially for larger records, as you would have to check every word before you send it. It might be simpler (yeah I know, not necesarilly better) to change the codes in the databse file. It would make retrieving them faster.

(catching breath) So, any bright ideas? I really appreciate any help...
Quote Reply
Re: Automatically convert to URL In reply to
 
Quote:
I am better at programming in C/C++; PERL is OK, but I'm not so hot with the syntax.

AND

Quote:
I know it won't work in the way you suggested, Eliot, but this is what I had been toying with.

Based on these two statements, HOW DO YOU KNOW IT WON'T WORK??!?!?!?!

Your syntax is not even aligned with the overall syntax structure of DBMAN.

Best of luck! I was only trying to help. Hope you find the answer!

(And using grep BTW would make it just as fast as parsing data from the database file.)

Wink

Regards,

------------------
Eliot Lee....
Former Handle: Eliot
* Check Resource Center
* Search Forums
Quote Reply
Re: Automatically convert to URL In reply to
LOL. Well, Yeah, I know they kinda controdict each other. I know the syntax doesn't work because I've tried it already. *grin* I've tried a couple variations of it as well. I have absolutely no problem with the code IF I know EXACTLY what I'm matching. ie., if this = this. It's when I try to match this = th*. Well, yes, "this" starts with th, so that would be true! But the string I am trying to use, at least in the way I've tried using it so far, tries to make an exact match for the * character, instead of using it as a wildcard.

*sigh* I'm still working on it, and I'll definately post it once I get it. Smile

Oh yeah... and I just worded my reply wrong. I meant to say, the code snipette I included would not work the way you suggested. I understand what you're saying, though, and it is a very good idea. I've shifted gears a bit towards that idea.

[This message has been edited by leisurelee (edited March 08, 2000).]
Quote Reply
Re: Automatically convert to URL In reply to
Okay...I understand now what you meant in your Reply...

Wink

(No hard feelings...)

First of all, your syntax for the wildcard character is wrong...(as you have realized).

The code for wildcards should be:

Code:
(\*+)

So the codes you for the url array would be the following:

Code:
@urls = ('http(\*+)', 'https(\*+)', '(\*+)@(\*+)');

Also, if you are placing this in the sub-routines I suggested, then you should the record hash codes, like the following:

Code:
foreach $URL_string(@urls) {
if (lc($rec{$column}) =~ /$URL_string/) {
$rec{$column} =~ s/$header/$URL_string/$middle/$URL_string/$closer/ig;
}
}

Hope this helps.

Regards,

------------------
Eliot Lee....
Former Handle: Eliot
* Check Resource Center
* Search Forums
Quote Reply
Re: Automatically convert to URL In reply to
Okay... I've got it ALMOST working. *laugh*

Here's my snippette:

Code:
<A HREF="http">http</A>://www.url.com

How do I, when I first locate the http, assign the entire word (full address) to a variable?

Quote Reply
Re: Automatically convert to URL In reply to
Like I mentioned before you need to use the $rec (record hash) codes, to replace the text in the URL link codes.

Regards,

------------------
Eliot Lee....
Former Handle: Eliot
* Check Resource Center
* Search Forums
Quote Reply
Re: Automatically convert to URL In reply to
Perhaps I'm denser than a dark-matter inversion, but I swore I tried that. Either I had my coding wrong (took a few tries to get it where it is now), or it didn't work, thus why I reverted back to $in.

Now, maybe we have our wires crossed or something, but it DOES replace the code as I would like it to. It just doesn't replace enough of it. It does a literal match for @urls variables ('http', 'https') and ONLY converts that part of the code to an href.

What I'd like it to do is, when the routine processes the data stream and it sees "http", to recognise it as a valid pattern, clip the entire word (http AND the entire address after it) and convert it into an HREF. Right now, it only converts the pattern, http. I don't know if it's because I'm not using $rec, or because an non-alpha-numeric character (://) is terminating the word prematurely, or what.

I'll give the $rec another go when I get back to the office in the morning. I'll post the end results here, and hopefully it will be positive. Smile

Thanks again!

--Lee
Quote Reply
Re: Automatically convert to URL In reply to
Our channels are NOT crossed, you need to use the record hash in order to replace the regular values that include http with the link codes.

For example:

Code:
foreach $column (@db_cols) {
$rec{$column} =~ s/~~/, /g;
}

Translation:

For every occurrence of ~~ in the fields, replace it with a , and space.

Regards,


------------------
Eliot Lee....
Former Handle: Eliot
* Check Resource Center
* Search Forums
Quote Reply
Re: Automatically convert to URL In reply to
Good job...I am sure that Gossamer Threads script users and general programmers will find this modification useful. It has been requested at least three times in the past two months in this Forum alone.

And I know it has been requested by LINKS users as well.

Good job!

And you should document the Mod and submit it to the Resource Center. Yet I would recommend making the instructions a little bit more clearer...For example:

Code:
#######################################
# BEGIN Addition #
# Add the following code. You #
# will need to take out the second #
# closing } after the above line, #
# so there is only one }, as shown. #
#######################################
foreach $URL_string (@urls) {
if (lc($in{$col}) =~ /$URL_string/) { # Checks to see if any addresses are in the column
$add1 = (lc($in{$col})); # Gets the entire column record for searching
if ($add1 =~ /$URL_string*(\S+)/i) { # Locates the ENTIRE URL in the string
$replace = "<A HREF=$URL_string$1>$URL_string$1</A>"; # Replacement HTML code
$instance = "$URL_string$1"; # Combines the @urls match to the rest of the address
}
$in{$col} =~ s/$instance/$replace/ig; # Replaces the text address with the HTML code
}
}
}
#######################################
# END Addition #
#######################################

AFTER the following codes:

Code:
push (@input_err, "$col (Invalid date format)") unless &date_to_unix($in{$col}); }

AND BEFORE the following codes:

Code:
if ($#input_err+1 > 0) { # since there are errors, let's build

Regards,

------------------
Eliot Lee....
Former Handle: Eliot
* Check Resource Center
* Search Forums
Quote Reply
Re: Automatically convert to URL In reply to
Okay, I have a functional script now, that writes the code into the flatfile with the record. I will work next on putting it into the html.pl file, to grep the output. But for now...

...and here we go!

In default.cfg, add the following line somewhere in the code:
Code:
@urls = ('http', 'https', 'ftp://');

* You can add or remove any header codes you want. the :// on ftp is a panic code, in case you have "ftp" anywhere else in your text. (i.e., check out my ftp site at...)



In DB.CGI, replace sub validate_record() code with the following:
Code:
push (@input_err, "$col (Invalid date format)") unless &date_to_unix($in{$col});
}
#######################################
# BEGIN Addition #
# Add the following code. You #
# will need to take out the second #
# closing } after the above line, #
# so there is only one }, as shown. #
#######################################

foreach $URL_string (@urls) {
if (lc($in{$col}) =~ /$URL_string/) { # Checks to see if any addresses are in the column
$add1 = (lc($in{$col})); # Gets the entire column record for searching
if ($add1 =~ /$URL_string*(\S+)/i) { # Locates the ENTIRE URL in the string
$replace = "<A HREF=$URL_string$1>$URL_string$1</A>"; # Replacement HTML code
$instance = "$URL_string$1"; # Combines the @urls match to the rest of the address
}
$in{$col} =~ s/$instance/$replace/ig; # Replaces the text address with the HTML code
}
}
}
#######################################
# END Addition #
#######################################
if ($#input_err+1 > 0) { # since there are errors, let's build
...

This works for web addresses only, currently, but can be easily modified for other matches. For example, we have an internal database system at work that I could link to, because the records in that database have a common address. (eg., http://internal.hp.com/data/BPR01121.html, where BPR01121 is the document number, and internal.hp.com/data/ is the common address.) For instances like this, change the following lines:

Code:
@urls = ('http', 'BP', 'ftp');

...

if ($add1 =~ /BP*(\S+)/i) {
$replace = "<A HREF=http://internal.hp.com/data/BP$1.html>Document BP$1</A>";
$instance = "$URL_string$1";
}
if ($add1 =~ /http*(\S+)/i) {
$replace = "<A HREF=$URL_string$1>$URL_string$1</A>";
$instance = "$URL_string$1";
}
if ($add1 =~ /ftp*(\S+)/i) {
$replace = "<A HREF=$URL_string$1>$URL_string$1</A>";
$instance = "$URL_string$1";
}
...

* Notice that because I am looking for a specific URL type, I had to break them into three different matches. Otherwise you will mess up the output. Before, everything is treated the same.

I hope this is helpful, and not too confusing. I can make this into a MOD document, and perhaps someone will find this useful. Smile


[This message has been edited by leisurelee (edited March 09, 2000).]
Quote Reply
Re: Automatically convert to URL In reply to
Okay, stop the traffic.

One BIG flaw of this routine is that it redo's the linking whenever you modify the record. This is bad. The modified sub validate_record needs to be copied, and renamed something like sub validate_add_record, and should only be called when adding records.
Quote Reply
Re: Automatically convert to URL In reply to
Got some code for ya that may work:

Code:
foreach $column (@db_cols) {
$rec{$column} =~ s/([\w]+:\/\/[\w-?&;#~=\.\/]+[\w])/<A HREF="$1" target=\"new\">$1<\/A>/g;
$rec{$column} =~ s/(mailto:[\w-?@&;#~=\.\/]+[\w])/<A HREF="$1" target=\"new\">$1<\/A>/g;
}

Put these codes in the sub html_record routine.

Regards,

------------------
Eliot Lee....
Former Handle: Eliot
* Check Resource Center
* Search Forums
Quote Reply
Re: Automatically convert to URL In reply to
Wow. I'm impressed. :-) Works like a charm! Thank you, bud!
Quote Reply
Re: Automatically convert to URL In reply to
Well, Eliot, it works peachy. Unless the text you are searching for is in an URL.

example:

searching for "hp"

finds it in "http://www.hp.com"

the URL is there, up to hp. so you get:

Code:
<A HREF="http://www.">http://www.</A>hp.com

you click the link, and naturally you get an error.

Now, whenever a search is done, the regular DBman bolds the text found. I've added to that, to bold and change the bgcolor of the text to yellow, to make it very obvious where the matches are.

If that code is removed, the link works fine, but the search match would not be indicated (bolded or highlighted). They like the yellow. Very annoying. Wink

How might you get around this problem?

Thanks!


----------------------------------------
Lee Leisure
HP Customer Relations Manager
----------------------------------------
* NOTE: This message is intended
for personal purposes only, and does
not imply the position or opinion of
the Hewlett Packard Company.


[This message has been edited by leisurelee (edited March 15, 2000).]

[This message has been edited by leisurelee (edited March 16, 2000).]
Quote Reply
Re: Automatically convert to URL In reply to
I had a thought... is there a way that the autoURLing could be applied to the database BEFORE the search is performed? That would, I believe, resolve the above problem, would it not?

------------------
----------------------------------------
Lee Leisure
HP Customer Relations Manager
----------------------------------------
* NOTE: This message is intended
for personal purposes only, and does
not imply the position or opinion of
the Hewlett Packard Company.