website/snippets/HTMLCodeDemoElement/HTMLCodeDemoElement.php

84 lines
No EOL
2.2 KiB
PHP

<?php
use const VVWebsite\ICONS_DIR;
require_once VV::root("src/Consts.php");
$HTMLCodeDemoElement = new class {
public bool $valid = false;
public readonly array $files;
private readonly string $dir;
private readonly string $namespace;
public function __construct() {
$this->namespace = array_key_exists("id", $_GET) ? $_GET["id"] : null;
if ($this->namespace) {
$this->dir = "snippets/HTMLCodeDemoElement/{$this->namespace}/";
if (is_dir(VV::root($this->dir))) {
// Strip "." and ".."
$this->files = array_filter(scandir(VV::root($this->dir), SCANDIR_SORT_DESCENDING), function (string $item) {
return substr($item, 0, 1) !== ".";
});
$this->valid = true;
}
}
}
public static function name(string $file): string {
return substr($file, 0, strlen($file) - 4);
}
public static function language(string $file): string {
return explode(".", $file, 3)[1];
}
public static function icon(string $file): string {
$file = self::language($file);
$path = ICONS_DIR . "languages/{$file}.svg";
// Return language icon if exists in icons dir else default to text
return is_file(VV::root($path)) ? VV::embed($path) : VV::embed(ICONS_DIR . "languages/txt.svg");
}
public function file_path(string $file): string {
return $this->dir . $file;
}
}
?>
<code-demo>
<?php if ($HTMLCodeDemoElement->valid): ?>
<div class="header">
<?php foreach ($HTMLCodeDemoElement->files as $file): ?>
<button class="inline" data-lang="<?= $HTMLCodeDemoElement::language($file) ?>" data-file="<?= $HTMLCodeDemoElement::name($file) ?>">
<?= $HTMLCodeDemoElement::icon($file) ?>
<p><?= substr($file, 0, strlen($file) - 4) ?></p>
</button>
<?php endforeach; ?>
</div>
<div class="body">
<?php foreach ($HTMLCodeDemoElement->files as $file): ?>
<div class="tab" data-file="<?= $HTMLCodeDemoElement::name($file) ?>">
<?= VV::include($HTMLCodeDemoElement->file_path($file)) ?>
</div>
<?php endforeach; ?>
</div>
<?php else: ?>
<div class="header">
<button>
<p>Not Found</p>
</button>
</div>
<div class="body"><p>404</p></div>
<?php endif; ?>
</code-demo>
<script type="module" src="/assets/js/snippets/HTMLCodeDemoElement.mjs"></script>