From f32369f66924e0c008370c29462af57fd218267a Mon Sep 17 00:00:00 2001 From: intrigeri Date: Sat, 12 Jul 2008 23:04:35 +0200 Subject: added new plugin: pedigree Signed-off-by: intrigeri --- IkiWiki/Plugin/pedigree.pm | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 IkiWiki/Plugin/pedigree.pm (limited to 'IkiWiki/Plugin') diff --git a/IkiWiki/Plugin/pedigree.pm b/IkiWiki/Plugin/pedigree.pm new file mode 100644 index 000000000..efe6c3e77 --- /dev/null +++ b/IkiWiki/Plugin/pedigree.pm @@ -0,0 +1,53 @@ +#!/usr/bin/perl +# -*- cperl-indent-level: 8; -*- +# Ikiwiki pedigree plugin. +package IkiWiki::Plugin::pedigree; + +use warnings; +use strict; +use IkiWiki 2.00; + +sub import { #{{{ + hook(type => "pagetemplate", id => "pedigree", call => \&pagetemplate); +} # }}} + +sub pedigree ($) { #{{{ + my $page=shift; + + my @ret; + my $path=""; + my $title=$config{wikiname}; + my $i; + + my @pagepath=(split("/", $page)); + my $pageloc=@pagepath - 1; + foreach my $dir (@pagepath) { + next if $dir eq 'index'; + push @ret, { + url => urlto($path, $page), + page => $title, + level => $i, + is_root => ($i eq 0), + is_first_child => ($i eq 1), + is_mother => ($i eq ($pageloc - 1)), + is_grand_mother => ($i eq ($pageloc - 2)) + }; + $path.="/".$dir; + $title=pagetitle($dir); + $i++; + } + return @ret; +} #}}} + +sub pagetemplate (@) { #{{{ + my %params=@_; + my $page=$params{page}; + my $template=$params{template}; + + if ($template->query(name => "pedigree")) { + $template->param(pedigree => [pedigree($page)]); + } + +} # }}} + +1 -- cgit v1.2.3 From 16cf69477d9511debba84152629d9f08a6e643a5 Mon Sep 17 00:00:00 2001 From: intrigeri Date: Sun, 13 Jul 2008 14:51:20 +0200 Subject: pedigree: fixed misc bugs Signed-off-by: intrigeri --- IkiWiki/Plugin/pedigree.pm | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'IkiWiki/Plugin') diff --git a/IkiWiki/Plugin/pedigree.pm b/IkiWiki/Plugin/pedigree.pm index efe6c3e77..b051a6da2 100644 --- a/IkiWiki/Plugin/pedigree.pm +++ b/IkiWiki/Plugin/pedigree.pm @@ -17,10 +17,10 @@ sub pedigree ($) { #{{{ my @ret; my $path=""; my $title=$config{wikiname}; - my $i; + my $i=0; my @pagepath=(split("/", $page)); - my $pageloc=@pagepath - 1; + my $pagedepth=@pagepath; foreach my $dir (@pagepath) { next if $dir eq 'index'; push @ret, { @@ -28,12 +28,12 @@ sub pedigree ($) { #{{{ page => $title, level => $i, is_root => ($i eq 0), - is_first_child => ($i eq 1), - is_mother => ($i eq ($pageloc - 1)), - is_grand_mother => ($i eq ($pageloc - 2)) + is_second_ancestor => ($i eq 1), + is_grand_mother => ($i eq ($pagedepth - 2)), + is_mother => ($i eq ($pagedepth - 1)), }; $path.="/".$dir; - $title=pagetitle($dir); + $title=IkiWiki::pagetitle($dir); $i++; } return @ret; -- cgit v1.2.3 From df42a5ef21fd3e97ab287fa48ccd3aafd34e0375 Mon Sep 17 00:00:00 2001 From: intrigeri Date: Sun, 13 Jul 2008 21:14:29 +0200 Subject: pedigree: added _but_root & _but_two_oldest loops ... after having learned a bit of Perl, knocked my head against Perl references and arrays of hashes, tried to use some nice functionnal programming constructs - no success - to make things more generic... I'm back to the roots, with this simple code :) Signed-off-by: intrigeri --- IkiWiki/Plugin/pedigree.pm | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) (limited to 'IkiWiki/Plugin') diff --git a/IkiWiki/Plugin/pedigree.pm b/IkiWiki/Plugin/pedigree.pm index b051a6da2..3d1e3764d 100644 --- a/IkiWiki/Plugin/pedigree.pm +++ b/IkiWiki/Plugin/pedigree.pm @@ -26,7 +26,7 @@ sub pedigree ($) { #{{{ push @ret, { url => urlto($path, $page), page => $title, - level => $i, + absdepth => $i, is_root => ($i eq 0), is_second_ancestor => ($i eq 1), is_grand_mother => ($i eq ($pagedepth - 2)), @@ -39,14 +39,45 @@ sub pedigree ($) { #{{{ return @ret; } #}}} +sub forget_oldest ($@) { #{{{ + my $offset=shift; + my @pedigree=@_; + my @ret; + my $parent; + unless ($offset ge scalar(@pedigree)) { + for (my $i=0; $i < $offset; $i++) { + shift @pedigree; + } + while (@pedigree) { + # Doing so does not modify the original @pedigree, we've + # got our own copy of its "content" (i.e. a pile of + # references to hashes)... + $parent=shift @pedigree; + # ... but we have no copy of the referenced hashes, so we + # actually are modifying them in-place, but we don't care + # here since reldepth has to be computed everytime anyway. + $parent->{reldepth}=($parent->{absdepth} - $offset); + push @ret, $parent; + } + } + return @ret; +} #}}} + sub pagetemplate (@) { #{{{ my %params=@_; my $page=$params{page}; my $template=$params{template}; - if ($template->query(name => "pedigree")) { - $template->param(pedigree => [pedigree($page)]); - } + if ($template->query(name => "pedigree") + or $template->query(name => "pedigree_but_root") + or $template->query(name => "pedigree_but_two_oldest") + ) + { + my @pedigree=pedigree($page); + $template->param(pedigree => \@pedigree); + $template->param(pedigree_but_root => [forget_oldest(1, @pedigree)]); + $template->param(pedigree_but_two_oldest => [forget_oldest(2, @pedigree)]); + } } # }}} -- cgit v1.2.3 From b857b229d8ff3601bbf01fafa51a3b3d3da928d8 Mon Sep 17 00:00:00 2001 From: intrigeri Date: Mon, 14 Jul 2008 14:16:17 +0200 Subject: pedigree: fix RELDEPTH in PEDIGREE_BUT_ROOT... ... at least when it's not used in the same template as PEDIGREE_BUT_TWO_OLDEST (see Known bugs section in pedigree.mdwn for details) Signed-off-by: intrigeri --- IkiWiki/Plugin/pedigree.pm | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'IkiWiki/Plugin') diff --git a/IkiWiki/Plugin/pedigree.pm b/IkiWiki/Plugin/pedigree.pm index 3d1e3764d..1bc9ee14d 100644 --- a/IkiWiki/Plugin/pedigree.pm +++ b/IkiWiki/Plugin/pedigree.pm @@ -54,8 +54,11 @@ sub forget_oldest ($@) { #{{{ # references to hashes)... $parent=shift @pedigree; # ... but we have no copy of the referenced hashes, so we - # actually are modifying them in-place, but we don't care - # here since reldepth has to be computed everytime anyway. + # actually are modifying them in-place, which + # means the second (and following) calls to + # this function overwrite the previous one's + # reldepth values => known bug if PEDIGREE_BUT_ROOT and + # PEDIGREE_BUT_TWO_OLDEST are used in the same template $parent->{reldepth}=($parent->{absdepth} - $offset); push @ret, $parent; } @@ -68,16 +71,18 @@ sub pagetemplate (@) { #{{{ my $page=$params{page}; my $template=$params{template}; - if ($template->query(name => "pedigree") - or $template->query(name => "pedigree_but_root") - or $template->query(name => "pedigree_but_two_oldest") - ) - { - my @pedigree=pedigree($page); - $template->param(pedigree => \@pedigree); - $template->param(pedigree_but_root => [forget_oldest(1, @pedigree)]); - $template->param(pedigree_but_two_oldest => [forget_oldest(2, @pedigree)]); - } + my @pedigree=pedigree($page) + if ($template->query(name => "pedigree") + or $template->query(name => "pedigree_but_root") + or $template->query(name => "pedigree_but_two_oldest") + ); + + $template->param(pedigree => \@pedigree) + if ($template->query(name => "pedigree")); + $template->param(pedigree_but_root => [forget_oldest(1, @pedigree)]) + if ($template->query(name => "pedigree_but_root")); + $template->param(pedigree_but_two_oldest => [forget_oldest(2, @pedigree)]) + if ($template->query(name => "pedigree_but_two_oldest")); } # }}} -- cgit v1.2.3 From b391b5a80bd0e18a21339e6f33f7710de0c5595d Mon Sep 17 00:00:00 2001 From: intrigeri Date: Mon, 14 Jul 2008 14:52:34 +0200 Subject: pedigree: added DISTANCE loop variable Signed-off-by: intrigeri --- IkiWiki/Plugin/pedigree.pm | 1 + 1 file changed, 1 insertion(+) (limited to 'IkiWiki/Plugin') diff --git a/IkiWiki/Plugin/pedigree.pm b/IkiWiki/Plugin/pedigree.pm index 1bc9ee14d..f91ea94b4 100644 --- a/IkiWiki/Plugin/pedigree.pm +++ b/IkiWiki/Plugin/pedigree.pm @@ -27,6 +27,7 @@ sub pedigree ($) { #{{{ url => urlto($path, $page), page => $title, absdepth => $i, + distance => ($pagedepth - $i), is_root => ($i eq 0), is_second_ancestor => ($i eq 1), is_grand_mother => ($i eq ($pagedepth - 2)), -- cgit v1.2.3 From 55000fd779816fa059e51e9fd01c7e6772b8efc7 Mon Sep 17 00:00:00 2001 From: intrigeri Date: Tue, 15 Jul 2008 12:35:12 +0200 Subject: pedigree: rewrote with different design (and updated testsuite + docs accordingly) Signed-off-by: intrigeri --- IkiWiki/Plugin/pedigree.pm | 57 ++++--------------- doc/plugins/pedigree.mdwn | 112 +++++++++++++------------------------ t/pedigree.t | 109 ++++++------------------------------ t/pedigree/templates/pedigree.tmpl | 6 -- 4 files changed, 69 insertions(+), 215 deletions(-) (limited to 'IkiWiki/Plugin') diff --git a/IkiWiki/Plugin/pedigree.pm b/IkiWiki/Plugin/pedigree.pm index f91ea94b4..eb8bfa83b 100644 --- a/IkiWiki/Plugin/pedigree.pm +++ b/IkiWiki/Plugin/pedigree.pm @@ -18,20 +18,22 @@ sub pedigree ($) { #{{{ my $path=""; my $title=$config{wikiname}; my $i=0; + my $depth=0; + my $height=0; my @pagepath=(split("/", $page)); my $pagedepth=@pagepath; foreach my $dir (@pagepath) { next if $dir eq 'index'; + $depth=$i; + $height=($pagedepth - $depth); push @ret, { url => urlto($path, $page), page => $title, - absdepth => $i, - distance => ($pagedepth - $i), - is_root => ($i eq 0), - is_second_ancestor => ($i eq 1), - is_grand_mother => ($i eq ($pagedepth - 2)), - is_mother => ($i eq ($pagedepth - 1)), + depth => $depth, + height => $height, + "depth_$depth" => 1, + "height_$height" => 1, }; $path.="/".$dir; $title=IkiWiki::pagetitle($dir); @@ -40,51 +42,14 @@ sub pedigree ($) { #{{{ return @ret; } #}}} -sub forget_oldest ($@) { #{{{ - my $offset=shift; - my @pedigree=@_; - my @ret; - my $parent; - unless ($offset ge scalar(@pedigree)) { - for (my $i=0; $i < $offset; $i++) { - shift @pedigree; - } - while (@pedigree) { - # Doing so does not modify the original @pedigree, we've - # got our own copy of its "content" (i.e. a pile of - # references to hashes)... - $parent=shift @pedigree; - # ... but we have no copy of the referenced hashes, so we - # actually are modifying them in-place, which - # means the second (and following) calls to - # this function overwrite the previous one's - # reldepth values => known bug if PEDIGREE_BUT_ROOT and - # PEDIGREE_BUT_TWO_OLDEST are used in the same template - $parent->{reldepth}=($parent->{absdepth} - $offset); - push @ret, $parent; - } - } - return @ret; -} #}}} - sub pagetemplate (@) { #{{{ my %params=@_; my $page=$params{page}; my $template=$params{template}; - my @pedigree=pedigree($page) - if ($template->query(name => "pedigree") - or $template->query(name => "pedigree_but_root") - or $template->query(name => "pedigree_but_two_oldest") - ); - - $template->param(pedigree => \@pedigree) - if ($template->query(name => "pedigree")); - $template->param(pedigree_but_root => [forget_oldest(1, @pedigree)]) - if ($template->query(name => "pedigree_but_root")); - $template->param(pedigree_but_two_oldest => [forget_oldest(2, @pedigree)]) - if ($template->query(name => "pedigree_but_two_oldest")); - + if ($template->query(name => "pedigree")) { + $template->param(pedigree => [pedigree($page)]); + } } # }}} 1 diff --git a/doc/plugins/pedigree.mdwn b/doc/plugins/pedigree.mdwn index 41f70745c..15c032838 100644 --- a/doc/plugins/pedigree.mdwn +++ b/doc/plugins/pedigree.mdwn @@ -1,98 +1,74 @@ [[!template id=plugin name=pedigree author="intrigeri"]] [[!tag type/useful]] -This plugin provides a bunch of loops that one can use in his/her -`HTML::Template`'s to iterate over all or a subset of a page's -parents. One can think of pedigree as "`PARENTLINKS` on steroids". +This plugin offers a `HTML::Template` loop that iterates over all or +a subset of a page's parents, providing a few bonus possibilities, +such as styling the parent links depending on their place in the path. +One can think of pedigree as "`PARENTLINKS` on steroids". [[!toc ]] Content ======= -Loop variables --------------- +This plugin provides one template loop, called `PEDIGREE`, that +returns the same parents list as `PARENTLINKS` would; as a bonus, +every path element returned by the `PEDIGREE` loop has the following +variables set: -Inside any loop provided by the pedigree plugin, every path element -has not only the `URL` and `PAGE` variables, as with `PARENTLINKS`, -but also the following ones: - -* `ABSDEPTH` (positive integer): depth of the path leading to the +* `URL` (string): url to the current path element +* `PAGE` (string): title of the current path element +* `DEPTH` (positive integer): depth of the path leading to the current path element, counting from the wiki's root, which has - `ABSDEPTH=0` -* `DISTANCE` (positive integer): distance, expressed in path elements, + `DEPTH=0` +* `HEIGHT` (positive integer): distance, expressed in path elements, from the current page to the current path element; e.g. this is 1 for the current page's mother, 2 for its grand-mother, etc. -* `IS_ROOT` (boolean): true if, and only if, this path element is the - wiki's root -* `IS_SECOND_ANCESTOR` (boolean): true if, and only if, this path - element is the first one after the wiki's root, on the path leading - to the current page -* `IS_GRAND_MOTHER` (boolean): true if, and only if, this path element - is the current page's grand-mother -* `IS_MOTHER` (boolean): true if, and only if, this path element - is the current page's mother - -Loops ------ - -### `PEDIGREE` - -Returns the same parents list as `PARENTLINKS` would, along with -additional loop variables as explained above. - -### `PEDIGREE_BUT_ROOT` - -Returns the same parents list as `PEDIGREE` would, **but** the wiki -root (i.e. homepage). - -In addition to pedigree's common loop variables, `PEDIGREE_BUT_ROOT` -provides `RELDEPTH` (positive integer), whose value, for a given -parent, is its relative depth, i.e. the depth of the path leading to -it, counting from the first element returned by this loop. - -### `PEDIGREE_BUT_TWO_OLDEST` - -Returns the same parents list as `PEDIGREE` would, **but** the wiki -root (i.e. homepage) and the next path component. - -In addition to pedigree's common loop variables, -`PEDIGREE_BUT_TWO_OLDEST` provides `RELDEPTH`: depth of the path -leading to the current parent, relative to the first element returned -by this loop. +* `DEPTH_n` (boolean): true if, and only if, `DEPTH==n` +* `HEIGHT_n` (boolean): true if, and only if, `HEIGHT==n` Usage ===== +The `DEPTH_n` and `HEIGHT_n` variables allow the template writer to +skip arbitrary elements in the parents list: they are arbitrary +page-range selectors. + +The `DEPTH` and `HEIGHT` variables allow the template writer to apply +general treatment, depending on one of these variables, to *every* +parent: they are counters. + Styling parents depending on their depth ---------------------------------------- Say you want the parent links to be styled depending on their depth in -the path leading to the current page; just add the following lines in -`page.tmpl`: +the path going from the wiki root to the current page; just add the +following lines in `page.tmpl`: - " class="parentdepth"> + " class="depth"> / -Then write the appropriate CSS bits for `a.parentdepth1`, etc. +Then write the appropriate CSS bits for `a.depth1`, etc. -Skip some parents, style the others depending on their distance ---------------------------------------------------------------- +Skip some parents, style the others depending on their distance to the current page +----------------------------------------------------------------------------------- -Say you want to display the parents links, skipping the wiki homepage, -styled depending on their distance from the current page; just add the +Say you want to display all the parents links but the wiki homepage, +styled depending on their distance to the current page; just add the following lines in `page.tmpl`: - - " class="parentdistance"> + + + + " class="height"> / -Then write the appropriate CSS bits for `a.parentdistance1`, etc. +Then write the appropriate CSS bits for `a.height1`, etc. Full-blown example ------------------ @@ -106,9 +82,9 @@ and/or CSS generated for some special path components; e.g.:
    - + - +
  • ">
  • @@ -119,12 +95,12 @@ and/or CSS generated for some special path components; e.g.: - +
    ">
    - +
    ">
    @@ -135,11 +111,3 @@ and/or CSS generated for some special path components; e.g.:
