Saaze / saaze

An all-inclusive, flat-file CMS for simple websites and blogs.
https://saaze.dev
MIT License
48 stars 6 forks source link

Saaze generates static files for root-directory only, relative positioning would be preferred #1

Open eklausme opened 3 years ago

eklausme commented 3 years ago

Suggestion for new feature: I would like to have a static build, which can be moved to any directory. Currently, Saaze generates

href="/..."

Therefore, I cannot put multiple builds from Saaze into the root-directory of my web-server. It would be good, if that could be changed to a relative reference, like:

href="./..."

Correspondingly, the templates have to be changed accordingly.

Thanks.

eklausme commented 3 years ago

I changed PHP source code and templates for achieving this.

Patch the core source is:

  1. cd vendor/saaze/core/src
  2. and apply patch with patch -p0 < saazeCoreSrc.patch.

Patch content of saazeCoreSrc.patch is:

diff -Naur ../../../../../saazeref/vendor/saaze/core/src/Commands/BuildCommand.php ./Commands/BuildCommand.php                                                         
--- ../../../../../saazeref/vendor/saaze/core/src/Commands/BuildCommand.php     2020-10-27 17:55:07.000000000 +0100                                                    
+++ ./Commands/BuildCommand.php 2021-04-04 18:49:58.000000000 +0200                                                                                                    
@@ -12,6 +12,8 @@                                                                                                                                                      
 use Symfony\Component\Console\Input\InputInterface;                                                                                                                   
 use Symfony\Component\Console\Output\OutputInterface;                                                                                                                 

+                                                                                                                                                                      
+                                                                                                                                                                      
 class BuildCommand extends Command                                                                                                                                    
 {                                                                                                                                                                     
     protected static $defaultName = 'build';
@@ -54,6 +56,7 @@
             $dest = base_path() . "/{$dest}";
         }

+        $GLOBALS['buildDest'] = $dest; // root directory
         $output->writeln("<info>Building static site in {$dest}...</info>");

         $startTime = microtime(true);
