diff --git a/composer.json b/composer.json index c6ea2a3..48f788c 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ "ext-curl": "*", "ext-json": "*", "minicli/curly": "^0.2.0", - "league/commonmark": "^2.0" + "league/commonmark": "^2.0", + "minicli/stencil": "^0.1.1" }, "require-dev": { "pestphp/pest": "^2.5", diff --git a/composer.lock b/composer.lock index aadb0c0..d775c52 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "2f8e0d508d32f363516043e825ffb290", + "content-hash": "0a0d5cff347d14bee706f9ac2098ad29", "packages": [ { "name": "dflydev/dot-access-data", @@ -314,6 +314,50 @@ }, "time": "2022-10-07T07:40:59+00:00" }, + { + "name": "minicli/stencil", + "version": "0.1.1", + "source": { + "type": "git", + "url": "https://github.com/minicli/stencil.git", + "reference": "a328319f68229007b65d286ae2bd77cf2b7b9dca" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/minicli/stencil/zipball/a328319f68229007b65d286ae2bd77cf2b7b9dca", + "reference": "a328319f68229007b65d286ae2bd77cf2b7b9dca", + "shasum": "" + }, + "require": { + "php": ">=8" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^3.5", + "pestphp/pest": "^1.0", + "phpunit/phpunit": "^9.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Minicli\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Dummy template generator", + "homepage": "https://github.com/minicli/stencil", + "keywords": [ + "minicli", + "template" + ], + "support": { + "issues": "https://github.com/minicli/stencil/issues", + "source": "https://github.com/minicli/stencil/tree/0.1.1" + }, + "time": "2022-06-21T08:51:47+00:00" + }, { "name": "nette/schema", "version": "v1.2.3", @@ -3188,16 +3232,16 @@ }, { "name": "sebastian/diff", - "version": "5.0.1", + "version": "5.0.3", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/diff.git", - "reference": "aae9a0a43bff37bd5d8d0311426c87bf36153f02" + "reference": "912dc2fbe3e3c1e7873313cc801b100b6c68c87b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/aae9a0a43bff37bd5d8d0311426c87bf36153f02", - "reference": "aae9a0a43bff37bd5d8d0311426c87bf36153f02", + "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/912dc2fbe3e3c1e7873313cc801b100b6c68c87b", + "reference": "912dc2fbe3e3c1e7873313cc801b100b6c68c87b", "shasum": "" }, "require": { @@ -3243,7 +3287,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/diff/issues", "security": "https://github.com/sebastianbergmann/diff/security/policy", - "source": "https://github.com/sebastianbergmann/diff/tree/5.0.1" + "source": "https://github.com/sebastianbergmann/diff/tree/5.0.3" }, "funding": [ { @@ -3251,7 +3295,7 @@ "type": "github" } ], - "time": "2023-03-23T05:12:41+00:00" + "time": "2023-05-01T07:48:21+00:00" }, { "name": "sebastian/environment", diff --git a/src/Content.php b/src/Content.php index 32e857f..0e3991f 100644 --- a/src/Content.php +++ b/src/Content.php @@ -8,17 +8,17 @@ */ class Content { - /** @var string Raw content */ - public $raw; + /** @var ?string Raw content */ + public ?string $raw = ""; /** @var array Front-matter key-pairs */ - public $front_matter = []; + public array $front_matter = []; /** @var string Body of content in markdown */ - public $body_markdown; + public string $body_markdown = ""; /** @var string Body of content in html */ - public $body_html; + public string $body_html = ""; /** * Content constructor. @@ -56,10 +56,10 @@ public function frontMatterHas(string $key): bool /** * @param string $key - * @param string $default_value - * @return string|null + * @param ?string $default_value + * @return ?string */ - public function frontMatterGet(string $key, $default_value = null) + public function frontMatterGet(string $key, ?string $default_value = null): ?string { if ($this->frontMatterHas($key)) { return $this->front_matter[$key] ?: $default_value; @@ -72,7 +72,7 @@ public function frontMatterGet(string $key, $default_value = null) * @param string $key * @param string $value */ - public function frontMatterSet(string $key, string $value) + public function frontMatterSet(string $key, string $value): void { $this->front_matter[$key] = $value; } @@ -81,7 +81,7 @@ public function frontMatterSet(string $key, string $value) * @param ContentParser $parser * @param bool $parse_markdown */ - public function parse(ContentParser $parser, bool $parse_markdown = false) + public function parse(ContentParser $parser, bool $parse_markdown = false): void { $parser->parse($this, $parse_markdown); } @@ -100,7 +100,7 @@ public function getFrontMatter(): string return $content; } - public function updateRaw() + public function updateRaw(): void { $raw = $this->getFrontMatter(); $raw .= $this->body_markdown; diff --git a/src/ContentParser.php b/src/ContentParser.php index d899e71..da9d57b 100644 --- a/src/ContentParser.php +++ b/src/ContentParser.php @@ -23,19 +23,19 @@ class ContentParser { /** @var string */ - protected $original_content; + protected string $original_content; /** @var array */ - protected $front_matter; + protected array $front_matter; /** @var string */ - protected $markdown; + protected string $markdown; /** @var array */ - protected $custom_tag_parsers; + protected array $custom_tag_parsers; /** @var array */ - protected $parser_params; + protected array $parser_params; /** * ContentParser constructor. @@ -51,7 +51,7 @@ public function __construct(array $parser_params = []) $this->addCustomTagParser('github', new GithubCustomTagParser()); } - public function addCustomTagParser($name, CustomTagParserInterface $tag_parser) + public function addCustomTagParser($name, CustomTagParserInterface $tag_parser): void { $this->custom_tag_parsers[$name] = $tag_parser; } @@ -98,11 +98,7 @@ public function getFrontMatter(string $front_matter): array return $vars; } - /** - * @return string|string[]|null - * @throws \Exception - */ - public function getHtmlBody($markdown) + public function getHtmlBody($markdown): string|null { $environment = new Environment(); $environment->addExtension(new CommonMarkCoreExtension()); @@ -128,7 +124,7 @@ public function getHtmlBody($markdown) * @param string $text * @return string */ - public function parseSpecial($text) + public function parseSpecial(string $text): string { return preg_replace_callback_array([ '/^\{%\s(.*)\s(.*)\s%}/m' => function ($match) { diff --git a/src/CustomTagParser/AudioTagParser.php b/src/CustomTagParser/AudioTagParser.php index 2b902d4..27e533f 100644 --- a/src/CustomTagParser/AudioTagParser.php +++ b/src/CustomTagParser/AudioTagParser.php @@ -2,15 +2,20 @@ namespace Parsed\CustomTagParser; +use Minicli\FileNotFoundException; +use Minicli\Stencil; use Parsed\CustomTagParserInterface; class AudioTagParser implements CustomTagParserInterface { - public function parse($tag_value, array $params = []) + /** + * @throws FileNotFoundException + */ + public function parse(string $tag_value, array $params = []): string { - return ""; + $tplDir = $params['stencilDir'] ?? __DIR__ . '/../../tpl'; + + $stencil = new Stencil($tplDir); + return $stencil->applyTemplate('audio', ['tag_value' => $tag_value]); } } diff --git a/src/CustomTagParser/GithubCustomTagParser.php b/src/CustomTagParser/GithubCustomTagParser.php index c1521ff..1905517 100644 --- a/src/CustomTagParser/GithubCustomTagParser.php +++ b/src/CustomTagParser/GithubCustomTagParser.php @@ -8,12 +8,12 @@ class GithubCustomTagParser implements CustomTagParserInterface { /** - * Returns referred github content as a markdown code block. - * @param $tag_value + * Returns referred GitHub content as a Markdown code block. + * @param string $tag_value * @param array $params * @return string */ - public function parse($tag_value, array $params = []) + public function parse(string $tag_value, array $params = []): string { if ($this->validateUrl($tag_value)) { $client = new Client(); @@ -30,10 +30,10 @@ public function parse($tag_value, array $params = []) /** * Validates a Github URL - * @param $github_url + * @param string $github_url * @return bool */ - public function validateUrl($github_url) + public function validateUrl(string $github_url): bool { preg_match('@^(?:https://)?([^/]+)@i', $github_url, $matches); return ($matches[1] == 'github.com'); @@ -43,10 +43,10 @@ public function validateUrl($github_url) * Transforms URL to obtain raw version of content * ex. web: https://github.com/minicli/librarian/blob/master/config_sample.php * ex. raw: https://raw.githubusercontent.com/minicli/librarian/master/config_sample.php - * @param $github_url + * @param string $github_url * @return string */ - public function getRawUrl($github_url) + public function getRawUrl(string $github_url): string { $raw_url = str_replace('github.com', 'raw.githubusercontent.com', $github_url); return str_replace('/blob', '', $raw_url); diff --git a/src/CustomTagParser/TwitterCustomTagParser.php b/src/CustomTagParser/TwitterCustomTagParser.php index 5b9c80b..975e035 100644 --- a/src/CustomTagParser/TwitterCustomTagParser.php +++ b/src/CustomTagParser/TwitterCustomTagParser.php @@ -8,30 +8,30 @@ class TwitterCustomTagParser implements CustomTagParserInterface { /** - * @param $tag_value + * @param string $tag_value * @param array $params * @return string */ - public function parse($tag_value, array $params = []) + public function parse(string $tag_value, array $params = []): string { return $this->fetchTwitterEmbed($tag_value); } /** * Returns embeddable tweet - * @param $tweet_id + * @param string $tweet_id * @return string */ - public function fetchTwitterEmbed($tweet_id) + public function fetchTwitterEmbed(string $tweet_id): string { $client = new Client(); - $response = $client->get('https://publish.twitter.com/oembed?url=https://twitter.com/erikaheidi/status/' . $tweet_id); + $response = $client->get('https://publish.twitter.com/oembed?url=https://twitter.com/twitterdev/status/' . $tweet_id); if ($response['code'] == 200) { $body = json_decode($response['body'], true); return $body['html']; } - return " [ Original Tweet ]"; + return " [ Original Tweet ]"; } } diff --git a/src/CustomTagParser/VideoTagParser.php b/src/CustomTagParser/VideoTagParser.php index ae76096..554d955 100644 --- a/src/CustomTagParser/VideoTagParser.php +++ b/src/CustomTagParser/VideoTagParser.php @@ -2,15 +2,20 @@ namespace Parsed\CustomTagParser; +use Minicli\FileNotFoundException; +use Minicli\Stencil; use Parsed\CustomTagParserInterface; class VideoTagParser implements CustomTagParserInterface { - public function parse($tag_value, array $params = []) + /** + * @throws FileNotFoundException + */ + public function parse(string $tag_value, array $params = []): string { - return ""; + $tplDir = $params['stencilDir'] ?? __DIR__ . '/../../tpl'; + + $stencil = new Stencil($tplDir); + return $stencil->applyTemplate('video', ['tag_value' => $tag_value]); } } diff --git a/src/CustomTagParser/YoutubeCustomTagParser.php b/src/CustomTagParser/YoutubeCustomTagParser.php index db58f54..b2f98bf 100644 --- a/src/CustomTagParser/YoutubeCustomTagParser.php +++ b/src/CustomTagParser/YoutubeCustomTagParser.php @@ -2,34 +2,44 @@ namespace Parsed\CustomTagParser; +use Minicli\FileNotFoundException; +use Minicli\Stencil; use Parsed\CustomTagParserInterface; class YoutubeCustomTagParser implements CustomTagParserInterface { - protected $width; - protected $height; + protected int $width; + protected int $height; + protected string $tplDir; /** * @param string $tag_value * @param array $params * @return string + * @throws FileNotFoundException */ - public function parse($tag_value, array $params = []) + public function parse(string $tag_value, array $params = []): string { $this->width = $params['width'] ?? 560; $this->height = $params['height'] ?? 315; + $this->tplDir = $params['stencilDir'] ?? __DIR__ . '/../../tpl'; return $this->getEmbed($tag_value); } /** - * @param string $video_url + * @param string $tag_value * @return string + * @throws FileNotFoundException */ - public function getEmbed($tag_value) + public function getEmbed(string $tag_value): string { - return '
- -
'; + $stencil = new Stencil($this->tplDir); + + return $stencil->applyTemplate('youtube', [ + 'tag_value' => $tag_value, + 'width' => $this->width, + 'height' => $this->height + ]); } } diff --git a/src/CustomTagParserInterface.php b/src/CustomTagParserInterface.php index 35bb529..2fca359 100644 --- a/src/CustomTagParserInterface.php +++ b/src/CustomTagParserInterface.php @@ -4,5 +4,5 @@ interface CustomTagParserInterface { - public function parse($tag_value, array $params = []); + public function parse(string $tag_value, array $params = []); } diff --git a/tests/Feature/ContentParserTest.php b/tests/Feature/ContentParserTest.php index 7081b38..ebdd51d 100644 --- a/tests/Feature/ContentParserTest.php +++ b/tests/Feature/ContentParserTest.php @@ -18,17 +18,17 @@ $parser = new ContentParser(); $article = $parser->parse(new Content($this->raw_content)); - expect($article)->toBeObject(); - expect($article->raw)->toBeString(); + expect($article)->toBeObject() + ->and($article->raw)->toBeString(); }); test('it loads content and parses front matter', function () { $parser = new ContentParser(); $article = $parser->parse(new Content($this->raw_content)); - expect($article->frontMatterGet('title'))->toEqual("Content Title"); - expect($article->frontMatterGet('custom'))->toEqual("custom"); - expect($article->frontMatterHas('custom'))->toBeTrue(); + expect($article->frontMatterGet('title'))->toEqual("Content Title") + ->and($article->frontMatterGet('custom'))->toEqual("custom") + ->and($article->frontMatterHas('custom'))->toBeTrue(); }); test('it loads content and parses markdown', function () { @@ -51,6 +51,6 @@ $content = "Trying `parsed`\n"; $article = $parser->parse(new Content($content), true); - expect($article->body_html)->not()->toBeNull(); - expect($article->body_html)->toContain("parsed"); + expect($article->body_html)->not()->toBeNull() + ->and($article->body_html)->toContain("parsed"); }); diff --git a/tests/Feature/ContentTest.php b/tests/Feature/ContentTest.php index 8cce15c..4c2d69a 100644 --- a/tests/Feature/ContentTest.php +++ b/tests/Feature/ContentTest.php @@ -23,9 +23,9 @@ test('it parses content with front matter', function () { $content = new Content($this->raw_content); $content->parse(new ContentParser(), true); - expect($content->frontMatterHas('title'))->toBeTrue(); - expect($content->frontMatterGet('title'))->toEqual("Content Title"); - expect($content->body_html)->toEqual("

Testing

\n"); + expect($content->frontMatterHas('title'))->toBeTrue() + ->and($content->frontMatterGet('title'))->toEqual("Content Title") + ->and($content->body_html)->toEqual("

Testing

\n"); }); test('it creates a valid front matter', function () { @@ -38,6 +38,6 @@ $content = new Content($this->raw_content); $content->parse(new ContentParser()); - expect($article->getFrontMatter())->toEqual($content->getFrontMatter()); - expect($article->frontMatterGet('custom'))->toEqual('custom'); + expect($article->getFrontMatter())->toEqual($content->getFrontMatter()) + ->and($article->frontMatterGet('custom'))->toEqual('custom'); }); diff --git a/tpl/audio.tpl b/tpl/audio.tpl new file mode 100644 index 0000000..71a5948 --- /dev/null +++ b/tpl/audio.tpl @@ -0,0 +1,4 @@ + \ No newline at end of file diff --git a/tpl/video.tpl b/tpl/video.tpl new file mode 100644 index 0000000..39f52e1 --- /dev/null +++ b/tpl/video.tpl @@ -0,0 +1,4 @@ + \ No newline at end of file diff --git a/tpl/youtube.tpl b/tpl/youtube.tpl new file mode 100644 index 0000000..abae307 --- /dev/null +++ b/tpl/youtube.tpl @@ -0,0 +1,6 @@ +