From 3f1287d1721124e3a0752678a8c928959442b333 Mon Sep 17 00:00:00 2001 From: Jasmine Amalia <67263692+jasm1nii@users.noreply.github.com> Date: Sun, 22 Oct 2023 15:42:07 +0700 Subject: [PATCH] add PHP precompiling Signed-off-by: Jasmine Amalia <67263692+jasm1nii@users.noreply.github.com> --- README.md | 7 ++++--- feed_generator.php | 8 ++++---- feed_generator_functions.php | 25 ++++++++++++++++++++++--- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 8431a15..833b39c 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,9 @@ an RSS/atom feed generator for my personal site :cat: tested to work with PHP ve ## how it works 1. match files in a specificied directory. -2. load the DOM of each file. -3. for each child of `` in the XML feed, output a string value from one of the following HTML elements/file properties, in order of priority: +2. precompile any PHP files, if found. +3. load the DOM of each file. +4. for each child of `` in the XML feed, output a string value from one of the following HTML elements/file properties, in order of priority: | original HTML | XML output | |-------------------------------|---------------------------------| @@ -32,7 +33,7 @@ an RSS/atom feed generator for my personal site :cat: tested to work with PHP ve -4. save all of the above into a new file named **articles.xml** (default name, but can be changed). +5. save all of the above into a new file named **articles.xml** (default name, but can be changed). ## ways to use - configure a cron job on your web server to run automatically every now and then. diff --git a/feed_generator.php b/feed_generator.php index db7ae38..fd35671 100644 --- a/feed_generator.php +++ b/feed_generator.php @@ -32,15 +32,15 @@ /* -------------------- */ // PATH TO FETCH PAGES FROM - ## __DIR__ is the directory where *this script* is located. in my case, i first need to go up two directories to get to the site root. - $site_root = dirname(__DIR__, 2); + ## __DIR__ is the directory where *this script* is located. + $site_root = dirname(__DIR__, 2).'/public_html'; ## once i'm there, i specify the parent directory where i keep all of my blog pages. ## because the values of $blog_root and $blog_entries will be used for generating entry links, forward slashes are a *must*. $blog_root = $site_root.'/blog/articles'; - ## then, specify a pattern that matches the path of each individual page. for example, this will match /YYYY/MM/DD/entry.html. - $blog_entries = $blog_root.'/*/*/*/*.html'; + ## then, specify a pattern that matches the path of each individual page. for example, this will match /YYYY/MM/DD/entry (any file extension). + $blog_entries = $blog_root.'/*/*/*/[entry]*'; /* -------------------- */ diff --git a/feed_generator_functions.php b/feed_generator_functions.php index 5621fa9..ea8ba50 100644 --- a/feed_generator_functions.php +++ b/feed_generator_functions.php @@ -1,8 +1,18 @@ ($max_entries-1) ) break; + } $i = 0; - foreach ((glob($blog_entries)) as $article) { + foreach ((glob($blog_entries.'html')) as $article) { $article_content = file_get_contents($article); // force libxml to parse all HTML elements, including HTML 5. by default, the extension can only read valid HTML 4. libxml_use_internal_errors(true); @@ -88,6 +98,7 @@ } if(++$i > ($max_entries-1) ) break; + $data[$i] = [ 'title'=>$title_data, 'id'=>$id_data, @@ -151,11 +162,19 @@ $entry->addChild('published',$published); $content = $data[$i]['content']; - $entry->addChild('content', nl2br(preg_replace("/\n\s+/", "",(htmlspecialchars($content, ENT_XML1))))); + $content_child = $entry->addChild('content', nl2br(preg_replace("/\n\s+/", "",(htmlspecialchars($content, ENT_XML1))))); + $content_child->addAttribute('type','html'); } - echo $sxe->saveXML($blog_root . DIRECTORY_SEPARATOR . $file); + $sxe->saveXML($blog_root . DIRECTORY_SEPARATOR . $file); + + $del_tmp = unlink($article_php.'_generator-tmp.html'); echo nl2br(strtoupper(date("h:i:sa")) . ' - Feed successfully generated in ' . realpath($blog_root) . DIRECTORY_SEPARATOR . $file . "\n"); + if ($del_tmp) { + echo 'Temporary generator files successfully deleted.'; + } else { + echo 'Temporary generator files could not be automatically deleted - check your directories to delete them manually.'; + } echo 'Validate your feed at https://validator.w3.org/feed/'; ?> \ No newline at end of file