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.
 
 
 
 
 
 

71 lines
1.9 KiB

<?php
namespace App\SearchDisplace\Output;
class HtmlOutput {
protected $data;
protected $content;
protected $indexes = [];
protected $additions = [];
public function __construct($data)
{
$this->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->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($this->content)) {
$html .= '<br>';
$diff = 4;
}
if(isset($element->paragraph)) {
$this->additions[$element->paragraph][] = strlen(utf8_decode($html)) - strlen(utf8_decode($element->content)) - strlen("</$element->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;
}
}