Skip to content
This repository has been archived by the owner on Dec 20, 2023. It is now read-only.

Commit

Permalink
Drops dependency and renames plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
jensscherbl committed Jan 17, 2023
1 parent 8c6491d commit 7024a90
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 27 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# templates-xml
Post-processing for XML and XHTML templates. Ensures well-formed output and strips whitespace between nodes.
# Kirby XHTML

Ensures well-formed XML and XHTML output for [Kirby][1] templates and strips whitespace between nodes.

[1]: https://getkirby.com
15 changes: 7 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "kenshodigital/templates-xml",
"description": "Post-processing for XML and XHTML templates.",
"name": "kenshodigital/kirby-xhtml",
"description": "Ensures well-formed XML and XHTML output for Kirby templates.",
"type": "kirby-plugin",
"version": "2.0.0",
"homepage": "https://github.com/kenshodigital/templates-xml",
"version": "1.0.0",
"homepage": "https://github.com/kenshodigital/kirby-xhtml",
"license": "MIT",
"authors":
[
Expand All @@ -17,15 +17,14 @@
"php": "^8.2",
"ext-dom": "*",
"ext-libxml": "*",
"getkirby/cms": "^3.8.3",
"getkirby/composer-installer": "^1.2",
"kenshodigital/templates": "^2.0"
"getkirby/cms": "^3.9",
"getkirby/composer-installer": "^1.2"
},
"autoload":
{
"psr-4":
{
"Kenshō\\Templates\\": "./src/"
"Kenshō\\Xhtml\\": "./src/"
}
}
}
16 changes: 4 additions & 12 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
<?php declare(strict_types=1);

namespace Kenshō\Templates;
namespace Kenshō\XHTML;

use DOMDocument;
use Kirby\Cms\App;

/**
* @noinspection PhpUnhandledExceptionInspection
*/
App::plugin('kenshodigital/templates-xml', [
App::plugin('kensho/xhtml', [
'hooks' => [
/**
* Ensures well-formed output and
* strips whitespace between nodes
* for XML templates.
*/
'page.render:after' => function (string $contentType, array $data, string $html): string {
if (\in_array($contentType, Xml::TYPES)) {
$html = (new Xml(new DOMDocument))->process($html);
if (\in_array($contentType, XMLProcessor::TYPES)) {
$html = (new XMLProcessor(new DOMDocument))->process($html);
}
return $html;
},
Expand Down
13 changes: 8 additions & 5 deletions src/Xml.php → src/XMLProcessor.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<?php declare(strict_types=1);

namespace Kenshō\Templates;
namespace Kenshō\XHTML;

use DOMDocument;

/**
* Ensures well-formed XML output and
* strips whitespace between nodes.
* Ensures well-formed XML and XHTML
* output and strips whitespace between
* nodes.
*/
class Xml implements Processor
readonly class XMLProcessor
{
public const TYPES = [
'htm',
Expand All @@ -20,6 +21,8 @@ class Xml implements Processor
'xsl',
];

private const XML_DECLARATION = '<?xml version="1.0" encoding="utf-8"?>';

public function __construct(
private readonly DOMDocument $document
) {
Expand All @@ -28,7 +31,7 @@ public function __construct(

public function process(string $output): string
{
$this->document->loadXML($output);
$this->document->loadXML(self::XML_DECLARATION . $output);

return $this->document->saveXML();
}
Expand Down

0 comments on commit 7024a90

Please sign in to comment.