2007-07-27 07:09:54 +02:00
|
|
|
Sometimes you want to match a page only if it has certain properties. The use
|
|
|
|
case I have in mind is this: show me all the pages that have children. You
|
|
|
|
can't do that with a pagespec, so I created a plugin that adds some pagespec
|
|
|
|
functions.
|
|
|
|
|
|
|
|
`match_relative(blah)` will match a page x if a pagespec from x would match
|
|
|
|
`blah`. This is only actually useful with relative pagespecs.
|
|
|
|
|
|
|
|
`match_has_child(blah)` will match a child if it has a descendant named
|
|
|
|
`blah`. If blah is empty, any child will match.
|
|
|
|
|
|
|
|
So if I have:
|
|
|
|
|
|
|
|
* foo
|
|
|
|
* foo/blah
|
|
|
|
* foo/bar
|
|
|
|
* foo/bar/blah
|
|
|
|
* foo/bar/bahoo
|
|
|
|
* foo/baz
|
|
|
|
* foo/baz/goo
|
|
|
|
* foo/baz/goo/blah
|
|
|
|
|
|
|
|
A pagespec `match_relative(./blah)` will match `foo/bar/bahoo`, because
|
|
|
|
a pagespec of `./blah` from `bahoo` would match `foo/bar/blah`. A
|
|
|
|
pagespec of `match_has_child(blah)` would match `foo`, `foo/bar`,
|
|
|
|
`foo/baz`, and `foo/baz/goo`.
|
|
|
|
|
|
|
|
Note that if you try to inline `*/blah` you will match `foo/blah`,
|
|
|
|
`foo/bar/blah`, and `foo/baz/goo/blah` -- that is, the blah pages
|
|
|
|
themselves rather than any relatives of theirs.
|
|
|
|
|
|
|
|
This patch is useful for (among other things) constructing blogging
|
2007-08-08 16:14:19 +02:00
|
|
|
systems where leaf nodes are organized hierarchically; using `has_child`,
|
2007-07-27 07:09:54 +02:00
|
|
|
you can inline only leaf nodes and ignore "intermediate" nodes.
|
2007-08-08 16:14:19 +02:00
|
|
|
`match_relative` can be used recursively to match properties of arbitrary
|
2007-07-27 07:09:54 +02:00
|
|
|
complexity: "show me all the pages who have children called foo that
|
|
|
|
have children called blah". I'm not sure what use it is, though.
|
|
|
|
|
|
|
|
You can see the patch in action at
|
|
|
|
<http://ikidev.betacantrips.com/conditionaltest/>,
|
|
|
|
so named because I had hoped that something in conditional.pm could
|
|
|
|
help me. I know the name "relative" sucks, feel free to come up with a
|
|
|
|
better one. --Ethan
|
|
|
|
|
|
|
|
<pre>
|
|
|
|
diff -urNX ignorepats ikiwiki/IkiWiki/Plugin/relative.pm ikidev/IkiWiki/Plugin/relative.pm
|
|
|
|
--- ikiwiki/IkiWiki/Plugin/relative.pm 1969-12-31 16:00:00.000000000 -0800
|
|
|
|
+++ ikidev/IkiWiki/Plugin/relative.pm 2007-07-26 21:48:10.642686000 -0700
|
|
|
|
@@ -0,0 +1,39 @@
|
|
|
|
+#!/usr/bin/perl
|
|
|
|
+# relative.pm: support for pagespecs on possible matches
|
|
|
|
+package IkiWiki::Plugin::relative;
|
|
|
|
+
|
|
|
|
+use warnings;
|
|
|
|
+use strict;
|
|
|
|
+use IkiWiki 2.00;
|
|
|
|
+
|
|
|
|
+package IkiWiki::PageSpec;
|
|
|
|
+
|
2008-12-17 21:22:16 +01:00
|
|
|
+sub match_relative($$;@) {
|
2007-07-27 07:09:54 +02:00
|
|
|
+ my $parent = shift;
|
|
|
|
+ my $spec = shift;
|
|
|
|
+ my %params = @_;
|
|
|
|
+
|
|
|
|
+ foreach my $page (keys %IkiWiki::pagesources) {
|
|
|
|
+ next if $page eq $parent;
|
|
|
|
+ if (IkiWiki::pagespec_match($page, $spec, location => $parent)) {
|
|
|
|
+ return IkiWiki::SuccessReason->new("$parent can match $spec against $page");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return IkiWiki::FailReason->new("$parent can't match $spec against anything");
|
2008-12-17 21:22:16 +01:00
|
|
|
+}
|
2007-07-27 07:09:54 +02:00
|
|
|
+
|
2008-12-17 21:22:16 +01:00
|
|
|
+sub match_has_child($$;@) {
|
2007-07-27 07:09:54 +02:00
|
|
|
+ my $page = shift;
|
|
|
|
+ my $childname = shift;
|
|
|
|
+ my $spec;
|
2008-12-17 21:22:16 +01:00
|
|
|
+ if ($childname) {
|
2007-07-27 07:09:54 +02:00
|
|
|
+ $spec = "$page/$childname or $page/*/$childname";
|
2008-12-17 21:22:16 +01:00
|
|
|
+ }
|
|
|
|
+ else {
|
2007-07-27 07:09:54 +02:00
|
|
|
+ $spec = "$page/*";
|
2008-12-17 21:22:16 +01:00
|
|
|
+ }
|
2007-07-27 07:09:54 +02:00
|
|
|
+
|
|
|
|
+ return match_relative($page, $spec, @_);
|
2008-12-17 21:22:16 +01:00
|
|
|
+}
|
2007-07-27 07:09:54 +02:00
|
|
|
+
|
|
|
|
+1
|
2007-08-08 16:21:36 +02:00
|
|
|
</pre>
|
|
|
|
|
2008-07-21 13:31:33 +02:00
|
|
|
[[!tag patch]]
|
2007-08-12 00:46:15 +02:00
|
|
|
|
2007-08-08 16:21:36 +02:00
|
|
|
> This looks really interesting. It reminds me of XPath and its conditionals.
|
|
|
|
> Those might actually work well adapted to pagespecs. For instance, to write
|
|
|
|
> "match any page with a child blah", you could just write *[blah] , or if you
|
|
|
|
> don't want to use relative-by-default in the conditionals, *[./blah].
|
|
|
|
> -- [[JoshTriplett]]
|