diff options
Diffstat (limited to 'src/web/lib')
| -rw-r--r-- | src/web/lib/aria.php | 17 | ||||
| -rw-r--r-- | src/web/lib/hooks.php | 10 | ||||
| -rw-r--r-- | src/web/lib/ie.php | 32 | ||||
| -rw-r--r-- | src/web/lib/image.php | 96 | ||||
| -rw-r--r-- | src/web/lib/markdown.php | 36 |
5 files changed, 191 insertions, 0 deletions
diff --git a/src/web/lib/aria.php b/src/web/lib/aria.php new file mode 100644 index 0000000..0e06b97 --- /dev/null +++ b/src/web/lib/aria.php @@ -0,0 +1,17 @@ +<?php /* Copyright (c) 2024 Freya Murphy */ + +function aria_section(string $id, ?string $title = NULL): string +{ + $out = ''; + if ($title) { + $idh = $id . '_heading'; + $out .= sprintf('<div id="%s" class="section" role="region" aria-labelledby="%s">', + $id, $idh); + $out .= sprintf('<h2 class="heading" id="%s">%s</h2>', + $idh, $title); + } else { + $out .= sprintf('<div id="%s" class="section" role="region">', + $id); + } + return $out; +} diff --git a/src/web/lib/hooks.php b/src/web/lib/hooks.php new file mode 100644 index 0000000..70853e1 --- /dev/null +++ b/src/web/lib/hooks.php @@ -0,0 +1,10 @@ +<?php /* Copyright (c) 2024 Freya Murphy */ + +function CRIMSON_pre_route_hook(Router $router): void { +} + +function CRIMSON_error_hook(?array $req, int $code): never { + $error_controller = ROUTER->load_controller('error'); + $error_controller->code($code); + CRIMSON_DIE(); +} diff --git a/src/web/lib/ie.php b/src/web/lib/ie.php new file mode 100644 index 0000000..ead6c1a --- /dev/null +++ b/src/web/lib/ie.php @@ -0,0 +1,32 @@ +<?php /* Copyright (c) 2024 Freya Murphy */ + +/// IE 5 though 8 support IE conditional comments +/// IE 4 does not (need to fall back to user agent) + +{ + if (preg_match('/MSIE\s(?P<v>\d+)/i', @$_SERVER['HTTP_USER_AGENT'], $B)) { + define('IE_VERSION', $B['v']); + } else { + define('IE_VERSION', FALSE); + } + + if (IE_VERSION == FALSE || IE_VERSION > 4) { + // ADD COND COMMENTS + define('IE_START', "<!--[if lt IE 8 ]>"); + define('IE_END', "<![endif]-->"); + } else { + // IE4 DETECTED, DO NOT ADD COMMENTS + define('IE_START', ''); + define('IE_END', ''); + } +} + +function ie(string $inner) { + return IE_START . $inner . IE_END; +} + +function ie_ua(string $inner, int $ver) { + if (IE_VERSION == $ver) + return $inner; + return ''; +} diff --git a/src/web/lib/image.php b/src/web/lib/image.php new file mode 100644 index 0000000..7b6ec0e --- /dev/null +++ b/src/web/lib/image.php @@ -0,0 +1,96 @@ +<?php /* Copyright (c) 2024 Freya Murphy */ + + + +function __get_mime($type) +{ + switch ($type) { + case 'mp4': + return 'video/mp4'; + case 'webm': + return 'video/webm'; + case 'gif': + return 'image/gif'; + case 'png': + return 'image/png'; + case 'jpg': + return 'image/jpeg'; + case 'webp': + return 'image/webp'; + default: + return NULL; + } +} + +function __make_source( + $name, + $format, + $media) +{ + if ($media) { + $media = "media=\"$media\""; + } else { + $media = ''; + } + $path = Base::get_url('public/' . $name . '.' . $format, TRUE); + $mime = __get_mime($format); + return sprintf('<source type="%s" srcset="%s" %s>', + $mime, $path, $media); +} + +function image( + $name, + $alt, + $formats = array('webp', 'png'), + $animated = FALSE, + $attrs = array(), + + $height = NULL, + $width = NULL, + $size = NULL) :string +{ + + if ($animated === TRUE) { + $animated = array('gif'); + } + + if (!$animated) { + $animated = array(); + } + + $out = "<picture>"; + + foreach ($formats as $format) { + $media = count($animated) ? '(prefers-reduced-motion: reduce)' : NULL; + $out .= __make_source($name, $format, $media); + } + + foreach ($animated as $format) { + $out .= __make_source($name, $format, NULL); + } + + $format = end($formats); + $path = Base::get_url('public/' . $name . '.' . $format, TRUE); + $out .= "<img src=\"$path\""; + if ($alt) { + $alt = lang($alt); + $attrs['alt'] = $alt; + $attrs['title'] = $alt; + } + if ($width) { + $attrs['width'] = $width; + } + if ($height) { + $attrs['height'] = $height; + } + if ($size) { + $attrs['width'] = $size; + $attrs['height'] = $size; + } + foreach ($attrs as $key => $value) { + $out .= " $key=\"$value\""; + } + $out .= '></picture>'; + + return $out; +} diff --git a/src/web/lib/markdown.php b/src/web/lib/markdown.php new file mode 100644 index 0000000..5279a1f --- /dev/null +++ b/src/web/lib/markdown.php @@ -0,0 +1,36 @@ +<?php /* Copyright (c) 2024 Freya Murphy */ + +class MarkdownParser { + + private $parsedown; + + function __construct() { + $this->parsedown = new ParsedownExtra(); + } + + /** + * @return array<string,mixed> + */ + function parse(string $path): array { + $content = file_get_contents($path); + $data = array( + 'meta' => array(), + 'content' => $content + ); + if (str_starts_with($content, '---')) { + $parts = explode('---', $content); + $data['content'] = trim(implode('---', array_slice($parts, 2))); + $meta = array_filter(explode("\n", $parts[1]), fn($x) => $x != ''); + foreach ($meta as $set) { + $parts = explode(": ", $set); + $key = trim($parts[0]); + $value = trim($parts[1]); + $data['meta'][$key] = $value; + } + + } + $data['content'] = $this->parsedown->text($data['content']); + return $data; + } + +} |