diff options
| author | David T. Sadler <davidtsadler@googlemail.com> | 2021-05-17 15:59:24 +0100 |
|---|---|---|
| committer | David T. Sadler <davidtsadler@googlemail.com> | 2021-05-17 15:59:24 +0100 |
| commit | bbf7974715b9a43bab207df75ef69209cdf13850 (patch) | |
| tree | 13078ed02a7fec8f28b1d6046e6533f1be2aec54 /scripts | |
| parent | 3e9a949c823917e0f4189628959fa4d3eb908db4 (diff) | |
Move to building localy rather than on remote server
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/build.php | 96 | ||||
| -rw-r--r-- | scripts/build_www_site.php | 46 | ||||
| -rw-r--r-- | scripts/functions.php | 105 |
3 files changed, 83 insertions, 164 deletions
diff --git a/scripts/build.php b/scripts/build.php index eecb677..e610298 100644 --- a/scripts/build.php +++ b/scripts/build.php @@ -2,111 +2,45 @@ require_once __DIR__.DIRECTORY_SEPARATOR.'functions.php'; -$hostname = null; -$output = null; +$hostname = 'davidtsadler.com'; -$opts = getopt( - 'h:o:', - [ - 'hostname:', - 'help::', - 'output:', - ] -); +$geminiSrc = __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'gemini'; -if ($opts === false) { - usage(); -} - -foreach($opts as $opt => $value) { - if ($value === false) { - continue; - } - - switch ($opt) - { - case 'h': - case 'hostname': - $hostname = $value; - break; - case 'help': - usage(); - break; - case 'o': - case 'output': - $output = $value; - break; - } -} - - -if (!$hostname || !$output) { - usage(); -} - -if (!is_dir($output)) { - echo "Unable to locate specified output directory $output.\n\n"; - exit(1); -} - -if (!is_writable($output)) { - echo "Output directory $output is not writable.\n\n"; - exit(1); -} - -$geminiOutput = "$output/gemini"; - -if (file_exists($geminiOutput)) { - echo "Gemini build directory $geminiOutput already exists.\n\n"; +if (!is_dir($geminiSrc)) { + echo "Unable to locate gemini directory.\n\n"; exit(1); } -$wwwOutput = "$output/www"; - -if (file_exists($wwwOutput)) { - echo "WWW build directory $wwwOutput already exists.\n\n"; +if (!is_readable($geminiSrc)) { + echo "Gemini directory is not readable.\n\n"; exit(1); } -$src = __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'src'; +$output = __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'www'; -if (!is_dir($src)) { - echo "Unable to locate src directory $src.\n\n"; +if (!is_dir($output)) { + echo "Unable to locate output directory.\n\n"; exit(1); } -if (!is_readable($src)) { - echo "Src directory $src is not readable.\n\n"; +if (!is_writable($output)) { + echo "Output directory is not writable.\n\n"; exit(1); } $htmlTemplateDiretory = __DIR__.DIRECTORY_SEPARATOR.'../html_templates'; if (!is_dir($htmlTemplateDiretory)) { - echo "Unable to locate html template directory $htmlTemplateDiretory.\n\n"; + echo "Unable to locate html template directory.\n\n"; exit(1); } if (!is_readable($htmlTemplateDiretory)) { - echo "Src directory $htmlTemplateDiretory is not readable.\n\n"; - exit(1); -} - -$assetsDiretory = __DIR__.DIRECTORY_SEPARATOR.'../www_assets'; - -if (!is_dir($assetsDiretory)) { - echo "Unable to locate assets directory $assetsDiretory.\n\n"; + echo "HTML template directory is not readable.\n\n"; exit(1); } -if (!is_readable($assetsDiretory)) { - echo "Src directory $assetsDiretorY is not readable.\n\n"; - exit(1); -} - -$pages = getPages($src, $geminiOutput, $wwwOutput); - -buildGeminiSite($pages); +$pages = getPages($geminiSrc, $output); -buildWWWSite($pages, $hostname, $htmlTemplateDiretory, $assetsDiretory, $wwwOutput); +buildWWWSite($pages, $hostname, $htmlTemplateDiretory); diff --git a/scripts/build_www_site.php b/scripts/build_www_site.php new file mode 100644 index 0000000..e610298 --- /dev/null +++ b/scripts/build_www_site.php @@ -0,0 +1,46 @@ +<?php declare(strict_types=1); + +require_once __DIR__.DIRECTORY_SEPARATOR.'functions.php'; + +$hostname = 'davidtsadler.com'; + +$geminiSrc = __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'gemini'; + +if (!is_dir($geminiSrc)) { + echo "Unable to locate gemini directory.\n\n"; + exit(1); +} + +if (!is_readable($geminiSrc)) { + echo "Gemini directory is not readable.\n\n"; + exit(1); +} + +$output = __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'www'; + +if (!is_dir($output)) { + echo "Unable to locate output directory.\n\n"; + exit(1); +} + +if (!is_writable($output)) { + echo "Output directory is not writable.\n\n"; + exit(1); +} + +$htmlTemplateDiretory = __DIR__.DIRECTORY_SEPARATOR.'../html_templates'; + +if (!is_dir($htmlTemplateDiretory)) { + echo "Unable to locate html template directory.\n\n"; + exit(1); +} + +if (!is_readable($htmlTemplateDiretory)) { + echo "HTML template directory is not readable.\n\n"; + exit(1); +} + +$pages = getPages($geminiSrc, $output); + +buildWWWSite($pages, $hostname, $htmlTemplateDiretory); + diff --git a/scripts/functions.php b/scripts/functions.php index 50acc22..3db5744 100644 --- a/scripts/functions.php +++ b/scripts/functions.php @@ -6,19 +6,7 @@ const LIST_ITEM = '/^\*\s+(.*)/'; const PRE = '/^```(.*)?/'; const QUOTE = '/^>\s+(.*)/'; -function usage(): void -{ - echo "Usage: php build.php [options] - - -h --hostname <hostname> Hostname of site. Used when generating HTML. - --help Displays this message. - -o --output <directory> Directory where site will be built into. - -"; - exit(1); -} - -function getPages(string $src, string $geminiOutput, string $wwwOutput): array +function getPages(string $src, string $output): array { $src = realpath($src); @@ -33,36 +21,33 @@ function getPages(string $src, string $geminiOutput, string $wwwOutput): array if (!is_file($fileInfo->getRealPath())) { continue; } - $pages[] = getPageMetaData($fileInfo, $src, $geminiOutput, $wwwOutput); + $pages[] = getPageMetaData($fileInfo, $src, $output); } return $pages; } -function getPageMetaData(\SplFileInfo $fileInfo, string $src, string $geminiOutput, string $wwwOutput): array +function getPageMetaData(\SplFileInfo $fileInfo, string $src, string $output): array { $input = $fileInfo->getRealPath(); $pathData = parsePath($input); - $geminiUrl = str_replace($src, '', $input); - $wwwUrl = str_replace('.gmi', '.html', $geminiUrl); + $url = str_replace([$src, '.gmi'], ['', '.html'], $input); $contentData = parseContent(file_get_contents($input)); return [ - 'input' => $input, - 'geminiUrl' => $geminiUrl, - 'wwwUrl' => $wwwUrl, - 'date' => $pathData['date'], - 'tag' => $pathData['tag'], - 'isPost' => $pathData['isPost'], - 'isTag' => $pathData['isTag'], - 'title' => $contentData['title'], - 'author' => $contentData['author'], - 'html' => gemtext2hmtl(file_get_contents($input)), - 'geminiOutput' => "$geminiOutput$geminiUrl", - 'wwwOutput' => "$wwwOutput$wwwUrl", + 'input' => $input, + 'url' => $url, + 'date' => $pathData['date'], + 'tag' => $pathData['tag'], + 'isPost' => $pathData['isPost'], + 'isTag' => $pathData['isTag'], + 'title' => $contentData['title'], + 'author' => $contentData['author'], + 'html' => gemtext2hmtl(file_get_contents($input)), + 'output' => "$output$url", ]; } @@ -117,24 +102,10 @@ function parseContent(string $content): array ]; } -function buildGeminiSite(array $pages): void +function buildWWWSite(array $pages, string $hostname, string $htmlTemplateDiretory): void { foreach ($pages as $page) { - $destDirectory = dirname($page['geminiOutput']); - - if (!file_exists($destDirectory) && !mkdir($destDirectory, 0777, true)) { - echo "Unable to create Gemini site directory $destDirectory."; - exit(1); - } - - copy($page['input'], $page['geminiOutput']); - } -} - -function buildWWWSite(array $pages, string $hostname, string $htmlTemplateDiretory, $assetsDiretory, string $output): void -{ - foreach ($pages as $page) { - $destDirectory = dirname($page['wwwOutput']); + $destDirectory = dirname($page['output']); if (!file_exists($destDirectory) && !mkdir($destDirectory, 0777, true)) { echo "Unable to create WWW site directory $destDirectory."; @@ -142,7 +113,7 @@ function buildWWWSite(array $pages, string $hostname, string $htmlTemplateDireto } file_put_contents( - $page['wwwOutput'], + $page['output'], buildHtmlFile( $page['title'], $page['html'], @@ -151,8 +122,6 @@ function buildWWWSite(array $pages, string $hostname, string $htmlTemplateDireto ); } - copyWWWAssets($assetsDiretory, $output); - generateAtomFeeds($pages, $hostname); } @@ -248,36 +217,6 @@ function gemtext2hmtl(string $gemtext): string return implode('', $html); } -function copyWWWAssets(string $assetsDiretory, string $output): void -{ - $assetsDiretory = realpath($assetsDiretory); - - $iter = new \RecursiveIteratorIterator( - new \RecursiveDirectoryIterator($assetsDiretory, \FilesystemIterator::SKIP_DOTS), - \RecursiveIteratorIterator::SELF_FIRST - ); - - foreach ($iter as $fileInfo) { - if (!is_file($fileInfo->getRealPath())) { - continue; - } - - $input = $fileInfo->getRealPath(); - $url = str_replace($assetsDiretory, '', $input); - - $destFile = $output.DIRECTORY_SEPARATOR.$url; - - $destDirectory = dirname($destFile); - - if (!file_exists($destDirectory) && !mkdir($destDirectory, 0777, true)) { - echo "Unable to create WWW asset directory $destDirectory."; - exit(1); - } - - copy($input, $destFile); - } -} - function generateAtomFeeds(array $pages, string $hostname): void { $posts = array_filter($pages, fn ($post) => $post['isPost']); @@ -312,7 +251,7 @@ function generateAtomFeeds(array $pages, string $hostname): void /** * Get the page that lists all posts. */ - $allPostsIndex = array_values(array_filter($pages, fn ($post) => preg_match('/posts\/index/', $post['wwwUrl']) == 1))[0]; + $allPostsIndex = array_values(array_filter($pages, fn ($post) => preg_match('/posts\/index/', $post['url']) == 1))[0]; $allPostsIndex['posts'] = $posts; @@ -322,11 +261,11 @@ function generateAtomFeeds(array $pages, string $hostname): void function tagToAtomFeed(array $tag, string $hostname): void { file_put_contents( - str_replace('index.html', 'atom.xml', $tag['wwwOutput']), + str_replace('index.html', 'atom.xml', $tag['output']), buildAtomFeed( $tag['title'], - "https://$hostname".str_replace('index.html', 'atom.xml', $tag['wwwUrl']), - "https://$hostname".$tag['wwwUrl'], + "https://$hostname".str_replace('index.html', 'atom.xml', $tag['url']), + "https://$hostname".$tag['url'], $tag['posts'][0]['date'].'T12:00:00Z', implode('', array_map(fn ($post) => postToAtomEntry($post, $hostname), $tag['posts'])) ) @@ -337,7 +276,7 @@ function postToAtomEntry(array $post, string $hostname): string { return buildAtomEntry( $post['title'], - "https://$hostname{$post['wwwUrl']}", + "https://$hostname{$post['url']}", $post['author'], "{$post['date']}T12:00:00Z", $post['html'], |
