Skip to content

Commit

Permalink
Merge pull request #930 from WolframResearch/bugfix/fix-indented-code
Browse files Browse the repository at this point in the history
Bugfix: Remove left padding of code instead of using `StringTrim`
  • Loading branch information
rhennigan authored Nov 15, 2024
2 parents ae50a49 + 29de1e6 commit cc4609c
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions Source/Chatbook/Formatting.wl
Original file line number Diff line number Diff line change
Expand Up @@ -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[
Expand Down Expand Up @@ -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*)
Expand Down

0 comments on commit cc4609c

Please sign in to comment.