- -Known bugs -========== - -If `PEDIGREE_BUT_ROOT` and `PEDIGREE_BUT_TWO_OLDEST` are used in the -same `HTML::Template`, `RELDEPTH` has wrong values inside the -`PEDIGREE_BUT_ROOT` loop. This can be fixed if anyone needs this to -be working. diff --git a/t/pedigree.t b/t/pedigree.t index aa78cbe67..74871cfa8 100755 --- a/t/pedigree.t +++ b/t/pedigree.t @@ -24,61 +24,15 @@ $expected{'pedigree'} = { "" => [], "ikiwiki" => [], - "ikiwiki/pagespec" => [ - {absdepth => 0, - distance => 2, - is_root => 1, - is_second_ancestor => '', - is_grand_mother => 1, - is_mother => '', - }, - {absdepth => 1, - distance => 1, - is_root => '', - is_second_ancestor => 1, - is_grand_mother => '', - is_mother => 1, - }, - ], - "ikiwiki/pagespec/attachment" => [ - {absdepth => 0, - distance => 3, - is_root => 1, - is_second_ancestor => '', - is_grand_mother => '', - is_mother => '', - }, - {absdepth => 1, - distance => 2, - is_root => '', - is_second_ancestor => 1, - is_grand_mother => 1, - is_mother => '', - }, - {absdepth => 2, - distance => 1, - is_root => '', - is_second_ancestor => '', - is_grand_mother => '', - is_mother => 1, - }, - ], - }; - -$expected{'pedigree_but_root'} = - { - "" => [], - "ikiwiki" => [], - "ikiwiki/pagespec" => [], - "ikiwiki/pagespec/attachment" => [], - }; - -$expected{'pedigree_but_two_oldest'} = - { - "" => [], - "ikiwiki" => [], - "ikiwiki/pagespec" => [], - "ikiwiki/pagespec/attachment" => [], + "ikiwiki/pagespec" => + [ {depth => 0, height => 2, }, + {depth => 1, height => 1, }, + ], + "ikiwiki/pagespec/attachment" => + [ {depth => 0, height => 3, depth_0 => 1, height_3 => 1}, + {depth => 1, height => 2, }, + {depth => 2, height => 1, }, + ], }; # Test function @@ -87,15 +41,6 @@ sub test_loop($$) { my $expected=shift; my $template; my %params; - my $offset; - - if ($loop eq 'pedigree') { - $offset=0; - } elsif ($loop eq 'pedigree_but_root') { - $offset=1; - } elsif ($loop eq 'pedigree_but_two_oldest') { - $offset=2; - } ok($template=template('pedigree.tmpl'), "template created"); ok($params{template}=$template, "params populated"); @@ -103,12 +48,6 @@ sub test_loop($$) { while ((my $page, my $exp) = each %{$expected}) { my @path=(split("/", $page)); my $pagedepth=@path; - my $expdepth; - if (($pagedepth - $offset) >= 0) { - $expdepth=$pagedepth - $offset; - } else { - $expdepth=0; - } my $msgprefix="$page $loop"; # manually run the plugin hook @@ -117,28 +56,18 @@ sub test_loop($$) { IkiWiki::Plugin::pedigree::pagetemplate(%params); my $res=$template->param($loop); - is(scalar(@$res), $expdepth, "$msgprefix: path length"); + is(scalar(@$res), $pagedepth, "$msgprefix: path length"); # logic & arithmetic validation tests - for (my $i=0; $i<$expdepth; $i++) { + for (my $i=0; $i<$pagedepth; $i++) { my $r=$res->[$i]; - is($r->{distance}, $pagedepth - $r->{absdepth}, - "$msgprefix\[$i\]: distance = pagedepth - absdepth"); - ok($r->{absdepth} ge 0, "$msgprefix\[$i\]: absdepth>=0"); - ok($r->{distance} ge 0, "$msgprefix\[$i\]: distance>=0"); - unless ($loop eq 'pedigree') { - ok($r->{reldepth} ge 0, "$msgprefix\[$i\]: reldepth>=0"); - TODO: { - local $TODO = "Known bug" if - (($loop eq 'pedigree_but_root') - && ($i >= $offset)); - is($r->{reldepth} + $offset, $r->{absdepth}, - "$msgprefix\[$i\]: reldepth+offset=absdepth"); - } - } + is($r->{height}, $pagedepth - $r->{depth}, + "$msgprefix\[$i\]: height = pagedepth - depth"); + ok($r->{depth} ge 0, "$msgprefix\[$i\]: depth>=0"); + ok($r->{height} ge 0, "$msgprefix\[$i\]: height>=0"); } # comparison tests, iff the test-suite has been written - if (scalar(@$exp) eq $expdepth) { - for (my $i=0; $i<$expdepth; $i++) { + if (scalar(@$exp) eq $pagedepth) { + for (my $i=0; $i<$pagedepth; $i++) { my $e=$exp->[$i]; my $r=$res->[$i]; map { is($r->{$_}, $e->{$_}, "$msgprefix\[$i\]: $_"); } keys %$e; @@ -151,6 +80,4 @@ sub test_loop($$) { } # Main -map { - test_loop($_, $expected{$_}); -} ('pedigree', 'pedigree_but_root', 'pedigree_but_two_oldest'); +test_loop('pedigree', $expected{'pedigree'}); diff --git a/t/pedigree/templates/pedigree.tmpl b/t/pedigree/templates/pedigree.tmpl index 3590244ee..5fa25733a 100644 --- a/t/pedigree/templates/pedigree.tmpl +++ b/t/pedigree/templates/pedigree.tmpl @@ -2,9 +2,3 @@ - - - - - - -- cgit v1.2.3 From 486f460132434db1eaff92dcadb265011f394bf1 Mon Sep 17 00:00:00 2001 From: intrigeri Date: Tue, 15 Jul 2008 16:09:40 +0200 Subject: pedigree rename to parentlinks: renamed files, to start with Signed-off-by: intrigeri --- IkiWiki/Plugin/parentlinks.pm | 55 +++++++++++++++ IkiWiki/Plugin/pedigree.pm | 55 --------------- doc/plugins/parentlinks.mdwn | 113 +++++++++++++++++++++++++++++++ doc/plugins/pedigree.mdwn | 113 ------------------------------- t/parentlinks.t | 83 +++++++++++++++++++++++ t/parentlinks/templates/parentlinks.tmpl | 4 ++ t/pedigree.t | 83 ----------------------- t/pedigree/templates/pedigree.tmpl | 4 -- 8 files changed, 255 insertions(+), 255 deletions(-) create mode 100644 IkiWiki/Plugin/parentlinks.pm delete mode 100644 IkiWiki/Plugin/pedigree.pm create mode 100644 doc/plugins/parentlinks.mdwn delete mode 100644 doc/plugins/pedigree.mdwn create mode 100755 t/parentlinks.t create mode 100644 t/parentlinks/templates/parentlinks.tmpl delete mode 100755 t/pedigree.t delete mode 100644 t/pedigree/templates/pedigree.tmpl (limited to 'IkiWiki/Plugin') diff --git a/IkiWiki/Plugin/parentlinks.pm b/IkiWiki/Plugin/parentlinks.pm new file mode 100644 index 000000000..eb8bfa83b --- /dev/null +++ b/IkiWiki/Plugin/parentlinks.pm @@ -0,0 +1,55 @@ +#!/usr/bin/perl +# -*- cperl-indent-level: 8; -*- +# Ikiwiki pedigree plugin. +package IkiWiki::Plugin::pedigree; + +use warnings; +use strict; +use IkiWiki 2.00; + +sub import { #{{{ + hook(type => "pagetemplate", id => "pedigree", call => \&pagetemplate); +} # }}} + +sub pedigree ($) { #{{{ + my $page=shift; + + my @ret; + my $path=""; + my $title=$config{wikiname}; + my $i=0; + my $depth=0; + my $height=0; + + my @pagepath=(split("/", $page)); + my $pagedepth=@pagepath; + foreach my $dir (@pagepath) { + next if $dir eq 'index'; + $depth=$i; + $height=($pagedepth - $depth); + push @ret, { + url => urlto($path, $page), + page => $title, + depth => $depth, + height => $height, + "depth_$depth" => 1, + "height_$height" => 1, + }; + $path.="/".$dir; + $title=IkiWiki::pagetitle($dir); + $i++; + } + return @ret; +} #}}} + +sub pagetemplate (@) { #{{{ + my %params=@_; + my $page=$params{page}; + my $template=$params{template}; + + if ($template->query(name => "pedigree")) { + $template->param(pedigree => [pedigree($page)]); + } +} # }}} + +1 diff --git a/IkiWiki/Plugin/pedigree.pm b/IkiWiki/Plugin/pedigree.pm deleted file mode 100644 index eb8bfa83b..000000000 --- a/IkiWiki/Plugin/pedigree.pm +++ /dev/null @@ -1,55 +0,0 @@ -#!/usr/bin/perl -# -*- cperl-indent-level: 8; -*- -# Ikiwiki pedigree plugin. -package IkiWiki::Plugin::pedigree; - -use warnings; -use strict; -use IkiWiki 2.00; - -sub import { #{{{ - hook(type => "pagetemplate", id => "pedigree", call => \&pagetemplate); -} # }}} - -sub pedigree ($) { #{{{ - my $page=shift; - - my @ret; - my $path=""; - my $title=$config{wikiname}; - my $i=0; - my $depth=0; - my $height=0; - - my @pagepath=(split("/", $page)); - my $pagedepth=@pagepath; - foreach my $dir (@pagepath) { - next if $dir eq 'index'; - $depth=$i; - $height=($pagedepth - $depth); - push @ret, { - url => urlto($path, $page), - page => $title, - depth => $depth, - height => $height, - "depth_$depth" => 1, - "height_$height" => 1, - }; - $path.="/".$dir; - $title=IkiWiki::pagetitle($dir); - $i++; - } - return @ret; -} #}}} - -sub pagetemplate (@) { #{{{ - my %params=@_; - my $page=$params{page}; - my $template=$params{template}; - - if ($template->query(name => "pedigree")) { - $template->param(pedigree => [pedigree($page)]); - } -} # }}} - -1 diff --git a/doc/plugins/parentlinks.mdwn b/doc/plugins/parentlinks.mdwn new file mode 100644 index 000000000..15c032838 --- /dev/null +++ b/doc/plugins/parentlinks.mdwn @@ -0,0 +1,113 @@ +[[!template id=plugin name=pedigree author="intrigeri"]] +[[!tag type/useful]] + +This plugin offers a `HTML::Template` loop that iterates over all or +a subset of a page's parents, providing a few bonus possibilities, +such as styling the parent links depending on their place in the path. +One can think of pedigree as "`PARENTLINKS` on steroids". + +[[!toc ]] + +Content +======= + +This plugin provides one template loop, called `PEDIGREE`, that +returns the same parents list as `PARENTLINKS` would; as a bonus, +every path element returned by the `PEDIGREE` loop has the following +variables set: + +* `URL` (string): url to the current path element +* `PAGE` (string): title of the current path element +* `DEPTH` (positive integer): depth of the path leading to the + current path element, counting from the wiki's root, which has + `DEPTH=0` +* `HEIGHT` (positive integer): distance, expressed in path elements, + from the current page to the current path element; e.g. this is + 1 for the current page's mother, 2 for its grand-mother, etc. +* `DEPTH_n` (boolean): true if, and only if, `DEPTH==n` +* `HEIGHT_n` (boolean): true if, and only if, `HEIGHT==n` + +Usage +===== + +The `DEPTH_n` and `HEIGHT_n` variables allow the template writer to +skip arbitrary elements in the parents list: they are arbitrary +page-range selectors. + +The `DEPTH` and `HEIGHT` variables allow the template writer to apply +general treatment, depending on one of these variables, to *every* +parent: they are counters. + +Styling parents depending on their depth +---------------------------------------- + +Say you want the parent links to be styled depending on their depth in +the path going from the wiki root to the current page; just add the +following lines in `page.tmpl`: + + + " class="depth"> + + / + + +Then write the appropriate CSS bits for `a.depth1`, etc. + +Skip some parents, style the others depending on their distance to the current page +----------------------------------------------------------------------------------- + +Say you want to display all the parents links but the wiki homepage, +styled depending on their distance to the current page; just add the +following lines in `page.tmpl`: + + + + + " class="height"> + + / + + +Then write the appropriate CSS bits for `a.height1`, etc. + +Full-blown example +------------------ + +Let's have a look at a more complicated example; combining the boolean +loop variables provided by this plugin (`IS_ROOT` and friends) and +`HTML::Template` flow control structures, you can have custom HTML +and/or CSS generated for some special path components; e.g.: + + +
+
    + + + + + +
  • ">
  • +
    +
    +
    +
