[MKDoc-commit] [1.6] replace old sitemap with group-unaware version of dynamic sitemap

bruno at mkdoc.demon.co.uk bruno at mkdoc.demon.co.uk
Thu Aug 18 13:20:43 BST 2005


Log Message:
-----------
[1.6] replace old sitemap with group-unaware version of dynamic sitemap

Tags:
----
mkdoc-1-6

Modified Files:
--------------
    mkd/flo/plugin:
        Sitemap.pm
    mkd/templates/sitemap:
        en.html
        nesting.html

-------------- next part --------------
Index: Sitemap.pm
===================================================================
RCS file: /var/spool/cvs/mkd/flo/plugin/Sitemap.pm,v
retrieving revision 1.7.2.14
retrieving revision 1.7.2.15
diff -Lflo/plugin/Sitemap.pm -Lflo/plugin/Sitemap.pm -u -r1.7.2.14 -r1.7.2.15
--- flo/plugin/Sitemap.pm
+++ flo/plugin/Sitemap.pm
@@ -1,8 +1,8 @@
-# -------------------------------------------------------------------------------------
+# ------------------------------------------------------------------------
 # flo::plugin::Sitemap
-# -------------------------------------------------------------------------------------
-# Author : Jean-Michel Hiver.
-# Copyright : (c) MKDoc Holdings Ltd, 2003.
+# ------------------------------------------------------------------------
+# Author : Sam Tregar.
+# Copyright : (c) MKDoc Holdings Ltd, 2005.
 #
 # This file is part of MKDoc. 
 # 
@@ -20,127 +20,139 @@
 # along with MKDoc; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 #
-# -------------------------------------------------------------------------------------
+# ------------------------------------------------------------------------
 package flo::plugin::Sitemap;
-use flo::Standard;
 use strict;
-use base qw /flo::Plugin/;
-use Encode;
+use warnings;
 
-our $Level = 0;
+=head1 NAME
 
+flo::plugin::Sitemap - a sitemap which shows nearby documents only
 
-sub _name_default { '.sitemap.html' }
+=head1 SYNOPSIS
+
+Access this plugin like this:
+
+  http://www.example.com/.sitemap.html
+  http://www.example.com/foo/.sitemap.html
+
+=head1 DESCRIPTION
+
+This plugin implements a scope-limited sitemap for a given document.
+Links are shown for all ancestores, direct children and siblings.  If
+the user has permission to visit a document it will be a link.  If it
+has children of its own it will show a [+].  Hidden documents are
+shown only to editors.
+
+=cut
+
+use flo::Standard;
+use lib::sql::Condition;
+use base qw /flo::Plugin/;
 
+sub _name_default { '.sitemap.html' }
 
