Skip to content

Commit

Permalink
render manual close #243
Browse files Browse the repository at this point in the history
- rendered  html is stored in assets/manual
folder is auto-generated
- created two no Filesystem feature:
    - readfile
    - folderflush
  • Loading branch information
vincent-peugnet committed Nov 9, 2023
1 parent 154f921 commit 6ac0b4e
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
assets/atom/*
assets/render/*
assets/global/*
assets/manual/*
assets/css/tagcolors.css
assets/js/*
database/*
Expand Down
29 changes: 22 additions & 7 deletions app/class/Controllerinfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,31 @@ public function __construct($render)
public function desktop()
{
if ($this->user->isinvite()) {
if (file_exists(Model::MAN_FILE)) {
$render = new Servicerender($this->router, $this->pagemanager, true);
$htmlman = file_get_contents(Model::MAN_FILE);
$htmlman = $render->rendermanual($htmlman);
$version = getversion();
$mandir = Model::MAN_RENDER_DIR;
try {
$manual = Fs::readfile("$mandir/manual_$version.html");
$summary = Fs::readfile("$mandir/summary_$version.html");
} catch (RuntimeException $e) {
try {
$mansrc = Fs::readfile(Model::MAN_FILE);
$render = new Servicerender($this->router, $this->pagemanager, true);
$manual = $render->rendermanual($mansrc);

$sum = new Summary(['min' => 2, 'max' => 4, 'sum' => $render->sum()]);
$summary = $sum->sumparser();
$sum = new Summary(['min' => 2, 'max' => 4, 'sum' => $render->sum()]);
$summary = $sum->sumparser();

$this->showtemplate('info', ['version' => getversion(), 'manual' => $htmlman, 'summary' => $summary]);
Fs::folderflush($mandir);
Fs::dircheck($mandir, true, 0775);

Fs::writefile("$mandir/manual_$version.html", $manual);
Fs::writefile("$mandir/summary_$version.html", $summary);
} catch (RuntimeException $e) {
$manual = '⚠️ Error while trying to access MANUAL.md file.';
$summary = '';
}
}
$this->showtemplate('info', ['version' => getversion(), 'manual' => $manual, 'summary' => $summary]);
}
}
}
50 changes: 49 additions & 1 deletion app/class/Fs.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Wcms;

use DomainException;
use LogicException;
use RuntimeException;

/**
* File system related tools. No Wcms specific param should be set here.
Expand Down Expand Up @@ -119,6 +121,27 @@ public static function accessfile(string $path, bool $createdir = false): bool
}
}

/**
* Read file as string
*
* @param string $path the file path
* @return string the file as string
*
* @throws Notfoundexception if the diven path is not a file
* @throws Fileexception if an error reading the file occured
*/
public static function readfile(string $path): string
{
if (!is_file($path)) {
throw new Notfoundexception("The given path `$path` is not a file");
}
$file = file_get_contents($path);
if ($file === false) {
throw new Fileexception("Error when reading file `$path`");
}
return $file;
}



/**
Expand Down Expand Up @@ -151,7 +174,7 @@ public static function recursecopy($src, $dst, $perm = Model::FOLDER_PERMISSION)
* @param string $file Filename to delete
*
* @throws Notfoundexception If file does not exist
* @throws Fileexception Id file cannot be deleted
* @throws Fileexception If file cannot be deleted
* @throws Unlinkexception If PHP unlink function fails for another reason
*/
public static function deletefile(string $file): void
Expand All @@ -167,4 +190,29 @@ public static function deletefile(string $file): void
throw new Unlinkexception($file);
}
}

/**
* Delete all files in a given folder
*
* @throws Notfoundexception If folder does not exist
* @throws Fileexception If a file cannot be deleted
* @throws Unlinkexception If PHP unlink function fails for another reason
*/
public static function folderflush(string $path): void
{
$path = trim($path, '/');
$files = glob("$path/*");
if ($files === false) {
throw new Notfoundexception("Error while trying to scan directory $path");
}
try {
foreach ($files as $file) {
self::deletefile($file);
}
} catch (Notfoundexception $e) {
throw new LogicException(
'Problem with Fs::folderflush(), path given to Fs::deletefile() does not exist.'
);
}
}
}
1 change: 1 addition & 0 deletions app/class/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ abstract class Model
{
public const CONFIG_FILE = 'config.json';
public const MAN_FILE = 'MANUAL.md';
public const MAN_RENDER_DIR = 'assets/manual/';
public const ASSETS_CSS_DIR = 'assets/css/';
public const ASSETS_ATOM_DIR = 'assets/atom/';
public const COLORS_FILE = self::ASSETS_CSS_DIR . 'tagcolors.css';
Expand Down
6 changes: 5 additions & 1 deletion app/fn/fn.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ function isreportingerrors()
return function_exists('Sentry\init') && !empty(Wcms\Config::sentrydsn());
}


/**
* Get W version using VERSION file in root directory.
* @return string W's current version.
* If the file cannot be read, `unknown` is used as output.
*/
function getversion(): string
{
if (file_exists('VERSION')) {
Expand Down

0 comments on commit 6ac0b4e

Please sign in to comment.