Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

Regex for dl/dt/dd or table

Quote Reply
Regex for dl/dt/dd or table
I have a field text; there i have something like

blabla, blabla

Monday: 10-12
Tuesday:12-14
...
Sunday: 10-19

blablabla

I also use bbcodes like b or i in my text.

Then i have a global like

Code:
sub {
my ($rec) = @_;
my $d = $rec->{Text};
utf8::encode($d);

$d = GT::CGI::html_escape($d);
$d =~ s,\[(/?b)\],<$1>,g;
$d =~ s,\[(/?B)\],<$1>,g;
return $d;
}

Now i would like to do something like this:

[table]
Monday: 10-12
Tuesday:12-14
...
Sunday: 10-19
[/table]

My global should take everything between [table] and [/table] and substitue like this:
<table> then for all lines
New line start = <tr><td>
: = </td><td>
Line end = </td></tr>
end lines
</table>

the same should go with dl, dt, dd, i think. But i have no idea how todo it. May someone has an idea?
Quote Reply
Re: [Robert] Regex for dl/dt/dd or table In reply to
Hi,

Can you show me an actual example of the output you are wanting? (in HTML form)

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] Regex for dl/dt/dd or table In reply to
Quote:
Monday: 12-14
Tueday: 12-18
Wednesday: 12-18

because we dont have Monday and Sunday always, we need another mark at the begin and end

Quote:
[t]
Monday:12-14
Tueday:12-18
Wednesday:12-18
[/t]

then it should be

Quote:
<table><tr><td>Monday</td><td>12-14</td></tr><tr><td>Tuesday</td><td>12-14</td></tr><tr><td>Wednesday</td><td>12-14</td></tr></table>

Search for: [t] till [/t],
substitute [t] with <table>
then take any row and do
substitute begin with <tr><td>
substitute : with </td><td>
substitute row end with </td></tr> and delete "new line" to have all in one row
substitute [/t] with [/table]

One row is important, because i substitute \n with <br/> also.
Quote Reply
Re: [Robert] Regex for dl/dt/dd or table In reply to
You could try something like this:

Code:
#!perl

use CGI;
use strict;

my $test = q|sdfsdfsdfsdf
[t]
Monday: 12-14
Tueday: 12-18
Wednesday: 12-18
[/t]
dsfsdfsdfsd
fsdfsdfsdf
|;

$test =~ s{\[t\](.*)\[\/t\]}{do_table($1)}segm;

print "Now: $test\n";

sub do_table {

my @vals;
foreach (split /\n/, $_[0]) {
chomp;
next if length $_ < 1;
my ($day,$times) = split /:/, $_;
push @vals, qq|
<tr>
<td>$day</td>
<td>$times</td>
</tr>\n|
}

return qq|<table>| . join("",@vals) . qq|</table>|;
}

=cut
<table>
<tr>
<td>Monday</td>
<td>12-14</td>
</tr>
</table>
=cut

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] Regex for dl/dt/dd or table In reply to
Looks clear and logical, thank you.
How can i do this in a global, i never have used a function there?
Quote Reply
Re: [Robert] Regex for dl/dt/dd or table In reply to
Hi,

I would try adding it into a plugin. Do you have ULTRAGlobals installed? If so, open that up (admin/Plugins/ULTRAGlobals.pm), and then near the bottom (BEFORE the last line - 1; ), add in:

Code:
sub do_markup_table {
$_[0] =~ s{\[t\](.*)\[\/t\]}{do_table($1)}segm;
return $_[0];
}

sub do_table {

my @vals;
foreach (split /\n/, $_[0]) {
chomp;
next if length $_ < 1;
my ($day,$times) = split /:/, $_;
push @vals, qq|
<tr>
<td>$day</td>
<td>$times</td>
</tr>\n|
}

return qq|<table>| . join("",@vals) . qq|</table>|;
}

Then call from your template with:

Code:
<%Plugins::ULTRAGlobals::do_markup_table($Description)%>

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] Regex for dl/dt/dd or table In reply to
Thank you very much, but i really want to have it as a global.
Can we

$_[0] =~ s{\[t\](.*)\[\/t\]}{do_table($1)}segm;

divide this into parts and add a while do between the parts?
Quote Reply
Re: [Robert] Regex for dl/dt/dd or table In reply to
Why do you want it as a global? They are less efficient. It's much better (and cleaner) to have them in a .pm file

You could get away with doing it in a global as:

do_markup_table
Code:
sub {
$_[0] =~ s{\[t\](.*)\[\/t\]}{do_table($1)}segm;
return $_[0];

sub do_table {

my @vals;
foreach (split /\n/, $_[0]) {
chomp;
next if length $_ < 1;
my ($day,$times) = split /:/, $_;
push @vals, qq|
<tr>
<td>$day</td>
<td>$times</td>
</tr>\n|
}

return qq|<table>| . join("",@vals) . qq|</table>|;
}

}

Then call with <%do_markup_table($Description)%>

But it may not work as expected, and its all much harder to debug in the future if anything goes wrong.

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] Regex for dl/dt/dd or table In reply to
Hmm, ok. If there are üproblems, i should follow your suggestion. I like to change things without ftp or ssh.

http://gossamer.ultranerds.co.uk/Products/Plugins/Gossamer_Links_Plugins/Free_Plugins/ULTRAGlobals_L217/

I tried to download, but got an error.
Quote Reply
Re: [Robert] Regex for dl/dt/dd or table In reply to
Hi,

Please find it attached :)

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] Regex for dl/dt/dd or table In reply to
Thank you very much.
Quote Reply
Re: [Robert] Regex for dl/dt/dd or table In reply to
Finally i set up a second field for the table data after i tried to magane things like i want for an hour without success.
Now i show field 1 and then field 2 with a global using your sub to move data to table.
Thank you again. :)