Skip to content

Commit d3004fe

Browse files
committed
Use Blade components instead of directives
1 parent ca08342 commit d3004fe

File tree

7 files changed

+174
-129
lines changed

7 files changed

+174
-129
lines changed

config/torchlight.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
'token' => env('TORCHLIGHT_TOKEN'),
1414

1515
// If you want to register the blade directives, set this to true.
16-
'blade_directives' => true,
16+
'blade_components' => true,
1717
];

src/Blade/BladeManager.php

Lines changed: 12 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -8,72 +8,15 @@
88
use Hammerstone\Torchlight\Block;
99
use Hammerstone\Torchlight\Client;
1010
use Illuminate\Http\Response;
11+
use Illuminate\Support\Str;
1112

1213
class BladeManager
1314
{
1415
protected static $blocks = [];
1516

16-
public static function registerDirectives($app)
17+
public static function registerBlock(Block $block)
1718
{
18-
$class = static::class;
19-
20-
$app['blade.compiler']->directive('torchlight', function ($args) use ($class) {
21-
return "<?php $class::openBlock($args); ?>";
22-
});
23-
24-
$app['blade.compiler']->directive('endtorchlight', function () use ($class) {
25-
return "<?php $class::closeBlock(); ?>";
26-
});
27-
}
28-
29-
public static function openBlock($language = null, $code = null, $theme = null)
30-
{
31-
if (is_null($code)) {
32-
// If the developer didn't pass any code in, then we assume
33-
// they are going to render it in the blade view, so we
34-
// need to capture it.
35-
ob_start();
36-
} else if (is_file($code)) {
37-
$code = file_get_contents($code);
38-
} else if (is_file(resource_path($code))) {
39-
$code = file_get_contents(resource_path($code));
40-
}
41-
42-
$block = Block::make()->setLanguage($language);
43-
44-
if ($theme) {
45-
$block->setTheme($theme);
46-
}
47-
48-
if ($code) {
49-
$block->setCode($code);
50-
}
51-
5219
static::$blocks[] = $block;
53-
54-
if ($code) {
55-
// If they gave us the code already, then there will be no
56-
// closing directive so we close ourselves.
57-
static::closeBlock();
58-
}
59-
}
60-
61-
public static function closeBlock()
62-
{
63-
/** @var \Hammerstone\Torchlight\Block $block */
64-
$block = last(static::$blocks);
65-
66-
if (!$block->code) {
67-
// Close the block, cleaning the buffer out and storing it
68-
// as the developer's code they'd like us to highlight.
69-
$code = ob_get_clean();
70-
71-
$block = $block->setCode($code, $unrwapPreTags = true);
72-
}
73-
74-
// Echo out a unique placeholder into the buffer, which is captured
75-
// by us, so we can replace it with highlighted code later.
76-
echo $block->placeholder();
7720
}
7821

7922
public static function render(Response $response)
@@ -90,8 +33,16 @@ public static function render(Response $response)
9033
$content = $response->content();
9134

9235
foreach ($blocks as $block) {
93-
// Substitute all the placeholders that we left with the highlighted html.
94-
$content = str_replace($block->placeholder(), $block->html, $content);
36+
$swap = [
37+
$block->placeholder() => $block->highlighted,
38+
$block->placeholder('classes') => $block->classes,
39+
$block->placeholder('styles') => $block->styles,
40+
];
41+
42+
foreach ($swap as $search => $replace) {
43+
// Substitute all the placeholders that we left with the highlighted html.
44+
$content = str_replace($search, $replace, $content);
45+
}
9546
}
9647

9748
return $response->setContent($content);

src/Blade/CodeComponent.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* @author Aaron Francis <aarondfrancis@gmail.com|https://twitter.com/aarondfrancis>
4+
*/
5+
6+
namespace Hammerstone\Torchlight\Blade;
7+
8+
use Hammerstone\Torchlight\Block;
9+
use Illuminate\View\Component;
10+
use Hammerstone\Torchlight\Blade\BladeManager;
11+
12+
class CodeComponent extends Component
13+
{
14+
public $language;
15+
16+
public $theme;
17+
18+
public $contents;
19+
20+
public $block;
21+
22+
/**
23+
* Create a new component instance.
24+
*
25+
* @param $language
26+
* @param null $theme
27+
* @param null $contents
28+
*/
29+
public function __construct($language, $theme = null, $contents = null)
30+
{
31+
$this->language = $language;
32+
$this->theme = $theme;
33+
$this->contents = $contents;
34+
35+
$this->block = Block::make()->setLanguage($this->language)->setTheme($this->theme);
36+
}
37+
38+
public function capture($contents)
39+
{
40+
$contents = $contents ?: $this->contents;
41+
42+
if (is_file(resource_path($contents))) {
43+
$contents = file_get_contents(resource_path($contents));
44+
}
45+
46+
BladeManager::registerBlock($this->block->setCode($contents));
47+
}
48+
49+
/**
50+
* Get the view / contents that represent the component.
51+
*
52+
* @return string
53+
*/
54+
public function render()
55+
{
56+
// Put all of the attributes on the code element, merging in our placeholder
57+
// classes and style string. Echo out the slot, but capture capture it
58+
// using output buffering. We then pass it through as the contents to
59+
// highlight, leaving the placeholder there so we can replace it
60+
// later with fully highlighted code.
61+
return <<<'EOT'
62+
<code {{
63+
$attributes->except('style')->merge([
64+
'class' => $block->placeholder('classes'),
65+
'style' => $attributes->get('style') . $block->placeholder('styles')
66+
])
67+
}}><?php ob_start(); ?>{{ $slot }}<?php $capture(ob_get_clean()) ?>{{ $block->placeholder() }}</code>
68+
EOT;
69+
70+
}
71+
}

src/Block.php

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ class Block
1616

1717
public $code;
1818

19-
public $html;
19+
public $highlighted;
20+
21+
public $wrapped;
22+
23+
public $classes;
24+
25+
public $styles;
2026

2127
public $cacheBust = 0;
2228

@@ -59,20 +65,19 @@ public function id()
5965
*/
6066
public function hash()
6167
{
62-
return md5(
63-
$this->language .
64-
$this->theme .
65-
$this->code .
66-
$this->cacheBust
67-
);
68+
return md5($this->language . $this->theme . $this->code . $this->cacheBust);
6869
}
6970

7071
/**
7172
* @return string
7273
*/
73-
public function placeholder()
74+
public function placeholder($extra = '')
7475
{
75-
return "__torchlight-block-{$this->id()}__";
76+
if ($extra) {
77+
$extra = "_$extra";
78+
}
79+
80+
return "__torchlight-block-{$this->id()}{$extra}__";
7681
}
7782

7883
/**
@@ -92,7 +97,9 @@ public function setLanguage($language)
9297
*/
9398
public function setTheme($theme)
9499
{
95-
$this->theme = $theme;
100+
if ($theme) {
101+
$this->theme = $theme;
102+
}
96103

97104
return $this;
98105
}
@@ -101,24 +108,9 @@ public function setTheme($theme)
101108
* @param $code
102109
* @return $this
103110
*/
104-
public function setCode($code, $unwrapPreTags = false)
111+
public function setCode($code)
105112
{
106-
$code = $this->clean($code);
107-
108-
if ($unwrapPreTags) {
109-
$lines = explode("\n", $code);
110-
111-
if (trim(head($lines)) === '<pre>' && trim(last($lines)) === '</pre>') {
112-
array_pop($lines);
113-
array_shift($lines);
114-
115-
$code = implode("\n", $lines);
116-
117-
$code = $this->clean($code);
118-
}
119-
}
120-
121-
$this->code = $code;
113+
$this->code = $this->clean($code);
122114

123115
return $this;
124116
}
@@ -135,12 +127,12 @@ public function setCacheBust($number)
135127
}
136128

137129
/**
138-
* @param $html
130+
* @param $wrapped
139131
* @return $this
140132
*/
141-
public function setHtml($html)
133+
public function setWrapped($wrapped)
142134
{
143-
$this->html = $html;
135+
$this->wrapped = $wrapped;
144136

145137
return $this;
146138
}
@@ -206,5 +198,4 @@ protected function dedent($code)
206198
})
207199
->implode("\n");
208200
}
209-
210201
}

0 commit comments

Comments
 (0)