Skip to content

Commit

Permalink
Less Regex(es)
Browse files Browse the repository at this point in the history
  • Loading branch information
taufik-nurrohman committed Jan 26, 2025
1 parent c5f3fda commit bf7d1f3
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 74 deletions.
2 changes: 1 addition & 1 deletion about.page
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ icon: 'M14,3V5H17.59L7.76,14.83L9.17,16.24L19,6.41V10H21V3M19,19H5V5H12V3H5C3.89
color: '#2b2ee6'
author: Taufik Nurrohman
type: Markdown
version: 2.0.3
version: 2.0.4
...

This extension converts relative links in anchors and images (and some attributes on other elements registered in
Expand Down
125 changes: 52 additions & 73 deletions index.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -14,91 +14,70 @@ function content($content) {
$alter = $state->x->link ?? [];
$alter_content = (array) ($alter->content ?? []);
$alter_data = (array) ($alter->data ?? []);
$z = '\s*(?>\s[\p{L}\p{N}_:-]+(?>=(?>"[^"]*"|\'[^\']*\'|[^\/>]*))?)*\s*';
if ($alter_content) {
foreach ($alter_content as $k => $v) {
if (!$v || false === \strpos($content, '</' . $k . '>')) {
continue;
}
$content = \preg_replace_callback('/(<' . \x($k) . $z . '>)([\s\S]*?)(<\/' . \x($k) . '>)/iu', static function ($m) use ($v) {
$m[2] = \is_callable($v) ? \fire($v, [$m[2], (new \HTML($m[1]))[2] ?? []]) : \x\link\link($m[2]);
return $m[1] . $m[2] . $m[3];
}, $content);
$r = "";
foreach (\apart($content, \array_keys($alter_content)) as $v) {
if (1 !== $v[1] && 2 !== $v[1]) {
$r .= $v[0];
continue;
}
}
if ($alter_data) {
$keep = (static function ($tags) use ($z) {
$out = [];
foreach ($tags as $tag) {
$out[] = '<' . \x($tag) . $z . '>[\s\S]*?<\/' . \x($tag) . '>';
}
return \implode('|', $out);
})(\array_keys($alter_content));
$out = "";
foreach (\preg_split('/(' . $keep . ')/iu', $content, -1, \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY) as $part) {
$n = \strtok(\substr($part, 1, -1), " \n\r\t>");
if ($part && '<' === $part[0] && '>' === \substr($part, -1) && '</' . $n . '>' === \substr($part, -(\strlen($n) + 3))) {
$out .= !empty($alter_data[$n]) ? \preg_replace_callback('/^<[\p{L}\p{N}_:-]+' . $z . '>/iu', static function ($m) use ($alter_data) {
return \x\link\data($m[0], $alter_data);
}, $part) : $part;
} else {
$out .= \x\link\data($part, $alter_data);
$k = '/' !== $v[0][1] ? \strtok(\substr($v[0], 1, -1), " \n\r\t>") : \P;
if (false !== \strpos($t = \substr($v[0], 0, $v[2]), 'on') && \preg_match('/\bon[^=]+=/', $t)) {
$e = new \HTML($v[0]);
foreach ($e[2] as $kk => $vv) {
if ('on' === \substr($kk, 0, 2)) {
$e[$kk] = \x\link\content\script($vv, $e[2]);
}
}
$v[0] = (string) $e;
}
$content = $out;
}
return $content;
}
function data($content, $data) {
if (!$content || false === \strpos($content, '<')) {
return $content;
}
$z = '\s*(?>\s[\p{L}\p{N}_:-]+(?>=(?>"[^"]*"|\'[^\']*\'|[^\/>]*))?)*\s*';
foreach ($data as $k => $v) {
if (!$v || (
false === \strpos($content, '</' . $k . '>') &&
false === \strpos($content, '<' . $k . ' ') &&
false === \strpos($content, '<' . $k . "\n") &&
false === \strpos($content, '<' . $k . "\r") &&
false === \strpos($content, '<' . $k . "\t")
)) {
continue;
}
$v = (array) $v;
$content = \preg_replace_callback('/<' . \x($k) . $z . '\/?>/iu', static function ($m) use ($k, $v) {
if (false === \strpos($m[0], '=')) {
return $m[0];
if (false !== \strpos($v[0], 'style=')) {
$e = new \HTML($v[0]);
if (isset($e['style'])) {
$e['style'] = \x\link\content\style($e['style'], $e[2]);
}
$that = new \HTML($m[0]);
// Need to do the hard way for the `on*` and `style` attribute(s)
foreach ($that[2] as $kk => $vv) {
if (0 !== \strpos($kk, 'on') && 'style' !== $kk) {
continue;
}
$vvv = $that[$kk];
if (\is_callable($vv = $v[$kk] ?? \P)) {
$vvv = \fire($vv, [$vvv, $kk, $k], $that);
} else {
$vvv = \call_user_func(__NAMESPACE__ . "\\content\\" . ('style' === $kk ? $kk : 'script'), $vvv, $that[2]);
$v[0] = (string) $e;
}
if ($a = $alter_content[$k] ?? 0) {
$c = \substr($v[0], $v[2], -(2 + \strlen($k) + 1));
$e = new \HTML(\substr($v[0], 0, $v[2]));
if ($aa = $alter_data[$k] ?? 0) {
foreach ($aa as $kk => $vv) {
if (!$vv || !isset($e[$kk])) {
continue;
}
if (\is_callable($vv)) {
$e[$kk] = \fire($vv, [$e[$kk], $kk, $k], $e);
continue;
}
$e[$kk] = \x\link\link($e[$kk]);
}
$that[$kk] = $vvv;
}
foreach ($v as $kk => $vv) {
if (!$vv || !isset($that[$kk])) {
if (\is_callable($a)) {
$c = \fire($a, [$c, $e[2]], $e);
$r .= $e . $c . '</' . $k . '>';
continue;
}
$r .= \x\link\link($c) . '</' . $k . '>';
continue;
}
if ($a = $alter_data[$k] ?? 0) {
$e = new \HTML($v[0]);
foreach ($a as $kk => $vv) {
if (!$vv || !isset($e[$kk])) {
continue;
}
$vvv = $that[$kk];
if (\is_callable($vv)) {
$vvv = \fire($vv, [$vvv, $kk, $k], $that);
} else {
$vvv = \x\link\link($vvv);
$e[$kk] = \fire($vv, [$e[$kk], $kk, $k], $e);
continue;
}
$that[$kk] = $vvv;
$e[$kk] = \x\link\link($e[$kk]);
}
return (string) $that;
}, $content);
$r .= $e;
continue;
}
$r .= $v[0];
}
return $content;
return $r;
}
function kick($path) {
return \x\link\link($path ?? \lot('url')->current());
Expand Down
4 changes: 4 additions & 0 deletions test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ src="a"
<img src="a" srcset="a 1x, b 2x,c 3x,
d/e 4x, f?g=200 200w">

<link href="a" rel="stylesheet"/>

<script src="a"></script>

<script>
const link = '/foo/bar'; // Can’t decide if this is a relative link :\
const link = 'http://127.0.0.1/foo/bar';
Expand Down

0 comments on commit bf7d1f3

Please sign in to comment.