add PHP precompiling

Signed-off-by: Jasmine Amalia <67263692+jasm1nii@users.noreply.github.com>
This commit is contained in:
Jasmine Amalia 2023-10-22 15:42:07 +07:00
parent 70250019f4
commit 3f1287d172
Signed by untrusted user who does not match committer: jasm1nii
GPG Key ID: 9C88CED5BF43BD08
3 changed files with 30 additions and 10 deletions

View File

@ -5,8 +5,9 @@ an RSS/atom feed generator for my personal site :cat: tested to work with PHP ve
## how it works ## how it works
1. match files in a specificied directory. 1. match files in a specificied directory.
2. load the DOM of each file. 2. precompile any PHP files, if found.
3. for each child of `<entry>` in the XML feed, output a string value from one of the following HTML elements/file properties, in order of priority: 3. load the DOM of each file.
4. for each child of `<entry>` 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 | | 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 ## ways to use
- configure a cron job on your web server to run automatically every now and then. - configure a cron job on your web server to run automatically every now and then.

View File

@ -32,15 +32,15 @@
/* -------------------- */ /* -------------------- */
// PATH TO FETCH PAGES FROM // 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. ## __DIR__ is the directory where *this script* is located.
$site_root = dirname(__DIR__, 2); $site_root = dirname(__DIR__, 2).'/public_html';
## once i'm there, i specify the parent directory where i keep all of my blog pages. ## 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*. ## 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'; $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. ## 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.'/*/*/*/*.html'; $blog_entries = $blog_root.'/*/*/*/[entry]*';
/* -------------------- */ /* -------------------- */

View File

@ -1,8 +1,18 @@
<?php <?php
date_default_timezone_set($timezone); date_default_timezone_set($timezone);
$i = 0;
foreach ((glob($blog_entries.'.php')) as $article_php) {
ob_start();
include $article_php;
$compiled = ob_get_contents();
ob_end_clean();
file_put_contents($article_php.'_generator-tmp.html', $compiled);
if(++$i > ($max_entries-1) ) break;
}
$i = 0; $i = 0;
foreach ((glob($blog_entries)) as $article) { foreach ((glob($blog_entries.'html')) as $article) {
$article_content = file_get_contents($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. // 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); libxml_use_internal_errors(true);
@ -88,6 +98,7 @@
} }
if(++$i > ($max_entries-1) ) break; if(++$i > ($max_entries-1) ) break;
$data[$i] = [ $data[$i] = [
'title'=>$title_data, 'title'=>$title_data,
'id'=>$id_data, 'id'=>$id_data,
@ -151,11 +162,19 @@
$entry->addChild('published',$published); $entry->addChild('published',$published);
$content = $data[$i]['content']; $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"); 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/'; echo 'Validate your feed at https://validator.w3.org/feed/';
?> ?>