Skip to content

Commit

Permalink
Use FE styles to determine correct display functions for other templa…
Browse files Browse the repository at this point in the history
…te boxes
  • Loading branch information
rhennigan committed Jan 4, 2025
1 parent 63ac922 commit 3ed11d6
Showing 1 changed file with 59 additions and 6 deletions.
65 changes: 59 additions & 6 deletions Source/Chatbook/Serialization.wl
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ $$graphicsBox = Alternatives[
(* Serialize the first argument of these and ignore the rest *)
$stringStripHeads = Alternatives[
ActionMenuBox,
AdjustmentBox,
ButtonBox,
CellGroupData,
FrameBox,
Expand Down Expand Up @@ -1499,12 +1500,6 @@ fasterCellToString0[ box: TemplateBox[ args_, name_String, ___ ] ] :=
fasterCellToString0 @ f @ args /; ! MissingQ @ f && f =!= makeExpressionString
];

fasterCellToString0[ TemplateBox[ { args___ }, ___, InterpretationFunction -> f_, ___ ] ] :=
fasterCellToString0 @ f @ args;

fasterCellToString0[ TemplateBox[ args_, ___, InterpretationFunction -> f_, ___ ] ] :=
fasterCellToString0 @ f @ args;

fasterCellToString0[ OverlayBox[ { a_, ___ }, ___ ] ] :=
fasterCellToString0 @ a;

Expand Down Expand Up @@ -2315,6 +2310,64 @@ fasterCellToString0[ DynamicModuleBox[ a___ ] ] /; ! TrueQ @ $CellToStringDebug
"DynamicModule[\[LeftSkeleton]" <> ToString @ Length @ HoldComplete @ a <> "\[RightSkeleton]]"
);

(* ::**************************************************************************************************************:: *)
(* ::Subsubsubsection::Closed:: *)
(*Unhandled TemplateBoxes*)
fasterCellToString0[ box: TemplateBox[ args_, ___ ] ] :=
With[ { f = getTemplateBoxFunction @ box },
fasterCellToString0 @ applyTemplateBoxDisplayFunction[ f, args ] /; ! MissingQ @ f
];

(* ::**************************************************************************************************************:: *)
(* ::Subsubsubsubsection::Closed:: *)
(*getTemplateBoxFunction*)
getTemplateBoxFunction // beginDefinition;
getTemplateBoxFunction[ TemplateBox[ __, InterpretationFunction -> f: Except[ $$unspecified ], ___ ] ] := f;
getTemplateBoxFunction[ TemplateBox[ __, DisplayFunction -> f: Except[ $$unspecified ], ___ ] ] := f;
getTemplateBoxFunction[ TemplateBox[ _, name_String, ___, InterpretationFunction -> Automatic, ___ ] ] := name;
getTemplateBoxFunction[ TemplateBox[ _, name_String, ___ ] ] := getTemplateBoxFunction @ name;
getTemplateBoxFunction[ name_String ] := Lookup[ $templateBoxCache, name, getTemplateBoxFunction0 @ name ];
getTemplateBoxFunction // endDefinition;


getTemplateBoxFunction0 // beginDefinition;

getTemplateBoxFunction0[ name_String ] :=
getTemplateBoxFunction0[ name, usingFrontEnd @ CurrentValue @ { StyleDefinitions, name, TemplateBoxOptions } ];

getTemplateBoxFunction0[
name_,
KeyValuePattern[ InterpretationFunction|"InterpretationFunction" -> f: Except[ $$unspecified ] ]
] := $templateBoxCache[ name ] = f;

getTemplateBoxFunction0[
name_,
KeyValuePattern[ InterpretationFunction|"InterpretationFunction" -> Automatic ]
] := $templateBoxCache[ name ] = name;

getTemplateBoxFunction0[
name_,
KeyValuePattern[ DisplayFunction|"DisplayFunction" -> f: Except[ $$unspecified ] ]
] := $templateBoxCache[ name ] = f;

getTemplateBoxFunction0[ name_, _ ] :=
$templateBoxCache[ name ] = Missing[ "NotFound" ];

getTemplateBoxFunction0 // endDefinition;


$templateBoxCache = <| |>;

(* ::**************************************************************************************************************:: *)
(* ::Subsubsubsubsection::Closed:: *)
(*applyTemplateBoxDisplayFunction*)
applyTemplateBoxDisplayFunction // beginDefinition;
applyTemplateBoxDisplayFunction[ f_, TemplateBox[ args_, ___ ] ] := applyTemplateBoxDisplayFunction[ f, args ];
applyTemplateBoxDisplayFunction[ f_String, a_List ] := f <> "[" <> StringRiffle[ fasterCellToString0 /@ a, ", " ] <> "]";
applyTemplateBoxDisplayFunction[ f_, { args___ } ] := f @ args;
applyTemplateBoxDisplayFunction[ f_, args___ ] := f @ args;
applyTemplateBoxDisplayFunction // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Subsubsubsection::Closed:: *)
(*Ignored/Skipped*)
Expand Down

0 comments on commit 3ed11d6

Please sign in to comment.