Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve formatting for outputs that contain $ to denote currency #567

55 changes: 44 additions & 11 deletions Source/Chatbook/Formatting.wl
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ makeResultCell0[ codeBlockCell[ language_String, code_String ] ] :=

makeResultCell0[ inlineCodeCell[ code_String ] ] := makeInlineCodeCell @ code;

makeResultCell0[ mathCell[ math_String ] ] /; StringMatchQ[ math, (DigitCharacter|"."|","|" ").. ] :=
math;

makeResultCell0[ mathCell[ math_String ] ] :=
With[ { boxes = Quiet @ InputAssistant`TeXAssistant @ StringTrim @ math },
If[ MatchQ[ boxes, _RawBoxes ],
Expand Down Expand Up @@ -671,10 +674,10 @@ $textDataFormatRules = {
"``" ~~ code__ ~~ "``" /; StringFreeQ[ code, "``" ] :> inlineCodeCell @ code,
"`" ~~ code: Except[ WhitespaceCharacter ].. ~~ "`" /; inlineSyntaxQ @ code :> inlineCodeCell @ code,
"`" ~~ code: Except[ "`"|"\n" ].. ~~ "`" :> inlineCodeCell @ code,
"$$" ~~ math: Except[ "$" ].. ~~ "$$" :> mathCell @ math,
"$$" ~~ math__ ~~ "$$" /; StringFreeQ[ math, "$$" ] :> mathCell @ math,
"\\(" ~~ math__ ~~ "\\)" /; StringFreeQ[ math, "\\)" ] :> mathCell @ math,
"\\[" ~~ math__ ~~ "\\]" /; StringFreeQ[ math, "\\]" ] :> mathCell @ math,
"$" ~~ math: Except[ "$" ].. ~~ "$" :> mathCell @ math
"$" ~~ math: Except[ "$" ].. ~~ "$" /; probablyMathQ @ math :> mathCell @ math
};

(* ::**************************************************************************************************************:: *)
Expand Down Expand Up @@ -708,15 +711,32 @@ $dynamicSplitRules = {

(* cSpell: ignore textit, textbf *)
$stringFormatRules = {
"***" ~~ text: Except[ "*" ].. ~~ "***" :> styleBox[ text, FontWeight -> Bold, FontSlant -> Italic ],
"___" ~~ text: Except[ "_" ].. ~~ "___" :> styleBox[ text, FontWeight -> Bold, FontSlant -> Italic ],
"**" ~~ text: Except[ "*" ].. ~~ "**" :> styleBox[ text, FontWeight -> Bold ],
"__" ~~ text: Except[ "_" ].. ~~ "__" :> styleBox[ text, FontWeight -> Bold ],
"*" ~~ text: Except[ "*" ].. ~~ "*" :> styleBox[ text, FontSlant -> Italic ],
"_" ~~ text: Except[ "_" ].. ~~ "_" :> styleBox[ text, FontSlant -> Italic ],
"[" ~~ label: Except[ "[" ].. ~~ "](" ~~ url: Except[ ")" ].. ~~ ")" :> hyperlink[ label, url ],
"\\textit{" ~~ text__ ~~ "}" /; StringFreeQ[ text, "{"|"}" ] :> styleBox[ text, FontSlant -> Italic ],
"\\textbf{" ~~ text__ ~~ "}" /; StringFreeQ[ text, "{"|"}" ] :> styleBox[ text, FontWeight -> Bold ]
"***" ~~ text: Except[ "*" ].. ~~ "***" /; StringFreeQ[ text, "\n" ] :>
styleBox[ text, FontWeight -> Bold, FontSlant -> Italic ],

"___" ~~ text: Except[ "_" ].. ~~ "___" /; StringFreeQ[ text, "\n" ] :>
styleBox[ text, FontWeight -> Bold, FontSlant -> Italic ],

"**" ~~ text: Except[ "*" ].. ~~ "**" /; StringFreeQ[ text, "\n" ] :>
styleBox[ text, FontWeight -> Bold ],

"__" ~~ text: Except[ "_" ].. ~~ "__" /; StringFreeQ[ text, "\n" ] :>
styleBox[ text, FontWeight -> Bold ],

"*" ~~ text: Except[ "*" ].. ~~ "*" /; StringFreeQ[ text, "\n" ] :>
styleBox[ text, FontSlant -> Italic ],

"_" ~~ text: Except[ "_" ].. ~~ "_" /; StringFreeQ[ text, "\n" ] :>
styleBox[ text, FontSlant -> Italic ],

"[" ~~ label: Except[ "[" ].. ~~ "](" ~~ url: Except[ ")" ].. ~~ ")" :>
hyperlink[ label, url ],

"\\textit{" ~~ text__ ~~ "}" /; StringFreeQ[ text, "{"|"}" ] :>
styleBox[ text, FontSlant -> Italic ],

"\\textbf{" ~~ text__ ~~ "}" /; StringFreeQ[ text, "{"|"}" ] :>
styleBox[ text, FontWeight -> Bold ]
};

(* ::**************************************************************************************************************:: *)
Expand Down Expand Up @@ -1613,6 +1633,19 @@ nameQ[ ___ ] := False;
inlineSyntaxQ[ s_String ] := ! StringStartsQ[ s, "`" ] && Internal`SymbolNameQ[ unescapeInlineMarkdown @ s<>"x", True ];
inlineSyntaxQ[ ___ ] := False;

(* ::**************************************************************************************************************:: *)
(* ::Subsection::Closed:: *)
(*probablyMathQ*)
probablyMathQ[ s_String ] := And[
StringFreeQ[ s, "\n" ],
StringLength @ s < 100,
Or[ StringMatchQ[ s, (LetterCharacter|DigitCharacter|"("|")").. ],
StringContainsQ[ s, "+" | "-" | "=" | "^" | ("\\" ~~ WordCharacter..) ]
]
];

probablyMathQ[ ___ ] := False;

(* ::**************************************************************************************************************:: *)
(* ::Subsection::Closed:: *)
(*unescapeInlineMarkdown*)
Expand Down
1 change: 1 addition & 0 deletions Source/Chatbook/Prompting.wl
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ $basePromptComponents[ "MathExpressions" ] = "\
* Write math expressions using LaTeX and surround them with dollar signs: $$x^2 + y^2$$";

$basePromptComponents[ "EscapedCharacters" ] = "\
* If your response contains currency in dollars, write with an escaped dollar sign: \\$99.95.
* IMPORTANT! Whenever you write a literal backtick (`) or dollar sign ($) in text, ALWAYS escape it with a backslash. \
Example: It costs me \\$99.95 every time you forget to escape \\` or \\$ properly!";

Expand Down
41 changes: 39 additions & 2 deletions Source/Chatbook/Serialization.wl
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ $$graphicsBox = $graphicsHeads[ ___ ] | TemplateBox[ _, "Legended", ___ ];
$stringStripHeads = Alternatives[
ButtonBox,
CellGroupData,
FormBox,
FrameBox,
ItemBox,
PaneBox,
Expand Down Expand Up @@ -840,7 +839,7 @@ fasterCellToString0[
(* TeXAssistantTemplate *)
fasterCellToString0[ TemplateBox[ KeyValuePattern[ "input" -> string_ ], "TeXAssistantTemplate" ] ] := (
needsBasePrompt[ "Math" ];
"$" <> string <> "$"
"$$" <> string <> "$$"
);

(* Inline WL code template *)
Expand All @@ -866,6 +865,43 @@ fasterCellToString0[ TemplateBox[ { args___ }, ___, InterpretationFunction -> f_
fasterCellToString0[ TemplateBox[ args_, ___, InterpretationFunction -> f_, ___ ] ] :=
fasterCellToString0 @ f @ args;

(* ::**************************************************************************************************************:: *)
(* ::Subsubsubsection::Closed:: *)
(*TeX*)
fasterCellToString0[ FormBox[
StyleBox[ RowBox @ { "L", StyleBox[ AdjustmentBox[ "A", ___ ], ___ ], "T", AdjustmentBox[ "E", ___ ], "X" }, ___ ],
TraditionalForm,
___
] ] := "LaTeX";

fasterCellToString0[ FormBox[
StyleBox[ RowBox @ { "T", AdjustmentBox[ "E", ___ ], "X" }, ___ ],
TraditionalForm,
___
] ] := "TeX";

fasterCellToString0[ box: FormBox[ _, TraditionalForm, ___ ] ] :=
serializeTraditionalForm @ box;

(* ::**************************************************************************************************************:: *)
(* ::Subsubsubsubsection::Closed:: *)
(*serializeTraditionalForm*)
serializeTraditionalForm // beginDefinition;

serializeTraditionalForm[ box: FormBox[ inner_, ___ ] ] := serializeTraditionalForm[ box ] =
Module[ { string },
string = Quiet @ ExportString[ Cell @ BoxData @ box, "TeXFragment" ];
If[ StringQ @ string && StringMatchQ[ string, "\\("~~__~~"\\)"~~WhitespaceCharacter... ],
fixLineEndings @ StringReplace[
StringTrim @ string,
StartOfString~~"\\("~~math__~~"\\)"~~EndOfString :> "$$"<>math<>"$$"
],
fasterCellToString0 @ inner
]
];

serializeTraditionalForm // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Subsubsubsection::Closed:: *)
(*Math Boxes*)
Expand Down Expand Up @@ -1149,6 +1185,7 @@ fasterCellToString0[ DynamicModuleBox[ a___ ] ] /; ! TrueQ @ $CellToStringDebug
(* ::**************************************************************************************************************:: *)
(* ::Subsubsubsection::Closed:: *)
(*Ignored/Skipped*)
fasterCellToString0[ FormBox[ box_, ___ ] ] := fasterCellToString0 @ box;
fasterCellToString0[ $ignoredBoxPatterns ] := "";
fasterCellToString0[ $stringStripHeads[ a_, ___ ] ] := fasterCellToString0 @ a;

Expand Down