diff options
Diffstat (limited to 'scripts/functions.php')
| -rw-r--r-- | scripts/functions.php | 105 |
1 files changed, 22 insertions, 83 deletions
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'], |
