【しばらく編集不可モードで運営します】 編集(管理者用) | 差分 | 新規作成 | 一覧 | RSS | FrontPage | 検索 | 更新履歴

plugin/linktree.pl -

リンクツリープラグインのソースです。 プラグインをハックしたい人のために。

use strict;

package linktree;

my $max_depth = 10;
my $WikiName = q(\b([A-Z][a-z]+([A-Z][a-z]+)+)\b);
my $PureBracketName = q(\[\[([^#]\S*?)\]\]);

sub plugin_block {
    my ($escaped_argument, $context) = @_;
    $escaped_argument =~ /(\d+),(.*)/;
    my ($depth, $pagename) = ($1, $2);
    if ($max_depth < $depth) {
        $depth = $max_depth;
    }
    my $db = $context->{database};
    my %visited;
    my $html = &linktree($db, $pagename, \%visited, $depth);
    if ($html) {
        return "<ul>" . $html . "</ul>";
    }
}

sub linktree {
    my ($db, $pagename, $visited, $depth) = @_;
    my $html = "";

    # Depth check.
    return "" if ($depth < 0);
    
    # Skip invalid page.
    return "" if ($pagename =~ / /);
    
    # If already visited, return nothing.
    return "" if $visited->{$pagename};

    # Get current content;
    my $content = $db->{$pagename};

    # return unless exist
    return "" unless ($content);

    # Find recursively.
    $html .= "<li>";
    $html .= &main::inline(&main::armor_name($pagename));
    $html .= "\n";
    $html .= "<ul>\n";
    my %pages;
    while ($content =~ /($WikiName|$PureBracketName)/g) {
        $pages{&main::unarmor_name($1)}++;
    }
    foreach my $linkpage (sort keys %pages) {
        $visited->{$pagename}++;
        $html .= &linktree($db, &main::unarmor_name($linkpage), $visited, $depth - 1);
        $visited->{$pagename}--;
    }
    $html .= "</ul>\n";
    return $html;
}

sub plugin_usage {
    return {
        name => 'linktree',
        version => '1.0',
        author => 'Hiroshi Yuki <hyuki@hyuki.com>',
        syntax => '#linktree(depth,PageName)',
        description => 'Show pages linked from PageName recursively.',
        example => '#linktree(3,FrontPage)',
    };
}

1;