From 2b142a78363b31309bfd874c898ba21881680bd3 Mon Sep 17 00:00:00 2001 From: Alex Puiu Date: Thu, 3 Mar 2022 18:06:38 +0200 Subject: [PATCH] Update indexes after HTML conversion. --- app/SearchDisplace/Output/HtmlOutput.php | 71 ++++++++++++++++++++ app/SearchDisplace/SearchAndDisplaceJSON.php | 40 ++++------- 2 files changed, 84 insertions(+), 27 deletions(-) create mode 100644 app/SearchDisplace/Output/HtmlOutput.php diff --git a/app/SearchDisplace/Output/HtmlOutput.php b/app/SearchDisplace/Output/HtmlOutput.php new file mode 100644 index 0000000..6e08c04 --- /dev/null +++ b/app/SearchDisplace/Output/HtmlOutput.php @@ -0,0 +1,71 @@ +content = $data['content']; + $this->indexes = $data['indexes']; + } + + public function getData() + { + $html = $this->convertToHTML(); + + $indexes = $this->updateIndexes(); + + return [ + 'content' => $html, + 'indexes' => $indexes + ]; + } + + protected function convertToHTML() + { + $html = ''; + $url = url('/') . '/contracts-images'; + + foreach($this->content as $key => $element) { + $diff = 0; + if($element->tag !== 'img') { + $html .= "<$element->tag style=\"$element->style\">$element->contenttag>"; + } else { + $src = $url . '/' . str_replace(' ', '%20', $element->src); + $html .= "style src=\"$src\" alt=\"$element->details\">"; + } + + if($key !== array_key_last($this->content)) { + $html .= '
'; + $diff = 4; + } + + if(isset($element->paragraph)) { + $this->additions[$element->paragraph][] = strlen($html) - strlen($element->content) - strlen("tag>") - $diff; + } + } + + return $html; + } + + protected function updateIndexes() + { + $indexes = $this->indexes; + + foreach($this->additions as $akey => $addition) { + foreach($addition as $ckey => $change) { + $indexes[$akey][$ckey]['start'] += $change; + $indexes[$akey][$ckey]['end'] += $change; + $indexes[$akey][$ckey]['original_start'] += $change; + $indexes[$akey][$ckey]['original_end'] += $change; + } + } + + return $indexes; + } +} \ No newline at end of file diff --git a/app/SearchDisplace/SearchAndDisplaceJSON.php b/app/SearchDisplace/SearchAndDisplaceJSON.php index 8f7469e..444ce4c 100644 --- a/app/SearchDisplace/SearchAndDisplaceJSON.php +++ b/app/SearchDisplace/SearchAndDisplaceJSON.php @@ -3,6 +3,7 @@ namespace App\SearchDisplace; use Illuminate\Support\Facades\Storage; +use App\SearchDisplace\Output\HtmlOutput; class SearchAndDisplaceJSON { @@ -31,12 +32,11 @@ class SearchAndDisplaceJSON return; } - $sd = $this->applySD($content); + $searchDisplace = $this->applySD($content); - return [ - 'content' => $this->convertToHTML($sd['content']), - 'indexes' => $sd['indexes'] - ]; + $convert = new HtmlOutput($searchDisplace); + + return $convert->getData(); } protected function getContent() @@ -64,7 +64,14 @@ class SearchAndDisplaceJSON $changed = $search->execute(); if($changed) { - $indexes = $changed['indexes']; + foreach($changed['indexes'] as $key => $searcher) { + foreach($searcher as $change) { + if($change['start']) { + $indexes[$key][] = $change; + $element->paragraph = $key; + } + } + } $element->content = $changed['content']; } } @@ -74,25 +81,4 @@ class SearchAndDisplaceJSON '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->contenttag>"; - } else { - $src = $url . '/' . str_replace(' ', '%20', $element->src); - $html .= "style src=\"$src\" alt=\"$element->details\">"; - } - - if($key !== array_key_last($elements)) { - $html .= '
'; - } - } - - return $html; - } } \ No newline at end of file