Типа я (zilogic) wrote in changelog,
Типа я
zilogic
changelog

[livejournal] r19430: LJSUP-9220: provide additional string ma...

Committer: amyshkin
LJSUP-9220: provide additional string manipulation functions to S2 layers
U   trunk/bin/upgrading/s2layers/core1.s2
U   trunk/cgi-bin/LJ/S2.pm
Modified: trunk/bin/upgrading/s2layers/core1.s2
===================================================================
--- trunk/bin/upgrading/s2layers/core1.s2	2011-07-05 06:35:34 UTC (rev 19429)
+++ trunk/bin/upgrading/s2layers/core1.s2	2011-07-05 07:18:17 UTC (rev 19430)
@@ -840,6 +840,12 @@
 function builtin html_get_img_urls (string s) : string[]
 "Returns an array of URLs of images from given HTML";
 
+function builtin truncate_to_paragraph (string s, string m) : string
+"Truncates given string to p/br tag or first delimiter. Depends on the mode (p|br|first)";
+
+function builtin get_metadata (string s) : string{}
+"Extracts metadata from p-tags";
+
 function builtin rand (int high) : int
 "Returns a random integer between 1 and \$high, inclusive.";
 

Modified: trunk/cgi-bin/LJ/S2.pm
===================================================================
--- trunk/cgi-bin/LJ/S2.pm	2011-07-05 06:35:34 UTC (rev 19429)
+++ trunk/cgi-bin/LJ/S2.pm	2011-07-05 07:18:17 UTC (rev 19430)
@@ -2361,6 +2361,64 @@
     return LJ::html_get_img_urls( \$s );
 }
 
+sub truncate_to_paragraph {
+    my ( $ctx, $text, $mode ) = @_;
+
+    my $re = $mode eq 'p'
+        ? qr/(.*?)<\/p>/s
+        : $mode eq 'b'
+            ? qr/(.*?)<br\s?\/?>/s
+            : qr/(.*?)(?:<br\s?\/?>|<\/p>)/s;
+
+    while ($text =~ /$re/g) {
+        my $p = $1;
+        return $p if $p =~ /\S/;
+    }
+
+    return '';
+}
+
+sub get_metadata {
+    my ( $ctx, $text ) = @_;
+
+    my %data;
+    my $p = HTML::TokeParser->new(\$text);
+    my $ctoken = undef;
+
+    while (my $token = $p->get_token) {
+        if ($token->[0] eq 'S' and $token->[1] eq 'p' and ref $token->[2] eq 'HASH') {
+            my $attrs = $token->[2];
+
+            if ( exists $attrs->{'class'} && $attrs->{'class'} ) {
+                my @classes = split(/\s+/, $attrs->{'class'});
+
+                for my $class ( @classes ) {
+                    next unless $class =~ /^data\-(.+)$/;
+                    $ctoken = $1;
+                    $data{$ctoken} = '';
+                    last;
+                }
+            }
+        }
+        elsif ($token->[1] eq 'p' and $ctoken ) {
+            $ctoken = undef;
+        }
+        elsif ( $ctoken ) {
+            if ( $token->[0] eq 'S' ) {
+                $data{$ctoken} .= $token->[4];
+            }
+            elsif ($token->[0] eq 'E' ) {
+                $data{$ctoken} .= $token->[2];
+            }
+            elsif ($token->[0] eq 'T' ) {
+                $data{$ctoken} .= $token->[1];
+            }
+        }
+    }
+
+    return \%data;
+}
+
 sub ehtml
 {
     my ($ctx, $text) = @_;
@@ -3989,6 +4047,7 @@
     return LJ::Text->truncate_to_word_with_ellipsis(
         'str'   => $this->{'text'},
         'chars' => $limit || 80,
+        'strip_html'  => 1,
     ) . '';
 }
 

Tags: livejournal, pm, s2, zilogic
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments