Skip to content

Commit

Permalink
MD tables
Browse files Browse the repository at this point in the history
  • Loading branch information
energy-coresky committed Feb 5, 2025
1 parent 272db9d commit 4487595
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 88 deletions.
2 changes: 1 addition & 1 deletion assets/dev.css
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ hr {
border-top: 1px solid #ccc;
border-bottom:0;
}
pre, fieldset {
fieldset {/* pre, */
width: 95%;
margin-bottom:10px;
}
Expand Down
2 changes: 1 addition & 1 deletion sky.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class SKY implements PARADISE
const ERR_DETECT = 1;
const ERR_SHOW = 3;
const ERR_SUPPRESSED = 4;
const CORE = '0.576 2025-01-25T14:48:59+02:00 energy';
const CORE = '0.577 2025-02-05T19:41:20+02:00 energy';

public $tracing = '';
public $error_prod = '';
Expand Down
10 changes: 5 additions & 5 deletions w2/_data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ blk_chr: {
'-*_=': leaf_h2r, '#': leaf_h6,
'-*+': blk_ul, '0123456789': blk_ol, '>': blk_bq,
'-:|': leaf_table,
'[': in_square,
'[': square,
'<': leaf_html,
'~`': leaf_fenced,
'': leaf_p,
Expand All @@ -103,10 +103,10 @@ blk_re: [
"/^()([\*\-\+])(\s+|\z)/", # for blk_ul [1]
"/^()(>)(\s*)/", # for blk_bq [2]
]
in_chr: {
'*_': in_ems, '~': in_del, '^': in_sup, '=': in_mark, ':': in_emoji_dl, '|': in_table,
'[': in_square, '`': in_code,
}
#in_chr: {
# '*_': in_ems, '~': in_del, '^': in_sup, '=': in_mark, ':': in_emoji_dl, '|': in_table,
# '[': square, '`': in_code,
#}
email_re: "/^[a-z\d.!#$%&'*+/=?^_`{|}~-]+@[a-z\d](?:[a-z\d-]{0,61}[a-z\d])?(?:\.[a-z\d](?:[a-z\d-]{0,61}[a-z\d])?)*$/i"
tagfilter: @csv title textarea style xmp iframe noembed noframes script plaintext
tags: @csv {
Expand Down
130 changes: 83 additions & 47 deletions w2/md.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ private function leaf_html($x, &$t) {
if ($blank && $x->empty || !$blank && false !== strpos($x->line, $x->grab))
$x->grab = false;
$x->empty or $this->j += strlen($x->line);
return $this->push('#raw', $x->line, ['c' => 'r']);
return $this->push('y-html', $x->line);//, ['c' => 'r']
}

private function leaf_dl($x, &$t) {# :dl-dt-dd
}

Expand All @@ -61,10 +62,9 @@ private function leaf_h2r($x, $y) {
if (!preg_match("/^\\{$y}[\\$y \t]*$/", $x->line))
return false;
$z = str_replace(["\t", ' '], '', $x->line);
$p =& $this->up->name;
$h12 = '=' == $y ? 'h1' : ('-' == $y ? 'h2' : false);
if ($h12 && 'p' == $p && $z == chop($x->line) && !$x->close) {
$p = $h12;
if ($h12 && 'p' == $this->up->name && $z == chop($x->line) && !$x->close) {
$this->up->name = $h12;
$br =& $this->last->left;
if ($br && $br->name == 'br')
$br->name = '#skip';
Expand All @@ -80,38 +80,65 @@ private function leaf_h2r($x, $y) {

private function leaf_table($x, &$y) {
if ($x->table) {
'tbody' == $this->up->name or $this->push('tbody');
$this->push('tr');
$line = trim($x->line);
if ($line && '|' == $line[0])
$line = substr($line, 1);
$ary = preg_split("/\s*\|\s*/", $line && '|' == $line[-1] ? substr($line, 0, -1) : $line);
foreach ($x->table as $n => $align)
$this->push('td', $ary[$n] ?? '', $align);
$this->close('tr');
$tbody = $this->up->name;
if ('tbody' == $tbody) {
$tr =& $this->last->right;
} else {
$tbody = $this->push('tbody');
$tr =& $tbody->val;
}
$this->line($x);
$tr = $this->tr('td', $x->table, $tr);
return $tr->up = $tbody;
} elseif ('p' != $this->up->name || '' !== chop($x->line, "\t -:|")) {
return $this->leaf_p($x, $y);
return $this->leaf_p($x, $y);
} else {
$x->table = array_map(function ($v) {
$align = ':' == $v[0] ? ['align' => 'left'] : [];
return ':' != $v[-1] ? $align : ['align' => $align ? 'center' : 'right'];
$align = ':' == $v[0] ? ' align="left"' : '';
return ':' != $v[-1] ? $align : ($align ? ' align="center"' : ' align="right"');
}, preg_split("/\s*\|\s*/", trim($x->line, "|\t ")));
$line = '';
foreach ($this->gen($this->up->val) as $node)
'#text' != $node->name or $line .= $node->val;
$ary = preg_split("/\s*\|\s*/", trim($line, "|\n\t "));
if (count($ary) != count($x->table))
$first = $this->up->val;
$td = 'yd' == $first->name ? 1 : 0;
if (end($x->td)->right->val == "\n")
$td++;
if (count($x->table) != 1 + count($x->td) - $td)
return $x->table = false;
$this->up->name = 'table';
$this->parent($this->up);
$thead = $this->push('thead', null, ['t' => chop($line)]);
$this->push('#text', "\n");
$this->push('tr', null, ['t' => $x->line]);
foreach ($x->table as $n => $align)
$this->push('th', $ary[$n], $align + ['t' => '']);
$this->last($thead);
$this->parent($this->up, 'table');
$this->push('thead', $this->tr('th', $x->table, $first)); //, null, ['t' => chop($line)]
return $this->add($x->line, ['c' => 'r']);
}
return $this->j += strlen($x->line);
}

private function tr($tag, $ary, $node) {
$val = "<$tag" . array_shift($ary) . '>';
$close = false;
if ('yd' == $node->name) {
$node->val = $val;
$node->attr['t'] = '|';
$tr = XML::node('tr', $node);
} else {
$tr = XML::node('tr', $yd = XML::node('yd', $val, null, $node));
[$node->left, $node] = [$yd, $yd];
}
for ($node->up = $tr, $last = $node; $node = $node->right; $last = $node) {
$node->up = $tr;
$yd = 'yd' == $node->name;
if ($ary && $yd) {
$node->val = "</$tag><$tag" . array_shift($ary) . '>';
$node->attr['t'] = '|';
} elseif ($close) {
$node->name = '#skip';
} elseif ($yd) {
$node->val = "</$tag>";
$node->attr['t'] = '|';
$close = true;
}
}
if (!$close) {
$yd = XML::node('yd', str_repeat("</$tag><$tag>", count($ary)) . "</$tag>");
[$last->right, $yd->left, $yd->up] = [$yd, $last, $tr];
}
return $tr;
}

private function leaf_p($x, &$y) {
Expand Down Expand Up @@ -222,7 +249,8 @@ protected function parse(): ?stdClass {
$this->old_blk($x, $y, $empty);
if ($x->empty && $this->close('p')) {
$this->use_close($x);
} elseif ($x->empty && $this->close('tbody')) {
} elseif ($x->empty && $x->table) {
$this->close('tbody');
$this->close('table');
$x->table = false;
} else {
Expand All @@ -236,26 +264,34 @@ protected function parse(): ?stdClass {
return $this->root->val;
}

private function line($x) {
$in =& $this->in;
$j =& $this->j;
private function line($x, $line = '') {
if ('' === $line) {
$in =& $this->in;
$j =& $this->j;
$x->td = [];
} else {
$in =& $line;
$j = 0;
}
while (!in_array($y = $in[$j] ?? '', ['', "\n"], true)) {
$next = $in[1 + $j] ?? '';
if ("\\" == $y) { # escape
$esc = '' !== $next && strpbrk($next, MD::$MD->esc);
if ($esc) { #$this->push('#esc', $next, ['t' => "\\$next"]); #$j += 2;
if ('' !== $next && strpbrk($next, MD::$MD->esc)) {
$this->add("\\", ['c' => 'r']);
$this->add($next);
} else {
$this->add("\\");// 2do <br>
}
} elseif ('|' == $y) {
$j++;
$x->td[] = $this->push('yd', '|', ['i' => count($x->td)]);
} elseif (strpbrk($y, MD::$MD->esc)) {
$img = '!' == $y;
if (!$img && '[' != $y || !$this->in_square($x, $y, true, $img)) {
$uu = $this->spn($y);
if (!$img && '[' != $y || !$this->square($x, $y, true, $img)) {
$uu = substr($in, $j, strspn($in, $y, $j));
if (strpbrk($y, "*_")) {
$j += $sz = strlen($uu);
$this->inline($uu, $sz, $in[$j - $sz - 1] ?? '', $in[$j] ?? '');
$this->stk_call($uu, $sz, $in[$j - $sz - 1] ?? '', $in[$j] ?? '');
} else {
$this->add($uu);
}
Expand All @@ -267,7 +303,7 @@ private function line($x) {
$this->add($t2 = chop($y));
$this->push('br', 0, ['t' => $rest = substr($y, strlen($t2))]);
$j += strlen($rest);
} elseif (':' == ($in[$j + $n] ?? '') && $this->auto_link($j, $y)) {
} elseif (':' == ($in[$j + $n] ?? '') && $this->auto_link($j, $y, $in)) {
$j += strlen($y);
$this->push('a', $y, ['href' => $y, 'c' => 'g']);
} else {
Expand Down Expand Up @@ -337,7 +373,7 @@ private function new_blk($x, &$y) {
}
}

private function inline($uu, $sz, $left, $right) {
private function stk_call($uu, $sz, $left, $right) {
$u2 = $uu[0] . $uu[0];
$y = ($em = 1 == $sz) ? $uu : $u2;
$close = $left !== '' && !strpbrk($left, " \t\r\n");
Expand All @@ -346,26 +382,26 @@ private function inline($uu, $sz, $left, $right) {
if ($close && $node) {
$node->val = $em ? '<em>' : '<strong>';
$node = false;
$this->push('#raw', $em ? '</em>' : '</strong>', ['t' => $uu]);
$this->push('y', $em ? '</em>' : '</strong>', ['t' => $uu]);
} elseif ($open && !$node) {
$node = $this->push('#raw', $uu, ['t' => $uu]);
$node = $this->push('y', $uu, ['t' => $uu]);
} else {
$this->push('#text', $uu);
}
}

private function auto_link($j, &$y) {
private function auto_link($j, &$y, &$in) {
if (!preg_match("/\bhttps?$/ui", $y, $m1))
return false;
if (!preg_match("/^:\/\/[^\s<]+\b\/*/ui", substr($this->in, $j + strlen($y)), $m2)) //2do substr
if (!preg_match("/^:\/\/[^\s<]+\b\/*/ui", substr($in, $j + strlen($y)), $m2)) //2do substr
return false;
if ($m1[0] === $y)
return $y .= $m2[0];
$y = substr($y, 0, -strlen($m1[0]));
return false;
}

private function in_square($x, $y, $inline = false, $img = false) { # 2do footnote
private function square($x, $y, $inline = false, $img = false) { # 2do footnote
$j = $img ? 1 + $this->j : $this->j;
if (!$len = strlen($head = Rare::bracket($this->in, '[', $j, "\n\\")))
return false;
Expand Down Expand Up @@ -430,7 +466,7 @@ function md_nice($in) {
$out .= pre(tag(html($code), true === $type ? '' : "class=\"language-$type\"", 'code'), '');
}
$type = '';
} elseif ('#raw' == $node->name) {
} elseif ('y' == $node->name[0]) {
$out .= $node->val;
} elseif ('#' == $node->name[0]) {
'' === $type ? ($out .= html($node->val)) : ($code .= $node->val);
Expand Down
7 changes: 4 additions & 3 deletions w2/show.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Show # php jet yaml html bash || php_method md || var diff log
const lay_l = '<table cellpadding="0" cellspacing="0" style="width:100%"><tr><td class="tdlnum code" style="width:10px">';
const lay_m = '</td><td style="padding-left:1px;vertical-align:top">';
const lay_r = '</td></tr></table>';
const style = 'style="tab-size:4; margin:0; color:%s; background-color:%s"';//width:100%%;
const style = 'style="tab-size:4; line-height:14px; margin:0; color:%s; background-color:%s"';//width:100%%;

private static $bg;
private static $clr;
Expand Down Expand Up @@ -270,7 +270,7 @@ static function doc($markdown, $render = 'md_nice', $hightlight = true) {
static function highlight_md($code, &$bg) { # r g d c m j - gray
self::scheme();
$md = new MD($code);
$out = $node = $bg = '';
$out = $node = $bg = $html = '';
$hl = '=';
$attr = function ($n) use (&$node, $md) {
return $md->attr($node, $n);
Expand All @@ -282,7 +282,7 @@ static function highlight_md($code, &$bg) { # r g d c m j - gray
$hl = '*';
} elseif (is_string($node->val)) {
if ($cnt = substr_count($node->val, "\n")) {
$bg .= str_pad('', $cnt, '#skip' == $node->name ? '=' : $hl);
$bg .= str_pad('', $cnt, $html ? '+' : ('#skip' == $node->name ? '=' : $hl));
if (is_null($node->right) && !$attr('last'))
$hl = '=';
}
Expand All @@ -292,6 +292,7 @@ static function highlight_md($code, &$bg) { # r g d c m j - gray
$out .= html($node->val);
}
}
$html = 'y-html' == $node->name && '' !== $node->val;
}
$bg .= $hl;
return $out;
Expand Down
Loading

0 comments on commit 4487595

Please sign in to comment.