Nofu/app/generator/HTMLGenerator.php

143 lines
5 KiB
PHP
Raw Normal View History

2024-06-14 17:20:01 +02:00
<?php
namespace KTH\HTMLGenerator;
use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\Extension\HeadingPermalink\HeadingPermalinkExtension;
use League\CommonMark\Extension\TableOfContents\TableOfContentsExtension;
use League\CommonMark\MarkdownConverter;
class HTMLGenerator {
private $favicons;
private $screenshots;
/**
* Constructs a new HTMLGenerator object.
*
* @param array $services An array of service configurations.
*/
public function __construct(array $services) {
foreach ($services as $service) {
if (!empty($service['favicon'])) {
$favicons[] = $service['favicon'];
}
if (!empty($service['screenshot'])) {
$screenshots[] = $service['screenshot'];
}
}
$this->favicons = array_unique($favicons, SORT_LOCALE_STRING);
$this->screenshots = array_unique($screenshots, SORT_LOCALE_STRING);
if (isset($_SESSION['reimport'])) {
$this->resizeImg();
unset($_SESSION['reimport']);
}
$this->copyUserFile();
}
/**
* Resizes the favicons and screenshots.
*
* @return void
*/
private function resizeImg(): void {
$image = new \Zebra_Image();
foreach ($this->favicons as $favicon) {
if (!str_starts_with(strtolower($favicon), 'http')) {
if (is_file('../data/imgs/favicons/' . $favicon)) {
$image->source_path = '../data/imgs/favicons/' . $favicon;
$image->target_path = '../public/imgs/favicons/' . $favicon . '.webp';
$image->preserve_aspect_ratio = true;
$image->enlarge_smaller_images = false;
$image->resize(32, 32);
$image->target_path = '../public/imgs/big_favicons/' . $favicon . '.webp';
$image->resize(128, 128);
}
}
}
foreach ($this->screenshots as $screenshot) {
if (!str_starts_with(strtolower($screenshot), 'http')) {
if (is_file('../data/imgs/screenshots/' . $favicon)) {
$image->source_path = '../data/imgs/screenshots/' . $screenshot;
$image->target_path = '../public/imgs/screenshots/' . $screenshot . '.webp';
$image->preserve_aspect_ratio = true;
$image->enlarge_smaller_images = false;
$image->resize(1120);
$image->target_path = '../public/imgs/thumbs/' . $screenshot . '.webp';
$image->resize(250);
}
}
}
}
/**
* Copy user-provided CSS, JS, and SVG files to the public directory.
*
* @return void
*/
private function copyUserFile(): void {
if (file_exists('../data/assets/css/user.css')) {
copy('../data/assets/css/user.css', '../public/assets/css/user.css');
}
if (file_exists('../data/assets/js/user.js')) {
copy('../data/assets/js/user.js', '../public/assets/js/user.js');
}
foreach (glob("../data/assets/icons/*.svg") as $filePath) {
$filename = pathinfo($filePath, PATHINFO_BASENAME);
copy($filePath, $filename);
}
}
/**
* Generate the user documentation in HTML format.
*
* @return string | null The HTML content of the user documentation.
*/
public function genUserDoc(): string | null {
if (file_exists('../data/help.md')) {
$configMD = [
'table_of_contents' => [
'html_class' => 'table-of-contents',
'position' => 'top',
'style' => 'bullet',
'min_heading_level' => 1,
'max_heading_level' => 6,
'normalize' => 'relative',
'placeholder' => null,
],
'heading_permalink' => [
'html_class' => 'heading-permalink',
'id_prefix' => 'content',
'apply_id_to_heading' => false,
'heading_class' => '',
'fragment_prefix' => 'content',
'insert' => 'before',
'min_heading_level' => 1,
'max_heading_level' => 6,
'title' => 'Permalink',
'symbol' => '',
'aria_hidden' => true,
],
];
$environment = new Environment($configMD);
$environment->addExtension(new CommonMarkCoreExtension());
$environment->addExtension(new HeadingPermalinkExtension());
$environment->addExtension(new TableOfContentsExtension());
$converter = new MarkdownConverter($environment);
return $converter->convert(file_get_contents('../data/help.md'))->getContent();
} else {
return null;
}
}
}