mkdocs-plugins icon indicating copy to clipboard operation
mkdocs-plugins copied to clipboard

neoteroi.spantable does not support code with pipes

Open yves-chevallier opened this issue 1 year ago • 0 comments

The following will fail:

::spantable::

| Foo | Bar |
| --- | --- |
| Baz | `|` |

::end-spantable::

One hack is to store temporary replace codes with a placeholder:

--- a/neoteroi/mkdocs/markdown/tables/__init__.py
+++ b/neoteroi/mkdocs/markdown/tables/__init__.py
@@ -115,6 +115,15 @@ def read_table(markdown: str, cls: Type[T] = Table) -> Optional[T]:
     records: List[List[str]] = []
 
     for line in markdown.splitlines():
+
+        # Code blocks could contain pipes, so we need to store them and replace them later
+        code = []
+        def store(match):
+            code.append(match.group(0))
+            return f"@@{len(code) - 1}@@"
+
+        line = re.sub(r"`[^`]+`", store, line)
+
         if _TABLE_SEPARATOR_LINE_PATTERN.match(line):
             continue
 
@@ -122,6 +131,11 @@ def read_table(markdown: str, cls: Type[T] = Table) -> Optional[T]:
 
         if matches:
             row = [match.group(1).strip() for match in matches]
+
+            for i, cell in enumerate(row):
+                row[i] = re.sub(
+                    r"@@(\d+)@@", lambda m: code[int(m.group(1))], cell)
+
             if not headers:
                 headers = row
             else:

yves-chevallier avatar Jul 15 '24 13:07 yves-chevallier