Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

## Unreleased

### Added
- `Torchlight::findTorchlightIds` method to regex a string and return all Torchlight placeholders.
- `BladeManager::getBlocks`
- `BladeManager::clearBlocks`

### Changed
- Added square brackets around the Torchlight ID in the Block placeholder.
- The BladeManager no longer clears the blocks while rendering. Needed for Jigsaw.

## 0.3.0 - 2021-05-22

- Add `Torchlight` facade
Expand Down
40 changes: 26 additions & 14 deletions src/Blade/BladeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,27 @@
namespace Torchlight\Blade;

use Illuminate\Http\Response;
use Illuminate\Support\Arr;
use Torchlight\Block;
use Torchlight\Client;
use Torchlight\Torchlight;

class BladeManager
{
protected static $blocks = [];

public static function registerBlock(Block $block)
{
static::$blocks[] = $block;
static::$blocks[$block->id()] = $block;
}

public static function getBlocks()
{
return static::$blocks;
}

public static function clearBlocks()
{
static::$blocks = [];
}

public static function renderResponse(Response $response)
Expand All @@ -37,23 +48,24 @@ public static function renderContent($content)
return $content;
}

$blocks = (new Client)->highlight(static::$blocks);
Torchlight::highlight(static::$blocks);

static::$blocks = [];
$ids = Torchlight::findTorchlightIds($content);

foreach ($blocks as $block) {
$swap = [
$block->placeholder() => $block->highlighted,
$block->placeholder('classes') => $block->classes,
$block->placeholder('styles') => $block->styles,
];
$swap = [];

foreach ($swap as $search => $replace) {
// Substitute all the placeholders that we left with the highlighted html.
$content = str_replace($search, $replace, $content);
foreach ($ids as $id) {
/** @var Block $block */
if (!$block = Arr::get(static::$blocks, $id)) {
continue;
}

// Swap out all the placeholders that we left.
$swap[$block->placeholder()] = $block->highlighted;
$swap[$block->placeholder('classes')] = $block->classes;
$swap[$block->placeholder('styles')] = $block->styles;
}

return $content;
return str_replace(array_keys($swap), array_values($swap), $content);
}
}
2 changes: 1 addition & 1 deletion src/Blade/CodeComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function capture($contents)

if (is_file($contents)) {
$contents = file_get_contents($contents);
} elseif (is_file(resource_path($contents))) {
} elseif (is_callable('resource_path') && is_file(resource_path($contents))) {
$contents = file_get_contents(resource_path($contents));
}

Expand Down
2 changes: 1 addition & 1 deletion src/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function placeholder($extra = '')
$extra = "_$extra";
}

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

/**
Expand Down
14 changes: 14 additions & 0 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use Illuminate\Contracts\Cache\Repository;
use Illuminate\Contracts\Container\Container;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
Expand Down Expand Up @@ -112,4 +113,17 @@ public function cache()
// If the config value is null, the default cache will be used.
return Cache::store($this->config('cache'));
}

/**
* Return all the Torchlight IDs in a given string.
*
* @param string $content
* @return array
*/
public function findTorchlightIds($content)
{
preg_match_all('/__torchlight-block-\[(.+?)\]/', $content, $matches);

return array_values(array_unique(Arr::get($matches, 1, [])));
}
}
61 changes: 61 additions & 0 deletions tests/FindIdsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* @author Aaron Francis <aaron@hammerstone.dev|https://twitter.com/aarondfrancis>
*/

namespace Torchlight\Tests;

use Torchlight\Block;
use Torchlight\Torchlight;

class FindIdsTest extends BaseTest
{
/** @test */
public function it_will_find_all_the_ids()
{
$standard = Block::make();
$custom1 = Block::make('custom-id');
$custom2 = Block::make('custom-1234');

$content = <<<EOT
{$standard->placeholder()}
{$standard->placeholder('styles')}

{$custom1->placeholder()}
{$custom1->placeholder('styles')}

<code style="{$custom2->placeholder('styles')}">{$custom2->placeholder()}</code>
EOT;

$found = Torchlight::findTorchlightIds($content);

$this->assertContains($standard->id(), $found);
$this->assertContains('custom-id', $found);
$this->assertContains('custom-1234', $found);
}

/** @test */
public function it_only_returns_one_per()
{
$standard = Block::make();

$content = <<<EOT
{$standard->placeholder()}
{$standard->placeholder()}
{$standard->placeholder()}
{$standard->placeholder()}
{$standard->placeholder()}
EOT;

$found = Torchlight::findTorchlightIds($content);

$this->assertContains($standard->id(), $found);
$this->assertCount(1, $found);
}

/** @test */
public function its_always_an_array()
{
$this->assertEquals([], Torchlight::findTorchlightIds('not found'));
}
}
32 changes: 32 additions & 0 deletions tests/MiddlewareAndComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,36 @@ public function code_contents_can_be_a_file()
return $request['blocks'][0]['code'] === rtrim(file_get_contents(config_path('app.php'), '\n'));
});
}

/** @test */
public function two_components_work()
{
$this->withoutExceptionHandling();
$response = [
'blocks' => [[
'id' => 'id1',
'classes' => 'torchlight1',
'styles' => 'background-color: #111111;',
'highlighted' => 'response 1',
], [
'id' => 'id2',
'classes' => 'torchlight2',
'styles' => 'background-color: #222222;',
'highlighted' => 'response 2',
]]
];

Http::fake([
'api.torchlight.dev/*' => Http::response($response, 200),
]);

$response = $this->getView('two-simple-php-hello-world.blade.php');

$expected = <<<EOT
<code class="torchlight1" style="background-color: #111111;">response 1</code>
<code class="torchlight2" style="background-color: #222222;">response 2</code>
EOT;

$this->assertEquals($expected, rtrim($response->content()));
}
}
7 changes: 7 additions & 0 deletions tests/Support/two-simple-php-hello-world.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<x-torchlight-code torchlight-id='id1' language='php'>
echo "hello world 1";
</x-torchlight-code>

<x-torchlight-code torchlight-id='id2' language='php'>
echo "hello world 2";
</x-torchlight-code>