Skip to content

Commit

Permalink
implement skipTags method
Browse files Browse the repository at this point in the history
  • Loading branch information
yggverse committed Apr 5, 2024
1 parent 9ec48ba commit 75685f3
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,25 @@ var_dump(
);
```

#### Body::skipTags

Strip gemini tags from Gemini document

```
var_dump(
$body->skipTags() // strip all tags
);
var_dump(
$body->skipTags(
[ // 1- and 2- level headers only
"##",
"###"
]
)
);
```

### Link

Inline links parser.
Expand Down
85 changes: 85 additions & 0 deletions src/Gemtext/Body.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,89 @@ public function findLinks(string $protocol = 'gemini'): array

return $matches;
}

public function skipTags(array $tags = []): string
{
$lines = [];

foreach ($this->_lines as $line)
{
$line = trim(
$line
);

if ($tags)
{
foreach ($tags as $tag)
{
if(!in_array($tag, ['#', '##', '###', '=>', '*', '```']))
{
continue;
}

switch (true)
{
case str_starts_with($line, '#'):

$line = preg_replace(
sprintf(
'/^%s([^#]+)/ui',
$tag
),
'$1',
$line
);

break;

case str_starts_with($line, '*'):

$line = preg_replace(
'/^\*(.*)/ui',
'$1',
$line
);

break;

default:

$line = preg_replace(
sprintf(
'/^%s(.*)/ui',
$tag
),
'$1',
$line
);
}
}
}

else
{
$line = preg_replace(
[
'/^#([^#]+)/ui',
'/^##([^#]+)/ui',
'/^###([^#]+)/ui',
'/^=>(.*)/ui',
'/^\*(.*)/ui',
'/^```(.*)/ui',
],
'$1',
$line
);
}

$lines[] = trim(
$line
);
}

return implode(
PHP_EOL,
$lines
);
}
}

0 comments on commit 75685f3

Please sign in to comment.