package LiveGeez::Directives; use base qw(HTML::Parser Exporter); BEGIN { use strict; use vars qw($VERSION @EXPORT @gFile $u $p); $VERSION = '0.20'; @EXPORT = qw(ParseDirectives); use LiveGeez::Services; require Convert::Ethiopic::System; require Convert::Ethiopic::Date; require Convert::Ethiopic::String; require HTML::Entities; require LiveGeez::HTML; require LiveGeez::URI; require LiveGeez::CacheAsSERA; # use diagnostics; $u = new Convert::Ethiopic::System ( "UTF8" ); $p = new LiveGeez::Directives ( api_version => 3, start_h => ['start', "self, tagname, attr, text"], default_h => [sub { push ( @gFile, @_ ) }, 'text'] ); } sub attrToString { my ( $attr ) = @_; my $args; foreach ( keys %$attr ) { $args .= " $_=\"$attr->{$_}\""; } $args; } sub SysInFontMenu { my ( $attr ) = @_; delete ( $attr->{menu} ); $attr->{name} = "sysIn" unless ( exists($attr->{name}) ); my $attrs = attrToString ( $attr ); < MENU } sub SysOutFontMenu { my ( $attr, $DEFAULTSYSOUT ) = @_; $attr->{name} = "sysOut" unless ( exists($attr->{name}) ); if ( $attr->{script} ) { $attr->{onChange} = ( $attr->{script} eq "js-standard" ) ? qq(window.open('$scriptURL?sys=' + this.options[this.selectedIndex].value + '&file=$file', '_top');") : $attr->{script} ; delete ( $attr->{script} ); } my $attrs = attrToString ( $attr ); < MENU } sub LangOutMenu { my ( $attr, $DEFAULTLANG, $DEFAULTREGION ) = @_; $attr->{name} = "langOut" unless ( exists($attr->{name}) ); delete ( $attr->{langmenu} ); my $attrs = attrToString ( $attr ); my $menu; if ( $DEFAULTREGION eq "er" ) { $menu = qq( ); } elsif ( $DEFAULTREGION eq "et" ) { $menu = qq( ); } elsif ( $DEFAULTREGION eq "*" ) { $menu = qq( ); } $menu; } sub LangInMenu { delete ( $attr->{langmenu} ); $_ = LangInMenu ( @_ ); s/OUT/IN/g; $_; } sub UpdateHREF { my ($self, $attr) = @_; # # there is no "is_ethiopic" attribute any more, make sure this works for all link types. # my $uri = new URI ( $attr->{href} ); if ( my $scheme = $uri->scheme ) { return ( $attr->{href} ) if ( $scheme eq "mailto" || $scheme eq "file" ); # return ( 0 ) if ( $attr->{nolivegeezlink} ); } else { my $uri_out = URI->new_abs ( $attr->{href}, $self->{uri}->{_uri} ); # return ( $uri_out->canonical ) if ( $attr->{nolivegeezlink} || $uri->path !~ "\.sera\." ); return ( $uri_out->canonical ) unless ( $uri->path =~ "\.sera\." || $attr->{livegeezlink} ); $uri = $uri_out; } # print STDERR "HREF: $attr->{href}\n"; $self->{request}->{config}->{uris}->{file_query} . $uri->canonical; } sub UpdateBase { my ($self, $attr) = @_; my $uri = new URI ( $attr->{href} ); ( $uri->scheme || !exists($attr->{href}) ) ? 0 : $uri->abs ( $self->{uri}->{_uri} ); } sub UpdateFrame { my ($self, $attr) = @_; my $uri = new URI ( $attr->{src} ); ( $uri->scheme ) ? 0 : $self->{request}->{config}->{uris}->{file_query} . $uri->abs ( $self->{uri}->{_uri} ); } sub start { my ($self, $tagname, $attr, $text) = @_; my $test = 0; if ( $tagname eq "livegeez" ) { if ( $attr->{menu} ) { $text = ( $attr->{menu} =~ /out/i ) ? SysOutFontMenu ( $attr, $self->{request}->{config}->{sysout} ) : SysInFontMenu ( $attr ) ; delete ( $attr->{menu} ); } elsif ( $attr->{langmenu} ) { $text = ( $attr->{langmenu} =~ /out/i ) ? LangOutMenu ( $attr, $self->{request}->{config}->{lang}, $self->{request}->{config}->{region} ) : LangInMenu ( $attr, $self->{request}->{config}->{lang}, $self->{request}->{config}->{region} ) ; } elsif ( $attr->{formfile} ) { # $text = qq( ); # # make sure this works # $text = qq( ); } elsif ( $attr->{formcookie} ) { $text = qq( ); } elsif ( $attr->{formmenu} ) { delete ( $attr->{formmenu} ); $text = SysOutFontMenu ( $attr, $self->{request}->{config}->{sysout} ) } elsif ( $attr->{formsubmit} ) { my $value = ( $attr->{value} ) ? $attr->{value} : "Reopen" ; $text = qq( ); } elsif ( $attr->{formmacfriendly} ) { $text = qq( Mac Friendly<\/nobr> ); } elsif ( $attr->{date} ) { my ( $day, $month, $year, $calsys ); if ( $attr->{date} eq "today" ) { $self->{dontCache} = 1 unless ( $attr->{pragma} && ( $attr->{pragma} eq "cache-ok" ) ); } else { ( $day, $month, $year, $calsys ) = split ( ",", $attr->{date} ) } my $lang = ( $attr->{lang} ) ? $attr->{lang} : $self->{request}->{sysOut}->{langNum}; $calsys = ( $attr->{cal} ) ? $attr->{cal} : ( $calsys ) ? $calsys : "eu"; $calsys = ( $calsys eq "eu" ) ? "euro" : "ethio"; my $d = ( $attr->{date} eq "today" ) ? ( $calsys eq "euro" ) ? new Convert::Ethiopic::Date ( "today" ) : new Convert::Ethiopic::Date ( "today" )->convert : new Convert::Ethiopic::Date ( cal => $calsys, date => $day, month => $month, year => $year )->convert ; $d->lang ( $lang ); $text = Convert::Ethiopic::String->new ( $d->getFormattedDate, $u, $self->{request}->{sysOut} )->convert ( 1 ); } elsif ( $attr->{number} ) { my $n = new Convert::Ethiopic::Number ( $attr->{number} ); $text = Convert::Ethiopic::String->new ( $n->convert, $u, $self->{request}->{sysOut} )->convert ( 1 ); } } elsif ( ($tagname eq "form") && ( $attr->{livegeezget} ) ) { $text = qq(
); } elsif ( ($tagname eq "form") && ( $attr->{livegeezpost} ) ) { $text = qq( ); } elsif ( ($tagname eq "form") && ( $attr->{action} =~ /LIVEGEEZLINK/i ) ) { $text =~ s/LIVEGEEZLINK/$self->{scriptRoot}/i; } elsif ( ( $tagname eq "a" && $attr->{href} ) && ( exists($attr->{livegeezlink}) || exists($attr->{nolivegeezlink}) || ($attr->{href} =~ /\.sera\./) ) && $attr->{href} !~ $self->{scriptRoot} ) { unless ( $attr->{nolivegeezlink} ) { my $newref = $self->UpdateHREF ($attr); $text =~ s/$attr->{href}/$newref/; $text =~ s/ LIVEGEEZLINK//i; } elsif ( $self->{notSERA} ) { # # we need to keep these around for smart linking in sera docs # $text =~ s/ NOLIVEGEEZLINK//i; } } elsif ( $tagname eq "frame" ) { if ( my $newref = $self->UpdateFrame ($attr) ) { $text =~ s/$attr->{src}/$newref/; } } elsif ( $tagname eq "base" ) { if ( my $newref = $self->UpdateBase ($attr) ) { $text =~ s/$attr->{href}/$newref/; } elsif ( !exists($attr->{href}) ) { # # we didn't have an href attribute, add one # $newref = $self->{request}->{uri}->doc_root; $text =~ s|>| href="$newref/">|; } $self->{baseUpdated} = 1; } elsif ( $tagname eq "link" ) { if ( my $newref = $self->UpdateBase ($attr) ) { $text =~ s/$attr->{href}/$newref/; } } push ( @gFile, $text ); } sub preconditionHTML { $_ = $_[0]; s/datesys/cal/g; # update from old to new s/]+)?>/\n<\/form>/oig; s/]+?)?LIVEGEEZFORM>/\n \n \n \n /oig; $_; } sub ParseDirectives { my ( $file, $htmlData ) = ( @_ ); # $#gFile = 100; $p->{scriptRoot} = $file->{scriptRoot}; $p->{baseUpdated} = 0; $p->{notSERA} = ( $file->{refsUpdated} == -1 ) ? 0 : 1; $p->{uri} = new LiveGeez::URI ( $file->{request}->{uri}->canonical ); $p->{request} = $file->{request}; $htmlData = preconditionHTML ( $htmlData ); # printf STDERR "Begin Directives[$$]\n"; $p->parse ( $htmlData ); # printf STDERR "End Directives[$$]\n"; $_ = join ( "", @gFile ); $#gFile = -1; $p->{uri} = $p->{request} = undef; s/(\n$1/i if ( !$p->{baseUpdated} && ( $file->{request}->{uri}->scheme || $file->{request}->{config}->{set_local_base} ) ); $file->{dontCache} = 1 if ( $p->{dontCache} ); $file->{refsUpdated} = 1; # printf STDERR "Exit Directives[$$]\n"; $_; } ######################################################### # Do not change this, Do not put anything below this. # File must return "true" value at termination 1; ########################################################## __END__ =head1 NAME LiveGeez::CacheAsSERA - HTML Conversion for LiveGe'ez =head1 SYNOPSIS $cacheFile = LiveGeez::CacheAsSERA::HTML($f, $sourceFile) Where $f is a File.pm object and $sourceFile is the pre-cached file name. =head1 DESCRIPTION CacheAsSERA.pm contains the routines for conversion of HTML document content from Ethiopic encoding systems into SERA for document caching and later conversion into other Ethiopic systems. =head1 AUTHOR Daniel Yacob, L =head1 SEE ALSO S> =cut