43 |
|
|
44 |
package Search::Estraier::Document; |
package Search::Estraier::Document; |
45 |
|
|
46 |
|
use Carp qw/confess/; |
47 |
|
|
48 |
=head1 Search::Estraier::Document |
=head1 Search::Estraier::Document |
49 |
|
|
50 |
Document for HyperEstraier |
Document for HyperEstraier |
60 |
my $self = {@_}; |
my $self = {@_}; |
61 |
bless($self, $class); |
bless($self, $class); |
62 |
|
|
63 |
|
$self->{id} = -1; |
64 |
|
|
65 |
$self ? return $self : return undef; |
$self ? return $self : return undef; |
66 |
} |
} |
67 |
|
|
68 |
|
|
69 |
=head2 add_attr |
=head2 add_attr |
70 |
|
|
71 |
|
Add an attribute. |
72 |
|
|
73 |
$doc->add_attr( name => 'value' ); |
$doc->add_attr( name => 'value' ); |
74 |
|
|
75 |
|
B<FIXME>: delete attribute using |
76 |
|
|
77 |
|
$doc->add_attr( name => undef ); |
78 |
|
|
79 |
=cut |
=cut |
80 |
|
|
81 |
sub add_attr { |
sub add_attr { |
83 |
my $attrs = {@_}; |
my $attrs = {@_}; |
84 |
|
|
85 |
while (my ($name, $value) = each %{ $attrs }) { |
while (my ($name, $value) = each %{ $attrs }) { |
86 |
$name =~ s/\s\s+/ /gs; |
$self->{attrs}->{_s($name)} = _s($value); |
87 |
$value =~ s/\s\s+/ /gs; |
} |
88 |
push @{$self->{$name}}, $value; |
|
89 |
|
return 1; |
90 |
|
} |
91 |
|
|
92 |
|
|
93 |
|
=head2 add_text |
94 |
|
|
95 |
|
Add a sentence of text. |
96 |
|
|
97 |
|
$doc->add_text('this is example text to display'); |
98 |
|
|
99 |
|
=cut |
100 |
|
|
101 |
|
sub add_text { |
102 |
|
my $self = shift; |
103 |
|
my $text = shift; |
104 |
|
return unless defined($text); |
105 |
|
|
106 |
|
push @{ $self->{dtexts} }, _s($text); |
107 |
|
} |
108 |
|
|
109 |
|
|
110 |
|
=head2 add_hidden_text |
111 |
|
|
112 |
|
Add a hidden sentence. |
113 |
|
|
114 |
|
$doc->add_hidden_text('this is example text just for search'); |
115 |
|
|
116 |
|
=cut |
117 |
|
|
118 |
|
sub add_hidden_text { |
119 |
|
my $self = shift; |
120 |
|
my $text = shift; |
121 |
|
return unless defined($text); |
122 |
|
|
123 |
|
push @{ $self->{htexts} }, _s($text); |
124 |
|
} |
125 |
|
|
126 |
|
=head2 id |
127 |
|
|
128 |
|
Get the ID number of document. If the object has never been registred, C<-1> is returned. |
129 |
|
|
130 |
|
print $doc->id; |
131 |
|
|
132 |
|
=cut |
133 |
|
|
134 |
|
sub id { |
135 |
|
my $self = shift; |
136 |
|
return $self->{id}; |
137 |
|
} |
138 |
|
|
139 |
|
=head2 attr_names |
140 |
|
|
141 |
|
Get a list of attribute names of a document object. |
142 |
|
|
143 |
|
my @attrs = $doc->attr_names; |
144 |
|
|
145 |
|
=cut |
146 |
|
|
147 |
|
sub attr_names { |
148 |
|
my $self = shift; |
149 |
|
confess "attr_names return array, not scalar" if (! wantarray); |
150 |
|
return sort keys %{ $self->{attrs} }; |
151 |
|
} |
152 |
|
|
153 |
|
|
154 |
|
=head2 attr |
155 |
|
|
156 |
|
Get the value of an attribute. |
157 |
|
|
158 |
|
my $value = $doc->attr( 'attribute' ); |
159 |
|
|
160 |
|
=cut |
161 |
|
|
162 |
|
sub attr { |
163 |
|
my $self = shift; |
164 |
|
my $name = shift; |
165 |
|
|
166 |
|
return $self->{'attrs'}->{ $name }; |
167 |
|
} |
168 |
|
|
169 |
|
=head2 dump_draft |
170 |
|
|
171 |
|
print $doc->dump_draft; |
172 |
|
|
173 |
|
=cut |
174 |
|
|
175 |
|
sub dump_draft { |
176 |
|
} |
177 |
|
|
178 |
|
=head2 delete |
179 |
|
|
180 |
|
Empty document object |
181 |
|
|
182 |
|
$doc->delete; |
183 |
|
|
184 |
|
=cut |
185 |
|
|
186 |
|
sub delete { |
187 |
|
my $self = shift; |
188 |
|
|
189 |
|
foreach my $data (qw/attrs dtexts stexts/) { |
190 |
|
delete($self->{$data}); |
191 |
} |
} |
192 |
|
|
193 |
|
return 1; |
194 |
|
} |
195 |
|
|
196 |
|
|
197 |
|
=head2 _s |
198 |
|
|
199 |
|
Remove multiple whitespaces from string, as well as whitespaces at beginning or end |
200 |
|
|
201 |
|
my $text = _s(" this is a text "); |
202 |
|
$text = 'this is a text'; |
203 |
|
|
204 |
|
=cut |
205 |
|
|
206 |
|
sub _s { |
207 |
|
my $text = shift || return; |
208 |
|
$text =~ s/\s\s+/ /gs; |
209 |
|
$text =~ s/^\s+//; |
210 |
|
$text =~ s/\s+$//; |
211 |
|
return $text; |
212 |
} |
} |
213 |
|
|
214 |
|
|