invalid tabulator expression parsing
@@ -521,6 +521,51 @@ def split_mtext_string(s: str, size: int = 250) -> List[str]:
return chunks
+def plain_mtext2(
+ text: str,
+ split=False,
+ tabsize: int = 4,
+) -> Union[List[str], str]:
+ """Returns the plain MTEXT content as a single string or a list of
+ strings if `split` is ``True``. Replaces ``\\P`` by ``\\n`` and removes
+ other controls chars and inline codes.
+
+ Args:
+ text: MTEXT content string
+ split: split content at line endings ``\\P``
+
+ """
+ content: List[str] = []
+ paragraph: List[str] = []
+
+ # localize enum to speed up inner loop
+ _, word, stack, space, nbsp, tabulator, new_paragraph, *_ = TokenType
+ tab_replacement = " " * tabsize
+
+ for token in MTextParser(text):
+ t = token.type
+ if t == word:
+ paragraph.append(token.data)
+ elif t == space or t == nbsp:
+ paragraph.append(" ")
+ elif t == new_paragraph:
+ # NEW_COLUMN is not a line break!
+ content.append("".join(paragraph))
+ paragraph.clear()
+ elif t == tabulator:
+ paragraph.append(tab_replacement)
+ elif t == stack:
+ upr, lwr, divider = token.data
+ paragraph.append(upr + divider + lwr)
+
+ if paragraph:
+ content.append("".join(paragraph))
+ if split:
+ return content
+ else:
+ return "\n".join(content)
+
+
def escape_dxf_line_endings(text: str) -> str:
# replacing '\r\n' and '\n' by '\P' is required when exporting, else an
# invalid DXF file would be created.
@@ -1489,7 +1534,13 @@ class MTextParser:
paragraph_scanner.consume()
tab_stops.append(type_ + parse_float_expr())
else:
- tab_stops.append(parse_float())
+ float_expr = parse_float_expr()
+ if float_expr:
+ tab_stops.append(float(float_expr))
+ else:
+ # invalid float expression, consume invalid letter
+ # and try again:
+ paragraph_scanner.consume()
ctx.paragraph = ParagraphProperties(
indent, left, right, align, tuple(tab_stops)
)
