Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
taufik-nurrohman committed Sep 25, 2024
1 parent fca37af commit 660511c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ c_s_s(?string $from): ?string;
~~~ php
<?php

h_t_m_l(?string $from, int $level = 1): ?string;
h_t_m_l(?string $from): ?string;
~~~

### JS
Expand Down
17 changes: 7 additions & 10 deletions index/h-t-m-l.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

namespace x\minify {
function h_t_m_l(?string $from, int $level = 1): ?string {
function h_t_m_l(?string $from): ?string {
if ("" === ($from = \trim($from ?? ""))) {
return null;
}
Expand Down Expand Up @@ -35,7 +35,7 @@ function h_t_m_l(?string $from, int $level = 1): ?string {
}
// <https://en.wikipedia.org/wiki/Conditional_comment>
if ('<![endif]-->' === \substr($chop, -12)) {
$to .= \substr($chop, 0, $n = \strpos($chop, '>') + 1) . h_t_m_l(\substr($chop, $n, -12), $level) . \substr($chop, -12);
$to .= \substr($chop, 0, $n = \strpos($chop, '>') + 1) . h_t_m_l(\substr($chop, $n, -12)) . \substr($chop, -12);
}
if ("" !== $from && ' ' === $from[0]) {
$to = \rtrim($to);
Expand All @@ -53,7 +53,7 @@ function h_t_m_l(?string $from, int $level = 1): ?string {
$from = \substr($from, $b = \strlen($m[0]));
if (false !== \strpos('!?', $m[0][1])) {
$from = \ltrim($from);
$to = \rtrim($to) . h_t_m_l\e($m[0], $level);
$to = \rtrim($to) . h_t_m_l\e($m[0]);
continue;
}
$n = \substr(\strtok($m[0], " \n\t>"), 1);
Expand All @@ -74,7 +74,7 @@ function h_t_m_l(?string $from, int $level = 1): ?string {
}
$value = \call_user_func($f, $value);
}
$to .= h_t_m_l\e($m[0], $level) . $value . '</' . $n . '>';
$to .= h_t_m_l\e($m[0]) . $value . '</' . $n . '>';
continue;
}
// `</asdf>`
Expand Down Expand Up @@ -119,7 +119,7 @@ function h_t_m_l(?string $from, int $level = 1): ?string {
} else {
if (0 === \strpos($from, ' </' . $n . '>')) {
$from = \substr($from, 3 + \strlen($n) + 1);
$to .= h_t_m_l\e($m[0], $level) . ' </' . $n . '>';
$to .= h_t_m_l\e($m[0]) . ' </' . $n . '>';
continue;
}
$from = \ltrim($from);
Expand All @@ -128,7 +128,7 @@ function h_t_m_l(?string $from, int $level = 1): ?string {
}
}
}
$to .= h_t_m_l\e($m[0], $level);
$to .= h_t_m_l\e($m[0]);
continue;
}
$from = \substr($from, 1);
Expand Down Expand Up @@ -213,7 +213,7 @@ function a(string $to): string {
}
return \strtr($to, $of);
}
function e(string $from, int $level): string {
function e(string $from): string {
$to = "";
foreach (\preg_split('/("[^"]*"|\'[^\']*\'|[^!\/<=>?\s]+)/', $from, -1, \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY) as $v) {
if ("" === ($v = \trim($v))) {
Expand All @@ -238,9 +238,6 @@ function e(string $from, int $level): string {
return $s;
}, $v);
}
if (2 === $level) {
// TODO
}
}
$to .= $v;
}
Expand Down
34 changes: 24 additions & 10 deletions index/p-h-p.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ function p_h_p(?string $from): ?string {
$to = "";
foreach ($tokens as $k => $v) {
if (\is_array($v)) {
echo \token_name($v[0]) . '<br/>' . \htmlspecialchars(\json_encode($v[1])) . '<br/><br/>';
if ('_CAST' === \substr(\token_name($v[0]), -5)) {
$to .= '(' . \trim(\substr($v[1], 1, -1)) . ')';
continue;
}
// echo \token_name($v[0]) . '<br/>' . \htmlspecialchars(\json_encode($v[1])) . '<br/><br/>';
if (\T_CLOSE_TAG === $v[0]) {
if ($k === $count - 1) {
$to = \trim($to, ';') . ';';
Expand All @@ -33,15 +37,16 @@ function p_h_p(?string $from): ?string {
$to = \trim($to) . $v[1];
continue;
}
if (\T_DNUMBER === $v[0] || \T_LNUMBER === $v[0]) {
// <https://wiki.php.net/rfc/numeric_literal_separator>
$test = \strtr($v[1], ['_' => ""]);
if (\T_DNUMBER === $v[0]) {
$test = \rtrim(\trim($test, '0'), '.');
} else {
$test = \ltrim($test, '0');
if (\T_DNUMBER === $v[0]) {
$test = \rtrim(\trim(\strtr($v[1], ['_' => ""]), '0'), '.');
if (false === \strpos($test = "" !== $test ? $test : '0', '.')) {
$test .= '.0';
}
if ('(int)' === \substr($to, -5)) {
$to = \substr($to, 0, -5) . \var_export((int) $test, true);
continue;
}
$to .= "" === $test ? '0' : $test;
$to .= $test;
continue;
}
if (\T_ECHO === $v[0] || \T_PRINT === $v[0]) {
Expand All @@ -58,6 +63,15 @@ function p_h_p(?string $from): ?string {
$to .= 'S';
continue;
}
if (\T_LNUMBER === $v[0]) {
$test = \ltrim(\strtr($v[1], ['_' => ""]), '0');
if ('(float)' === \substr($to, -7)) {
$to = \substr($to, 0, -7) . \var_export((float) $test, true);
continue;
}
$to .= "" !== $test ? $test : '0';
continue;
}
if (\T_OPEN_TAG === $v[0]) {
$to .= \trim($v[1]) . ' ';
continue;
Expand Down Expand Up @@ -86,7 +100,7 @@ function p_h_p(?string $from): ?string {
$to .= $v[1];
continue;
}
echo \htmlspecialchars(\json_encode($v)) . '<br/><br/>';
// echo \htmlspecialchars(\json_encode($v)) . '<br/><br/>';
if (false !== \strpos(')]', $v)) {
$to = \trim(\trim($to, ',')) . $v;
continue;
Expand Down
12 changes: 12 additions & 0 deletions test/p-h-p/4.max
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

return [
( asdf ) $asdf,
( float ) $asdf,
( float ) 0,
( float ) 1,
( int ) $asdf,
( int ) 0.0,
( int ) 1.0,
( string ) $asdf,
];

0 comments on commit 660511c

Please sign in to comment.