Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating dependencies and adding templates #3

Merged
merged 3 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
58 changes: 51 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 11 additions & 11 deletions src/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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);
}
Expand All @@ -100,7 +100,7 @@ public function getFrontMatter(): string
return $content;
}

public function updateRaw()
public function updateRaw(): void
{
$raw = $this->getFrontMatter();
$raw .= $this->body_markdown;
Expand Down
20 changes: 8 additions & 12 deletions src/ContentParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}
Expand Down Expand Up @@ -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());
Expand All @@ -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) {
Expand Down
15 changes: 10 additions & 5 deletions src/CustomTagParser/AudioTagParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<audio controls>" .
"<source src=\"$tag_value\" type=\"audio/mpeg\">" .
"Your browser does not support the audio element." .
"</audio>";
$tplDir = $params['stencilDir'] ?? __DIR__ . '/../../tpl';

$stencil = new Stencil($tplDir);
return $stencil->applyTemplate('audio', ['tag_value' => $tag_value]);
}
}
14 changes: 7 additions & 7 deletions src/CustomTagParser/GithubCustomTagParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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');
Expand All @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions src/CustomTagParser/TwitterCustomTagParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 " [ <a href='https://twitter.com/erikaheidi/status/$tweet_id'>Original Tweet</a> ]";
return " [ <a href='https://twitter.com/twitterdev/status/$tweet_id'>Original Tweet</a> ]";
}
}
15 changes: 10 additions & 5 deletions src/CustomTagParser/VideoTagParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<video controls>" .
"<source src=\"$tag_value\" type=\"video/mp4\">" .
"Your browser does not support the video tag." .
"</video>";
$tplDir = $params['stencilDir'] ?? __DIR__ . '/../../tpl';

$stencil = new Stencil($tplDir);
return $stencil->applyTemplate('video', ['tag_value' => $tag_value]);
}
}
Loading