-sub run
-{
+sub run {
     my $self = shift;
-    $| = 1;
     $self->render_http (
-	self       => $self,
-	root       => _structure (_results()),
-	__input__  => 'XML',
-	__output__ => 'XHTML',
+      self       => $self,
+      __input__  => 'XML',
+      __output__ => 'XHTML',
        );
+
     return 'TERMINATE';
 }
 
-
 sub template_path { '/sitemap' }
 
+sub nodes {
+    my $self = shift;
+    my $doc  = flo::Standard::current_document();
+    $self->{_current_doc} = $doc;
 
-sub _structure
-{
-    my $results = shift;
-    local $Level = 0;
-    
-    my $res = _structure_recurse ($results);
-    _reorder ($res->[0]);
-    return shift (@{$res});
-}
+    my ($tree, $parent, $cur) = $self->ancestor_tree($doc);
 
+    # add nodes for siblings if there is a parent (i.e. not for root)
+    $self->add_children($parent, $doc->parent) if $parent;
 
-sub _reorder
-{
-    my $node = shift;
-    return unless (defined $node->{Children} && scalar @{$node->{Children}});
-    
-    my $sort_on = $node->{Sort_By};
-    my $desc    = $node->{Order_By};
-    
-    if ($sort_on eq 'Sibling_Position')
-    {
-	$node->{Children} = ($desc) ?
-	[ sort { $b->{$sort_on} <=> $a->{$sort_on} } @{$node->{Children}} ] :
-	[ sort { $a->{$sort_on} <=> $b->{$sort_on} } @{$node->{Children}} ];
-    }
-    else
-    {
-	$node->{Children} = ($desc) ?
-	[ sort { $b->{$sort_on} cmp $a->{$sort_on} } @{$node->{Children}} ] :
-	[ sort { $a->{$sort_on} cmp $b->{$sort_on} } @{$node->{Children}} ];
-    }
-    
-    _reorder ($_) for (@{$node->{Children}});
-}
+    # add child nodes for the current doc
+    $self->add_children($cur, $doc);
 
+    return $tree;
+}
 
-sub _structure_recurse
-{
-    $Level++;
+# adds child nodes, sorting based on document sort ordering
+sub add_children {
+    my ($self, $node, $doc) = @_;
+    my $cur = $self->{_current_doc};
+    
+    my @nodes = map { $self->new_node($_) } 
+                grep { $_->id != $cur->id }
+                $self->get_children($doc);
+    push @{$node->{children}}, @nodes;
 
-    my $flat = shift;
-    scalar @{$flat} <= 1 and return [ shift @{$flat} ];
+    # sort the sibling list
+    $node->{children} = $self->sort_children($doc, $node->{children});
+}
 
-    my @res = shift @{$flat};
-    while ( scalar @{$flat} )
-    {   
-        my $next_lvl = $flat->[0]->{Full_Path} =~ tr/\//\//;
-        ($Level == $next_lvl) and do {
-            push @res, shift @{$flat};
-            next;
-        };
+sub sort_children {
+    my ($self, $doc, $nodes) = @_;
 
-        ($Level < $next_lvl)  and do {
-            $res[-1]->{Children} = _structure_recurse ($flat);
-        };
+    my $sort_on = $doc->{Sort_By};
 
-        ($Level > $next_lvl) and last;
+    my @result;
+    if ($sort_on eq 'Sibling_Position') {
+	@result = sort { $a->{doc}{$sort_on} <=> $b->{doc}{$sort_on} } @$nodes;
+    } else {
+	@result = sort { $a->{doc}{$sort_on} cmp $b->{doc}{$sort_on} } @$nodes;
     }
 
-    $Level--;
-    return [ @res ];
+    @result = reverse @result if $doc->{Order_By};
+
+    return \@result;
 }
 
+sub ancestor_tree {
+    my ($self, $doc) = @_;
 
-sub _results
-{
-    my $sql  = <<EOF;
-SELECT Description, Full_Path, Title, Lang, Sort_By, Order_By, Date_Created, Date_Last_Modified, Sibling_Position
-FROM Document
-ORDER BY Full_Path ASC
-EOF
-    
-    my $dbh = lib::sql::DBH->get();
-    my $sth = $dbh->prepare_cached ($sql);
-    $sth->execute();
-    
-    my @res = ();
-    while (my $h = $sth->fetchrow_hashref())
-    {
-	Encode::_utf8_on ($h->{Description});
-	Encode::_utf8_on ($h->{Title});
-	
-	# stick stuff in the sitemap only if it's showable
-	my $o = bless $h, 'flo::Record::Document';
-	push @res, $h if ($o->is_showable());
+    # build nodes for the ancestors and the current doc
+    my @nodes = map { $self->new_node($_) } ($doc->ancestors, $doc);
+
+    # build the tree, top down
+    my $head = (my $p = shift @nodes);
+    my $last;
+    foreach my $node (@nodes) {
+        $last = $p;
+        $p->{children} = [ $node ];
+        $p = $p->{children}[0];
     }
-    return \@res;
+
+    # return the root and pointer to the current doc and its parent
+    return ($head, $last, $p);
 }
 
+# creates a new node hash for use in the template, takes a document as
+# the sole arg
+sub new_node {
+    my ($self, $doc) = @_;
+    my $children = $self->get_children($doc);
+    return { 
+            doc          => $doc,
+            has_children => @$children ? 1 : 0,
+            is_selected  => $doc->id == $self->{_current_doc}->id,
+           };
+}
 
-1;
+# fetch children from a document, allowing editors to see hidden documents
+sub get_children {
+    my ($self, $doc) = @_;
+    return $doc->children_showable() 
+      unless $self->user and $self->user->group eq 'editor';
 
+    # filter out unshowable but keep the hidden ones
+    my @children = grep { $_->is_showable or $_->is_hidden } $doc->children();
+    
+    return wantarray ? @children : \@children;
+}
 
-__END__
+1;
Index: nesting.html
===================================================================
RCS file: /var/spool/cvs/mkd/templates/sitemap/Attic/nesting.html,v
retrieving revision 1.1.2.6
retrieving revision 1.1.2.7
diff -Ltemplates/sitemap/nesting.html -Ltemplates/sitemap/nesting.html -u -r1.1.2.6 -r1.1.2.7
--- templates/sitemap/nesting.html
+++ templates/sitemap/nesting.html
@@ -1,20 +1,74 @@
-<li xmlns:petal="http://purl.org/petal/1.0/">
-  <a 
-    href="#"
-    hreflang="en"
+<li 
+  xmlns:petal="http://purl.org/petal/1.0/"
+>
+  <dl class="sitemap-item">
+  <dt>
+  <tt 
+    class="expand"
+    petal:condition="false: child/is_selected;
+                     true:  child/has_children"
+  >[<a 
+      href="#"
+      hreflang="en"
+      lang="en"
+      xml:lang="en"
+      title="Expand sitemap for ${child/doc/Title}"
+      petal:attributes="href string:${child/doc/Full_Path}.sitemap.html; 
+                        hreflang child/doc/Lang; 
+                        lang child/doc/Lang;
+                        xml:lang child/doc/Lang"
+    >+</a>]</tt>
+  <tt
+    class="contract"
+    petal:condition="true: child/is_selected;
+                     true: child/has_children"
+  >[<a 
+      href="#"
+      hreflang="en"
+      lang="en"
+      xml:lang="en"
+      title="Contract sitemap for ${child/doc/Title}"
+      petal:attributes="href string:${child/doc/Full_Path}../.sitemap.html; 
+                        hreflang child/doc/Lang; 
+                        lang child/doc/Lang;
+                        xml:lang child/doc/Lang"
+    >&#8211;</a>]</tt>
+  <tt
+    petal:condition="false: child/has_children"
+  >[<span class="bullet">&#8226;</span>]</tt> 
+  <strong petal:omit-tag="false: child/is_selected">  
+    <a 
+      href="#"
+      hreflang="en"
+      lang="en"
+      xml:lang="en"
+      petal:attributes="href child/doc/Full_Path; 
+                        hreflang child/doc/Lang; 
+                        title child/doc/Description; 
+                        lang child/doc/Lang;
+                        xml:lang child/doc/Lang"
+      petal:content="child/doc/Title"
+    >
+        Child Document Title
+    </a>
+  </strong>
+  </dt>
+  <dd
     lang="en"
     xml:lang="en"
-    petal:attributes="href child/Full_Path; 
-                      hreflang child/Lang; 
-                      lang child/Lang;
-                      xml:lang child/Lang"
-    petal:content="child/Title"
-  >Child Document Title</a>
-  <ul 
-    petal:define="children child/Children"
+    petal:attributes="lang child/doc/Lang;
+                      xml:lang child/doc/Lang"
+    petal:content="child/doc/Description"
+    >
+    Child document description goes here
+  </dd>
+  </dl>
+  <ul
+    class="sitemap"
+    petal:define="children child/children"
     petal:condition="children"
     petal:repeat="child children"
   >
-  <?include file="/sitemap/nesting.html"?>
+    <?include file="/sitemap/nesting.html"?>
   </ul>
 </li>
Index: en.html
===================================================================
RCS file: /var/spool/cvs/mkd/templates/sitemap/Attic/en.html,v
retrieving revision 1.1.2.23
retrieving revision 1.1.2.24
diff -Ltemplates/sitemap/en.html -Ltemplates/sitemap/en.html -u -r1.1.2.23 -r1.1.2.24
--- templates/sitemap/en.html
+++ templates/sitemap/en.html
@@ -14,28 +14,19 @@
   dir="ltr"
   petal:define="
     here                  self;
-    uri                   self/uri;
     title                 string:Sitemap;
+    uri                   self/uri;
     lang                  self/lang;
     dir                   self/direction;
     align                 self/align;
     align_opposite        self/align_opposite;
-    root_uri              self/root/uri;
-    root_title            self/root/title;
-    root_lang             self/root/lang;
-    parent_uri            self/parent/uri;
-    parent_lang           self/parent/lang;
-    parent_title          self/parent/title;
-    sitemap               plugin: flo::plugin::Sitemap;
-    search                plugin: flo::plugin::Search;
-    search_uri            search/uri;
-            "
+    sitemap               plugin: flo::plugin::Sitemap;"
   petal:attributes="lang lang; xml:lang lang; dir dir;"
   xmlns:petal="http://purl.org/petal/1.0/"
   xmlns="http://www.w3.org/1999/xhtml"
 >
 
-<!--? This is the <head> for public documents ?-->
+<!--? public file header ?-->
 <?include file="/fragments/head_public/"?>
 
   <body 
@@ -46,36 +37,28 @@
     petal:set="child root"
   >
 
-<!--? This is the header it contains the navigational elements at the top of the page. ?-->
+<!--? navigation header ?-->
 <?include file="/fragments/header/"?>
 
-    <div class="content">
-
-          <h1 
-            lang="en"
-            xml:lang="en"
-            dir="ltr"
-            align="left"
-            petal:attributes="align align"
-          >
-            <a
-              id="page_content"
-              name="page_content"
-              petal:content="title"
-            >Sitemap</a>
-          </h1>
-           
-          <ul>
-            <?include file="/sitemap/nesting.html"?>
-          </ul>
-
-    </div>
+  <div class="content">
+    <h1 
+      lang="en"
+      xml:lang="en"
+      dir="ltr"
+      align="left"
+      petal:attributes="align align"
+      petal:define="child self/nodes"
+    >
+      Sitemap
+    </h1>
+ 
+    <ul 
+      class="sitemap"
+    >
+       <?include file="/sitemap/nesting.html"?>
+    </ul>
+  </div>    
 
-    <div class="sidebar">
-
-        <!--? This is the list of link components. ?-->
-        <?include file="/fragments/menu_quick_links/"?>
-
-    </div>
   </body>
 </html>
+


More information about the MKDoc-commit mailing list