--- Webpacus/lib/Webpacus/Model/WebPAC.pm 2005/11/23 21:52:35 119 +++ Webpacus/lib/Webpacus/Model/WebPAC.pm 2005/11/24 22:29:29 135 @@ -10,6 +10,7 @@ use WebPAC::DB; use WebPAC::Output::TT; use WebPAC::Search::Estraier 0.02; +use File::Slurp; =head1 NAME @@ -65,6 +66,7 @@ my $db_path = $c->config->{webpac}->{db_path}; my $template_path = $c->config->{webpac}->{template_path}; + $self->{template_path} = $template_path; $log->debug("using db path '$db_path', template path '$template_path'"); @@ -97,6 +99,32 @@ } +=head2 iconv_on_save + + my $out = $m->iconv_on_save( $content ); + +Convert data saved to disk in Webpac encoding. + +=cut + +sub iconv_on_save { + my $self = shift; + + $self->{iconv_save} ||= new Text::Iconv( + $self->config->{webpac}->{out_encoding}, + $self->config->{webpac}->{webpac_encoding}, + ); + + $self->{iconv_save}->convert( @_ ); +} + + +=head2 search + + my $m->search( 'query phrase', 'result_template.tt', \@add_attr ); + +=cut + sub search { my ( $self, $query, $template, $add_attr ) = @_; @@ -143,8 +171,56 @@ return \@html_results; } +=head2 save_html - + $m->save_html( '/full/path/to/file', $content ); + +It will use C to convert content encoding back to +Webpac codepage, recode JavaScript Unicode entities (%u1234), +strip extra newlines at beginning and end, and save to +C and if that succeeds, just rename +it over original file which should be atomic on filesystem level. + +=cut + +sub save_html { + my ($self, $path, $content) = @_; + + $content = $self->iconv_on_save( $content ) || die "no content?"; + + sub _conv_js { + my $t = shift || return; + return $self->{iconv}->convert(chr(hex($t))); + } + $content =~ s/%u([a-fA-F0-9]{4})/_conv_js($1)/gex; + $content =~ s/^[\n\r]+//s; + $content =~ s/[\n\r]+$/\n/s; + + write_file($path . '.new', $content) || die "can't save ${path}.new $!"; + rename $path . '.new', $path || die "can't rename to $path: $!"; +} + +=head2 load_html + + my $html = $m->load_html('/full/path/to/file'); + +This will convert file from Webpac encoding to Catalyst and +convert that data to escaped HTML (for sending into +C<<