#?SuikaWikiConfig/2.0

Plugin:
  @Name: WikiPageProp
  @Description:
    @@@: WikiPage property
    @@lang:en
  @License: %%Perl%%
  @Author:
    @@Name:
      @@@@: Wakaba
      @@@lang:ja
      @@@script:Latn
    @@Mail[list]: w@suika.fam.cx
  @Date.RCS:
    $Date: 2004/07/25 06:54:29 $
  @RequiredModule[list]:
    WikiFormat
  @Use:
    use Message::Util::Error;
  @Namespace:
    @@sw: 
      http://suika.fam.cx/~wakaba/archive/2004/7/20/sw#

PluginConst:
  @NS_XHTML1:
    http://www.w3.org/1999/xhtml

FormattingRule:
  @Category[list]:
    view
    view-resource
    form-input
  @Name: last-modified
  @Description:
    @@@:
      Last-modified date-time of the WikiPage.
      \
      Note: Date-time value returned by this rule might not be 
      the "real" last-modified date-time, in case last-modified 
      value is not updated when content is modified (so called 
      "sage" feature).
    @@lang: en
  @Parameter:
    @@Name: page
    @@Type: WikiName
    @@Default: (current)
    @@Description:
      @@@@: Target WikiPage
      @@@lang:en
  @Formatting:
    __ATTRTEXT:%page__;
    my $page = length $p->{page} ?
                 [split $o->{wiki}->{config}->{name}->{space}->{separator_regex},
                        $p->{page}]:
                 $o->{wiki}->{var}->{page};
    my $lm;
    try {
      $lm = $o->{wiki}->{db}->get (lastmodified => $page);
    } catch SuikaWiki::DB::Util::Error with {
      my $err = shift;
      $err->throw if $err->{-type} eq 'ERROR_REPORTED';
      $lm = 0;
    };
    $p->{-parent}->append_text ( __FUNCPACK__::unixtime_to_string ($lm));
## TODO: format string

FormattingRule:
  @Category[list]: page-link
  @Name: last-modified
  @Description:
    @@@:
      Last-modified date-time of the WikiPage.
      \
      Note: Date-time value returned by this rule might not be
      the "real" last-modified date-time, in case last-modified
      value is not updated when content is modified (so called
      "sage" feature).
    @@lang: en
  @Formatting:
    my $date;
    if (defined $o->{link}->{dest}->{page_lastmodified}) {
      $date = $o->{link}->{dest}->{page_lastmodified};
    } else {
## TODO:
      my $page = $o->{link}->{dest}->{page_name};
      try {
        $date = $o->{wiki}->{db}->get (lastmodified => $page);
      } catch SuikaWiki::DB::Util::Error with {
        my $err = shift;
        $err->throw if $err->{-type} eq 'ERROR_REPORTED';
        $date = 0;
      };
    }
    $p->{-parent}->append_text (__FUNCPACK__::unixtime_to_string ($date));
## TODO: Update to new interface

FormattingRule:
  @Category[list]:
    view
    view-resource
    form-input
  @Name: page-name
  @Description:
    @@@:
      WikiName in string repersentation
    @@lang: en
  @Parameter:
    @@Name:separator
    @@Type:string
    @@Default:(config)
    @@Description:
      @@@@:Separator used to delimit namespace components (ie. path delimiter)
      @@@lang:en
  @Formatting:
    __ATTRTEXT:%page__;
    $p->{-parent}->append_text ($o->{wiki}->{var}->{page}->stringify
                                       (wiki => $o->{wiki}));

FormattingRule:
  @Category[list]:page-link
  @Name: page-name
  @Description:
    @@@:WikiName in string representation
    @@lang:en
  @Parameter:
    @@Name:relative
    @@Type:boolean
    @@Default:"0"
    @@Description:
      @@@@:Whether WikiName should be relative to "this" page
      @@@lang:en
  @Formatting:
    __ATTRTEXT:%relative__;
    if ($p->{relative}) {
      $p->{-parent}->append_text
        ($o->{link}->{dest}->{page_name_relative}->stringify
                                                     (wiki => $o->{wiki}));
    } else {
      $p->{-parent}->append_text
        ($o->{link}->{dest}->{page_name}->stringify (wiki => $o->{wiki}));
    }

FormattingRule:
  @Category[list]:page-link
  @Name: page-title
  @Description:
    @@@:Title of the WikiPage
    @@lang:en
  @Parameter:
    @@Name:relative
    @@Type:boolean
    @@Default:"0"
    @@Description:
      @@@@:Whether relative to "this" page
      @@@lang:en
  @Formatting:
    __ATTRTEXT:%relative__;
    if (defined $o->{link}->{dest}->{page_title}) {
 ## ISSUE: Is this work now?
      $p->{-parent}->append_text ($o->{link}->{dest}->{page_title});
    } elsif ($p->{relative}) {
      $p->{-parent}->append_text
        ($o->{link}->{dest}->{page_name_relative}->stringify
                                                     (wiki => $o->{wiki}));
    } else {
      $p->{-parent}->append_text
        ($o->{link}->{dest}->{page_name}->stringify (wiki => $o->{wiki}));
    }

FormattingRule:
  @Category[list]:
    view
    view-resource
  @Name:page-headline
  @Description:
    @@@:Headline summary of the WikiPage
    @@lang:en
  @Parameter:
    @@Name: page
    @@Type: WikiName
    @@Default: (current)
    @@Description:
      @@@@: WikiPage about which headline describes
      @@@lang: en
  @Formatting:
    $p->{-parent}->append_text (__FUNCPACK__->get_headsummary
                                 ($o->{wiki}->name ($p->{page} || $o->{wiki}->{var}->{page}),
                                  wiki => $o->{wiki}));
