Files
dobotai-platform/internal/outboundtext/math.go
T

56 lines
2.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package outboundtext
import (
"regexp"
)
// DingTalk cards render a small Markdown subset and no math typesetting, so a
// LaTeX formula reaches the user as raw source ($r = \sqrt{X^2 + Y^2}$).
// NormalizeMath rewrites such formulas into plain Unicode text that the card
// can show. It only touches content that actually carries math delimiters or
// well-known math commands, so ordinary prose is left byte-identical.
var (
mathBlockDelimRE = regexp.MustCompile(`\$\$([\s\S]+?)\$\$`)
mathSpanRE = regexp.MustCompile(`(?s)\$\$.+?\$\$|\\\[.+?\\\]|\\\(.+?\\\)|\$[^$\n]{1,400}\$`)
parenDelimRE = regexp.MustCompile(`\\\(([\s\S]+?)\\\)`)
bracketDelimRE = regexp.MustCompile(`\\\[([\s\S]+?)\\\]`)
sqrtRE = regexp.MustCompile(`\\sqrt\s*(?:\[([^\]]{1,20})\])?\s*\{([^{}]{1,200})\}`)
fracRE = regexp.MustCompile(`\\frac\s*\{([^{}]{1,200})\}\s*\{([^{}]{1,200})\}`)
supBracedRE = regexp.MustCompile(`\^\s*\{([^{}]{1,40})\}`)
subBracedRE = regexp.MustCompile(`_\s*\{([^{}]{1,40})\}`)
supCharRE = regexp.MustCompile(`\^([0-9A-Za-z+\-=()])`)
subCharRE = regexp.MustCompile(`_([0-9A-Za-z+\-=()])`)
commandRE = regexp.MustCompile(`\\([A-Za-z]+)`)
braceDelimRE = regexp.MustCompile(`\{([^{}]{0,200})\}`)
blankRunRE = regexp.MustCompile(`[ \t]{2,}`)
)
var mathCommands = map[string]string{
"times": "×", "cdot": "·", "div": "÷", "pm": "±", "mp": "∓",
"leq": "≤", "le": "≤", "geq": "≥", "ge": "≥", "neq": "≠", "ne": "≠",
"approx": "≈", "sim": "∼", "simeq": "≃", "equiv": "≡", "propto": "∝",
"infty": "∞", "partial": "∂", "nabla": "∇",
"alpha": "α", "beta": "β", "gamma": "γ", "delta": "δ", "Delta": "Δ",
"epsilon": "ε", "theta": "θ", "lambda": "λ", "mu": "μ", "rho": "ρ",
"sigma": "σ", "tau": "τ", "phi": "φ", "omega": "ω", "Omega": "Ω",
"eta": "η", "kappa": "κ", "nu": "ν", "xi": "ξ", "zeta": "ζ", "psi": "ψ",
"sum": "Σ", "prod": "Π", "int": "∫", "iint": "∬", "oint": "∮",
"in": "∈", "notin": "∉", "subset": "⊂", "subseteq": "⊆", "cup": "∪", "cap": "∩",
"rightarrow": "→", "to": "→", "longrightarrow": "→", "leftarrow": "←",
"Rightarrow": "⇒", "leftrightarrow": "↔", "mapsto": "↦",
"forall": "∀", "exists": "∃", "angle": "∠", "degree": "°", "circ": "°",
"perp": "⊥", "parallel": "∥", "therefore": "∴", "because": "∵",
"ldots": "…", "cdots": "…", "dots": "…", "prime": "′",
"quad": " ", "qquad": " ", "limits": "", "displaystyle": "",
"left": "", "right": "", "bmod": "mod ", "pmod": "mod ",
"text": "", "mathrm": "", "mathit": "", "operatorname": "",
"overline": "", "underline": "",
}
var escapedPunctuation = map[string]string{
`\\`: "\n", `\%`: "%", `\&`: "&", `\#`: "#", `\_`: "_", `\$`: "$",
`\{`: "{", `\}`: "}", `\,`: " ", `\;`: " ", `\!`: "",
`\(`: "", `\)`: "", `\[`: "", `\]`: "",
}