Gossamer Forum
Quote Reply
converting text to HTML
I'm setting up an articles database with LinksSQL. I am using the "details" page as the actual article. When I input an HTML file as the article, it displays perfectly. But when I input a straight text file, with no HTML, the text gets all squished together and the article is impossible to read.

Is there anyway to convert the text input to HTML or is there another way to make these articles display properly without having to edit everyone's submissions?

I'm still searching through the forums for an answer, but if someone could point me in the right direction, I'd appreciate it!

Here is the site so far:

http://216.150.136.224/articles/
Quote Reply
Re: [renken] converting text to HTML In reply to
Simplest way to do something like that, is replacing newlines with <br> tags. This should do it;

Great a new global called; nl_2_br

In this global, put the following code;

Code:
sub {
my $tmp = $_[0];
$tmp = ~s|\n|<br>|g;
return $tmp;
}

You then call it with;

<%nl_2_br($Description)%>

So this would change,

Quote:
A test yay!
A test yay!
A test yay!
A test yay!
A test yay!
A test yay!
A test yay!

..into;

Quote:
A test yay!<br>
A test yay!<br>
A test yay!<br>
A test yay!<br>
A test yay!<br>
A test yay!<br>
A test yay!<br>

Enjoy :)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] converting text to HTML In reply to
Don't forget the Windows users:
Code:
sub {
my $tmp = $_[0];
$tmp = ~s|\r?\n|<br>|g;
return $tmp;
}

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] converting text to HTML In reply to
Unfortunately, this did not work, it's spitting back a number rather than the text:

http://216.150.136.224/...les/Detailed/17.html
Quote Reply
Re: [renken] converting text to HTML In reply to
I don't know what's happening there, but here is a Perl one-liner, which works:
Under Windows:
Code:
perl -wle "my $a=\"aaa\r\nbbb\r\n\"; $a =~ s|\r?\n|<br>|g; print $a;"
Input is: aaa\r\nbbb\r\n
Result is: aaa<br>bbb<br>


Under Unix:
Code:
perl -wle 'my $a="aaa\r\nbbb\r\n"; $a =~ s|\r?\n|<br>|g; print $a;'
Input is: aaa\r\nbbb\r\n
Result is: aaa<br>bbb<br>

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [renken] converting text to HTML In reply to
Probably the problem is that the = ~ is divided by space, instead of using =~

Try:
Code:
sub {
my $tmp = $_[0];
$tmp =~ s|\r?\n|<br>|g;
return $tmp;
}

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [renken] converting text to HTML In reply to
Hi renken

You might want to have a look at installing a WYSIWYG editor depending on your needs.

The following post may give you an idea of whats required.

http://gossamer-threads.com/perl/gforum/gforum.cgi?post=271405

Regards

minesite
Quote Reply
Re: [minesite] converting text to HTML In reply to
I'd like to be able to have wyswyg as admin when I add descriptions, but not let users have it, is that possible?
Quote Reply
Re: [loxly] converting text to HTML In reply to
Hi loxly

It is possible, did you follow the link above.

Regards

minesite

Last edited by:

minesite: Mar 18, 2005, 5:23 PM
Quote Reply
Re: [webmaster33] converting text to HTML In reply to
Thank you so much! It works, it was that space! Thank you!
Quote Reply
Re: [webmaster33] converting text to HTML In reply to
After much fiddling around, we even tried a WYSIWIG editor, we're back to this little script. However, wondering if anyone can help with another bit of code.

When you input HTML, now it forces breaks where we don't want them. How can we tell it that if it encounters the tag <html> that it shouldn't use that other little script?

Here is what we tried in the global and it doesn't work (in fact it crashes the whole thing):

sub {
my $tmp = $_[0];
if( substr($tmp,0,6) eq "<html>")
{}
else{
$tmp =~ s/\n\/<br>/g;
}
return $tmp;
}
Quote Reply
Re: [renken] converting text to HTML In reply to
This should do it =)

Code:
sub {
my $tmp = $_[0];
if ($tmp =~ /\Q<html>/si) {
} else{
$tmp =~ s/\n/<br>/g;
}
return $tmp;
}

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!

Last edited by:

Andy: Mar 25, 2005, 2:58 AM
Quote Reply
Re: [Andy] converting text to HTML In reply to
Quote:
This should do it =)

I doubt it. Your version contains the same bug that Renken's does.

1) You use \Q<html> ....\Q is for excaping meta-characters of which there are none in <html> so \Q is redundant.

2) The use of an empty block { } shows poor logic. The code would be far better written with an "unless" which would reduce the code length by two lines.

