website/modules/snippet/Snippet.php
vlw e5a0a8169c Initial code commit (#1)
First (rushed 😦) release of the Vegvisir website.

Reviewed-on: https://codeberg.org/vegvisir/website/pulls/1
Co-authored-by: vlw <victor@vlw.se>
Co-committed-by: vlw <victor@vlw.se>
2024-08-30 10:01:58 +00:00

33 lines
No EOL
1.1 KiB
PHP

<?php
enum Snippet: string {
// Path relative from root
private const REL_BASE_PATH = "modules/snippet/snippets/";
// Languages
case JAVASCRIPT = ".js";
case CSS = ".css";
case PLAINTEXT = ".txt";
case PHP = ".php";
public static function put(string $name, self $lang): string {
// Create path relative from base with filename and language extension (from enum value)
$path = VV::root(self::REL_BASE_PATH . $name . $lang->value);
// Bail out if snippet can't be found
if (!is_file($path)) {
return "<pre><code class='language-plaintext'>!REF#</code></pre>";
}
// Use enum name in lowercase as highlight.js lanuage definer
$output = "<pre><code class='language-" . strtolower($lang->name) . "'>";
$output .= htmlspecialchars(file_get_contents($path));
$output .= "</code></pre>";
return $output;
}
}
?>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.10.0/styles/github-dark.min.css"/>
<script type="module"><?= VV::js("modules/snippet/snippet.js") ?></script>