Skip to content

Commit 42352d0

Browse files
committed
Added inline color parsing.
1 parent 24d4fc7 commit 42352d0

File tree

11 files changed

+228
-42
lines changed

11 files changed

+228
-42
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,25 +121,35 @@ I've composed a wiki page to describe features of this extension.
121121
Hello I should be in red text :D
122122
:color
123123

124+
:color red this is inline! :color
125+
124126
# 3 Character hex
125127
:color #AAA
126128
Hello!
127129
:color
128130

131+
:color #AAA this is inline! :color
132+
129133
# 6 Character hex
130134
:color #DADADA
131135
Hello!
132136
:color
133137

138+
:color #DADADA this is inline! :color
139+
134140
# RGB
135141
:color 255,255,255
136142
Hello!
137143
:color
138144

145+
:color 255,255,255 this is inline! :color
146+
139147
# RGBA
140148
:color 255,255,255,50
141149
Hello!
142150
:color
151+
152+
:color 255,255,255,50 this is inline! :color
143153
```
144154

145155
[More info](https://github.com/johnnyhuy/laravel-useful-commonmark-extension/wiki/Color)

src/Inline/Element/Color.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace JohnnyHuy\Laravel\Inline\Element;
4+
5+
use League\CommonMark\Inline\Element\AbstractStringContainer;
6+
7+
class Color extends AbstractStringContainer {}

src/Inline/Parser/ColorParser.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace JohnnyHuy\Laravel\Inline\Parser;
6+
7+
use JohnnyHuy\Laravel\Inline\Element\Color;
8+
use League\CommonMark\InlineParserContext;
9+
use League\CommonMark\Inline\Parser\AbstractInlineParser;
10+
11+
class ColorParser extends AbstractInlineParser
12+
{
13+
/**
14+
* @param InlineParserContext $inlineContext
15+
* @return bool
16+
*/
17+
public function parse(InlineParserContext $inlineContext)
18+
{
19+
$cursor = $inlineContext->getCursor();
20+
$savedState = $cursor->saveState();
21+
22+
$cursor->advance();
23+
24+
$regex = '/(?:color|colour)(?:\s(?:(\#?[A-z]+|\d{1,3}\,\s?\d{1,3}\,\s?\d{1,3}(\,\s?\d{1,3})?)))\s(.*(?!:color))\s(?:\:color)/';
25+
$validate = $cursor->match($regex);
26+
27+
if (!$validate) {
28+
$cursor->restoreState($savedState);
29+
return false;
30+
}
31+
32+
$matches = [];
33+
preg_match($regex, $validate, $matches);
34+
[, $color, $alpha, $content] = $matches;
35+
36+
if (preg_match('/[\,]+/', $color, $_)) {
37+
if (empty($alpha)) {
38+
$color = "rgb({$color})";
39+
} else {
40+
$color = "rgba({$color})";
41+
}
42+
} else if (preg_match('/[\#]+/', $color, $_)) {
43+
$color = "#{$color}";
44+
}
45+
46+
$data['color'] = $color;
47+
48+
$inlineContext->getContainer()->appendChild(new Color($content, $data));
49+
50+
return true;
51+
}
52+
53+
/**
54+
* @return string[]
55+
*/
56+
public function getCharacters()
57+
{
58+
return [':'];
59+
}
60+
}

src/Inline/Renderer/CodepenRenderer.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
namespace JohnnyHuy\Laravel\Inline\Renderer;
44

5-
use League\CommonMark\HtmlElement;
6-
use League\CommonMark\Util\Configuration;
5+
use ErrorException;
76
use JohnnyHuy\Laravel\Inline\Element\Codepen;
87
use League\CommonMark\ElementRendererInterface;
8+
use League\CommonMark\HtmlElement;
99
use League\CommonMark\Inline\Element\AbstractInline;
10-
use League\CommonMark\Util\ConfigurationAwareInterface;
1110
use League\CommonMark\Inline\Element\AbstractWebResource;
1211
use League\CommonMark\Inline\Renderer\InlineRendererInterface;
12+
use League\CommonMark\Util\Configuration;
1313

1414
class CodepenRenderer implements InlineRendererInterface
1515
{
@@ -20,10 +20,10 @@ class CodepenRenderer implements InlineRendererInterface
2020

2121
/**
2222
* @param AbstractInline|AbstractWebResource $inline
23-
* @param \League\CommonMark\ElementRendererInterface $htmlRenderer
23+
* @param ElementRendererInterface $htmlRenderer
2424
*
25-
* @return \League\CommonMark\HtmlElement|string
26-
* @throws \ErrorException
25+
* @return HtmlElement|string
26+
* @throws ErrorException
2727
*/
2828
public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
2929
{
@@ -39,7 +39,7 @@ public function render(AbstractInline $inline, ElementRendererInterface $htmlRen
3939
//seems that the used codepen url is invalid
4040
//or codepen is currently not available
4141
if (is_null($apiResponse)) {
42-
throw new \ErrorException('Codepen request returned null: ' . $apiUrl);
42+
throw new ErrorException('Codepen request returned null: ' . $apiUrl);
4343
}
4444

4545
//parse the oembed response
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace JohnnyHuy\Laravel\Inline\Renderer;
4+
5+
use InvalidArgumentException;
6+
use JohnnyHuy\Laravel\Inline\Element\Color;
7+
use JohnnyHuy\Laravel\Inline\Element\YouTube;
8+
use League\CommonMark\ElementRendererInterface;
9+
use League\CommonMark\HtmlElement;
10+
use League\CommonMark\Inline\Element\AbstractInline;
11+
use League\CommonMark\Inline\Element\AbstractWebResource;
12+
use League\CommonMark\Inline\Renderer\InlineRendererInterface;
13+
use League\CommonMark\Util\Configuration;
14+
use League\CommonMark\Util\ConfigurationAwareInterface;
15+
16+
class ColorRenderer implements InlineRendererInterface, ConfigurationAwareInterface
17+
{
18+
/**
19+
* @var Configuration
20+
*/
21+
protected $config;
22+
23+
/**
24+
* @param AbstractInline|AbstractWebResource $inline
25+
* @param ElementRendererInterface $htmlRenderer
26+
*
27+
* @return HtmlElement|string
28+
*/
29+
public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
30+
{
31+
if (!($inline instanceof Color)) {
32+
throw new InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
33+
}
34+
35+
return new HtmlElement('span', ['style' => "color: {$inline->getData('color')}"], $inline->getContent());
36+
}
37+
38+
/**
39+
* @param Configuration $configuration
40+
*/
41+
public function setConfiguration(Configuration $configuration)
42+
{
43+
$this->config = $configuration;
44+
}
45+
}

src/Inline/Renderer/GistRenderer.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22

33
namespace JohnnyHuy\Laravel\Inline\Renderer;
44

5-
use League\CommonMark\HtmlElement;
6-
use League\CommonMark\Util\Configuration;
75
use JohnnyHuy\Laravel\Inline\Element\Gist;
86
use League\CommonMark\ElementRendererInterface;
7+
use League\CommonMark\HtmlElement;
98
use League\CommonMark\Inline\Element\AbstractInline;
10-
use League\CommonMark\Util\ConfigurationAwareInterface;
119
use League\CommonMark\Inline\Element\AbstractWebResource;
1210
use League\CommonMark\Inline\Renderer\InlineRendererInterface;
11+
use League\CommonMark\Util\Configuration;
1312

1413
class GistRenderer implements InlineRendererInterface
1514
{
@@ -20,23 +19,20 @@ class GistRenderer implements InlineRendererInterface
2019

2120
/**
2221
* @param AbstractInline|AbstractWebResource $inline
23-
* @param \League\CommonMark\ElementRendererInterface $htmlRenderer
22+
* @param ElementRendererInterface $htmlRenderer
2423
*
25-
* @return \League\CommonMark\HtmlElement|string
24+
* @return HtmlElement|string
2625
*/
2726
public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
2827
{
2928
if (!($inline instanceof Gist)) {
3029
throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
3130
}
3231

33-
//generates the same script element, which you can see
34-
//in the "embed gist" input field
3532
$script = new HtmlElement('script', [
3633
'src' => $inline->getUrl().'.js'
3734
]);
3835

39-
//add a div wrapper around the script element
4036
return new HtmlElement('div', ['class' => 'gist-container'], $script);
4137
}
4238

src/Inline/Renderer/SoundCloudRenderer.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
namespace JohnnyHuy\Laravel\Inline\Renderer;
44

5-
use League\CommonMark\HtmlElement;
6-
use League\CommonMark\Util\Configuration;
7-
use League\CommonMark\ElementRendererInterface;
5+
use ErrorException;
86
use JohnnyHuy\Laravel\Inline\Element\SoundCloud;
7+
use League\CommonMark\ElementRendererInterface;
8+
use League\CommonMark\HtmlElement;
99
use League\CommonMark\Inline\Element\AbstractInline;
10-
use League\CommonMark\Util\ConfigurationAwareInterface;
1110
use League\CommonMark\Inline\Element\AbstractWebResource;
1211
use League\CommonMark\Inline\Renderer\InlineRendererInterface;
12+
use League\CommonMark\Util\Configuration;
1313

1414
class SoundCloudRenderer implements InlineRendererInterface
1515
{
@@ -20,10 +20,10 @@ class SoundCloudRenderer implements InlineRendererInterface
2020

2121
/**
2222
* @param AbstractInline|AbstractWebResource $inline
23-
* @param \League\CommonMark\ElementRendererInterface $htmlRenderer
23+
* @param ElementRendererInterface $htmlRenderer
2424
*
25-
* @return \League\CommonMark\HtmlElement|string
26-
* @throws \ErrorException
25+
* @return HtmlElement|string
26+
* @throws ErrorException
2727
*/
2828
public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
2929
{
@@ -35,16 +35,16 @@ public function render(AbstractInline $inline, ElementRendererInterface $htmlRen
3535
$url = "https://soundcloud.com/oembed?&format=json&url={$inline->getUrl()}&maxheight=166";
3636
$soundCloud = $this->getContent($url);
3737

38-
//seems that the used soundcloud url is invalid
39-
//or soundcloud is currently not available
40-
if (is_null($soundCloud)) {
41-
throw new \ErrorException('SoundCloud request returned null: ' . $url);
38+
// Seems that the used SoundCloud url is invalid
39+
// or SoundCloud is currently not available
40+
if ($soundCloud === null) {
41+
throw new ErrorException('SoundCloud request returned null: ' . $url);
4242
}
4343

44-
//parse the oembed response
44+
// Parse the embed response
4545
$soundCloud = json_decode($soundCloud);
4646

47-
//use the oembed html snippet as response
47+
// Use the embed html snippet as response
4848
return $soundCloud->html;
4949
}
5050

src/Inline/Renderer/YouTubeRenderer.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
namespace JohnnyHuy\Laravel\Inline\Renderer;
44

5-
use League\CommonMark\HtmlElement;
6-
use League\CommonMark\Util\Configuration;
75
use JohnnyHuy\Laravel\Inline\Element\YouTube;
86
use League\CommonMark\ElementRendererInterface;
7+
use League\CommonMark\HtmlElement;
98
use League\CommonMark\Inline\Element\AbstractInline;
10-
use League\CommonMark\Util\ConfigurationAwareInterface;
119
use League\CommonMark\Inline\Element\AbstractWebResource;
1210
use League\CommonMark\Inline\Renderer\InlineRendererInterface;
11+
use League\CommonMark\Util\Configuration;
12+
use League\CommonMark\Util\ConfigurationAwareInterface;
1313

1414
class YouTubeRenderer implements InlineRendererInterface, ConfigurationAwareInterface
1515
{
@@ -20,17 +20,17 @@ class YouTubeRenderer implements InlineRendererInterface, ConfigurationAwareInte
2020

2121
/**
2222
* @param AbstractInline|AbstractWebResource $inline
23-
* @param \League\CommonMark\ElementRendererInterface $htmlRenderer
23+
* @param ElementRendererInterface $htmlRenderer
2424
*
25-
* @return \League\CommonMark\HtmlElement|string
25+
* @return HtmlElement|string
2626
*/
2727
public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
2828
{
2929
if (!($inline instanceof YouTube)) {
3030
throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
3131
}
3232

33-
//create a new iframe with the given youtube url
33+
// Create a new iframe with the given youtube url
3434
$iframe = new HtmlElement('iframe', [
3535
'width' => 640,
3636
'height' => 390,
@@ -39,7 +39,7 @@ public function render(AbstractInline $inline, ElementRendererInterface $htmlRen
3939
'frameborder' => 0,
4040
]);
4141

42-
//return the iframe with a span as wrapper element
42+
// Return the iframe with a span as wrapper element
4343
return new HtmlElement('span', ['class' => 'youtube-video'], $iframe);
4444
}
4545

0 commit comments

Comments
 (0)