This commit is contained in:
Zander Thannhauser 2025-12-07 12:44:46 -06:00
parent c67cccc31f
commit 4d6169ff59
3 changed files with 148 additions and 132 deletions

View file

@ -1352,27 +1352,27 @@ class MarkdownExtra extends \Michelf\Markdown {
$text .= "<tr>\n";
foreach ($headers as $n => $header)
{
if (preg_match('/\[([a-zA-Z]+)\]/', $header, $matches))
if (preg_match('/\[@([a-zA-Z]+)\]/', $header, $matches))
{
$label = $matches[1];
if ( !in_array($label, $this->tom_cells_stack)
&& array_key_exists($label, $this->tom_cells))
{
array_push($this->tom_cells_stack, $label);
array_push($this->tom_cells_stack, $label);
$local_content = $this->tom_cells[$label];
$local_content = $this->tom_cells[$label];
if ($this->tom_cells_type[$label] == "span")
{
$processed = $this->runSpanGamut($local_content);
}
else
{
$processed = $this->runBasicBlockGamut($local_content);
}
if ($this->tom_cells_type[$label] == "span")
{
$processed = $this->runSpanGamut($local_content);
}
else
{
$processed = $this->runBasicBlockGamut($local_content);
}
array_pop($this->tom_cells_stack);
array_pop($this->tom_cells_stack);
$text .= " <th$attr[$n]>" . $processed . "</th>\n";
}
@ -1830,17 +1830,17 @@ class MarkdownExtra extends \Michelf\Markdown {
$less_than_tab = $this->tab_width - 1;
$text = preg_replace_callback('{
^[ ]{0,' . $less_than_tab . '}\[(.+?)\][ ]?: [ ]* \n
^[ ]{0,' . $less_than_tab . '}\[@(.+?)\][ ]?: [ ]* \n
(
(?>
(?!^\[\1\])
(?!^\[@\1\])
.*\n+
)+
)
# Closing marker.
\[\1\][ ]* (?= \n )
\[@\1\][ ]* (?= \n )
}xm',
array($this, '_stripTomCells_block_callback'),
$text);
@ -1858,14 +1858,14 @@ class MarkdownExtra extends \Michelf\Markdown {
protected function _stripTomCells_span_callback($matches)
{
$this->enter(__FUNCTION__);
$this->enter(__FUNCTION__);
$this->zprint("matches[1] = {$matches[1]}");
$this->zprint("matches[2] = {$matches[2]}");
$this->zprint("matches[1] = {$matches[1]}");
$this->zprint("matches[2] = {$matches[2]}");
$this->tom_cells[$matches[1]] = trim($matches[2]);
$this->tom_cells[$matches[1]] = trim($matches[2]);
$this->tom_cells_type[$matches[1]] = "span";
$this->tom_cells_type[$matches[1]] = "span";
$this->exit(__FUNCTION__);
@ -1874,16 +1874,16 @@ class MarkdownExtra extends \Michelf\Markdown {
protected function _stripTomCells_block_callback($matches)
{
$this->enter(__FUNCTION__);
$this->enter(__FUNCTION__);
$this->zprint("matches[1] = {$matches[1]}");
$this->zprint("matches[2] = {$matches[2]}");
$this->zprint("matches[1] = {$matches[1]}");
$this->zprint("matches[2] = {$matches[2]}");
$this->tom_cells[$matches[1]] = $this->outdent($matches[2]);
$this->tom_cells[$matches[1]] = $this->outdent($matches[2]);
$this->tom_cells_type[$matches[1]] = "block";
$this->tom_cells_type[$matches[1]] = "block";
$this->exit(__FUNCTION__);
$this->exit(__FUNCTION__);
return '';
}

View file

@ -9,12 +9,12 @@ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed auctor, nunc non fr
<table q=w>
<table q=w markdown="1">
| a | b | <p> c </p> <p> d </p> |
| - | :-: | - |
<caption> 1 2 3 | 4 5 6
<caption> q w e r t y <tr a="b"> | d | e | f |
| g <th a="b" align="left"> h </th> i |
| g <th a="b" style="text-align: left"> h </th> i |
<tr> x | y | z </tr>
x | y | z </tr> </tbody>
[ def ]
@ -58,7 +58,7 @@ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed auctor, nunc non fr
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed auctor, nunc non fringilla pulvinar, augue arcu tempor lacus, vel gravida justo sapien vel nibh. Curabitur eget dignissim lorem. Vivamus sit amet urna nec lorem aliquet viverra. Aliquam erat volutpat. Maecenas sed ante vitae erat feugiat faucibus. Integer porttitor nibh eu risus suscipit, ut efficitur mauris sollicitudin. Suspendisse potenti. Integer feugiat mi sed ligula sagittis, id sagittis justo ullamcorper.
<table>
<table markdown="1">
|a
|-
</table>
@ -66,7 +66,7 @@ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed auctor, nunc non fr
<table>
<table markdown="1">
|-
|b
</table>

64
test.py
View file

@ -97,7 +97,7 @@ def do_table_line(state, line):
# it ourselves
print("found our start tag");
out += m.group(0);
out += m.group(0) + "\n";
line = line[len(m.group(0)):];
@ -119,7 +119,7 @@ def do_table_line(state, line):
out += f"<{state.section_tag}>";
state.already_opened_section = 1;
out += m.group(0);
out += m.group(0) + "\n";
line = line[len(m.group(0)):];
@ -160,7 +160,7 @@ def do_table_line(state, line):
print(f'line = "{line}"');
out += "</caption>";
out += "</caption>" + "\n";
continue;
@ -168,7 +168,7 @@ def do_table_line(state, line):
if (m := re.match(passthrough_pattern, line)):
print(f'found passthrough tag: "{m.group(0)}"');
out += m.group(0);
out += m.group(0) + "\n";
line = line[len(m.group(0)):];
@ -187,11 +187,11 @@ def do_table_line(state, line):
column_index = 0;
if not state.already_opened_section:
out += f"<{state.section_tag}>";
out += f"<{state.section_tag}>" + "\n";
state.already_opened_section = 1;
if not already_open_tr:
out += "<tr>";
out += "<tr>" + "\n";
while line:
print("new cell");
@ -206,7 +206,7 @@ def do_table_line(state, line):
align = state.column_info[column_index]['align'];
if align != 'default':
attributes['align'] = align;
attributes['style'] = f'text-align: {align}';
print(f'attributes = "{attributes}"');
@ -225,7 +225,8 @@ def do_table_line(state, line):
line = line[1:];
print(f'line = "{line}"');
elif (m := re.match(r"<([a-z]+)(?:[\s]+([^<>]*))?>", line)):
# elif (m := re.match(r"<([a-z]+)(?:[\s]+([^<>]*))?>", line)):
elif (m := re.match(r"<(th|td)(?:[\s]+([^<>]*))?>", line)):
print("found opening HTML tag");
tag = m.group(1);
@ -262,16 +263,18 @@ def do_table_line(state, line):
line = line[len(m.group(0)):];
print(f'line = "{line}"');
elif (m := re.match(r"<([a-z]+)", line)):
print("found HTML open, but it's incomplete? huh?! throwing!");
# elif (m := re.match(r"<([a-z]+)", line)):
# print("found HTML open, but it's incomplete? huh?! throwing!");
raise SyntaxError("could not find '>' for HTML open tag");
# raise SyntaxError("could not find '>' for HTML open tag");
elif column_index < len(state.column_info):
print("found nothing, defaulting to column info");
tag = state.column_info[column_index]['default-tag']
print(f'tag = "{tag}"');
print(f'line = "{line}"');
else:
print(f"found nothing, defaulting default_tag ({state.default_cell_tag})");
@ -279,6 +282,8 @@ def do_table_line(state, line):
print(f'tag = "{tag}"');
print(f'line = "{line}"');
print("looking for closer");
content = "";
@ -358,20 +363,26 @@ def do_table_line(state, line):
column_index += 1;
# end the row of content
out += "\n";
# close tr on its own line
out += "</tr>" + "\n";
print(f'out = "{out}"');
return out;
def do_table(table_open_tag, header_lines, seperator_line, body_lines, optional_caption):
out = "";
# handle explicit table tag?
if table_open_tag:
out += table_open_tag + "\n";
open_tag = table_open_tag + "\n";
else:
# otherwise, add a default one:
out += "<table>" + "\n";
open_tag = "<table>" + "\n";
inner = "";
state = State(section_tag = "thead", default_cell_tag = "th");
@ -379,7 +390,10 @@ def do_table(table_open_tag, header_lines, seperator_line, body_lines, optional_
for line in header_lines:
html_table_line = do_table_line(state, line);
out += html_table_line + "\n";
inner += html_table_line;
if state.already_opened_section:
inner += "</thead>" "\n";
# Handle line seperator:
column_info = parse_colinfo(seperator_line);
@ -391,20 +405,22 @@ def do_table(table_open_tag, header_lines, seperator_line, body_lines, optional_
column_info = column_info);
for line in lines:
html_table_line = do_table_line(state, line);
inner += do_table_line(state, line);
out += html_table_line + "\n";
if state.already_opened_section:
inner += "</tbody>" "\n";
# Consider the optional caption
# Consider the optional caption.
# If it happens, it goes before everything else
if optional_caption:
out += f"<caption> {optional_caption} </caption>\n";
inner = f"<caption> {optional_caption} </caption>\n" + inner;
out += "</table>\n";
close_tag = "</table>\n";
for o in out.split("\n"):
for o in inner.split("\n"):
print(o);
return "\n\n" + out + "\n\n";
return "\n\n" + open_tag + inner + close_tag + "\n\n";
def handle_table(m):
print("handle_table");
@ -473,7 +489,7 @@ for o, c in product((1, 0), repeat=2):
[\n]{{2}}
# optional or required open table tag:
(?:(<table(?:[\s]+[^<>]*)?>) \n){{{o},1}}
(?:(<table[\s]+[^<>]*markdown="1"[^<>]*>) \n){{{o},1}}
# zero or one or more header rows:
((?: {row} \n){{{1-o},}})