+
+ + + + +
+ "> +
+ + +
+ "> +
+
+
+
+ + + + diff --git a/doc/plugins/pedigree.mdwn b/doc/plugins/pedigree.mdwn deleted file mode 100644 index 15c032838..000000000 --- a/doc/plugins/pedigree.mdwn +++ /dev/null @@ -1,113 +0,0 @@ -[[!template id=plugin name=pedigree author="intrigeri"]] -[[!tag type/useful]] - -This plugin offers a `HTML::Template` loop that iterates over all or -a subset of a page's parents, providing a few bonus possibilities, -such as styling the parent links depending on their place in the path. -One can think of pedigree as "`PARENTLINKS` on steroids". - -[[!toc ]] - -Content -======= - -This plugin provides one template loop, called `PEDIGREE`, that -returns the same parents list as `PARENTLINKS` would; as a bonus, -every path element returned by the `PEDIGREE` loop has the following -variables set: - -* `URL` (string): url to the current path element -* `PAGE` (string): title of the current path element -* `DEPTH` (positive integer): depth of the path leading to the - current path element, counting from the wiki's root, which has - `DEPTH=0` -* `HEIGHT` (positive integer): distance, expressed in path elements, - from the current page to the current path element; e.g. this is - 1 for the current page's mother, 2 for its grand-mother, etc. -* `DEPTH_n` (boolean): true if, and only if, `DEPTH==n` -* `HEIGHT_n` (boolean): true if, and only if, `HEIGHT==n` - -Usage -===== - -The `DEPTH_n` and `HEIGHT_n` variables allow the template writer to -skip arbitrary elements in the parents list: they are arbitrary -page-range selectors. - -The `DEPTH` and `HEIGHT` variables allow the template writer to apply -general treatment, depending on one of these variables, to *every* -parent: they are counters. - -Styling parents depending on their depth ----------------------------------------- - -Say you want the parent links to be styled depending on their depth in -the path going from the wiki root to the current page; just add the -following lines in `page.tmpl`: - - - " class="depth"> - - / - - -Then write the appropriate CSS bits for `a.depth1`, etc. - -Skip some parents, style the others depending on their distance to the current page ------------------------------------------------------------------------------------ - -Say you want to display all the parents links but the wiki homepage, -styled depending on their distance to the current page; just add the -following lines in `page.tmpl`: - - - - - " class="height"> - - / - - -Then write the appropriate CSS bits for `a.height1`, etc. - -Full-blown example ------------------- - -Let's have a look at a more complicated example; combining the boolean -loop variables provided by this plugin (`IS_ROOT` and friends) and -`HTML::Template` flow control structures, you can have custom HTML -and/or CSS generated for some special path components; e.g.: - - -
-
    - - - - - -
  • ">
  • -
    -
    -
    -
