From 29de1e611dd32f2b1f4623a2c797355520f9a23d Mon Sep 17 00:00:00 2001 From: Rick Hennigan Date: Fri, 15 Nov 2024 18:43:42 -0500 Subject: [PATCH] Bugfix: Remove left padding of code instead of using `StringTrim` --- Source/Chatbook/Formatting.wl | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/Source/Chatbook/Formatting.wl b/Source/Chatbook/Formatting.wl index 6e1a1c90..bb45952b 100644 --- a/Source/Chatbook/Formatting.wl +++ b/Source/Chatbook/Formatting.wl @@ -282,7 +282,7 @@ makeResultCell0[ str_String ] := formatTextString @ str; makeResultCell0[ codeBlockCell[ language_String, code_String ] ] := makeCodeBlockCell[ StringReplace[ StringTrim @ language, $externalLanguageRules, IgnoreCase -> True ], - StringTrim @ code + StringDelete[ code, { StartOfString~~Whitespace~~StartOfLine, Whitespace~~EndOfString } ] ]; makeResultCell0[ inlineCodeCell[ code_String ] ] := ReplaceAll[ @@ -1247,11 +1247,27 @@ $stringFormatRules = { (* ::Subsection::Closed:: *) (*makeCodeBlockCell*) makeCodeBlockCell // beginDefinition; -makeCodeBlockCell[ _, code_String ] /; StringMatchQ[ code, "!["~~__~~"]("~~__~~")" ] := image @ code; -makeCodeBlockCell[ _, code_String ] /; StringStartsQ[ code, "TOOLCALL: " ] := inlineToolCall @ code; -makeCodeBlockCell[ language_String, code_String ] := makeInteractiveCodeCell[ language, code ]; +makeCodeBlockCell[ _, code_String ] /; StringMatchQ[ StringTrim @ code, "!["~~__~~"]("~~__~~")" ] := image @ code; +makeCodeBlockCell[ _, code_String ] /; StringStartsQ[ StringTrim @ code, "TOOLCALL: " ] := inlineToolCall @ code; +makeCodeBlockCell[ language_String, code_String ] := makeInteractiveCodeCell[ language, stripCodePadding @ code ]; makeCodeBlockCell // endDefinition; +(* ::**************************************************************************************************************:: *) +(* ::Subsubsection::Closed:: *) +(*stripCodePadding*) +stripCodePadding // beginDefinition; + +stripCodePadding[ code_String ] := + Catch @ Module[ { padding }, + padding = First[ StringCases[ code, StartOfString~~Whitespace, 1 ], Throw @ code ]; + If[ AllTrue[ StringSplit[ code, "\n" ], StringStartsQ @ padding ], + StringDelete[ code, StartOfLine~~padding ], + code + ] + ]; + +stripCodePadding // endDefinition; + (* ::**************************************************************************************************************:: *) (* ::Subsection::Closed:: *) (*inlineToolCall*)