/[Frey]/trunk/lib/Frey/Shell/Grep.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/lib/Frey/Shell/Grep.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 842 - (show annotations)
Sun Dec 14 23:22:15 2008 UTC (15 years, 4 months ago) by dpavlin
File size: 2803 byte(s)
move html_escape so we don't hungle text before calling callback
1 package Frey::Shell::Grep;
2 use Moose;
3
4 extends 'Frey';
5 with 'Frey::Web';
6
7 use English;
8
9 has pattern => (
10 documentation => 'grep pattern',
11 is => 'rw',
12 isa => 'Str',
13 required => 1,
14 default => 'FIXME',
15 );
16
17 has path => (
18 documentation => 'path(s) to grep',
19 is => 'rw',
20 isa => 'Str',
21 required => 1,
22 default => 'lib/ t/ etc/',
23 );
24
25 has _grep_command => (
26 documentation => 'executed grep command',
27 is => 'rw',
28 isa => 'Str',
29 );
30
31 has results_as_data => (
32 is => 'rw',
33 isa => 'ArrayRef[HashRef]',
34 lazy => 1,
35 default =>
36 sub {
37
38 my ($self) = @_;
39
40 my $patt = $self->pattern;
41 my $opt = '';
42 $opt = '--' if $patt =~ m{^-};
43
44 if ( $patt =~ m{'} ) {
45 $patt = qq|"$patt"|;
46 } else {
47 $patt = qq|'$patt'|;
48 }
49
50 my $cmd = qq|grep -rn $opt $patt | . $self->path;
51 warn "# $cmd";
52 $self->_grep_command( $cmd );
53
54 my @results;
55
56 open(my $fh, '-|', $cmd) || die "can't open pipe to $cmd $!";
57 while(<$fh>) {
58 my ( $path, $line, $text ) = split(/:/,$_,3);
59 if ( ! $line ) {
60 push @results, { text => $path };
61 } else {
62 push @results, { path => $path, line => $line, text => $text };
63 }
64 }
65
66 if ( $INPUT_LINE_NUMBER > 0 ) { # closing pipe grep output results in error
67 close($fh) || die "can't close pipe to $cmd $!";
68 }
69
70 # warn "# ", $self->dump( @results );
71
72 return \@results;
73 });
74
75 sub as_markup {
76 my $self = shift;
77 my $callback = {@_};
78
79 warn "# callbacks: ",$self->dump( $callback ) if $callback;
80
81 my $patt = $self->pattern;
82 $self->title( $patt );
83
84 $self->add_css(qq|
85 dt.p {
86 color: #888;
87 white-space: pre-wrap;
88 font-family: monospace;
89 margin-top: 1em;
90 }
91 dd a {
92 float: left;
93 width: 3em;
94 }
95 dd code b {
96 background-color: #ff8;
97 }
98 |);
99
100 my $html;
101 my $last_path = '';
102
103 foreach my $result ( @{ $self->results_as_data } ) {
104
105 warn $self->dump( $result ) if $self->debug;
106
107 my $text = $result->{text} || die "no text";
108
109 if ( my $path = $result->{path} ) {
110 my $line = $result->{line} || die "no line";
111 if ( $path ne $last_path ) {
112 $html .= qq|<dt class="p">$path</dt>|;
113 }
114 if ( my $dd = $callback->{dd} ) {
115 $html .= $dd->( $patt, $path, $line, $text );
116 } else {
117 $text = $self->html_escape( $text );
118 $text =~ s{(\Q$patt\E)}{<b>$1</b>};
119 $html .= qq|<dd><a target="editor" href="/editor+$path+$line">$line</a> <code>$text</code>|;
120 }
121 $last_path = $path;
122 } else {
123 $text = $self->html_escape( $text );
124 $html .= qq|<dt>$text</dt>|;
125 }
126 }
127
128 if ( $html ) {
129 my $command = $self->_grep_command;
130 $html = qq|
131 <h1>$patt</h1>
132 <pre>$command</pre>
133 <dl>$html</dl>
134 |;
135 } else {
136 $html = $self->error( "No results for $patt\n" );
137 }
138
139 $self->add_head(qq|
140 <link rel="search" type="application/opensearchdescription+xml" title="Frey::Shell::Grep" href="/Frey::OpenSearch/grep_as_markup">
141 |);
142
143 return $html;
144 }
145
146 1;

  ViewVC Help
Powered by ViewVC 1.1.26