-
- - - - -
- "> -
- - -
- "> -
-
-
-
- - - - diff --git a/t/parentlinks.t b/t/parentlinks.t new file mode 100755 index 000000000..74871cfa8 --- /dev/null +++ b/t/parentlinks.t @@ -0,0 +1,83 @@ +#!/usr/bin/perl +# -*- cperl-indent-level: 8; -*- +# Testcases for the Ikiwiki pedigree plugin. + +use warnings; +use strict; +use Test::More 'no_plan'; + +my %expected; + +BEGIN { use_ok("IkiWiki"); } + +# Init +%config=IkiWiki::defaultconfig(); +$config{srcdir}=$config{destdir}="/dev/null"; +$config{underlaydir}="underlays/basewiki"; +$config{templatedir}="t/pedigree/templates"; +IkiWiki::loadplugins(); +IkiWiki::checkconfig(); +ok(IkiWiki::loadplugin("pedigree"), "pedigree plugin loaded"); + +# Test data +$expected{'pedigree'} = + { + "" => [], + "ikiwiki" => [], + "ikiwiki/pagespec" => + [ {depth => 0, height => 2, }, + {depth => 1, height => 1, }, + ], + "ikiwiki/pagespec/attachment" => + [ {depth => 0, height => 3, depth_0 => 1, height_3 => 1}, + {depth => 1, height => 2, }, + {depth => 2, height => 1, }, + ], + }; + +# Test function +sub test_loop($$) { + my $loop=shift; + my $expected=shift; + my $template; + my %params; + + ok($template=template('pedigree.tmpl'), "template created"); + ok($params{template}=$template, "params populated"); + + while ((my $page, my $exp) = each %{$expected}) { + my @path=(split("/", $page)); + my $pagedepth=@path; + my $msgprefix="$page $loop"; + + # manually run the plugin hook + $params{page}=$page; + $template->clear_params(); + IkiWiki::Plugin::pedigree::pagetemplate(%params); + my $res=$template->param($loop); + + is(scalar(@$res), $pagedepth, "$msgprefix: path length"); + # logic & arithmetic validation tests + for (my $i=0; $i<$pagedepth; $i++) { + my $r=$res->[$i]; + is($r->{height}, $pagedepth - $r->{depth}, + "$msgprefix\[$i\]: height = pagedepth - depth"); + ok($r->{depth} ge 0, "$msgprefix\[$i\]: depth>=0"); + ok($r->{height} ge 0, "$msgprefix\[$i\]: height>=0"); + } + # comparison tests, iff the test-suite has been written + if (scalar(@$exp) eq $pagedepth) { + for (my $i=0; $i<$pagedepth; $i++) { + my $e=$exp->[$i]; + my $r=$res->[$i]; + map { is($r->{$_}, $e->{$_}, "$msgprefix\[$i\]: $_"); } keys %$e; + } + } + # else { + # diag("Testsuite is incomplete for ($page,$loop); cannot run comparison tests."); + # } + } +} + +# Main +test_loop('pedigree', $expected{'pedigree'}); diff --git a/t/parentlinks/templates/parentlinks.tmpl b/t/parentlinks/templates/parentlinks.tmpl new file mode 100644 index 000000000..5fa25733a --- /dev/null +++ b/t/parentlinks/templates/parentlinks.tmpl @@ -0,0 +1,4 @@ + + + + diff --git a/t/pedigree.t b/t/pedigree.t deleted file mode 100755 index 74871cfa8..000000000 --- a/t/pedigree.t +++ /dev/null @@ -1,83 +0,0 @@ -#!/usr/bin/perl -# -*- cperl-indent-level: 8; -*- -# Testcases for the Ikiwiki pedigree plugin. - -use warnings; -use strict; -use Test::More 'no_plan'; - -my %expected; - -BEGIN { use_ok("IkiWiki"); } - -# Init -%config=IkiWiki::defaultconfig(); -$config{srcdir}=$config{destdir}="/dev/null"; -$config{underlaydir}="underlays/basewiki"; -$config{templatedir}="t/pedigree/templates"; -IkiWiki::loadplugins(); -IkiWiki::checkconfig(); -ok(IkiWiki::loadplugin("pedigree"), "pedigree plugin loaded"); - -# Test data -$expected{'pedigree'} = - { - "" => [], - "ikiwiki" => [], - "ikiwiki/pagespec" => - [ {depth => 0, height => 2, }, - {depth => 1, height => 1, }, - ], - "ikiwiki/pagespec/attachment" => - [ {depth => 0, height => 3, depth_0 => 1, height_3 => 1}, - {depth => 1, height => 2, }, - {depth => 2, height => 1, }, - ], - }; - -# Test function -sub test_loop($$) { - my $loop=shift; - my $expected=shift; - my $template; - my %params; - - ok($template=template('pedigree.tmpl'), "template created"); - ok($params{template}=$template, "params populated"); - - while ((my $page, my $exp) = each %{$expected}) { - my @path=(split("/", $page)); - my $pagedepth=@path; - my $msgprefix="$page $loop"; - - # manually run the plugin hook - $params{page}=$page; - $template->clear_params(); - IkiWiki::Plugin::pedigree::pagetemplate(%params); - my $res=$template->param($loop); - - is(scalar(@$res), $pagedepth, "$msgprefix: path length"); - # logic & arithmetic validation tests - for (my $i=0; $i<$pagedepth; $i++) { - my $r=$res->[$i]; - is($r->{height}, $pagedepth - $r->{depth}, - "$msgprefix\[$i\]: height = pagedepth - depth"); - ok($r->{depth} ge 0, "$msgprefix\[$i\]: depth>=0"); - ok($r->{height} ge 0, "$msgprefix\[$i\]: height>=0"); - } - # comparison tests, iff the test-suite has been written - if (scalar(@$exp) eq $pagedepth) { - for (my $i=0; $i<$pagedepth; $i++) { - my $e=$exp->[$i]; - my $r=$res->[$i]; - map { is($r->{$_}, $e->{$_}, "$msgprefix\[$i\]: $_"); } keys %$e; - } - } - # else { - # diag("Testsuite is incomplete for ($page,$loop); cannot run comparison tests."); - # } - } -} - -# Main -test_loop('pedigree', $expected{'pedigree'}); diff --git a/t/pedigree/templates/pedigree.tmpl b/t/pedigree/templates/pedigree.tmpl deleted file mode 100644 index 5fa25733a..000000000 --- a/t/pedigree/templates/pedigree.tmpl +++ /dev/null @@ -1,4 +0,0 @@ - - - - -- cgit v1.2.3 From 35668b87d3247a7de6c4dbb9edd7e0d909603524 Mon Sep 17 00:00:00 2001 From: intrigeri Date: Tue, 15 Jul 2008 16:25:39 +0200 Subject: pedigree rename to parentlinks: rename/adapt everything * Renamed to parentlinks every single variable or function called pedigree * Removed the parentlinks function from Render.pm * Enabled the new parentlinks plugin by default * Adapted testsuite and documentation to reflate the above facts Signed-off-by: intrigeri --- IkiWiki.pm | 3 ++- IkiWiki/Plugin/parentlinks.pm | 12 +++++------ IkiWiki/Render.pm | 18 ---------------- doc/plugins/parentlinks.mdwn | 37 +++++++++++++++++++++----------- t/parentlinks.t | 13 ++++++----- t/parentlinks/templates/parentlinks.tmpl | 4 ++-- 6 files changed, 40 insertions(+), 47 deletions(-) (limited to 'IkiWiki/Plugin') diff --git a/IkiWiki.pm b/IkiWiki.pm index d4e19c388..bcbbabbe0 100644 --- a/IkiWiki.pm +++ b/IkiWiki.pm @@ -77,7 +77,8 @@ sub defaultconfig () { #{{{ adminuser => undef, adminemail => undef, plugin => [qw{mdwn link inline htmlscrubber passwordauth openid - signinedit lockedit conditional recentchanges}], + signinedit lockedit conditional recentchanges + parentlinks}], libdir => undef, timeformat => '%c', locale => undef, diff --git a/IkiWiki/Plugin/parentlinks.pm b/IkiWiki/Plugin/parentlinks.pm index eb8bfa83b..a94cd469c 100644 --- a/IkiWiki/Plugin/parentlinks.pm +++ b/IkiWiki/Plugin/parentlinks.pm @@ -1,17 +1,17 @@ #!/usr/bin/perl # -*- cperl-indent-level: 8; -*- -# Ikiwiki pedigree plugin. -package IkiWiki::Plugin::pedigree; +# Ikiwiki parentlinks plugin. +package IkiWiki::Plugin::parentlinks; use warnings; use strict; use IkiWiki 2.00; sub import { #{{{ - hook(type => "pagetemplate", id => "pedigree", call => \&pagetemplate); + hook(type => "pagetemplate", id => "parentlinks", call => \&pagetemplate); } # }}} -sub pedigree ($) { #{{{ +sub parentlinks ($) { #{{{ my $page=shift; my @ret; @@ -47,8 +47,8 @@ sub pagetemplate (@) { #{{{ my $page=$params{page}; my $template=$params{template}; - if ($template->query(name => "pedigree")) { - $template->param(pedigree => [pedigree($page)]); + if ($template->query(name => "parentlinks")) { + $template->param(parentlinks => [parentlinks($page)]); } } # }}} diff --git a/IkiWiki/Render.pm b/IkiWiki/Render.pm index 990fcaaa1..8a79119cd 100644 --- a/IkiWiki/Render.pm +++ b/IkiWiki/Render.pm @@ -47,23 +47,6 @@ sub backlinks ($) { #{{{ return @links; } #}}} -sub parentlinks ($) { #{{{ - my $page=shift; - - my @ret; - my $pagelink=""; - my $path=""; - my $title=$config{wikiname}; - - foreach my $dir (split("/", $page)) { - next if $dir eq 'index'; - push @ret, { url => urlto($path, $page), page => $title }; - $path.="/".$dir; - $title=pagetitle($dir); - } - return @ret; -} #}}} - sub genpage ($$) { #{{{ my $page=shift; my $content=shift; @@ -121,7 +104,6 @@ sub genpage ($$) { #{{{ ? $config{wikiname} : pagetitle(basename($page)), wikiname => $config{wikiname}, - parentlinks => [parentlinks($page)], content => $content, backlinks => $backlinks, more_backlinks => $more_backlinks, diff --git a/doc/plugins/parentlinks.mdwn b/doc/plugins/parentlinks.mdwn index 15c032838..ff4139390 100644 --- a/doc/plugins/parentlinks.mdwn +++ b/doc/plugins/parentlinks.mdwn @@ -1,20 +1,19 @@ -[[!template id=plugin name=pedigree author="intrigeri"]] -[[!tag type/useful]] +[[!template id=plugin name=parentlinks core=1 author="[[intrigeri]]"]] +[[!tag type/link]] This plugin offers a `HTML::Template` loop that iterates over all or -a subset of a page's parents, providing a few bonus possibilities, -such as styling the parent links depending on their place in the path. -One can think of pedigree as "`PARENTLINKS` on steroids". +a subset of a page's parents. It also provides a few bonus +possibilities, such as styling the parent links depending on their +place in the path. [[!toc ]] Content ======= -This plugin provides one template loop, called `PEDIGREE`, that -returns the same parents list as `PARENTLINKS` would; as a bonus, -every path element returned by the `PEDIGREE` loop has the following -variables set: +This plugin provides one template loop, called `PARENTLINKS`, that +returns the list of parent pages for the current page. Every returned +path element has the following variables set: * `URL` (string): url to the current path element * `PAGE` (string): title of the current path element @@ -38,6 +37,18 @@ The `DEPTH` and `HEIGHT` variables allow the template writer to apply general treatment, depending on one of these variables, to *every* parent: they are counters. +Basic usage +----------- + +As in the default `page.tmpl`, one can simply display the list of +parent pages: + + + / + + + + Styling parents depending on their depth ---------------------------------------- @@ -45,7 +56,7 @@ Say you want the parent links to be styled depending on their depth in the path going from the wiki root to the current page; just add the following lines in `page.tmpl`: - + " class="depth"> / @@ -60,7 +71,7 @@ Say you want to display all the parents links but the wiki homepage, styled depending on their distance to the current page; just add the following lines in `page.tmpl`: - + " class="height"> @@ -81,7 +92,7 @@ and/or CSS generated for some special path components; e.g.:
    - + @@ -94,7 +105,7 @@ and/or CSS generated for some special path components; e.g.:
