Skip to content

Commit 47d38a5

Browse files
authored
Merge pull request #1010 from WolframResearch/bugfix/more-template-box-serialization
More template box serialization
2 parents 85754f9 + 3ed11d6 commit 47d38a5

File tree

2 files changed

+81
-15
lines changed

2 files changed

+81
-15
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"Checkmarked",
1212
"CICD",
1313
"clicktocopy",
14+
"CMYK",
1415
"Componentwise",
1516
"Connor",
1617
"datarepository",

Source/Chatbook/Serialization.wl

Lines changed: 80 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,23 @@ $boxOp = <| SuperscriptBox -> "^", SubscriptBox -> "_" |>;
199199

200200
(* How to choose TemplateBox arguments for serialization *)
201201
$templateBoxRules = <|
202-
"AssistantMessageBox" -> First,
203-
"ConditionalExpression" -> makeExpressionString,
204-
"GrayLink" -> First,
205-
"HyperlinkDefault" -> First,
206-
"Key0" -> First,
207-
"Key1" -> (Riffle[ #, "-" ] &),
208-
"RowDefault" -> Identity,
209-
"TransferFunctionModelFull" -> makeExpressionString,
210-
"UserMessageBox" -> First
202+
"AssistantMessageBox" -> First,
203+
"ConditionalExpression" -> makeExpressionString,
204+
"GrayLink" -> First,
205+
"HyperlinkDefault" -> First,
206+
"Key0" -> First,
207+
"Key1" -> (Riffle[ #, "-" ] &),
208+
"RowDefault" -> Identity,
209+
"TransferFunctionModelFull" -> makeExpressionString,
210+
"UserMessageBox" -> First,
211+
"CMYKColorSwatchTemplate" -> inputFormString @* Lookup[ "color" ],
212+
"GrayLevelColorSwatchTemplate" -> inputFormString @* Lookup[ "color" ],
213+
"HueColorSwatchTemplate" -> inputFormString @* Lookup[ "color" ],
214+
"LABColorSwatchTemplate" -> inputFormString @* Lookup[ "color" ],
215+
"LCHColorSwatchTemplate" -> inputFormString @* Lookup[ "color" ],
216+
"LUVColorSwatchTemplate" -> inputFormString @* Lookup[ "color" ],
217+
"RGBColorSwatchTemplate" -> inputFormString @* Lookup[ "color" ],
218+
"XYZColorSwatchTemplate" -> inputFormString @* Lookup[ "color" ]
211219
|>;
212220

213221
(* ::**************************************************************************************************************:: *)
@@ -232,6 +240,7 @@ $$graphicsBox = Alternatives[
232240
(* Serialize the first argument of these and ignore the rest *)
233241
$stringStripHeads = Alternatives[
234242
ActionMenuBox,
243+
AdjustmentBox,
235244
ButtonBox,
236245
CellGroupData,
237246
FrameBox,
@@ -1300,6 +1309,10 @@ fasterCellToString0[ TemplateBox[ args: { _, _, str_String, ___ }, "MessageTempl
13001309
fasterCellToString0 @ str
13011310
);
13021311

1312+
(* Percent References *)
1313+
fasterCellToString0[ TemplateBox[ KeyValuePattern[ "OutNumber" -> n_Integer ], "PercentRef", ___ ] ] :=
1314+
"%" <> ToString @ n;
1315+
13031316
(* Large Outputs *)
13041317
fasterCellToString0[ TemplateBox[ KeyValuePattern[ "shortenedBoxes" -> boxes_ ], "OutputSizeLimitTemplate", ___ ] ] :=
13051318
fasterCellToString0 @ boxes;
@@ -1487,12 +1500,6 @@ fasterCellToString0[ box: TemplateBox[ args_, name_String, ___ ] ] :=
14871500
fasterCellToString0 @ f @ args /; ! MissingQ @ f && f =!= makeExpressionString
14881501
];
14891502

1490-
fasterCellToString0[ TemplateBox[ { args___ }, ___, InterpretationFunction -> f_, ___ ] ] :=
1491-
fasterCellToString0 @ f @ args;
1492-
1493-
fasterCellToString0[ TemplateBox[ args_, ___, InterpretationFunction -> f_, ___ ] ] :=
1494-
fasterCellToString0 @ f @ args;
1495-
14961503
fasterCellToString0[ OverlayBox[ { a_, ___ }, ___ ] ] :=
14971504
fasterCellToString0 @ a;
14981505

@@ -2303,6 +2310,64 @@ fasterCellToString0[ DynamicModuleBox[ a___ ] ] /; ! TrueQ @ $CellToStringDebug
23032310
"DynamicModule[\[LeftSkeleton]" <> ToString @ Length @ HoldComplete @ a <> "\[RightSkeleton]]"
23042311
);
23052312

2313+
(* ::**************************************************************************************************************:: *)
2314+
(* ::Subsubsubsection::Closed:: *)
2315+
(*Unhandled TemplateBoxes*)
2316+
fasterCellToString0[ box: TemplateBox[ args_, ___ ] ] :=
2317+
With[ { f = getTemplateBoxFunction @ box },
2318+
fasterCellToString0 @ applyTemplateBoxDisplayFunction[ f, args ] /; ! MissingQ @ f
2319+
];
2320+
2321+
(* ::**************************************************************************************************************:: *)
2322+
(* ::Subsubsubsubsection::Closed:: *)
2323+
(*getTemplateBoxFunction*)
2324+
getTemplateBoxFunction // beginDefinition;
2325+
getTemplateBoxFunction[ TemplateBox[ __, InterpretationFunction -> f: Except[ $$unspecified ], ___ ] ] := f;
2326+
getTemplateBoxFunction[ TemplateBox[ __, DisplayFunction -> f: Except[ $$unspecified ], ___ ] ] := f;
2327+
getTemplateBoxFunction[ TemplateBox[ _, name_String, ___, InterpretationFunction -> Automatic, ___ ] ] := name;
2328+
getTemplateBoxFunction[ TemplateBox[ _, name_String, ___ ] ] := getTemplateBoxFunction @ name;
2329+
getTemplateBoxFunction[ name_String ] := Lookup[ $templateBoxCache, name, getTemplateBoxFunction0 @ name ];
2330+
getTemplateBoxFunction // endDefinition;
2331+
2332+
2333+
getTemplateBoxFunction0 // beginDefinition;
2334+
2335+
getTemplateBoxFunction0[ name_String ] :=
2336+
getTemplateBoxFunction0[ name, usingFrontEnd @ CurrentValue @ { StyleDefinitions, name, TemplateBoxOptions } ];
2337+
2338+
getTemplateBoxFunction0[
2339+
name_,
2340+
KeyValuePattern[ InterpretationFunction|"InterpretationFunction" -> f: Except[ $$unspecified ] ]
2341+
] := $templateBoxCache[ name ] = f;
2342+
2343+
getTemplateBoxFunction0[
2344+
name_,
2345+
KeyValuePattern[ InterpretationFunction|"InterpretationFunction" -> Automatic ]
2346+
] := $templateBoxCache[ name ] = name;
2347+
2348+
getTemplateBoxFunction0[
2349+
name_,
2350+
KeyValuePattern[ DisplayFunction|"DisplayFunction" -> f: Except[ $$unspecified ] ]
2351+
] := $templateBoxCache[ name ] = f;
2352+
2353+
getTemplateBoxFunction0[ name_, _ ] :=
2354+
$templateBoxCache[ name ] = Missing[ "NotFound" ];
2355+
2356+
getTemplateBoxFunction0 // endDefinition;
2357+
2358+
2359+
$templateBoxCache = <| |>;
2360+
2361+
(* ::**************************************************************************************************************:: *)
2362+
(* ::Subsubsubsubsection::Closed:: *)
2363+
(*applyTemplateBoxDisplayFunction*)
2364+
applyTemplateBoxDisplayFunction // beginDefinition;
2365+
applyTemplateBoxDisplayFunction[ f_, TemplateBox[ args_, ___ ] ] := applyTemplateBoxDisplayFunction[ f, args ];
2366+
applyTemplateBoxDisplayFunction[ f_String, a_List ] := f <> "[" <> StringRiffle[ fasterCellToString0 /@ a, ", " ] <> "]";
2367+
applyTemplateBoxDisplayFunction[ f_, { args___ } ] := f @ args;
2368+
applyTemplateBoxDisplayFunction[ f_, args___ ] := f @ args;
2369+
applyTemplateBoxDisplayFunction // endDefinition;
2370+
23062371
(* ::**************************************************************************************************************:: *)
23072372
(* ::Subsubsubsection::Closed:: *)
23082373
(*Ignored/Skipped*)

0 commit comments

Comments
 (0)