Class: Assistant::LatexFragmentFormatter
- Inherits:
-
Object
- Object
- Assistant::LatexFragmentFormatter
- Defined in:
- app/services/assistant/latex_fragment_formatter.rb
Overview
Protects explicit LaTeX fragments while a Sunny response passes through
Markdown and restores safe hooks for the on-demand KaTeX controller.
Constant Summary collapse
- INLINE_LATEX_PATTERN =
Dollar signs are common in Sunny responses, so only treat a single-dollar
span as math when it contains an unambiguous LaTeX marker. This renders
$\rightarrow$,$x^2$, and\frac{...}formulas without consuming
currency text such as "$31.79 spend ... $2,187.80 revenue". / \$(?!\$) (?<source> (?=[^$\n]*(?:\\[A-Za-z]+|[_^{}=])) [^$\n]+? ) \$(?!\$) /x- INLINE_CODE_PATTERN =
/(`+)(.*?)\1/m
Instance Method Summary collapse
-
#extract ⇒ Object
Replace supported delimiters with opaque tokens.
-
#initialize(content) ⇒ LatexFragmentFormatter
constructor
A new instance of LatexFragmentFormatter.
-
#restore(html) ⇒ Object
Restore escaped, readable fallback markup after all Markdown, sanitation, table, and entity-linking work is complete.
Constructor Details
#initialize(content) ⇒ LatexFragmentFormatter
Returns a new instance of LatexFragmentFormatter.
22 23 24 25 26 |
# File 'app/services/assistant/latex_fragment_formatter.rb', line 22 def initialize(content) @content = content @fragments = [] @placeholder_key = SecureRandom.hex(8).upcase end |
Instance Method Details
#extract ⇒ Object
Replace supported delimiters with opaque tokens. Fenced code has already
been extracted by ResponseFormatter; inline code is skipped here.
30 31 32 |
# File 'app/services/assistant/latex_fragment_formatter.rb', line 30 def extract transform_outside_inline_code(@content) { |text| extract_from_text(text) } end |
#restore(html) ⇒ Object
Restore escaped, readable fallback markup after all Markdown, sanitation,
table, and entity-linking work is complete.
36 37 38 39 40 41 |
# File 'app/services/assistant/latex_fragment_formatter.rb', line 36 def restore(html) @fragments.each_with_index do |fragment, index| html = html.gsub(placeholder(index)) { render_fragment(fragment) } end html end |