From 4ec245cfdfeb10e1910e1cb1aa6bb318b049b9c6 Mon Sep 17 00:00:00 2001 From: Chris Kankiewicz Date: Mon, 6 Jan 2020 14:06:06 -0700 Subject: [PATCH] Initial README parsing support --- app/Controllers/DirectoryController.php | 22 ++++- app/config/app.php | 7 ++ app/resources/sass/app.scss | 1 + app/resources/sass/markdown.scss | 104 ++++++++++++++++++++++++ app/resources/views/index.twig | 12 +++ composer.json | 1 + composer.lock | 48 ++++++++++- 7 files changed, 192 insertions(+), 3 deletions(-) create mode 100644 app/resources/sass/markdown.scss diff --git a/app/Controllers/DirectoryController.php b/app/Controllers/DirectoryController.php index 69a1f1b..6d0ce0c 100644 --- a/app/Controllers/DirectoryController.php +++ b/app/Controllers/DirectoryController.php @@ -3,6 +3,7 @@ namespace App\Controllers; use DI\Container; +use Parsedown; use PHLAK\Config\Config; use Slim\Psr7\Request; use Slim\Psr7\Response; @@ -19,6 +20,9 @@ class DirectoryController /** @var Container Application container */ protected $container; + /** @var Parsedown Parsedown component */ + protected $parsedown; + /** @var Twig Twig templating component */ protected $view; @@ -29,10 +33,15 @@ class DirectoryController * @param \PHLAK\Config\Config $config * @param \Slim\Views\Twig $view */ - public function __construct(Container $container, Config $config, Twig $view) - { + public function __construct( + Container $container, + Config $config, + Parsedown $parsedown, + Twig $view + ) { $this->container = $container; $this->config = $config; + $this->parsedown = $parsedown; $this->view = $view; } @@ -65,11 +74,20 @@ class DirectoryController $files->depth(0); } + $readmeFiles = Finder::create()->in($path)->depth(0)->name('/^README\.md$/i'); + if ($readmeFiles->hasResults() && ! $search) { + $readmeArray = iterator_to_array($readmeFiles); + $readme = $this->parsedown->parse( + array_shift($readmeArray)->getContents() + ); + } + return $this->view->render($response, 'index.twig', [ 'breadcrumbs' => $this->breadcrumbs($path), 'files' => $files, 'is_root' => $this->isRoot($path), 'search' => $search ?? null, + 'readme' => $readme ?? null, ]); } diff --git a/app/config/app.php b/app/config/app.php index 362c65e..ad95e92 100644 --- a/app/config/app.php +++ b/app/config/app.php @@ -49,4 +49,11 @@ return [ * Defualt value: 1000000000 */ 'max_hash_size' => Helpers::env('MAX_HASH_SIZE'), + + /** + * Parse and render README files inline. + * + * Default value: true + */ + 'render_readme' => Helpers::env('RENDER_README', true), ]; diff --git a/app/resources/sass/app.scss b/app/resources/sass/app.scss index 2cebffe..51ba35d 100644 --- a/app/resources/sass/app.scss +++ b/app/resources/sass/app.scss @@ -12,4 +12,5 @@ $fa-font-path: "./webfonts"; // Fonts @import url("https://fonts.googleapis.com/css?family=Work+Sans:200,400&display=swap"); +@import "markdown.scss"; @import "dark-mode.scss"; diff --git a/app/resources/sass/markdown.scss b/app/resources/sass/markdown.scss new file mode 100644 index 0000000..8f09635 --- /dev/null +++ b/app/resources/sass/markdown.scss @@ -0,0 +1,104 @@ +.markdown { + @apply font-sans; + + a { + @apply text-blue-600; + @apply underline; + + &:hover { + @apply text-blue-800; + } + } + + p, + blockquote, + ul, + ol, + dl, + table, + pre { + @apply my-4; + } + + ul, + ol { + @apply pl-8; + } + + ul { + @apply list-disc; + } + + ol { + @apply list-decimal; + } + + h1 { + @apply text-4xl; + @apply border-b; + @apply border-gray-400; + } + + h2 { + @apply text-3xl; + @apply border-b; + @apply border-gray-400; + } + + h3 { + @apply text-2xl; + } + + h4 { + @apply text-xl; + } + + h5 { + @apply text-base; + } + + h6 { + @apply text-base; + @apply text-gray-600; + } + + h1, + h2, + h3, + h4, + h5, + h6 { + @apply font-bold; + @apply leading-relaxed; + @apply mt-8; + @apply mb-4; + } + + h1 + p, + h2 + p, + h3 + p { + @apply mt-4; + } + + pre { + @apply bg-gray-200; + @apply rounded-sm; + @apply p-4; + @apply font-mono; + @apply text-sm; + @apply whitespace-pre; + } + + hr { + @apply border-t-2; + @apply border-gray-400; + } + + & > :first-child { + @apply mt-0; + } + + & > :last-child { + @apply mb-0; + } +} diff --git a/app/resources/views/index.twig b/app/resources/views/index.twig index 5383b94..29dbeb0 100644 --- a/app/resources/views/index.twig +++ b/app/resources/views/index.twig @@ -29,6 +29,18 @@ {{ include('components/file.twig') }} {% endfor %} + + {% if readme %} +
+
+ README.md +
+ +
+ {{ readme | raw }} +
+
+ {% endif %} {% include 'components/footer.twig' %} diff --git a/composer.json b/composer.json index 818ed93..18dbca9 100644 --- a/composer.json +++ b/composer.json @@ -11,6 +11,7 @@ ], "require": { "php": ">=7.1", + "erusev/parsedown": "^1.7", "phlak/config": "^6.0", "php-di/php-di": "^6.0", "php-di/slim-bridge": "^3.0", diff --git a/composer.lock b/composer.lock index e19e498..db99d3f 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,54 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "ee34e3c83048ffcba5f28c84ad5d06b4", + "content-hash": "b8d61605ee4f40b6ef5db770276ac304", "packages": [ + { + "name": "erusev/parsedown", + "version": "1.7.4", + "source": { + "type": "git", + "url": "https://github.com/erusev/parsedown.git", + "reference": "cb17b6477dfff935958ba01325f2e8a2bfa6dab3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/erusev/parsedown/zipball/cb17b6477dfff935958ba01325f2e8a2bfa6dab3", + "reference": "cb17b6477dfff935958ba01325f2e8a2bfa6dab3", + "shasum": "" + }, + "require": { + "ext-mbstring": "*", + "php": ">=5.3.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.8.35" + }, + "type": "library", + "autoload": { + "psr-0": { + "Parsedown": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Emanuil Rusev", + "email": "hello@erusev.com", + "homepage": "http://erusev.com" + } + ], + "description": "Parser for Markdown.", + "homepage": "http://parsedown.org", + "keywords": [ + "markdown", + "parser" + ], + "time": "2019-12-30T22:54:17+00:00" + }, { "name": "fig/http-message-util", "version": "1.1.3",