FormattingRule:
  @Category[list]:page-link
  @Name:page-headline
  @Description:
    @@@:Headline summary of the WikiPage
    @@lang:en
  @Formatting:
    $p->{-parent}->append_text (__FUNCPACK__->get_headsummary
                                 ($o->{link}->{dest}->{page_name},
                                  wiki => $o->{wiki}));

FormattingRule:
  @Category[list]: form-input
  @Name: wpp--next-anchor-index
  @Description:
    @@@: Next "anchor index" number
    @@lang: en
  @Formatting:
      my $page = $o->{wiki}->{var}->{page};
      my $content;
      my $content_prop;
      try {
        $content = $o->{wiki}->{db}->get (content => $page);
        $content_prop = $o->{wiki}->{db}->get (content_prop => $page);

        ## Get Format Handler
        my $format = SuikaWiki::Plugin->module_package ('WikiFormat')
                                      ->handler (\$content,
                                                 content_prop => $content_prop,
                                                 wiki => $o->{wiki});
  
        $p->{-parent}->append_text ($format->next_index_for_anchor
                                               (\$content,
                                                content_prop => $content_prop,
                                                wiki => $o->{wiki},
                                                page => $page));

      } catch SuikaWiki::DB::Util::Error with {
        my $err = shift;
        $err->throw if $err->{-type} eq 'ERROR_REPORTED';
      } catch SuikaWiki::Format::Definition::error with {
        # 
      };

Function:
  @Name: unixtime_to_string
  @Description
    @@@:
      Convert internal date-time format to other (possibly human readable) 
      format.
      \
      Note: Currently, only one fixed format is supported, whilst 
      future version is expected to support highly customizable 
      formatting supported by Message::Field::Date.
    @@lang:en
  @Main:
    my @date = gmtime shift;
    sprintf '%04d-%02d-%02d %02d:%02d:%02d +00:00',
            $date[5] + 1900,
            $date[4] + 1,
            @date[3,2,1,0];

Function:
  @Name: get_headsummary
  @Description:
    @@@: Returns headline summary
    @@lang:en
  @Main:
    my (undef, $page, %opt) = @_;
    $page ||= $opt{wiki}->{var}->{page};
    
    my $s;
    try {
      if ($opt{wiki}->{db}->exist (wpp__headsummary => $page)) {
        $s = $opt{wiki}->{db}->get (wpp__headsummary => $page);
      } else {
        my $content = $opt{wiki}->{db}->get (content => $page);
        my $content_prop = $opt{wiki}->{db}->get (content_prop => $page);
        my $mt;
        if (my $abs = $content_prop->get_attribute (<Q:sw:abstract>)) {
          $content = $abs->value;
          $mt = $content_prop->get_attribute_value (<Q:sw:abstract--type>);
        }
        
        ## Get Format Handler
        my $format = __FUNCPACK{WikiFormat}__
                                      ->handler (\$content,
                                                 serialized_media_type => $mt,
                                                 wiki => $opt{wiki});
        $s = $format->headsummary (\$content, wiki => $opt{wiki}); 

        $s =~ tr/\x0D//d;
        if (length ($s) > 500) {
          $s = substr ($s, 0, 500);
          $s =~ s/[^\x09\x0A\x20-\x7E]+$//;
          $s .= '...';
        }
        if ($s =~ /^([^\x0A]*(?:\x0A+[^\x0A]+){0,9})/) {
          $s = $1;
        }
        $s =~ s/[\x09\x0A\x20]+/\x20/gs;
        $s =~ s/\([^\(\)]+\)//g;
        $s =~ s/\[[^\[\]]+\]//g;
        $s =~ s/>>[0-9]+//g;
        $s =~ s/<[^<>]+>//g;
        $s =~ s/{[^{}]+}//g;
        $s =~ s/20[0-9][0-9]-[01][0-9]-[0-3][0-9][T\x20]*[0-2][0-9]:[0-5][0-9]:[0-5][0-9](?:\s*[+-][0-9][0-9]:[0-9][0-9])?//g;
        $s =~ s/ [Aa]n? / /g;
                  $s =~ s/ [Aa]t / @ /g;
                  $s =~ s/ [Tt]he / /g;
                  $s =~ s/,? and / & /g;
                  $s =~ s!,? or ! / !g;
                  $s =~ s! not !n't !g;
                  $s =~ s! (?:i|ha)s !'s !g;
                  $s =~ s! are !'re !g;
                  $s =~ s! [Ww]ith(in|out)? !' w/'.substr ($1,0,1).' '!ge;
        $s =~ s![.,]\x20?$!!g;
        $s =~ s/\x20+/\x20/g;
        if (length ($s) > 400) {
          $s = substr ($s, 0, 400);
          $s =~ s/[^\x20-\x7E]+$//s; # Remove broken EUC-JP char
          $s .= '...';
        }
        $opt{wiki}->{db}->set (wpp__headsummary => $page => $s);
      }
    } catch SuikaWiki::DB::Util::Error with {
      my $err = shift;
      $err->throw if $err->{-type} eq 'ERROR_REPORTED';
      $s = undef;
    };

    return $s;

ViewFragment:
  @Name: ws--post-content
  @Description:
    @@@: After content body -- last modified date and time of the WikiPage
    @@lang:en
  @Order: 800
  @Formatting:
        %block (
          id => last-modified,
          content => {%res(name=>{LastModified=});%last-modified;}p,
        );

Resource:
  @LastModified=:
    @@@:
      Last modified: 
    @@lang: en