Skip to content

Commit

Permalink
Some code cleanup on ParsedownGravTrait and Grav classes
Browse files Browse the repository at this point in the history
  • Loading branch information
mahagr committed Feb 17, 2018
1 parent 17ba58a commit 8286803
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
9 changes: 5 additions & 4 deletions system/src/Grav/Common/Grav.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,10 +449,11 @@ public function fallbackUrl($path)
$supported_types = $config->get('media.types');

// Check whitelist first, then ensure extension is a valid media type
if (!empty($fallback_types) && !in_array($uri_extension, $fallback_types)) {
return;
} elseif (!array_key_exists($uri_extension, $supported_types)) {
return;
if (!empty($fallback_types) && !\in_array($uri_extension, $fallback_types, true)) {

This comment has been minimized.

Copy link
@rhukster

rhukster Feb 17, 2018

Member

what's the \ for here? !\in_array

This comment has been minimized.

Copy link
@mahagr

mahagr Feb 17, 2018

Author Member

It's to slightly speed up the function call as it doesn't need to look into the current namespace first.

return false;
}
if (!array_key_exists($uri_extension, $supported_types)) {
return false;
}

$path_parts = pathinfo($path);
Expand Down
51 changes: 27 additions & 24 deletions system/src/Grav/Common/Markdown/ParsedownGravTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected function init($page, $defaults)
$grav = Grav::instance();

$this->page = $page;
$this->BlockTypes['{'] [] = "TwigTag";
$this->BlockTypes['{'] [] = 'TwigTag';
$this->special_chars = ['>' => 'gt', '<' => 'lt', '"' => 'quot'];

if ($defaults === null) {
Expand All @@ -56,6 +56,9 @@ protected function init($page, $defaults)
*
* @param $type
* @param $tag
* @param bool $continuable
* @param bool $completable
* @param $index
*/
public function addBlockType($type, $tag, $continuable = false, $completable = false, $index = null)
{
Expand All @@ -67,7 +70,7 @@ public function addBlockType($type, $tag, $continuable = false, $completable = f
$block = &$this->BlockTypes[$type];
}

if (!isset($index)) {
if (null === $index) {
$block[] = $tag;
} else {
array_splice($block, $index, 0, [$tag]);
Expand All @@ -86,10 +89,11 @@ public function addBlockType($type, $tag, $continuable = false, $completable = f
*
* @param $type
* @param $tag
* @param $index
*/
public function addInlineType($type, $tag, $index = null)
{
if (!isset($index) || !isset($this->InlineTypes[$type])) {
if (null === $index || !isset($this->InlineTypes[$type])) {
$this->InlineTypes[$type] [] = $tag;
} else {
array_splice($this->InlineTypes[$type], $index, 0, [$tag]);
Expand All @@ -109,7 +113,7 @@ public function addInlineType($type, $tag, $index = null)
*/
protected function isBlockContinuable($Type)
{
$continuable = in_array($Type, $this->continuable_blocks) || method_exists($this, 'block' . $Type . 'Continue');
$continuable = \in_array($Type, $this->continuable_blocks) || method_exists($this, 'block' . $Type . 'Continue');

return $continuable;
}
Expand All @@ -123,7 +127,7 @@ protected function isBlockContinuable($Type)
*/
protected function isBlockCompletable($Type)
{
$completable = in_array($Type, $this->completable_blocks) || method_exists($this, 'block' . $Type . 'Complete');
$completable = \in_array($Type, $this->completable_blocks) || method_exists($this, 'block' . $Type . 'Complete');

return $completable;
}
Expand Down Expand Up @@ -157,32 +161,31 @@ public function setSpecialChars($special_chars)

/**
* Ensure Twig tags are treated as block level items with no <p></p> tags
*
* @param array $line
* @return array|null
*/
protected function blockTwigTag($Line)
protected function blockTwigTag($line)
{
if (preg_match('/(?:{{|{%|{#)(.*)(?:}}|%}|#})/', $Line['body'], $matches)) {
$Block = [
'markup' => $Line['body'],
];

return $Block;
if (preg_match('/(?:{{|{%|{#)(.*)(?:}}|%}|#})/', $line['body'], $matches)) {
return ['markup' => $line['body']];
}

return null;
}

protected function inlineSpecialCharacter($Excerpt)
protected function inlineSpecialCharacter($excerpt)
{
if ($Excerpt['text'][0] === '&' && !preg_match('/^&#?\w+;/', $Excerpt['text'])) {
if ($excerpt['text'][0] === '&' && !preg_match('/^&#?\w+;/', $excerpt['text'])) {
return [
'markup' => '&amp;',
'extent' => 1,
];
}

if (isset($this->special_chars[$Excerpt['text'][0]])) {
if (isset($this->special_chars[$excerpt['text'][0]])) {
return [
'markup' => '&' . $this->special_chars[$Excerpt['text'][0]] . ';',
'markup' => '&' . $this->special_chars[$excerpt['text'][0]] . ';',
'extent' => 1,
];
}
Expand All @@ -199,11 +202,11 @@ protected function inlineImage($excerpt)
$excerpt['extent'] = $excerpt['extent'] + strlen($matches[1]) - 1;

return $excerpt;
} else {
$excerpt['type'] = 'image';
$excerpt = parent::inlineImage($excerpt);
}

$excerpt['type'] = 'image';
$excerpt = parent::inlineImage($excerpt);

// if this is an image process it
if (isset($excerpt['element']['attributes']['src'])) {
$excerpt = Excerpts::processImageExcerpt($excerpt, $this->page);
Expand All @@ -228,10 +231,10 @@ protected function inlineLink($excerpt)
$excerpt['extent'] = $excerpt['extent'] + strlen($matches[1]) - 1;

return $excerpt;
} else {
$excerpt = parent::inlineLink($excerpt);
}

$excerpt = parent::inlineLink($excerpt);

// if this is a link
if (isset($excerpt['element']['attributes']['href'])) {
$excerpt = Excerpts::processLinkExcerpt($excerpt, $this->page, $type);
Expand All @@ -243,10 +246,10 @@ protected function inlineLink($excerpt)
// For extending this class via plugins
public function __call($method, $args)
{
if (isset($this->$method) === true) {
$func = $this->$method;
if (isset($this->{$method}) === true) {
$func = $this->{$method};

return call_user_func_array($func, $args);
return \call_user_func_array($func, $args);
}

return null;
Expand Down

0 comments on commit 8286803

Please sign in to comment.