Skip to content

Commit

Permalink
complete rewrite of our text parser. walk through line by line instea…
Browse files Browse the repository at this point in the history
…d of globbing patterns.
  • Loading branch information
jszobody committed Jan 6, 2023
1 parent 180c066 commit 632ec00
Show file tree
Hide file tree
Showing 6 changed files with 332 additions and 139 deletions.
10 changes: 6 additions & 4 deletions dist/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class="table-wrapper md:shadow dark:shadow-none md:rounded-md overflow-hidden bg
class="inline-flex items-center gap-2 group hover:text-black dark:hover:text-slate-200 inline-block active:ring-1 active:ring-slate-500 scroll-mt-14 md:scroll-mt-8"
:class="hash == config.key && 'dark:text-slate-200'"
>
<span x-html="highlighted(config.name)"></span>
<span x-html="formatted(config.name)"></span>

<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="hidden group-hover:inline w-3 h-3 opacity-50">
<path stroke-linecap="round" stroke-linejoin="round" d="M13.19 8.688a4.5 4.5 0 011.242 7.244l-4.5 4.5a4.5 4.5 0 01-6.364-6.364l1.757-1.757m13.35-.622l1.757-1.757a4.5 4.5 0 00-6.364-6.364l-4.5 4.5a4.5 4.5 0 001.242 7.244" />
Expand All @@ -122,12 +122,12 @@ class="inline-flex items-center gap-2 group hover:text-black dark:hover:text-sla
<td class="py-2 lg:py-4 px-6 lg:px-4" style="overflow-wrap: anywhere"
:class="config.localValue == null && 'text-slate-400 italic'">
<span x-show="group.headings.length > 0" class="empty:hidden inline-block w-14 text-center lg:hidden py-1 mr-1 text-xs font-semibold rounded bg-green-100 dark:bg-green-900 text-green-700 dark:text-green-200" x-text="group.shortHeadings[1]"></span>
<span x-html="highlighted(config.localValue)"></span>
<span x-html="formatted(config.localValue)"></span>
</td>
<td x-show="config.hasMasterValue" class="py-2 lg:py-4 px-6 lg:px-4" style="overflow-wrap: anywhere"
:class="config.masterValue == null && 'text-slate-400 italic'">
<span x-show="group.headings.length > 0" class="empty:hidden inline-block w-14 text-center lg:hidden py-1 mr-1 text-xs font-semibold rounded bg-blue-100 dark:bg-blue-900 text-blue-700 dark:text-blue-200" x-text="group.shortHeadings[2]"></span>
<span x-html="highlighted(config.masterValue)"></span>
<span x-html="formatted(config.masterValue)"></span>
</td>
</tr>
</template>
Expand Down Expand Up @@ -299,7 +299,9 @@ class="px-4 py-1 rounded block"
document.body.style = "";
this.mobileNav = false;
},
highlighted(text) {
formatted(text) {
if(text) text = text.replaceAll("\n", "<br>");

return this.isUnfiltered() || text == null
? text
: text.replace(new RegExp(this.search,"gi"), "<mark>$&</mark>");
Expand Down
18 changes: 18 additions & 0 deletions src/Collections/Items.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace STS\Phpinfo\Collections;

use Illuminate\Support\Collection;

class Items extends Collection
{
public function localValue()
{
return $this->get(1);
}

public function appendLocalValue($text)
{
$this->put(1, $this->get(1) . $text);
}
}
160 changes: 160 additions & 0 deletions src/Collections/Lines.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php

namespace STS\Phpinfo\Collections;

use Illuminate\Support\Collection;

/**
* Used by our TextParser to help walk through the CLI text version
*/
class Lines extends Collection
{
protected int $index = 0;

/**
* Moves one line forward, regardless of what it is
*/
public function step(): string|null
{
$this->index++;

return $this->current();
}

/**
* Advances to the next usable line and returns the new line
*/
public function advance(): string|null
{
do {
$this->index++;
} while($this->shouldIgnore());

return $this->current();
}

/**
* Similar to the above advance() method, except this returns
* the CURRENT line before advancing
*/
public function consume(): string|null
{
$current = $this->current();

$this->advance();

return $current;
}

public function consumeUntil(callable $callback): Collection
{
$lines = new static;

do {
$current = $this->current();
$lines->push($current);
$this->step();
} while(!$callback($current));

return $lines;
}

public function shouldIgnore(): bool
{
return $this->currentIsBlank()
// || str_contains($this->current(), '_______________________________________________________________________')
|| in_array($this->current(), ['Configuration']);
}

public function currentIsBlank(): bool
{
return $this->current() === '';
}

public function previousIsBlank(): bool
{
return $this->previous() === '';
}

public function nextIsBlank(): bool
{
return $this->next() === '';
}

public function current(): string|null
{
return $this->get($this->index);
}

public function previous(): string|null
{
return $this->get($this->index - 1);
}

public function next(): string|null
{
return $this->get($this->index + 1);
}

public function isDivider(): bool
{
return str_contains($this->current(), '_______________________________________________________________________');
}

public function isModuleName(): bool
{
return !$this->hasItems()
&& $this->nextIsBlank()
&& strlen($this->current()) < 50;
}

public function isGroupTitle(): bool
{
if(str_contains($this->current(), " ")) {
return true;
}

return !$this->hasItems()
&& !$this->nextIsBlank()
&& strlen($this->current()) < 50;
}

public function isTableHeading(): bool
{
return in_array($this->items()->first(), ['Directive', 'Variable', 'Contribution', 'Module']);
}

public function isNote(): bool
{
return !$this->hasItems()
&& !$this->isDivider()
&& !$this->isGroupTitle()
&& strlen($this->current()) > 50;
}

public function items(): Items
{
$items = Items::make(explode(" => ", $this->current()));

// A few weird cases we need to fix
if($items->first() == "Features" && $items->count() == 1) {
$items->put(1, null);
}

return $items;
}

public function hasItems(): bool
{
return $this->items()->count() > 1;
}

public function consumeItems(): Items
{
$items = $this->items();

$this->advance();

return $items;
}
}
5 changes: 5 additions & 0 deletions src/Models/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public static function simple($name, $configName, $contents)
);
}

