Skip to content

Commit 0c8ac28

Browse files
Jigsaw Updates (#1)
Jigsaw updates
1 parent 137281d commit 0c8ac28

File tree

8 files changed

+153
-16
lines changed

8 files changed

+153
-16
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Added
6+
- `Torchlight::findTorchlightIds` method to regex a string and return all Torchlight placeholders.
7+
- `BladeManager::getBlocks`
8+
- `BladeManager::clearBlocks`
9+
10+
### Changed
11+
- Added square brackets around the Torchlight ID in the Block placeholder.
12+
- The BladeManager no longer clears the blocks while rendering. Needed for Jigsaw.
13+
314
## 0.3.0 - 2021-05-22
415

516
- Add `Torchlight` facade

src/Blade/BladeManager.php

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,27 @@
66
namespace Torchlight\Blade;
77

88
use Illuminate\Http\Response;
9+
use Illuminate\Support\Arr;
910
use Torchlight\Block;
10-
use Torchlight\Client;
11+
use Torchlight\Torchlight;
1112

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

1617
public static function registerBlock(Block $block)
1718
{
18-
static::$blocks[] = $block;
19+
static::$blocks[$block->id()] = $block;
20+
}
21+
22+
public static function getBlocks()
23+
{
24+
return static::$blocks;
25+
}
26+
27+
public static function clearBlocks()
28+
{
29+
static::$blocks = [];
1930
}
2031

2132
public static function renderResponse(Response $response)
@@ -37,23 +48,24 @@ public static function renderContent($content)
3748
return $content;
3849
}
3950

40-
$blocks = (new Client)->highlight(static::$blocks);
51+
Torchlight::highlight(static::$blocks);
4152

42-
static::$blocks = [];
53+
$ids = Torchlight::findTorchlightIds($content);
4354

44-
foreach ($blocks as $block) {
45-
$swap = [
46-
$block->placeholder() => $block->highlighted,
47-
$block->placeholder('classes') => $block->classes,
48-
$block->placeholder('styles') => $block->styles,
49-
];
55+
$swap = [];
5056

51-
foreach ($swap as $search => $replace) {
52-
// Substitute all the placeholders that we left with the highlighted html.
53-
$content = str_replace($search, $replace, $content);
57+
foreach ($ids as $id) {
58+
/** @var Block $block */
59+
if (!$block = Arr::get(static::$blocks, $id)) {
60+
continue;
5461
}
62+
63+
// Swap out all the placeholders that we left.
64+
$swap[$block->placeholder()] = $block->highlighted;
65+
$swap[$block->placeholder('classes')] = $block->classes;
66+
$swap[$block->placeholder('styles')] = $block->styles;
5567
}
5668

57-
return $content;
69+
return str_replace(array_keys($swap), array_values($swap), $content);
5870
}
5971
}

src/Blade/CodeComponent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function capture($contents)
4141

4242
if (is_file($contents)) {
4343
$contents = file_get_contents($contents);
44-
} elseif (is_file(resource_path($contents))) {
44+
} elseif (is_callable('resource_path') && is_file(resource_path($contents))) {
4545
$contents = file_get_contents(resource_path($contents));
4646
}
4747

src/Block.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function placeholder($extra = '')
127127
$extra = "_$extra";
128128
}
129129

130-
return "__torchlight-block-{$this->id()}{$extra}__";
130+
return "__torchlight-block-[{$this->id()}]{$extra}__";
131131
}
132132

133133
/**

src/Manager.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Illuminate\Contracts\Cache\Repository;
99
use Illuminate\Contracts\Container\Container;
10+
use Illuminate\Support\Arr;
1011
use Illuminate\Support\Facades\Cache;
1112
use Illuminate\Support\Str;
1213
use Illuminate\Support\Traits\Macroable;
@@ -112,4 +113,17 @@ public function cache()
112113
// If the config value is null, the default cache will be used.
113114
return Cache::store($this->config('cache'));
114115
}
116+
117+
/**
118+
* Return all the Torchlight IDs in a given string.
119+
*
120+
* @param string $content
121+
* @return array
122+
*/
123+
public function findTorchlightIds($content)
124+
{
125+
preg_match_all('/__torchlight-block-\[(.+?)\]/', $content, $matches);
126+
127+
return array_values(array_unique(Arr::get($matches, 1, [])));
128+
}
115129
}

