Skip to content

Commit

Permalink
tiny fix
Browse files Browse the repository at this point in the history
  • Loading branch information
energy-coresky committed Feb 27, 2025
1 parent 1dfffc6 commit c087c9b
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 40 deletions.
3 changes: 2 additions & 1 deletion w2/_data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ omis: # tags omission rules

#.md
esc: "/\"'`*_{}[]()<>#+-.!|^=~:$%&;,?@"
code_type: @csv jet php css js html yaml bash # 2do saw
code_type: @csv jet php css js html yaml bash md # 2do saw,xml,json
typ_2: @csv pre script style textarea
blk_chr: {
'-*_=': leaf_h2r, '#': leaf_h6,
Expand Down Expand Up @@ -114,6 +114,7 @@ tags: @csv >
h1 h2 h3 h4 h5 h6 head header hr html iframe legend li link main menu menuitem nav noframes
ol optgroup option p param search section summary table tbody td tfoot th thead title tr track ul
attr: @csv href start src alt align
puncts: "!\"#$%&'()*+,-./:;<=>?[]^_{|}~"
css: |
#md-doc pre, #md-doc code {
background-color: #eee;
Expand Down
4 changes: 2 additions & 2 deletions w2/dev.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ function j_attach() {
}
$cls = [];
if ('prod' == $type && -2 == $mode) {
$doc = is_file($fn = "$dir/README.md") ? Show::md(file_get_contents($fn)) : '';
$doc = is_file($fn = "$dir/README.md") ? Show::doc(file_get_contents($fn)) : '';
if (is_file($fn = "$dir/LICENSE"))
$doc .= Show::bash(file_get_contents($fn));
return [
Expand Down Expand Up @@ -412,7 +412,7 @@ function j_new() {

function j_readme() {
Plan::$pngdir = $dir = $_POST['dir'];
$html = is_file($fn = "$dir/README.md") ? Show::md(file_get_contents($fn)) : '';
$html = is_file($fn = "$dir/README.md") ? Show::doc(file_get_contents($fn)) : '';
if (is_file($fn = "$dir/LICENSE"))
$html .= Show::bash(file_get_contents($fn));
return ['html' => $html, 'dir' => $dir];
Expand Down
35 changes: 19 additions & 16 deletions w2/md.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class MD extends XML # the MarkDown, follow Common Mark https://spec.commonmark.org/
{ # and rich MarkDown from https://github.com/xoofx/markdig
const version = '0.599';
const version = '0.701';

static $MD;

Expand All @@ -12,7 +12,6 @@ class MD extends XML # the MarkDown, follow Common Mark https://spec.commonmark.
private $for = [];

function __construct(string $in = '', $tab = 2) {
set_time_limit(3);
parent::__construct($in, $tab);
MD::$MD or MD::$MD = Plan::set('main', fn() => yml('md', '+ @object @inc(md)', false, true));
}
Expand All @@ -30,6 +29,7 @@ private function leaf_html($x) {
} elseif ('!' == $m[1]) {
if ('<!--' == substr($x->line, 0, 4)) {
$x->grab = '-->';
$this->use_close($x);
} elseif ('<![CDATA[' == substr($x->line, 0, 9)) {
$x->grab = ']]>';
} else {
Expand All @@ -50,8 +50,6 @@ private function leaf_html($x) {
return false;
}
}
//$this->close('p');
//$this->use_close($x);
}
$blank = '<' == $x->grab;
if ($blank && $x->empty || !$blank && false !== strpos($x->line, $x->grab))
Expand Down Expand Up @@ -353,10 +351,9 @@ private function line($x) {
$img = '!' == $y;
if (!$img && '[' != $y || !$this->square($x, $y, true, $img)) {
if (strpbrk($y, "*_`~^=")) { # inlines
$b = isset($in[$j - 1]) && !strpbrk($in[$j - 1], " \t\r\n") ? 1 : 0; # end or not
$left = $in[$j - 1] ?? '';
$j += strlen($uu = substr($in, $j, strspn($in, $y, $j)));
$b += isset($in[$j]) && !strpbrk($in[$j], " \t\r\n") ? 2 : 0; # start or not
if (3 != $b || '*' == $y || '`' == $y) {
if ($b = $this->boundary($left, $y, $in[$j] ?? '')) {
$this->push('-i', $uu, ['b' => $b]);
$this->up->attr['in'] = $uu;
} else {
Expand Down Expand Up @@ -384,6 +381,19 @@ private function line($x) {
return "\n" == $y;
}

private function boundary($left, $y, $right) {
$as = '*' == $y || '`' == $y;
$spl = '' === $left || strpbrk($left, "\t \r\n");
$spr = '' === $right || strpbrk($right, "\t \r\n");
if (!$spl && !$spr && !$as) {
if (!$spl = strpbrk($left, MD::$MD->puncts))
$spr = strpbrk($right, MD::$MD->puncts);
}
$b = $spl ? 0 : 1; # end or not
$b += $spr ? 0 : 2; # start or not
return 3 != $b || $as ? $b : false;
}

private function auto_link($j, &$y, &$in) {
if (!preg_match("/\bhttps?$/ui", $y, $m1))
return false;
Expand Down Expand Up @@ -530,8 +540,7 @@ function inlines($p, $drop = false) {

private function use_close($x) {
$this->close('p');
$this->close('tbody');
if ($this->close('table'))
if ($this->close('table', true))
$x->table = $x->cvl = false;
$x->close && $this->last('li' == $x->close->name ? $x->close->up : $x->close);
$x->pad < 4 && $this->close('x-code');
Expand Down Expand Up @@ -599,10 +608,6 @@ private function set_x($n, $x, &$y = null, &$last = null) {
return $ary;
}

static function html($str) {
return str_replace(['<', '>'], ['&lt;', '&gt;'], $str);
}

private function spn($set, $j = 0, &$sz = null) { # collect $set
return substr($this->in, $j ?: $this->j, $sz = strspn($this->in, $set, $j ?: $this->j));
}
Expand All @@ -611,6 +616,4 @@ private function cspn($set, $j = 0, &$sz = null) { # collect other than $set
return substr($this->in, $j ?: $this->j, $sz = strcspn($this->in, $set, $j ?: $this->j));
}
}
/*
sprintf('<img src="%s" alt="%s">', $p4, substr($p3, 1, -1)) # image
trace($x->close, '===');*/
/*trace($x->close, '===');*/
2 changes: 1 addition & 1 deletion w2/root.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ static function _overview($i) {
case 'Readme':
//$fn = $menu[$i] == 'Readme';
$file = file_get_contents(DIR_S . ($fn ?? '/README.md'));
echo Show::md($file);
echo Show::doc($file);
break;
}
return $top;
Expand Down
26 changes: 7 additions & 19 deletions w2/show.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ static function bash($text) {
}, $text), 'style="background:#e0e7ff;"');
}

static function md($text) {
static function __2del($text) {
self::scheme();
$code = function ($text, $re) {
return preg_replace_callback("@$re@s", function ($m) {
Expand All @@ -245,19 +245,6 @@ static function md($text) {
return 'jet' == $m[1] ? self::jet(unhtml($m[2]), '-', true) : pre(html($m[2]), '');
}, $text);
};
if (!$exist = Plan::has('Parsedown')) {
if (is_dir('vendor/erusev/parsedown')) {
Plan::vendor();
$exist = true;
}
}
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|js|bash|yaml)">(.*?)</code></pre>');
}
$text = str_replace("\n\n", '<p>', unl($text));
return $code($text, "```(jet|php|html|css|js|bash|yaml|)(.*?)```");
}

static function doc($markdown, $render = 'md_nice', $hightlight = true) {
Expand All @@ -267,12 +254,12 @@ static function doc($markdown, $render = 'md_nice', $hightlight = true) {
return (string)$md;
}

static function highlight_md($code, &$bg) { # r g d c m j - gray
self::scheme();
static function md($code, $no_lines = false) { # r g d c m j - gray
self::scheme('z_php');
self::$bg['*'] = '#ffa';
$md = new MD($code);
$out = $node = $bg = $html = '';
$hl = '=';
self::$bg['*'] = '#ffa';
$attr = function ($n) use (&$node, $md) {
return $md->attr($node, $n);
};
Expand All @@ -297,8 +284,9 @@ static function highlight_md($code, &$bg) { # r g d c m j - gray
$html = '-html' == $node->name && '' !== $node->val;
$md->inlines($node);
}
$bg .= $hl;
return $out;
$x = self::xdata($bg .= $hl);
$x->len = strlen($bg);
return self::table(explode("\n", $out), $x, $no_lines);
}

static function css($code, $option = '', $no_lines = false) {
Expand Down
2 changes: 1 addition & 1 deletion w2/vendor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function c_md() {
$ary = get($fn);
$fn = $ary['download_url'];
}
echo tag(Show::md(file_get_contents($fn)), 'style="padding-left:10px"');
echo tag(Show::doc(file_get_contents($fn)), 'style="padding-left:10px"');
return true;
}

Expand Down

0 comments on commit c087c9b

Please sign in to comment.