<?php
/*
* Copyright (C) 2021 - All rights reserved.
* https://gigadrivegroup.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://gnu.org/licenses/>
*/
namespace App\Controller;
use Gigadrive\Bundle\MarkdownWikiBundle\Service\Storage\MarkdownWikiStorageInterface;
use Parsedown;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use function PHPUnit\Framework\assertDirectoryDoesNotExist;
/**
* Represents a controller used for serving pages.
*
* This controller is not routed by default. You may use it in
* your project, but it mainly serves as an example
* for using this bundle.
*
* @package Gigadrive\Bundle\MarkdownWikiBundle\Controller
* @author Mehdi Baaboura <mbaaboura@gigadrivegroup.com>
*/
class MarkdownWikiController extends AbstractController
{
public function __construct(
protected MarkdownWikiStorageInterface $storage,
) {
}
public function indexAction(string $path, Request $request): Response
{
$baseDocsPath = $this->getParameter('kernel.project_dir') . "/mxo-docs/";
$filesystem = new Filesystem();
$filePath = $baseDocsPath . $path . ".md";
$html = "";
if ($filesystem->exists($filePath)) {
// Parse content
$html = $this->parseContent($filePath);
} else {
throw $this->createNotFoundException("Page with path $baseDocsPath . $path not found.");
}
return $this->render("markdown/index.html.twig", [
"title" => "",
"description" => "",
"content" => $html
]);
}
function parseContent($filePath): string
{
$content = file_get_contents($filePath);
// Markdown-Parser initialisieren
$parser = new Parsedown();
// Markdown in HTML parsen
return $parser->text($content);
}
}