public static function noteOnly($note)
{
return (new static(collect()))->addNote($note);
}

public function addNote($note): self
{
$this->note = $note;
Expand Down
23 changes: 14 additions & 9 deletions src/Parsers/HtmlParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ public static function canParse(string $contents): bool

protected function parse(): void
{
//dd(new Module('Credits', $this->findGroupedConfigsFor($this->xpath()->query('//body//h1')[2])));

$this->version = str_replace('PHP Version ', '', $this->xpath()->query('//body//h1')[0]->nodeValue);

// For modules, we start by looking at all <h2> tags
Expand All @@ -47,17 +45,24 @@ protected function parse(): void
]))
);

// Credits
$this->modules->push(
new Module('Credits', $this->findGroupedConfigsFor($this->xpath()->query('//body//h1')[2]))
new Module(
$this->xpath()->query('//body//h1')[2]->nodeValue,
$this->findGroupedConfigsFor($this->xpath()->query('//body//h1')[2]))
);

// License
$this->modules->push(
new Module('License', collect([
(new Group(collect()))->addNote(
collect(collect($this->xpath()->query('//body//table//td'))->last()->childNodes)
->map->nodeValue->implode("\n")
)
]))
new Module(
collect($this->xpath()->query('//body//h2'))->last()->nodeValue,
collect([
Group::noteOnly(
collect(collect($this->xpath()->query('//body//table//td'))->last()->childNodes)
->map->nodeValue->implode("\n\n")
)
])
)
);
}

Expand Down
Loading

0 comments on commit 632ec00

Please sign in to comment.