Author: fmeschbe
Date: Sun May 20 15:29:00 2012
New Revision: 1340753

URL: http://svn.apache.org/viewvc?rev=1340753&view=rev
Log:
SLING-2002 Multiple fixes:
 - Add Sitemap
 - Properly support child pages
 - Add support for Django templates in content
 - Fix breadcrumb using the page title if available

Added:
    sling/site/trunk/content/sitemap.html
Modified:
    sling/site/trunk/lib/path.pm
    sling/site/trunk/lib/view.pm
    sling/site/trunk/readme.txt
    sling/site/trunk/templates/sidenav.mdtext

Added: sling/site/trunk/content/sitemap.html
URL: 
http://svn.apache.org/viewvc/sling/site/trunk/content/sitemap.html?rev=1340753&view=auto
==============================================================================
--- sling/site/trunk/content/sitemap.html (added)
+++ sling/site/trunk/content/sitemap.html Sun May 20 15:29:00 2012
@@ -0,0 +1,3 @@
+{% extends "skeleton.html" %}
+{% block title %}Sitemap{% endblock %}</h1>
+{% block content %}{{ sitemap|markdown }}{% endblock %}

Modified: sling/site/trunk/lib/path.pm
URL: 
http://svn.apache.org/viewvc/sling/site/trunk/lib/path.pm?rev=1340753&r1=1340752&r2=1340753&view=diff
==============================================================================
--- sling/site/trunk/lib/path.pm (original)
+++ sling/site/trunk/lib/path.pm Sun May 20 15:29:00 2012
@@ -6,7 +6,7 @@ use ASF::Value;
 our @patterns = (
        [qr!\.mdtext$!, single_narrative => { template => 
"single_narrative.html" }],
 
-#      [qr!^/sling/sitemap\.html$!, sitemap => { headers => { title => "Sling 
Sitemap" }} ],
+       [qr!^/sitemap\.html$!, sitemap => { headers => { title => "Sling 
Sitemap" }} ],
 
 ) ;
 

Modified: sling/site/trunk/lib/view.pm
URL: 
http://svn.apache.org/viewvc/sling/site/trunk/lib/view.pm?rev=1340753&r1=1340752&r2=1340753&view=diff
==============================================================================
--- sling/site/trunk/lib/view.pm (original)
+++ sling/site/trunk/lib/view.pm Sun May 20 15:29:00 2012
@@ -21,7 +21,7 @@ push @Dotiac::DTL::TEMPLATE_DIRS, "templ
 # This is most widely used view.  It takes a
 # 'template' argument and a 'path' argument.
 # Assuming the path ends in foo.mdtext, any files
-# like foo.page/bar.mdtext will be parsed and
+# like foo/bar.mdtext will be parsed and
 # passed to the template in the "bar" (hash)
 # variable.
 
@@ -36,7 +36,7 @@ sub single_narrative {
     read_text_file $file, \%args;
 
     my $page_path = $file;
-    $page_path =~ s/\.[^.]+$/.page/;
+    $page_path =~ s/\.[^.]+$//;
     if (-d $page_path) {
         for my $f (grep -f, glob "$page_path/*.mdtext") {
             $f =~ m!/([^/]+)\.mdtext$! or die "Bad filename: $f\n";
@@ -54,10 +54,16 @@ sub single_narrative {
 #              print STDOUT "$ke \n";
 #      }
 
+    # use the content as a template if it contains Django templates
+    if ($args{content} =~ /\{[{%][^}]*[%}]\}/) {
+        print STDOUT "Applying $args{path} as a Django template\n";
+        $args{content} = Dotiac::DTL->new(\$args{content})->render(\%args);
+    }
+    
     return Dotiac::DTL::Template($template)->render(\%args), html => \%args;
 }
 
-# Has the same behavior as the above for foo.page/bar.txt
+# Has the same behavior as the above for foo/bar.txt
 # files, parsing them into a bar variable for the template.
 # Otherwise presumes the template is the path.
 
@@ -67,7 +73,7 @@ sub news_page {
     $args{breadcrumbs} = breadcrumbs($args{path});
 
     my $page_path = $template;
-    $page_path =~ s/\.[^.]+$/.page/;
+    $page_path =~ s/\.[^.]+$//;
     if (-d $page_path) {
         for my $f (grep -f, glob "$page_path/*.mdtext") {
             $f =~ m!/([^/]+)\.mdtext$! or die "Bad filename: $f\n";
@@ -83,47 +89,105 @@ sub news_page {
     return Dotiac::DTL::Template($template)->render(\%args), html => \%args;
 }
 
+# Recursive Sitemap generation
+# Taken from: http://svn.apache.org/repos/asf/chemistry/site/trunk/lib/view.pm
 sub sitemap {
-    my %args = @_;
-    my $template = "content$args{path}";
-    $args{breadcrumbs} .= breadcrumbs($args{path});
-    my $dir = $template;
-    $dir =~ s!/[^/]+$!!;
-    opendir my $dh, $dir or die "Can't opendir $dir: $!\n";
-    my %data;
-    for (map "$dir/$_", grep $_ ne "." && $_ ne ".." && $_ ne ".svn", readdir 
$dh) {
-        if (-f and /\.mdtext$/) {
-            my $file = $_;
-            $file =~ s/^content//;
-            no warnings 'once';
-            for my $p (@path::patterns) {
-                my ($re, $method, $args) = @$p;
-                next unless $file =~ $re;
-                my $s = view->can($method) or die "Can't locate method: 
$method\n";
-                my ($content, $ext, $vars) = $s->(path => $file, %$args);
-                $file =~ s/\.mdtext$/.$ext/;
-                $data{$file} = $vars;
-                last;
-            }
-        }
-    }
+   my %args = @_;
+   my $template = "content$args{path}";
+   my $file = $template;
+
+   # Find the list of files
+   my ($dir) = ($file =~ /^(.*)\/.*?/);
+   my $entries = {};
+   sitemapFind($dir, $entries);
+
+   my $sitemap = "<ul>\n";
+   $sitemap = sitemapRender($sitemap, $entries, "");
+   $sitemap .= "</ul>\n";
+   $args{sitemap} = $sitemap;
+   
+   return Dotiac::DTL::Template($template)->render(\%args), html => \%args;   
+}
 
-    my $content = "";
+sub sitemapFind {
+   my ($dir, $entries) = @_;
+   $entries->{"title"} = "";
+   $entries->{"entries"} = {};
+   my %entries = ( "title"=>"", "entries"=>{} );
+
+   foreach my $item (<$dir/*>) {
+      my ($rel) = ($item =~ /^.*\/(.*?)$/);
+
+      if(-d $item) {
+         $rel .= ".mdtext" if(-f "$item.mdtext");
+         $entries->{"entries"}->{$rel} = {};
+         sitemapFind($item, $entries->{"entries"}->{$rel});
+      } else {
+         # Grab the title
+         my $title = $rel;
+         if($rel =~ /\.mdtext$/) {
+             my %args;
+             read_text_file $item, \%args;
+             $title = $args{"headers"}->{"title"};
+         } elsif ($rel =~ /\.png$/ || $rel =~ /\.jpg$/) {
+            next;
+         } else {
+             open F, "<$item";
+             my $file = "";
+             while(my $line = <F>) {
+                $file .= $line;
+             }
+             close F;
+
+             if($file =~ /block\s+title\s*\%\}(.*?)\{/) {
+                $title = $1;
+             } elsif($file =~ /title\>(.*?)\</) {
+                $title = $1;
+             }
+         }
+
+         # Process
+         if($rel =~ /^index\.(html|mdtext)$/) {
+            $entries->{"title"} = $title;
+         } else {
+            $entries->{entries}->{$rel}->{title} = $title;
+         }
+      }
+   }
+   return %entries;
+}
 
-    for (sort keys %data) {
-        $content .= "- [$data{$_}->{headers}->{title}]($_)\n";
-        for my $hdr (grep /^#/, split "\n", $data{$_}->{content}) {
-            $hdr =~ /^(#+)\s+([^#]+)?\s+\1\s+\{#([^}]+)\}$/ or next;
-            my $level = length $1;
-            $level *= 4;
-            $content .= " " x $level;
-            $content .= "- [$2]($_#$3)\n";
-        }
-    }
-    $args{content} = $content;
-    return Dotiac::DTL::Template($template)->render(\%args), html => \%args;
+sub sitemapRender {
+   my ($sitemap, $dir, $path) = @_;
+   my %entries = %{$dir->{"entries"}};
+
+   foreach my $e (sort keys %entries) {
+      my $fn = $e;
+      $fn =~ s/\.mdtext/.html/;
+      if($fn eq "images/" or $fn eq "resources/") {
+         next;
+      }
+
+      my $title = $entries{$e}->{title};
+      unless($title) {
+         $title = $e;
+      }
+
+      $sitemap .= "<li><a href=\"$path/$fn\">".$title."</a>";
+      if($entries{$e}->{entries}) {
+         my $parent = $e;
+         $parent =~ s/\.mdtext$//;
+         $sitemap .= "<ul>\n";
+         $sitemap = sitemapRender($sitemap, $entries{$e}, "$path/$parent");
+         $sitemap .= "</ul>\n";
+      }
+      $sitemap .= "</li>\n";
+   }
+   return $sitemap;
 }
 
+
+
 sub exports {
     my %args = @_;
     my $template = "content$args{path}";
@@ -179,16 +243,24 @@ sub breadcrumbs {
     for (@path) {
         $relpath .= "$sep$_";
         if ($_) {
-            $ext = ".html";
-            $sep = "/";
-            s/-/ /g;
-            s/(\w+)/\u\L$1/g;
+            $_ = "";
+            my $datafile = "content$relpath.mdtext";
+            my %data;
+            if (-f $datafile) {
+                read_text_file $datafile, \%data;
+                $ext = ".html";
+                $sep = "/";
+                my $title = ${data{headers}}{title};
+                if ($title) {
+                    $_ = $title;
+                }
+            }
         } else {
             $_ = "Home";
             $ext = "";
             $sep = "";
         }
-        push @rv, qq(<a href="$relpath$ext">$_</a>);
+        push @rv, qq(<a href="$relpath$ext">$_</a>) if $_;
     }
     return join "&nbsp;&raquo&nbsp;", @rv;
 }

Modified: sling/site/trunk/readme.txt
URL: 
http://svn.apache.org/viewvc/sling/site/trunk/readme.txt?rev=1340753&r1=1340752&r2=1340753&view=diff
==============================================================================
--- sling/site/trunk/readme.txt (original)
+++ sling/site/trunk/readme.txt Sun May 20 15:29:00 2012
@@ -1,2 +1,26 @@
 Working on the conversion of the Sling website to the ASF CMS,
 see https://issues.apache.org/jira/browse/SLING-2002
+
+Notes on pages:
+
+  * Start the file with a Title: line to define the page
+    title and the first H1 tag.
+
+  * The last modification information from SVN (revision,
+    committer, and date/time) is automatically added when
+    the page is rendered
+
+  * Excerpts can be added to a page using the Excerpt
+    metadata.
+
+  * Metadata from child pages can be referred to in the
+    content with the Django variable reference notation
+    using the child page name (without extension) as
+    its container; e.g.:
+          {{ childpage.headers.excerpt }}
+          {{ childpage.headers.title }}
+
+  * Content Pages can contain Django templates of the
+    form {{...}} and {%...%}. If so, the page content
+    is evaluated as a Django template before running
+    it through the page template.

Modified: sling/site/trunk/templates/sidenav.mdtext
URL: 
http://svn.apache.org/viewvc/sling/site/trunk/templates/sidenav.mdtext?rev=1340753&r1=1340752&r2=1340753&view=diff
==============================================================================
--- sling/site/trunk/templates/sidenav.mdtext (original)
+++ sling/site/trunk/templates/sidenav.mdtext Sun May 20 15:29:00 2012
@@ -8,6 +8,7 @@
 [API docs](http://sling.apache.org/apidocs/sling5/index.html)   
 [Wiki](http://s.apache.org/sling.wiki)   
 [FAQ](http://s.apache.org/sling.faq)   
+[Site Map](sitemap.html)
    
 **Project info**   
 [Downloads](http://sling.apache.org/site/downloads.cgi)   


Reply via email to