Repo for the search and displace core module including the interface to select files and search and displace operations to run on them.
https://searchanddisplace.com
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
98 lines
2.4 KiB
98 lines
2.4 KiB
<?php
|
|
|
|
namespace App\SearchDisplace;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class SearchAndDisplaceJSON
|
|
{
|
|
protected $file;
|
|
protected $searchers;
|
|
|
|
public function __construct($file, $searchers)
|
|
{
|
|
$this->file = $file . '/document.json';
|
|
$this->searchers = $searchers;
|
|
|
|
$this->storage = Storage::disk('local');
|
|
}
|
|
|
|
public function execute()
|
|
{
|
|
if(! $this->storage->exists("contracts/$this->file")) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$content = $this->getContent();
|
|
} catch (\Exception $exception) {
|
|
\Illuminate\Support\Facades\Log::info('EXCEPTION: ' . $exception->getMessage());
|
|
|
|
return;
|
|
}
|
|
|
|
$sd = $this->applySD($content);
|
|
|
|
return [
|
|
'content' => $this->convertToHTML($sd['content']),
|
|
'indexes' => $sd['indexes']
|
|
];
|
|
}
|
|
|
|
protected function getContent()
|
|
{
|
|
return json_decode($this->storage->get("contracts/$this->file"));
|
|
}
|
|
|
|
protected function applySD($elements)
|
|
{
|
|
$indexes = [];
|
|
|
|
foreach($elements as $element) {
|
|
if($element->tag === 'img')
|
|
continue;
|
|
|
|
$search = new SearchAndDisplace(
|
|
stripslashes($element->content),
|
|
[
|
|
'searchers' => $this->searchers,
|
|
],
|
|
false,
|
|
true
|
|
);
|
|
|
|
$changed = $search->execute();
|
|
|
|
if($changed) {
|
|
$indexes = $changed['indexes'];
|
|
$element->content = $changed['content'];
|
|
}
|
|
}
|
|
|
|
return [
|
|
'content' => $elements,
|
|
'indexes' => $indexes
|
|
];
|
|
}
|
|
|
|
protected function convertToHTML($elements)
|
|
{
|
|
$html = '';
|
|
$url = url('/') . '/contracts-images';
|
|
|
|
foreach($elements as $key => $element) {
|
|
if($element->tag !== 'img') {
|
|
$html .= "<$element->tag style=\"$element->style\">$element->content</$element->tag>";
|
|
} else {
|
|
$src = $url . '/' . str_replace(' ', '%20', $element->src);
|
|
$html .= "<img $element->style src=\"$src\" alt=\"$element->details\">";
|
|
}
|
|
|
|
if($key !== array_key_last($elements)) {
|
|
$html .= '<br>';
|
|
}
|
|
}
|
|
|
|
return $html;
|
|
}
|
|
}
|