@@ -122,6 +125,18 @@
     }

     /**
+     * @param string $full
+     * @param string $dest
+     * @return string
+     */
+    private function compRbase($full,$dest) {
+        if (strpos($full,$dest) != 0) return "";       // this is an error
+        $cnt = substr_count(substr($full,strlen($dest)),"/") - 1;      // count slashes in overlapping part of $full
+        if ($cnt <= 0) return ".";
+        return rtrim(str_repeat("../",$cnt),"/");
+    }
+
+    /**
      * @param CollectionInterface $collection
      * @param int $page
      * @param string $dest
@@ -148,8 +163,10 @@
         if (!is_dir($collectionDir)) {
             mkdir($collectionDir, 0777, true);
         }
+        $collectionDir .= "/index.html"; 

-        file_put_contents($collectionDir . '/index.html', $this->templateManager->renderCollection($collection, $page));
+        $GLOBALS['rbase'] = $this->compRbase($collectionDir,$GLOBALS['buildDest']);
+        file_put_contents($collectionDir, $this->templateManager->renderCollection($collection, $page));

         return true;
     }
@@ -178,8 +195,9 @@
         if (!is_dir($entryDir)) {
             mkdir($entryDir, 0777, true);
         }
-
-        file_put_contents("{$entryDir}/index.html", $this->templateManager->renderEntry($entry));
+        $entryDir .= "/index.html";
+        $GLOBALS['rbase'] = $this->compRbase($entryDir,$GLOBALS['buildDest']);
+        file_put_contents($entryDir, $this->templateManager->renderEntry($entry));

         return true;
     }

Patching the templates:

  1. cd templates
  2. Patch multiple files with patch -p0 < saazeTemplate.patch.

Patch content of saazeTemplate.patch is:

diff -Naur ../../saazeref/templates/collection.blade.php ./collection.blade.php
--- ../../saazeref/templates/collection.blade.php       2020-11-23 15:56:58.000000000 +0100
+++ ./collection.blade.php      2021-04-03 21:36:29.630508059 +0200
@@ -5,7 +5,7 @@
 @section('content')
 @foreach ($pagination->entries as $entry)
 <div class="mb-10 sm:mb-20">
-    <h2><a href="{{ $entry->url }}">{{ $entry->title }}</a></h2>
+    <h2><a href="{{ $GLOBALS['rbase'] }}{{ $entry->url }}">{{ $entry->title }}</a></h2>
     <p>{!! $entry->excerpt !!}</p>
 </div>
 @endforeach
diff -Naur ../../saazeref/templates/layout.blade.php ./layout.blade.php
--- ../../saazeref/templates/layout.blade.php   2020-11-23 15:56:58.000000000 +0100
+++ ./layout.blade.php  2021-04-04 18:36:07.000000000 +0200
@@ -17,7 +17,6 @@
     <link rel="stylesheet" href="https://unpkg.com/@tailwindcss/typography@0.2.x/dist/typography.min.css" />
     <link rel="stylesheet" href="https://unpkg.com/tailwindcss@^1.5/dist/utilities.min.css" />
 </head>
-
 <body>
     <header class="p-6 sm:px-10 sm:flex sm:justify-between sm:items-center mb-10 sm:mb-20">
         <div class="mb-4 sm:mb-0">
@@ -25,9 +24,9 @@
         </div>
         <nav>
             <ul class="flex">
-                <li><a href="/" class="text-purple-600">Home</a></li>
-                <li class="ml-10"><a href="/about" class="text-purple-600">About</a></li>
-                <li class="ml-10"><a href="/blog" class="text-purple-600">Blog</a></li>
+                <li><a href="{{ $GLOBALS['rbase'] }}/" class="text-purple-600">Home</a></li>
+                <li class="ml-10"><a href="{{ $GLOBALS['rbase'] }}/about" class="text-purple-600">About</a></li>
+                <li class="ml-10"><a href="{{ $GLOBALS['rbase'] }}/blog" class="text-purple-600">Blog</a></li>
             </ul>
         </nav>
     </header>
diff -Naur ../../saazeref/templates/posts/index.blade.php ./posts/index.blade.php
--- ../../saazeref/templates/posts/index.blade.php      2020-11-23 15:56:58.000000000 +0100
+++ ./posts/index.blade.php     2021-04-03 21:37:36.080509841 +0200
@@ -5,7 +5,7 @@
 @section('content')
 @foreach ($pagination->entries as $entry)
 <div class="mb-10 sm:mb-20">
-    <h2><a href="{{ $entry->url }}">{{ $entry->title }}</a></h2>
+    <h2><a href="{{ $GLOBALS['rbase'] }}{{ $entry->url }}">{{ $entry->title }}</a></h2>
     <p class="text-gray-600">
         {{ date('jS F Y', strtotime($entry->date)) }}
     </p>
@@ -14,10 +14,10 @@
 @endforeach
 <div class="flex justify-between my-20">
     @if ($pagination->nextUrl)
-    <a href="{{ $pagination->nextUrl }}">&larr; Older</a>
     @if ($pagination->nextUrl)
-    <a href="{{ $pagination->nextUrl }}">&larr; Older</a>
+    <a href="{{ $GLOBALS['rbase'] }}{{ $pagination->nextUrl }}">&larr; Older</a>
     @endif
     @if ($pagination->prevUrl)
-    <a href="{{ $pagination->prevUrl }}">Newer &rarr;</a>
+    <a href="{{ $GLOBALS['rbase'] }}{{ $pagination->prevUrl }}">Newer &rarr;</a>
     @endif
 </div>
 @endsection

I tried to embed this "relative-root" (called rbase) information in either Entry or Collection, but failed in both cases. So resorted to global variables.

As this feature has not been discussed, I just provide these patches and no PR.

Tested this patched version with static build and also with "server"-version, i.e., php saaze serve.