tests/FindIdsTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* @author Aaron Francis <aaron@hammerstone.dev|https://twitter.com/aarondfrancis>
4+
*/
5+
6+
namespace Torchlight\Tests;
7+
8+
use Torchlight\Block;
9+
use Torchlight\Torchlight;
10+
11+
class FindIdsTest extends BaseTest
12+
{
13+
/** @test */
14+
public function it_will_find_all_the_ids()
15+
{
16+
$standard = Block::make();
17+
$custom1 = Block::make('custom-id');
18+
$custom2 = Block::make('custom-1234');
19+
20+
$content = <<<EOT
21+
{$standard->placeholder()}
22+
{$standard->placeholder('styles')}
23+
24+
{$custom1->placeholder()}
25+
{$custom1->placeholder('styles')}
26+
27+
<code style="{$custom2->placeholder('styles')}">{$custom2->placeholder()}</code>
28+
EOT;
29+
30+
$found = Torchlight::findTorchlightIds($content);
31+
32+
$this->assertContains($standard->id(), $found);
33+
$this->assertContains('custom-id', $found);
34+
$this->assertContains('custom-1234', $found);
35+
}
36+
37+
/** @test */
38+
public function it_only_returns_one_per()
39+
{
40+
$standard = Block::make();
41+
42+
$content = <<<EOT
43+
{$standard->placeholder()}
44+
{$standard->placeholder()}
45+
{$standard->placeholder()}
46+
{$standard->placeholder()}
47+
{$standard->placeholder()}
48+
EOT;
49+
50+
$found = Torchlight::findTorchlightIds($content);
51+
52+
$this->assertContains($standard->id(), $found);
53+
$this->assertCount(1, $found);
54+
}
55+
56+
/** @test */
57+
public function its_always_an_array()
58+
{
59+
$this->assertEquals([], Torchlight::findTorchlightIds('not found'));
60+
}
61+
}

tests/MiddlewareAndComponentTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,36 @@ public function code_contents_can_be_a_file()
151151
return $request['blocks'][0]['code'] === rtrim(file_get_contents(config_path('app.php'), '\n'));
152152
});
153153
}
154+
155+
/** @test */
156+
public function two_components_work()
157+
{
158+
$this->withoutExceptionHandling();
159+
$response = [
160+
'blocks' => [[
161+
'id' => 'id1',
162+
'classes' => 'torchlight1',
163+
'styles' => 'background-color: #111111;',
164+
'highlighted' => 'response 1',
165+
], [
166+
'id' => 'id2',
167+
'classes' => 'torchlight2',
168+
'styles' => 'background-color: #222222;',
169+
'highlighted' => 'response 2',
170+
]]
171+
];
172+
173+
Http::fake([
174+
'api.torchlight.dev/*' => Http::response($response, 200),
175+
]);
176+
177+
$response = $this->getView('two-simple-php-hello-world.blade.php');
178+
179+
$expected = <<<EOT
180+
<code class="torchlight1" style="background-color: #111111;">response 1</code>
181+
<code class="torchlight2" style="background-color: #222222;">response 2</code>
182+
EOT;
183+
184+
$this->assertEquals($expected, rtrim($response->content()));
185+
}
154186
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<x-torchlight-code torchlight-id='id1' language='php'>
2+
echo "hello world 1";
3+
</x-torchlight-code>
4+
5+
<x-torchlight-code torchlight-id='id2' language='php'>
6+
echo "hello world 2";
7+
</x-torchlight-code>

0 commit comments

Comments
 (0)