X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=web%2Fbrowse.cgi;h=0ea35b981dce6afb3cb6f463399a52992fa4ecf6;hb=4727bc68c05d61385c0c5e34f32ace1361b48892;hp=5f1b2c35daefaf720e41471b500f961d9e8cc73e;hpb=322439d39c96d427ff98a9fe1469aba207e1e86e;p=webpac2 diff --git a/web/browse.cgi b/web/browse.cgi index 5f1b2c3..0ea35b9 100755 --- a/web/browse.cgi +++ b/web/browse.cgi @@ -1,8 +1,13 @@ #!/usr/bin/perl -w +use strict; + use Cwd qw/abs_path/; use CGI::Carp qw(fatalsToBrowser); use CGI::Simple; +use File::Slurp; +use Data::Dumper; +use Text::Iconv; use lib '../lib'; @@ -13,6 +18,13 @@ my $abs_path = abs_path($0); $abs_path =~ s#/[^/]*$#/../#; my $db_path = $abs_path . '/db/'; +my $template_path = "$abs_path/conf/output/tt"; +opendir(my $dir, $template_path) || die "can't open template path $template_path: $!"; +my @templates = grep { /\.tt$/i } readdir($dir); +my $css_file = 'user.css'; + +my $iconv_utf8 = new Text::Iconv('ISO-8859-2', 'UTF-8'); +my $iconv_loc = new Text::Iconv('UTF-8', 'ISO-8859-2'); my $db = new WebPAC::DB( path => $db_path, @@ -21,7 +33,7 @@ my $db = new WebPAC::DB( ); my $out = new WebPAC::Output::TT( - include_path => "$abs_path/conf/output/tt", + include_path => $template_path, filters => { foo => sub { shift } }, ); @@ -29,64 +41,245 @@ my $q = new CGI::Simple; my $self = $q->url( '-path_info'=>1, '-query'=>0, '-full'=>0 ); my $rec = $q->param('rec') || 1; +my $template_filename = $q->param('template') || $templates[0]; + +print $q->header( -charset => 'utf-8' ); + +##---- some handy subs + +sub update_file($$) { + my ($path, $content) = @_; + + $content = $iconv_loc->convert( $content ) || die "no content?"; + + sub _conv_js { + my $t = shift || return; + return $iconv_loc->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]+$//s; + + write_file($path . '.new', $content) || die "can't save ${path}.new $!"; + rename $path . '.new', $path || die "can't rename to $path: $!"; +} + +sub get_file_in_html($) { + my ($path) = @_; + + die "no path?" unless ($path); + + my $content = read_file($path) || die "can't read $path: $!"; + $content = $q->escapeHTML($iconv_utf8->convert($content)); + + return $content; +} -print $q->header; +##---- if ($q->path_info =~ m#xml#) { - my @ds = $db->load_ds($rec); + my $ds = $db->load_ds($rec); - if (@ds) { + if ($ds) { print qq{ - - }, $out->apply( - template => 'html_ffzg.tt', - data => \@ds, - ), qq{ + + }, $iconv_utf8->convert( $out->apply( + template => $template_filename, + data => $ds, + ) ), qq{ - - }; exit; } else { - print qq{ - Record $rec not found! - }; + print qq{ + + +Record $rec not found! + + + + + +}; + exit; } +} elsif ($q->path_info =~ m#template#) { + + my $template_path = $out->{'include_path'} . '/' . $template_filename; + + if ($q->param('save_template')) { + + update_file($template_path, $q->param('tt_template')); + + print qq{ + +$template_filename saved + + + + + + }; + exit; + + } + + my $tmpl = get_file_in_html($template_path); + + print qq{ + + +
+ + + +
+ + +  idle + + + +
+
+ + + +
+ }; + + exit; + +} elsif ($q->path_info =~ m#css#) { + + my $css_path = $abs_path . '/web/' . $css_file; + + + if ($q->param('save_css')) { + update_file($css_path, $q->param('user_css')); + + print qq{ + +$css_file saved + + + + + + }; + exit; + + } + + my $user_css = get_file_in_html($css_path); + + print qq{ + + +
+ + + +
+ +  idle +
+
+ + + +
+ }; + + exit; + } else { - print qq{ + + my $template_form = qq{ +
+ + +
+ }; + + print <<"_END_OF_HEAD_"; WebPAC simple browse interface + + + + + - + + +
+
db_path = $db_path
+template = $template_form
+css = $css_file
-   - none -   + + none + + + + + +
+ +
+ +
+Editor +template +css + +
+ no template loaded yet.
-
+ + +
+ +
no record loaded yet.
+ -}; + +_END_OF_HEAD_ -}; +}