-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp-transformer.php
More file actions
137 lines (118 loc) · 4.02 KB
/
Copy pathphp-transformer.php
File metadata and controls
137 lines (118 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/**
* Plugin Name: Blocks Engine PHP Transformer
* Plugin URI: https://github.com/Automattic/blocks-engine/tree/trunk/php-transformer
* Description: Canonical PHP primitives for transforming HTML, Markdown, and website artifacts into WordPress block outputs.
* Version: 0.6.1
* Requires PHP: 8.1
* Author: Automattic
* License: GPL-3.0-or-later
* Text Domain: blocks-engine-php-transformer
*
* @package BlocksEnginePhpTransformer
*/
declare(strict_types=1);
if ( ! defined('BLOCKS_ENGINE_PHP_TRANSFORMER_VERSION') ) {
define('BLOCKS_ENGINE_PHP_TRANSFORMER_VERSION', '0.6.1');
}
if ( ! defined('BLOCKS_ENGINE_PHP_TRANSFORMER_FILE') ) {
define('BLOCKS_ENGINE_PHP_TRANSFORMER_FILE', __FILE__);
}
if ( ! defined('BLOCKS_ENGINE_PHP_TRANSFORMER_DIR') ) {
define('BLOCKS_ENGINE_PHP_TRANSFORMER_DIR', __DIR__);
}
blocks_engine_php_transformer_load_autoloader();
if ( function_exists('do_action') ) {
do_action('blocks_engine_php_transformer_loaded');
}
/**
* Load Composer when available, otherwise register a local source autoloader.
*/
function blocks_engine_php_transformer_load_autoloader(): void
{
static $loaded = false;
if ( $loaded ) {
return;
}
$loaded = true;
$composerAutoload = __DIR__ . '/vendor/autoload.php';
if ( is_readable($composerAutoload) ) {
require_once $composerAutoload;
return;
}
spl_autoload_register(
static function (string $class): void {
$prefix = 'Automattic\\BlocksEngine\\PhpTransformer\\';
if ( 0 !== strncmp($class, $prefix, strlen($prefix)) ) {
return;
}
$relative = substr($class, strlen($prefix));
$path = __DIR__ . '/src/' . str_replace('\\', '/', $relative) . '.php';
if ( is_readable($path) ) {
require_once $path;
}
}
);
}
/**
* Return the active transformer version.
*/
function blocks_engine_php_transformer_version(): string
{
return BLOCKS_ENGINE_PHP_TRANSFORMER_VERSION;
}
/**
* Return the plugin/package directory.
*/
function blocks_engine_php_transformer_path(): string
{
return BLOCKS_ENGINE_PHP_TRANSFORMER_DIR;
}
/**
* Transform HTML into the canonical result envelope.
*
* @param array<string, mixed> $options Transformation options.
* @return array<string, mixed>
*/
function blocks_engine_php_transformer_transform_html(string $html, array $options = array()): array
{
return ( new Automattic\BlocksEngine\PhpTransformer\HtmlToBlocks\HtmlTransformer() )
->transform($html, $options)
->toArray();
}
/**
* Convert content between supported formats through the canonical result envelope.
*
* @param array<string, mixed> $options Transformation options.
* @return array<string, mixed>
*/
function blocks_engine_php_transformer_convert_format(string $content, string $fromFormat, string $toFormat, array $options = array()): array
{
return ( new Automattic\BlocksEngine\PhpTransformer\FormatBridge\FormatBridge() )
->convertResult($content, $fromFormat, $toFormat, $options)
->toArray();
}
/**
* Compile a generated website artifact into the canonical result envelope.
*
* @param array<string, mixed> $artifact Generated artifact input.
* @param array<string, mixed> $options Transformation options.
* @return array<string, mixed>
*/
function blocks_engine_php_transformer_compile_artifact(array $artifact, array $options = array()): array
{
return ( new Automattic\BlocksEngine\PhpTransformer\ArtifactCompiler\ArtifactCompiler() )
->compile($artifact, $options)
->toArray();
}
/**
* Resolve script-derived runtime context for a source document conversion.
*
* @param array<int|string, mixed> $files Source artifact files.
* @return array<string, mixed>
*/
function blocks_engine_php_transformer_runtime_context(string $html, string $sourcePath, array $files): array
{
return ( new Automattic\BlocksEngine\PhpTransformer\ArtifactCompiler\ArtifactCompiler() )
->runtimeContextForSource($html, $sourcePath, $files);
}