FEAT: add LaTeX rendering support for Mermaid flowcharts - #353
Conversation
| _LATEX_TEXT_ESCAPES = str.maketrans({ | ||
| "\\": R"\textbackslash{}", | ||
| "{": R"\{", | ||
| "}": R"\}", | ||
| "$": R"\$", | ||
| "&": R"\&", | ||
| "#": R"\#", | ||
| "_": R"\_", | ||
| "%": R"\%", | ||
| "~": R"\textasciitilde{}", | ||
| "^": R"\textasciicircum{}", | ||
| }) |
There was a problem hiding this comment.
Something feels fishy to me here. Why does the rendered not directly generate the correct string? Generating a syntax string and then doing translations on it is something that should be avoided because (1) it's a maintenance load (you may have to keep extending this list) and (2) what if parts of the string should not be replaced?
There was a problem hiding this comment.
This is a bit complicated...
With the new formatting class, only plain text, that would be inserted into "\text{}" is escaped due to special symbols in the names would be missinterpreted.
Already set latex code in e.g. "\frac{...}{...}" will not be escaped. ⛔ 🚪
Regarding maintenance: This escape dictionary represents a fixed LaTeX text-escaping tabel, that (hopefully) does not need maintenance... 🙃
| def __render_latex_key_and_value(key: str, value: Any) -> str: | ||
| if isinstance(value, (Fraction, int)): | ||
| no_pm = key.endswith("magnitude") or key == "pid" | ||
| return __render_latex_fraction(Fraction(value), plusminus=not no_pm) | ||
| return as_latex(value) |
There was a problem hiding this comment.
All these functions here are near copies of their as_string() equivalents. We have to identify some generic procedures that are taking place in each of these functions (such as "render for each line, then merge the lines and wrap them in some block"), so that their core remains the same and then only the (in this case) per-line renderer is changed when selecting a different output format.
That may also solve the problem with those translation tables in the top.
There was a problem hiding this comment.
Most of the copying is removed/minimalized due to introducing a formatter class that handles some repetitive definitions.
Other definitions minimalistic clones due to using the formatters.
…els in MermaidPrinter
Closes #349