-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathFrontEndResourceBuilder.wl
412 lines (312 loc) · 14.1 KB
/
FrontEndResourceBuilder.wl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
(* ::Package:: *)
(* ::Title:: *)
(*Manage FE expression resources*)
(* ::Section::Closed:: *)
(*Package Header*)
BeginPackage["Wolfram`ChatbookFrontEndResourceBuilder`"]
ClearAll[ "`*" ];
ClearAll[ "`Private`*" ];
color = Wolfram`Chatbook`Common`color;
WriteTextResource;
$ChatbookResources;
FEResource;
Begin["`Private`"]
(* ::Section::Closed:: *)
(*Paths*)
$inputFileName = Replace[ $InputFileName, "" :> NotebookFileName[ ] ];
$pacletDirectory = DirectoryName[ $inputFileName, 2 ];
$sourceDirectory = FileNameJoin @ { $pacletDirectory, "Developer", "Resources", "FrontEndResources" };
$sourceDirectory2 = FileNameJoin @ { $pacletDirectory, "Developer", "Resources", "Icons" };
$resourceLocation = FileNameJoin @ { $pacletDirectory, "FrontEnd", "TextResources", "ChatbookResources.tr" };
$resourceLocationDark = FileNameJoin @ { $pacletDirectory, "DarkModeSupport", "TextResources", "ChatbookResources.tr" };
(* ::Subsection::Closed:: *)
(*Load Paclet*)
PacletDirectoryLoad @ $pacletDirectory;
Get[ "Wolfram`Chatbook`" ];
(* ::Section::Closed:: *)
(*Load Resources*)
$resourceFiles = FileNames[ "*.wl", { $sourceDirectory, $sourceDirectory2 }, Infinity ];
$resourceNames = FileBaseName /@ $resourceFiles;
$ChatbookResources = AssociationThread[ $resourceNames, Import /@ $resourceFiles ];
(* ::Section::Closed:: *)
(*WriteTextResource*)
WriteTextResource::nores = "The file `1` doesn't contain a resource with name `2`.";
WriteTextResource::badkey = "Key `1` not found. Continuing without writing the new value.";
WriteTextResource::fallthru = "Using the InputForm string for unknown expression type: `1`";
WriteTextResource::badLang = "Unrecognized localization language: `1`";
WriteTextResource::badLangPath = "Localization filepath `1` does not match the intended language `2`";
WriteTextResource::badNewEntry = "Value of option \"NewEntry\" must be True, False, or a string.";
WriteTextResource::repeatNewEntry = "Trying to add an entry that already exists. Continuing without writing the new value.";
Options[WriteTextResource] = {"NewEntry" -> False, "NewEntryPrefix" -> "\n"};
Options[doWriteTextResource] = {"NewEntry" -> False, "NewEntryPrefix" -> "\n"};
WriteTextResource[ All ] :=
Module[ { written },
written = AssociationMap[
Function[
Quiet[
Check[
WriteTextResource[ # ],
WriteTextResource[ #, "NewEntry" -> True ],
WriteTextResource::badkey
],
WriteTextResource::badkey
]
],
$resourceNames
];
If[ AllTrue[ written, SameAs[ $resourceLocation ] ],
Success[ "Updated", <| "File" -> $resourceLocation, "Names" -> $resourceNames |> ],
Failure[ "SomethingHappened", <| "File" -> $resourceLocation, "Results" -> written |> ]
]
];
WriteTextResource[ All, "Dark" ] :=
Block[ { $resourceLocation = $resourceLocationDark },
WriteTextResource[ All ]
] /; BoxForm`sufficientVersionQ[14.3]
WriteTextResource[ All, "Dark" ] := WriteTextResource[ All ]
WriteTextResource[name_String, opts:OptionsPattern[]] := WriteTextResource[name -> FEResource[name], opts]
WriteTextResource[name_String -> data_, opts:OptionsPattern[]] :=
Module[{absfile, reducedOpts},
absfile = $resourceLocation;
If[FailureQ[absfile], Return[absfile]];
reducedOpts = Sequence @@ FilterRules[DeleteDuplicatesBy[First][Join[{opts}, Options[WriteTextResource]]], Options[doWriteTextResource]];
doWriteTextResource[{absfile, "@@resource ChatbookExpressions"} -> name -> data, reducedOpts]
]
doWriteTextResource[{file_, resourceLine_} -> data_, opts:OptionsPattern[]] :=
Module[{lines, resources, newresources, st},
(* read in all the lines of the resource file *)
If[FileExistsQ[file], lines = Import[file, "Lines"], Message[WriteTextResource::fnfnd, file]; Return[$Failed]];
(* group the lines for each resource together *)
resources = Split[lines, !StringStartsQ[StringTrim[#2], "@@resource " | "@|"]&];
Which[
MemberQ[resources, {resourceLine, __String}],
(* replace the indicated resource's old lines with the new ones *)
newresources = resources /. {resourceLine, oldlines__} :> {resourceLine,
Switch[data,
_String, data,
_String -> _, replaceKeyValueInString[StringRiffle[{oldlines}, "\n"], data, opts],
_, toTextResourceString[data]
]},
MatchQ[resources, {{__}}],
(* the file only defines one resource, without the @@resource line *)
newresources = {
Switch[data,
_String, data,
_String -> _, replaceKeyValueInString[StringRiffle[Flatten[resources], "\n"], data, opts],
_, toTextResourceString[data]]},
True,
Message[WriteTextResource::nores, file, resourceLine];
Return[$Failed]
];
(* write all the lines back into the file *)
st = OpenWrite[file];
WriteString[st, StringRiffle[Flatten[newresources], {"", "\n", "\n"}]];
Close[st]
]
(* ::Subsubsection::Closed:: *)
(*replaceKeyValueInString*)
Options[replaceKeyValueInString] = {"NewEntry" -> False, "NewEntryPrefix" -> "\n"};
replaceKeyValueInString[str_String, key_String -> value_, OptionsPattern[]] :=
Replace[OptionValue["NewEntry"], {
False :> changeKeyValueInString[str, key -> value],
newEntry_ :> addKeyValueInString[str, key -> value, newEntry,
Replace[OptionValue["NewEntryPrefix"], Except[_String] -> "\n"]
]
}]
(*
changeKeyValueInString is a hack, but a useful one. It looks through the string for
the first substring that looks like "key -> ". It then uses SyntaxLength to determine
the extent of the old value, and replaces it with the new.
This may fail spectacularly, but in practice it works remarkably well with strings
extracted from tr files.
*)
changeKeyValueInString[str_String, key_String -> value_] :=
Module[{keypos, valueLength, pre, post},
keypos = StringPosition[str, "\"" <> key <> "\"" ~~ (WhitespaceCharacter...) ~~ "->"];
If[MatchQ[keypos, {{_,_}}],
keypos = First[keypos],
Message[WriteTextResource::badkey, key];
Return[str]
];
valueLength = SyntaxLength[StringDrop[str, Last[keypos]]];
(* FIXME: check value length?? *)
(* remember the whitespace used on either side of the old value, and add it to the new value *)
With[{valueStr = StringTake[StringDrop[str, Last[keypos]], valueLength]},
pre = Replace[StringJoin[StringCases[valueStr, StartOfString ~~ Whitespace]], Except[_String] :> ""];
post = Replace[StringJoin[StringCases[valueStr, Whitespace ~~ EndOfString]], Except[_String] :> ""];
];
StringJoin[
StringTake[str, First[keypos] - 1],
"\"" <> key <> "\" ->",
pre,
toTextResourceString[value],
post,
StringDrop[str, Last[keypos] + valueLength]
]
]
(*
Update: SyntaxLength does not perform well with localized text that involves unicode
e.g. \:#### found in ChineseSimplified, ChineseTraditional, and Japanese, so we
now use ToExpression on lists of key-value pairs to determine the next key starting
position.
If the key is not found, the option "NewEntry"->_String allows creating a key. An empty
string creates the entry at the end of the list, but a longer string can be provided to place
it elsewhere within the list. The more specific the hint, the more accurate the placement.
"NewEntry"->False is the default to prevent overwriting the file if the key is not found.
Examples for MiscStrings.tr's FEStrings:
"KeyHint" -> "specialChars" inserts after "specialCharsTitle" (* end of group *)
"KeyHint" -> "specialCharsStruck" inserts after "specialCharsStruckTooltip" (* within group *)
*)
findNextKey[str_String, key_String, startOfString_:None] :=
Module[{expr, keypos},
expr = ToExpression[str];
keypos = Position[expr, If[startOfString === None, HoldPattern[key -> _], HoldPattern[k_String -> _] /; StringStartsQ[k, startOfString]]];
If[MatchQ[keypos, {{_}..}], keypos = Last[Last[keypos]], Return[None]];
(* at this point keypos should be an integer *)
If[keypos + 1 > Length[expr], None, Replace[expr[[keypos + 1]], {HoldPattern[k_String -> _] :> k, _ -> None}]]
]
(* assume no indentation in key-value lists *)
insertAndWrapKeyValue[str_String, pre_, key_ -> value_, post_, {start_, stop_}] :=
StringJoin[
StringTake[str, start - 1],
{pre, "\"" <> key <> "\"", " -> ", toTextResourceString[value], post},
StringDrop[str, stop - 1]
]
addToEnd[str_String, newEntryPrefix_, key_ -> value_, {start_, stop_}] :=
insertAndWrapKeyValue[str, ",\n" <> newEntryPrefix, key -> value, "", {start, stop}]
addToMiddle[str_String, newEntryPrefix_, key_ -> value_, {start_, stop_}] :=
insertAndWrapKeyValue[str, "\n" <> newEntryPrefix, key -> value, ",", {start, stop}];
replaceInMiddle[str_String, key_ -> value_, {start_, stop_}] :=
insertAndWrapKeyValue[str, "", key -> value, "", {start, stop}]
(* create a new entry *)
addKeyValueInString[str_String, key_String -> value_, newEntry_, newEntryPrefix_] :=
Module[{keypos, nextKey, nextKeyPos},
keypos = StringPosition[str, "\"" <> key <> "\"" ~~ (WhitespaceCharacter...) ~~ "->"];
keypos = If[MatchQ[keypos, {{_, _}}], First[First[keypos]], None];
If[keypos === None,
Which[
newEntry === False,
Message[WriteTextResource::badkey, key]; Return[str],
StringQ[newEntry] || TrueQ[newEntry],
(* Place new text near "key families". If no key family inferred from the newEntry string, then default to the end of the list. *)
nextKey = findNextKey[str, key, If[TrueQ[newEntry], "", newEntry]];
If[nextKey === None,
nextKeyPos = First @ First @ StringPosition[str, (WhitespaceCharacter...) ~~ "}" ~~ (WhitespaceCharacter...) ~~ EndOfString];
addToEnd[str, newEntryPrefix, key -> value, {nextKeyPos, nextKeyPos}]
,
nextKeyPos = First @ First @ StringPosition[str, (WhitespaceCharacter...) ~~ "\"" <> nextKey <> "\"" ~~ (WhitespaceCharacter...) ~~ "->"];
addToMiddle[str, newEntryPrefix, key -> value, {nextKeyPos, nextKeyPos}]
],
True,
Message[WriteTextResource::badNewEntry]; Return[str]
]
, (* else replace existing key *)
If[newEntry === False,
nextKey = findNextKey[str, key];
If[nextKeyPos === None,
nextKeyPos = First @ First @ StringPosition[str, (WhitespaceCharacter...) ~~ "}" ~~ (WhitespaceCharacter...) ~~ EndOfString];
addToEnd[str, newEntryPrefix, key -> value, {nextKeyPos, nextKeyPos}]
, (* else common case of key-value somewhere in the middle *)
nextKeyPos = First @ First @ StringPosition[str, "," ~~ (WhitespaceCharacter...) ~~ "\"" <> nextKey <> "\"" ~~ (WhitespaceCharacter...) ~~ "->"];
replaceInMiddle[str, key -> value, {keypos, nextKeyPos}]
]
,
Message[WriteTextResource::repeatNewEntry]; Return[str]
]
]
]
(* ::Subsubsection::Closed:: *)
(*toTextResourceString*)
toTextResourceString[CompressResourceValue[expr_]] :=
"CompressedData[\n" <> ToString[Compress[expr], InputForm, PageWidth -> 80] <> "\n]"
(*
This stylesheet is intended to prevent dynamics from firing while processing
cells and boxes. It's not bulletproof, and may not always be desireable, but
for now, we use it unconditionally.
*)
$exportPacketStyleDefinitions = StyleDefinitions -> Notebook[
{
Cell[StyleData[StyleDefinitions -> "Default.nb"]],
Cell[StyleData["Notebook"], DynamicUpdating -> False],
Cell[StyleData["SystemDockedCell"], DynamicUpdating -> False]
},
StyleDefinitions -> "PrivateStylesheetFormatting.nb"
];
toTextResourceString[cell_Cell] :=
Module[{nb, str, start},
nb = FEPrivate`WithContext[$Context, Notebook[{cell}, $exportPacketStyleDefinitions]];
str = First @ MathLink`CallFrontEnd[FrontEnd`ExportPacket[nb, "NotebookString"]];
start = Replace[StringPosition[str, StartOfLine ~~ "Cell["], {
{{start_, _}, ___} :> start,
_ -> None
}];
str = StringDrop[str, start-1];
StringTake[str, SyntaxLength[str]] // StringTrim
];
toTextResourceString[cell_ExpressionCell] :=
toTextResourceString[ToBoxes[Append[cell, StripOnInput -> True]]]
toTextResourceString[cells: {(_Cell | _ExpressionCell)..}] := StringJoin[
"{\n",
StringRiffle[toTextResourceString /@ cells, ",\n"],
"\n}"
]
(* use the front end to beautify box expressions, keep packed arrays compressed, etc *)
toTextResourceString[RawBoxes[boxExpression: head_[args___]]] :=
Module[{nb, str, start},
nb = FEPrivate`WithContext[$Context, Notebook[{Cell @ BoxData @ boxExpression}, $exportPacketStyleDefinitions]];
str = First @ MathLink`CallFrontEnd[FrontEnd`ExportPacket[nb, "NotebookString"]];
start = Replace[StringPosition[str, ToString[head] <> "["], {
{{start_, _}, ___} :> start,
_ -> None
}];
str = StringDrop[str, start-1];
StringTake[str, SyntaxLength[str]] // StringTrim
]
toTextResourceString[g_Graphics] := toTextResourceString[ToBoxes[g]]
(* automatically switch to FE formatting for known boxes *)
toTextResourceString[head_[args___]] := toTextResourceString[RawBoxes[head[args]]] /;
MemberQ[{GraphicsBox}, head]
(* automatically switch to FE formatting for known expression heads *)
toTextResourceString[head_[args___]] :=
Module[{nb, str, end},
nb = FEPrivate`WithContext[$Context, Notebook[{}, TaggingRules :> head[args]]];
str = First @ MathLink`CallFrontEnd[FrontEnd`ExportPacket[nb, "NotebookString"]];
end = Replace[StringPosition[str, StartOfLine ~~ "TaggingRules:>"], {
{{_, end_}, ___} :> end,
_ -> None
}];
str = StringDrop[str, end];
StringTake[str, SyntaxLength[str]] // StringTrim
] /; MemberQ[{Function}, head]
(*
SyntaxLength doesn't trim ending comments, and
"NotebookString" doesn't prevent the outline cache comments from being written.
So for notebooks, we do something a bit more hacky still.....
*)
toTextResourceString[nbExpr_Notebook] :=
Module[{nb, str},
nb = FEPrivate`WithContext[$Context, nbExpr];
str = First @ MathLink`CallFrontEnd[FrontEnd`ExportPacket[nb, "NotebookString"]];
Replace[
StringSplit[str, {
StartOfLine ~~ "(* Beginning of Notebook Content *)" ~~ EndOfLine,
StartOfLine ~~ "(* End of Notebook Content *)" ~~ EndOfLine
}],
{
{pre_, nbstr_, post_} :> nbstr,
_ -> $Failed
}
] // StringTrim
]
toTextResourceString[str_String] := str
toTextResourceString[other_] := (
Message[WriteTextResource::fallthru, Head[other]];
ToString[other, InputForm, PageWidth -> 80]
);
(* ::Section::Closed:: *)
(*FEResource*)
FEResource[ name_String ] := Lookup[ $ChatbookResources, name ];
(* ::Section::Closed:: *)
(*Package Footer*)
End[];
EndPackage[];