Add necessary escape characters for LaTeX and pretty printing

Signed-off-by: Danila Fedorin <danila.fedorin@gmail.com>
This commit is contained in:
Danila Fedorin 2023-12-01 16:25:39 -08:00
parent 22f3937523
commit 66fbfd1962

View File

@ -3,6 +3,29 @@ module Bergamot.Latex exposing (..)
import Bergamot.Syntax exposing (..)
import Bergamot.Rules exposing (..)
encodeStr : String -> String
encodeStr s =
let
go l =
case l of
'\\' :: xs -> '\\' :: go xs
'"' :: xs -> '\\' :: '"' :: go xs
x :: xs -> x :: go xs
[] -> []
in
String.fromList (go (String.toList s))
encodeLatex : String -> String
encodeLatex s =
let
go l =
case l of
'\\' :: xs -> String.toList "\\textbackslash " ++ go xs
x :: xs -> x :: go xs
[] -> []
in
String.fromList (go (String.toList s))
termToLatex : (a -> String) -> Term a -> String
termToLatex f t =
case t of
@ -24,7 +47,7 @@ termToLatex f t =
Call s ts -> "\\text{" ++ s ++ "}(" ++ String.join "," (List.map (termToLatex f) ts) ++ ")"
Var x -> f x
IntLit i -> String.fromInt i
StringLit s -> "\\texttt{" ++ "\"" ++ s ++ "\"" ++ "}"
StringLit s -> "\\texttt{" ++ "\"" ++ encodeLatex (encodeStr s) ++ "\"" ++ "}"
metavariableToLatex : Metavariable -> String
metavariableToLatex (MkMetavariable s) =