wp-md-rest-import/test.php

80 lines
2.5 KiB
PHP

<?php
/**
* Test script for Markdown Parser WP
*
* This script simulates the core functionality of the WordPress plugin
* in a non-WordPress environment for testing purposes.
*/
// Load Composer autoloader
require_once __DIR__ . '/vendor/autoload.php';
use Symfony\Component\Yaml\Yaml;
// Test URL
$url = 'https://git.rpi-virtuell.de/Comenius-Institut/FOERBICO/raw/branch/main/Website/content/posts/2025-03-20-Workshop-KlimaOER/index.md';
echo "Testing Markdown Parser WP functionality...\n\n";
echo "URL: $url\n\n";
try {
// Fetch markdown content
echo "Fetching markdown content...\n";
$content = file_get_contents($url);
if ($content === false) {
throw new Exception("Could not load file from URL.");
}
echo "Content fetched successfully.\n\n";
// Split YAML and Markdown
echo "Splitting YAML and Markdown content...\n";
$parts = explode('---', $content);
if (count($parts) < 3) {
throw new Exception("Invalid file format - YAML header not found.");
}
$yamlContent = trim($parts[1]);
$markdownContent = trim($parts[2]);
echo "Content split successfully.\n\n";
// Parse YAML
echo "Parsing YAML content...\n";
$parsedYaml = Yaml::parse($yamlContent);
$jsonOutput = json_encode(
$parsedYaml,
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
);
echo "YAML parsed successfully.\n\n";
// Remove title heading
echo "Processing markdown content...\n";
$markdownLines = explode("\n", $markdownContent);
foreach ($markdownLines as $key => $line) {
if (strpos(trim($line), '# ') === 0) {
unset($markdownLines[$key]);
break;
}
}
$cleanedMarkdown = implode("\n", $markdownLines);
echo "Markdown processed successfully.\n\n";
// Save results
echo "Saving results to files...\n";
file_put_contents('test-output-metadata.json', $jsonOutput);
file_put_contents('test-output-content.md', $cleanedMarkdown);
echo "Results saved successfully.\n\n";
// Display sample output
echo "=== JSON METADATA (SAMPLE) ===\n";
echo substr($jsonOutput, 0, 500) . "...\n\n";
echo "=== MARKDOWN CONTENT (SAMPLE) ===\n";
echo substr($cleanedMarkdown, 0, 500) . "...\n\n";
echo "Test completed successfully!\n";
echo "Full output saved to test-output-metadata.json and test-output-content.md\n";
} catch (Exception $e) {
echo "ERROR: " . $e->getMessage() . "\n";
exit(1);
}