Skip to content

Commit

Permalink
tiny fix
Browse files Browse the repository at this point in the history
  • Loading branch information
energy-coresky committed Dec 18, 2024
1 parent 57fbcb8 commit daef113
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
6 changes: 3 additions & 3 deletions w2/_data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ omis: # tags omission rules

#.js
keywords: @csv( )
break case class catch const continue debugger default delete do else export extends finally for function if import in
break case class catch const continue debugger default delete do else export extends finally for function if import in of
instanceof let new return super switch this throw try typeof var void while with yield
other: @csv( )
enum await implements package protected static interface private public
old: @csv( )
abstract boolean byte char double final float goto int long native short synchronized transient volatile
chars: "!#%&()*+,-./:;<=>?@[\]^`{|}~"
#.js
chars: "!#%&()*+,-.:;<=>?@[\]^`{|}~"
#.js /

#.define ================== this used in the sky_plan.php ========================
ENC: UTF-8
Expand Down
20 changes: 11 additions & 9 deletions w2/display.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ static function highlight_js($code, &$y, $u = '') { # r g d c m j - gray
$js = new JS($code);
$out = '';
foreach ($js->tokens($y) as $t => $y) {
if (T_COMMENT == $y->tok) { /* js comment */
$out .= self::span($u . 'c', $t);
} elseif (T_KEYWORD == $y->tok) {
$out .= self::span($u . ('@' == $t[0] ? 'g' : 'g'), $t);
} elseif ('"' == $t[0] || "'" == $t[0]) {
if (T_COMMENT == $y->tok) {
$out .= self::span($u . 'c', html($t));
} elseif (T_CONSTANT_ENCAPSED_STRING == $y->tok) {
$out .= self::span($u . 'r', $t);
} elseif (T_STRING == $y->tok) {
$out .= self::span($u . 'j', $t);
} elseif ($y->tok) {
$out .= self::span($u . ('@' == $t[0] ? 'g' : 'g'), $t);
} else {
#$out .= self::span($u . ('k' == $y->mode ? 'r' : 'd'), $t);
$out .= $t;
$out .= html($t);
}
}
return $out;
Expand Down Expand Up @@ -211,7 +211,7 @@ static function diff(string $new, string $old, &$mode = null, int $boundary = 0)
$new = explode("\n", unl($new));
$old = explode("\n", unl($old));
$diff = $eq = [];
for ($rN = '', $n = $l = $z = 0; $new && $old; $rN .= $chr) {
for ($rN = '', $n = $l = $z = 0; $new && $old; $rN .= $chr) { # 2do: LCS algo..
if (($sn = $new[0]) === $old[0]) {
$ary = [$chr = '=', ++$n, $sn, ++$l, $sn];
$z++ ? ($diff[] = $ary) : ($eq[] = $ary);
Expand Down Expand Up @@ -262,6 +262,8 @@ static function md($text) {
return self::php($php = unhtml($m[2]), '<?' == substr($php, 0, 2) ? '' : false, true);
if ('css' == $m[1])
return self::css(unhtml($m[2]), '', true);
if ('js' == $m[1])
return self::js(unhtml($m[2]), '', true);
if ('html' == $m[1])
return self::html(unhtml($m[2]), '', true);
return 'jet' == $m[1] ? self::jet(unhtml($m[2]), '-', true) : pre(html($m[2]), '');
Expand All @@ -276,10 +278,10 @@ static function md($text) {
if ($exist) {
$md = new Parsedown;
$text = preg_replace("~\"https://github.*?/([^/\.]+)[^/\"]+\"~", '"_png?$1=' . Plan::$pngdir . '"', $md->text($text));
return $code($text, '<pre><code class="language\-(jet|php|html|css|bash|yaml)">(.*?)</code></pre>');
return $code($text, '<pre><code class="language\-(jet|php|html|css|js|bash|yaml)">(.*?)</code></pre>');
}
$text = str_replace("\n\n", '<p>', unl($text));
return $code($text, "```(jet|php|html|css|bash|yaml|)(.*?)```");
return $code($text, "```(jet|php|html|css|js|bash|yaml|)(.*?)```");
}

static function css($code, $option = '', $no_lines = false) {
Expand Down
33 changes: 22 additions & 11 deletions w2/js.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class JS

function __construct(string $in = '') {
defined('T_KEYWORD') or define('T_KEYWORD', 10001);
self::$js or self::$js = yml('+ @object @inc(js)');
self::$js or self::$js = Plan::set('main', fn() => yml('js', '+ @object @inc(js)'));
$this->in = unl($in);
}

Expand Down Expand Up @@ -55,18 +55,19 @@ function mode(&$in, $k, $len, &$mode, $chr, $real = false) {
}

function tokens($y = false) {
$y or $y = (object)['mode' => 'd', 'find' => false];
$y or $y = (object)['tok' => 0, 'pv' => '', 'find' => false];
$len = strlen($in =& $this->in);
for ($j = 0; $j < $len; $j += strlen($t)) {
for ($j = 0, $t = ''; $j < $len; $j += strlen($t)) {
T_WHITESPACE == $y->tok or $y->pv = $t;
$t2 = $in[$j + 1] ?? '';
if ($y->found = $y->find) {
if (false === ($pos = strpos($in, $y->find, $j))) {
$t = substr($in, $j); # /* </style> */ is NOT comment inside <style>!
} else {
$t = substr($in, $j, $pos - $j + 2);
$t = substr($in, $j, $pos - $j + strlen($y->find));
$y->find = false;
}
$y->tok = T_COMMENT;
} elseif ('/' == $in[$j] && in_array($t2 = $in[$j + 1], ['*', '/'])) {
} elseif ('/' == $in[$j] && in_array($t2, ['*', '/'])) {
$t = '/' . $t2; # comment
$y->find = $t2 == "*" ? '*/' : "\n";
$y->tok = T_COMMENT;
Expand All @@ -75,11 +76,21 @@ function tokens($y = false) {
$y->tok = T_WHITESPACE;
} elseif (strpbrk($t = $in[$j], self::$js->chars)) {
$y->tok = 0;
} elseif ('"' == $t || "'" == $t) {
$sz = Rare::str($in, $j, $len) or $this->halt('Incorrect string');
$t = substr($in, $j, $sz - $j);
} elseif ($y->word = preg_match("/^[a-z\n_\$]+/i", substr($in, $j), $m)) {
$y->tok = in_array($t = $m[0], self::$js->keywords) ? T_KEYWORD : T_STRING;
} elseif ('/' == $t && "(" == $y->pv || '"' == $t || "'" == $t) {
$y->tok = T_CONSTANT_ENCAPSED_STRING;
if ($sz = Rare::str($in, $j, $len)) {// or $this->halt('Incorrect string');
$t = substr($in, $j, $sz - $j);
} else {
[$y->find, $t] = [$t, substr($in, $j)];
}
} elseif ($y->word = preg_match("/^[a-z\d_\$]+/i", substr($in, $j), $m)) {
if (is_num($t = $m[0])) {
$y->tok = T_LNUMBER;
} else {
$kw = '.' != $y->pv && in_array($t, self::$js->keywords);
$php = defined($c = "T_" . strtoupper($t));
$y->tok = $kw ? ($php ? constant($c) : T_KEYWORD) : T_STRING;
}
}
yield $t => $y;
}
Expand Down

0 comments on commit daef113

Please sign in to comment.