--- fine-grained-site-flow/H-W-NM-fine-grained-site-flow.pl 2005-05-06 10:34:32.615409008 +0300
+++ cgi-script/H-W-NM-serve.pl  2005-05-06 10:37:27.368842448 +0300
@@ -4,7 +4,7 @@
 use warnings;

 use HTML::Widgets::NavMenu;
-use File::Path;
+use CGI;
 use Template;

 my $css_style = <<"EOF";
@@ -378,9 +378,27 @@
     }
 );

+my $cgi = CGI->new();
+my $path_info = $cgi->path_info();
+
+my $found = 0;
+PAGE_LOOP:
 foreach my $page (@pages)
 {
     my $path = $page->{'path'};
+    my $title = $page->{'title'};
+    my $content = $page->{'content'};
+    if ($path_info eq "/$path")
+    {
+        $found = 1;
+        render_page($path, $title, $content);
+        last;
+    }    
+}
+
+sub render_page
+{
+    my ($path, $title, $content) = @_;
     my $nav_menu =
         HTML::Widgets::NavMenu->new(
             path_info => "/$path",
@@ -391,18 +409,7 @@

     my $nav_menu_results = $nav_menu->render();

-    my $file_path = $path;
-    if (($file_path =~ m{/$}) || ($file_path eq ""))
-    {
-        $file_path .= "index.html";
-    }
-    my $full_path = "dest/$file_path";
-    $full_path =~ m{^(.*)/[^/]+$};
-    # mkpath() throws an exception if it isn't successful, which will cause 
-    # this program to terminate. This is what we want.
-    mkpath($1, 0, 0755);
-    open my $out, ">", $full_path or
-        die "Could not open \"$full_path\" for writing!";
+    print $cgi->header();

     my $template =
         Template->new(
@@ -413,10 +420,10 @@

     my $vars =
     {
-        'title' => $page->{'title'},
+        'title' => $title,
         'css_style' => $css_style,
         'nav_menu_text' => join("\n", @{$nav_menu_results->{'html'}}) . "\n",
-        'content' => $page->{'content'} . "\n",
+        'content' => $content . "\n",
         'breadcrumbs' => $nav_menu_results->{leading_path},
         'nav_links' => $nav_menu_results->{'nav_links_obj'},
     };
@@ -466,8 +473,12 @@
 </html>
 EOF

-    $template->process(\$nav_links_template, $vars, $out);
+    $template->process(\$nav_links_template, $vars);
+}

-    close($out);
+if (!$found)
+{
+    print $cgi->header();
+    print "<html><body>Page not found!</body></html>\n";
 }