diff --git a/w2/_data.yaml b/w2/_data.yaml index 48ecdc1..85d0a2d 100644 --- a/w2/_data.yaml +++ b/w2/_data.yaml @@ -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, @@ -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; diff --git a/w2/dev.php b/w2/dev.php index d46a1d5..b572e21 100644 --- a/w2/dev.php +++ b/w2/dev.php @@ -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 [ @@ -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]; diff --git a/w2/md.php b/w2/md.php index 79cf5fc..3e247c5 100644 --- a/w2/md.php +++ b/w2/md.php @@ -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; @@ -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)); } @@ -30,6 +29,7 @@ private function leaf_html($x) { } elseif ('!' == $m[1]) { if (''; + $this->use_close($x); } elseif ('line, 0, 9)) { $x->grab = ']]>'; } else { @@ -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)) @@ -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 { @@ -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; @@ -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'); @@ -599,10 +608,6 @@ private function set_x($n, $x, &$y = null, &$last = null) { return $ary; } - static function html($str) { - return str_replace(['<', '>'], ['<', '>'], $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)); } @@ -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('%s', $p4, substr($p3, 1, -1)) # image -trace($x->close, '===');*/ \ No newline at end of file +/*trace($x->close, '===');*/ \ No newline at end of file diff --git a/w2/root.php b/w2/root.php index 9008c8d..0b1196e 100644 --- a/w2/root.php +++ b/w2/root.php @@ -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; diff --git a/w2/show.php b/w2/show.php index 3ae5fd9..0ab5a09 100644 --- a/w2/show.php +++ b/w2/show.php @@ -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) { @@ -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, '
(.*?)
'); - } - $text = str_replace("\n\n", '

', unl($text)); - return $code($text, "```(jet|php|html|css|js|bash|yaml|)(.*?)```"); } static function doc($markdown, $render = 'md_nice', $hightlight = true) { @@ -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); }; @@ -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) { diff --git a/w2/vendor.php b/w2/vendor.php index 0e09a87..a18c828 100644 --- a/w2/vendor.php +++ b/w2/vendor.php @@ -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; }