3) The main bug....why are you escaping the middle forward slash in the substitution replacement?

A corrected version would be:

Code:
sub {
my $tmp = $_[0];
unless ($tmp =~ /<html>/si) {
$tmp =~ s/\r?\n/<br>/sg;
}
return $tmp;
}

Of course, if the <html> tag is guaranteed to be at the beginning of the string and will always be lowercase then Renken's use of substr() is a better idea.[/code]
Quote Reply
Re: [Optical] converting text to HTML In reply to
Hi,

Quote:
1) You use \Q<html> ....\Q is for excaping meta-characters of which there are none in <html> so \Q is redundant.

Not really :P \Q will still work fine. It just stops ANY characters from being processed in any other form than its intended.

Quote:
2) The use of an empty block { } shows poor logic. The code would be far better written with an "unless" which would reduce the code length by two lines.

Agree on that one. I typed that global VERY early ;)

Quote:
3) The main bug....why are you escaping the middle forward slash in the substitution replacement?

Commonly known as a TYPO ;)

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!

Last edited by:

Andy: Mar 25, 2005, 3:00 AM
Quote Reply
Re: [Andy] converting text to HTML In reply to
Quote:
Not really :P \Q will still work fine.

Yes it will not cause an error but it's serving no purpose.

Quote:
It just stops ANY characters from being processed in any other form than its intended.

No, only meta-characters, which doesn't include angled brackets < >


Last edited by:

Optical: Mar 25, 2005, 3:30 AM
Quote Reply
Re: [Optical] converting text to HTML In reply to
No need to treat the whole input as single line.
Here is a shorter code:


Code:
perl -wle "my $a=qq|aaa\r\nbb<hTmL>bbb\r\nccc\r\n|;
$a !~ m|<html>|i && $a =~ s|\r?\n|<br>|g; print $a;"
Result:
Quote:
aaa
bb<hTmL>bbb
ccc
This is perfect, as we don't want newline converting, when <html> is available.


The next example is same code, with different input, with <html> missing:
Code:
perl -wle "my $a=qq|aaa\r\nbbbbb\r\nccc\r\n|;
$a !~ m|<html>|i && $a =~ s|\r?\n|<br>|g; print $a;"
Result:
Quote:
aaa<br>bbbbb<br>ccc<br>
This is perfect, as we want newline converting, when <html> is missing.

Further optimization:
Code:
perl -wle "$_=qq|aaa\r\nbbbbb\r\nccc\r\n|; ! m|<html>|i && s|\r?\n|<br>|g; print $_;"



So the final one-liner short global for newline to HTML converting (with <html> exception) should be:
Code:
sub {
return !m|<html>|i && s|\r?\n|<br>|g;
}
This final global is not tested, but based on pre-tests, it should work.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] converting text to HTML In reply to
Quote:
Here is a shorter code:
It's the same as the previous examples except you've just removed the if/unless block, which for the purposes of explanation to Renken and Andy, wasn't my goal as it would have just obfuscated the code making it harder to understand.
Quote:
Further optimization: perl -wle "$_=qq|aaa\r\nbbbbb\r\nccc\r\n|; ! m||i && s|\r?\n| |g; print $_;"
It's not optimized, just shortened, which isn't the same thing. Plus, if I were optimizing it I would have just done print; instead of print $_; Cool
Quote:
So the final one-liner short global for newline to HTML converting (with exception) should be: sub { return !m||i && s|\r?\n| |g; } This final global is not tested, but based on pre-tests, it should work.
Unfortunately it won't. You are trying to perform a regex against $_, which isn't defined so the regex replacing \r?\n with will always be executed, as ! m||i will always be true. Secondly, you are not returning what you think you are. Your "return" is actually returning the success or failure of the regex (or the number of substitutions) and not the html. If we were playing a game of who can write the shortest global, then I'd use:
Code:
sub { local $_ = $_[0]; /<html>/i || s/\r?\n/<br>/g; $_; }

Last edited by:

Optical: Mar 25, 2005, 5:19 AM
Quote Reply
Re: [Optical] converting text to HTML In reply to
Yes, I forgot handling input & output in the global I gave, so you correctly added them:
Code:
sub { local $_ = $_[0]; /<html>/i || s/\r?\n/<br>/g; $_; }

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [Optical] converting text to HTML In reply to
It works, it works, it works! You guys are awesome. I'm using Optical's code:
sub {
my $tmp = $_[0];
unless ($tmp =~ /<html>/si) {
$tmp =~ s/\r?\n/<br>/sg;
}
return $tmp;
}