Compare commits

...

2 Commits

Author SHA1 Message Date
89c87d942d Convert make-council-pages.sh script to use php script 2024-07-30 23:01:54 +10:00
cd9135e3bd Create a php version of page template 2024-07-30 23:00:02 +10:00
8 changed files with 135 additions and 17 deletions

View File

@@ -20,4 +20,4 @@ This is a bash script for creating a page in WordPress for each council.
If a page for a council already exists, the page will be updated instead. The source of councils for this script is the "council_names.json" file.
The script needs the [`jq`](https://jqlang.github.io/jq/), [`gomplate`](https://docs.gomplate.ca/), and [`wp`](https://wp-cli.org/) tools.
The script needs the [`jq`](https://jqlang.github.io/jq/), [`php`](https://www.php.net/) and [`wp`](https://wp-cli.org/) tools.

View File

@@ -18,14 +18,20 @@ function create_or_update_page() {
short_name=$(echo "$council_block" | jq -r '.shortName')
content=$(echo "$council_block" | jq -c | gomplate -d config=stdin:///in.json -f page-template)
content=$(echo "$council_block" | jq -c | php php-template/main.php)
if [ $? -eq 0 ]; then
if [[ -n "$page_id" ]]; then
echo "Update page $short_name (post $page_id)"
echo "$content" | wp post update "$page_id" --post_content="$content" $WP_FLAGS -
else
echo "Create page $short_name"
echo "$content" | wp post create --post_type=page --post_title="$short_name" --post_status=publish $WP_FLAGS -
fi
if [[ -n "$page_id" ]]; then
echo "Update page $short_name (post $page_id)"
echo "$content" | wp post update "$page_id" --post_content="$content" $WP_FLAGS -
else
echo "Create page $short_name"
echo "$content" | wp post create --post_type=page --post_title="$short_name" --post_status=publish $WP_FLAGS -
echo "Failed to generate page content for $short_name"
fi
}

View File

@@ -0,0 +1,30 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/php
{
"name": "PHP",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/php:1-8.3",
// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},
// Configure tool-specific properties.
// "customizations": {},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [8000],
// Use 'portsAttributes' to set default properties for specific forwarded ports. More info: https://code.visualstudio.com/docs/remote/devcontainerjson-reference.
"portsAttributes": {
"8000": {
"label": "Hello Remote World",
"onAutoForward": "notify"
}
}
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "sudo chmod a+x \"$(pwd)\" && sudo rm -rf /var/www/html && sudo ln -s \"$(pwd)\" /var/www/html"
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}

14
php-template/.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,14 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch application",
"type": "php",
"request": "launch",
"program": "${workspaceFolder}/main.php",
"args": ["${workspaceFolder}/example-input"],
"cwd": "${workspaceFolder}",
"port": 9000
}
]
}

View File

@@ -0,0 +1,15 @@
{
"shortName": "Ballarat",
"councilName": "Ballarat City Council",
"wardNames": [
"Alfredton",
"Delacombe",
"Sebastopol",
"Central",
"Wendouree",
"Golden Point",
"North",
"Brown Hill",
"Buninyong"
]
}

28
php-template/main.php Normal file
View File

@@ -0,0 +1,28 @@
<?php
require_once "page_renderer.php";
// TODO: Need to handle arguments better
// Assume that the first argument is a path to a file contain the config.
// If the argument isn't present, load the json from standard input
if (isset($argv[1])) {
$jsonData = file_get_contents($argv[1]);
} else {
$jsonData = file_get_contents("php://stdin");
}
$config = json_decode($jsonData, true);
// Check for decoding errors
if (json_last_error() !== JSON_ERROR_NONE) {
error_log('Error decoding JSON: ' . json_last_error_msg());
exit(1);
}
$renderer = new SPLPageRenderer();
$pageContent = $renderer->renderCouncilPage($config);
if ($pageContent === null) {
exit(2);
}
echo $pageContent;
exit(0);

View File

@@ -0,0 +1,27 @@
<?php
class SPLPageRenderer {
public function renderCouncilPage($config) {
ob_start();
set_error_handler(function($errno, $errstr, $errfile, $errline) {
// Clear any output if an error occurred
ob_get_clean();
error_log("Error: $errstr in $errfile on line $errline");
return true; // Prevent default error handling
});
require "template.php";
restore_error_handler();
$content = ob_get_clean();
// Explictly return null if we didn't generate any content
if (!empty($content)) {
return $content;
} else {
return null;
}
}
}

View File

@@ -1,20 +1,19 @@
<!-- wp:paragraph -->
<p>{{ (datasource "config").councilName }}</p>
<p><?php echo $config['councilName']; ?></p>
<!-- /wp:paragraph -->
{{ range $index, $element := (datasource "config").wardNames }}
<?php foreach ($config['wardNames'] as $index => $wardName): ?>
<!-- wp:heading {"className":"is-style-default"} -->
<h2 class="wp-block-heading is-style-default" id="{{ $element | strings.Slug }}">{{ $element }}</h2>
<h2 class="wp-block-heading is-style-default" id="<?php echo strtolower(str_replace(' ', '-', $wardName)); ?>"><?php echo $wardName; ?></h2>
<!-- /wp:heading -->
<!-- wp:group {"layout":{"type":"grid"}} -->
<div class="wp-block-group">
{{ range seq 1 5 }}
<?php for ($i = 0; $i < 5; $i++): ?>
<!-- wp:group {"layout":{"type":"flex","orientation":"vertical"}} -->
<div class="wp-block-group">
<!-- wp:heading {"fontSize":"medium"} -->
<h2 class="wp-block-heading has-medium-font-size">Candidate {{.}}</h2>
<h2 class="wp-block-heading has-medium-font-size">Candidate <?php echo $i + 1; ?></h2>
<!-- /wp:heading -->
<!-- wp:image {"aspectRatio":"1","scale":"cover","style":{"color":{}}} -->
@@ -26,8 +25,7 @@
<!-- /wp:paragraph -->
</div>
<!-- /wp:group -->
{{end}}
<?php endfor; ?>
</div>
<!-- /wp:group -->
{{end}}
<?php endforeach; ?>