- +
"> diff --git a/t/parentlinks.t b/t/parentlinks.t index 74871cfa8..593937a97 100755 --- a/t/parentlinks.t +++ b/t/parentlinks.t @@ -1,6 +1,6 @@ #!/usr/bin/perl # -*- cperl-indent-level: 8; -*- -# Testcases for the Ikiwiki pedigree plugin. +# Testcases for the Ikiwiki parentlinks plugin. use warnings; use strict; @@ -14,13 +14,12 @@ BEGIN { use_ok("IkiWiki"); } %config=IkiWiki::defaultconfig(); $config{srcdir}=$config{destdir}="/dev/null"; $config{underlaydir}="underlays/basewiki"; -$config{templatedir}="t/pedigree/templates"; +$config{templatedir}="t/parentlinks/templates"; IkiWiki::loadplugins(); IkiWiki::checkconfig(); -ok(IkiWiki::loadplugin("pedigree"), "pedigree plugin loaded"); # Test data -$expected{'pedigree'} = +$expected{'parentlinks'} = { "" => [], "ikiwiki" => [], @@ -42,7 +41,7 @@ sub test_loop($$) { my $template; my %params; - ok($template=template('pedigree.tmpl'), "template created"); + ok($template=template('parentlinks.tmpl'), "template created"); ok($params{template}=$template, "params populated"); while ((my $page, my $exp) = each %{$expected}) { @@ -53,7 +52,7 @@ sub test_loop($$) { # manually run the plugin hook $params{page}=$page; $template->clear_params(); - IkiWiki::Plugin::pedigree::pagetemplate(%params); + IkiWiki::Plugin::parentlinks::pagetemplate(%params); my $res=$template->param($loop); is(scalar(@$res), $pagedepth, "$msgprefix: path length"); @@ -80,4 +79,4 @@ sub test_loop($$) { } # Main -test_loop('pedigree', $expected{'pedigree'}); +test_loop('parentlinks', $expected{'parentlinks'}); diff --git a/t/parentlinks/templates/parentlinks.tmpl b/t/parentlinks/templates/parentlinks.tmpl index 5fa25733a..3ca3b0030 100644 --- a/t/parentlinks/templates/parentlinks.tmpl +++ b/t/parentlinks/templates/parentlinks.tmpl @@ -1,4 +1,4 @@ - + - + -- cgit v1.2.3