Repository navigation
Expand file tree
/
Copy pathUtils.wl
More file actions
410 lines (321 loc) · 12.4 KB
/
Copy pathUtils.wl
File metadata and controls
410 lines (321 loc) · 12.4 KB
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
BeginPackage["ConnorGray`NotebookWebsiteTools`Utils`"]
Needs["GeneralUtilities`" -> "GU`"]
RelativePath
ConvertToString
UniqueContext::usage = "UniqueContext[stem] generates a unique context name beginning with stem."
NotebookCells::usage = "NotebookCells[notebook] returns a list of all top-level cells in notebook, after flattening out cell groups."
HTMLEscape::usage = "HTMLEscape[string] escapes string so that it can be embedded in HTML as regular text."
GU`SetUsage[CellDataQ, "CellDataQ[expr$] returns True if expr$ is a valid cell content type, typically a string, TextData[$$] or BoxData[$$].
* Box expressions are not considered valid cell data, as they cannot validly appear as the first\
argument of Cell[$$].
"]
GU`SetUsage[CreateCacheDirectory, "
CreateCacheDirectory[path$] creates or confirms the existence of a cache directory at the specified path$.
This function is intended to aid in the creation of a directory containing
cached or built content that is recoverable or not important if deleted, while
being resistent to creating a cache directory at a location that contains
existing files that may not be easily recoverable if deleted.
This function follows the CACHEDIR.TAG (https://bford.info/cachedir/) specification.
If no file or directory exists at path$, a new directory will be created that
contains a valid CACHEDIR.TAG file.
If path$ is a non-empty directory containing a valid CACHEDIR.TAG file, path$
will be returned.
If path$ is an empty directory, a valid CACHEDIR.TAG file will be created and
path$ will be returned.
If path$ is an existing file, or non-empty directory that does not contain a
CACHEDIR.TAG value, an error will be returned.
If DeleteContents \[Rule] True is set and path$ is a valid cache directory, all
files other than CACHEDIR.TAG will be deleted. If path$ did not exist or was an
empty directory, it will be initialized as valid cache directory containing
only a CACHEDIR.TAG file.
"]
GU`SetUsage[HTMLFragmentQ, "
HTMLFragmentQ[expr$] returns True if expr$ can validlty appear as an element in a list that is a 3rd argument of XMLElement."]
GU`SetUsage[PrefixListsToRules, "
PrefixListsToRules[lists$] turns a list of prefix lists into nested rules suitable for use with RulesTree.
"]
GU`SetUsage[GetWebsiteFavicon, "
GetWebsiteFavicon[url$] attempts to retrieve the favicon of a website as an
Image or Graphics expression.
"]
Begin["`Private`"]
Needs["ConnorGray`NotebookWebsiteTools`Errors`"]
(*========================================================*)
SetFallthroughError[CreateCacheDirectory]
Options[CreateCacheDirectory] = {
DeleteContents -> False
}
(* TODO: Implement a scheme for locking cache directories to synchronize access
to them? *)
CreateCacheDirectory[
path0 : _?StringQ | File[_?StringQ],
OptionsPattern[]
] := Handle[_Failure] @ WrapRaised[
NotebookWebsiteError,
"Unable to create cache directory at ``",
InputForm[path0]
] @ Module[{
path = RaiseConfirm @ ExpandFileName[Replace[path0, File[s_?StringQ] :> s]],
result
},
result = createCacheDirectory[path];
(* Should throw an error or return `path`. *)
RaiseAssert[result === path];
If[TrueQ[OptionValue[DeleteContents]],
deleteCacheDirectoryContentsUnchecked[path];
];
path
]
(*------------------------------------*)
SetFallthroughError[createCacheDirectory]
createCacheDirectory[
path_?StringQ
] := Module[{
tagPath = FileNameJoin[{path, "CACHEDIR.TAG"}]
},
Replace[FileType[path], {
None :> (
(* The specified directory doesn't exist, so initialize a new
empty cache directory. *)
RaiseConfirm @ CreateDirectory[path];
RaiseConfirm @ Export[tagPath, $tagFileContents, "Text"];
Return[path, Module];
),
File :> Raise[NotebookWebsiteError, "Specified path is a non-directory file."],
Directory :> Null,
other_ :> Raise[NotebookWebsiteError, "Unexpected file type: ``", InputForm[other]]
}];
(*--------------------------------------*)
(* If the tag file exists, validate it. *)
(*--------------------------------------*)
If[FileExistsQ[tagPath],
(* TODO: Check that this file isn't a symbolic link either. *)
RaiseAssert[FileType[tagPath] === File];
Replace[ReadString[tagPath], {
contents_?StringQ :> (
If[!validTagFileContentsQ[contents],
Raise[NotebookWebsiteError, "Existing CACHEDIR.TAG file contains invalid header."]
];
(* The tag file exists and is valid, so this is a valid cache
directory. *)
Return[path, Module];
),
other_ :> Raise[NotebookWebsiteError, "Error reading existing CACHEDIR.TAG file contents: ``", other]
}];
];
(*-----------------------------------------------------------------*)
(* `path` is an existing directory but doesn't contain a tag file. *)
(*-----------------------------------------------------------------*)
If[FileNames[All, path] =!= {},
(* This directory isn't empty and it doesn't contain a tag file. It
might be a directory containing non-recoverable user files! Don't
risk deleting its contents. *)
(* NOTE:
This is the key user-facing error message of CreateCacheDirectory
and a big part of the utility of this function. *)
Raise[NotebookWebsiteError, StringJoin[
"Specified path is an existing non-empty directory without a CACHEDIR.TAG file. ",
"This operation may succeed if the existing directory contents are moved or deleted manually.",
"\n\nNOTE: This file path is being used as a location to store cached data or generated files. ",
"Data loss WILL occur if you store non-recoverable files in this location."
]]
];
(*----------------------------------------*)
(* `path` is an existing empty directory. *)
(*----------------------------------------*)
RaiseConfirm @ Put[$tagFileContents, tagPath];
RaiseConfirm @ Export[tagPath, $tagFileContents, "Text"];
path
]
(*------------------------------------*)
SetFallthroughError[deleteCacheDirectoryContentsUnchecked]
deleteCacheDirectoryContentsUnchecked[path_?DirectoryQ] := Module[{
files = FileNames[All, path]
},
files = Select[files, FileNameTake[#] =!= "CACHEDIR.TAG" &];
(*
Delete all existing contents of the cache directory, except the
CACHEDIR.TAG file.
*)
WrapRaised[NotebookWebsiteError, "Error deleting existing contents of cache directory"][
Scan[
entry |-> Replace[FileType[entry], {
File :> DeleteFile[entry],
Directory :> DeleteDirectory[entry, DeleteContents -> True]
}],
files
];
];
]
(*------------------------------------*)
(* TODO: Option to override the function name in the 'created by' message below. *)
$tagFileContents = "\
Signature: 8a477f597d28d172789f06886806bc55
# This file is a cache directory tag created by CreateCacheDirectory.
# For information about cache directory tags, see:
# http://www.brynosaurus.com/cachedir/
"
(*====================================*)
SetFallthroughError[validTagFileContentsQ]
validTagFileContentsQ[contents_?StringQ] :=
StringStartsQ[contents, "Signature: 8a477f597d28d172789f06886806bc55"]
(*========================================================*)
(*
Return the portion of `path` which is relative to `root`. If `path` is not relative to
`root`, a failure is returned.
TODO: What should this return if `root` and `path` are the same?
*)
RelativePath[
root_?StringQ,
path_?StringQ
] := Module[{
rootParts = FileNameSplit[root],
pathParts = FileNameSplit[path]
},
(* Ensure that `path` is a subdirectory of `root`. If it is, return the component
of the path which is relative to `root`. *)
If[! MatchQ[pathParts, {Sequence @@ rootParts, ___}],
$Failed,
FileNameJoin[pathParts[[Length[rootParts] + 1 ;;]]]
]
]
(*========================================================*)
ConvertToString[expr_] := Replace[expr, {
string_?StringQ /; StringMatchQ[string, "\"" ~~ ___ ~~ "\""] :> ToExpression[string],
string_?StringQ :> string,
items_?ListQ :> StringJoin[ConvertToString /@ items],
TextData[content_] :> ConvertToString[content],
StyleBox[content_, ___] :> ConvertToString[content],
BoxData[content_] :> ConvertToString[content],
RowBox[items_?ListQ] :> StringJoin[ConvertToString /@ items],
TemplateBox[items_?ListQ, "RowDefault"] :> StringJoin[ConvertToString /@ items],
ButtonBox[content_, ___] :> ConvertToString[content],
other_ :> Raise[NotebookWebsiteError, "no rule to convert form to string: ``", InputForm[other]]
}]
SetFallthroughError[ConvertToString]
(*========================================================*)
UniqueContext[stem_?StringQ] := Module[{ctx},
If[!TrueQ[Internal`SymbolNameQ[stem]],
Return[Failure["UniqueContext", <|
"MessageTemplate" -> "Invalid non-Symbol stem: ``"|>,
"MessageParameters" -> {stem}
]];
];
ctx = stem <> "$" <> ToString[$ModuleNumber] <> "`";
$ModuleNumber += 1;
ctx
]
(*========================================================*)
NotebookCells[
Notebook[cells:{___Cell}, ___?OptionQ]
] := flattenCellGroups[cells]
(*------------------------------------*)
flattenCellGroups[cells: {___Cell}] :=
Flatten @ Map[
Replace[{
Cell[
CellGroupData[groupCells:{___Cell}, ___],
___
] :> (
flattenCellGroups[groupCells]
),
Cell[group_CellGroupData, ___] :> (
Raise[NotebookWebsiteError, "Unexpected CellGroupData structure: ``", group]
),
normalCell_Cell :> normalCell,
other_ :> Raise[NotebookWebsiteError, "Unexpected notebook structure: ``", InputForm[other]]
}],
cells
]
SetFallthroughError[NotebookCells]
(*========================================================*)
(* NOTE: This function is required because exporting an XMLElement[..] using the
"XML" format escapes '<' and '>' characters in `text`, but exporting as
using the "HTMLFragment" format does not escape those characters. *)
HTMLEscape[text_?StringQ] := StringReplace[
ExportString[XMLElement["Text", {}, {text}], "XML"],
StartOfString ~~ "<Text>" ~~ content___ ~~ "</Text>" ~~ EndOfString :> content
]
SetFallthroughError[HTMLEscape]
(*========================================================*)
CellDataQ[expr_] :=
(* TODO: More obscure or deprecated forms. e.g. GraphicsData or OutputFormData? *)
MatchQ[expr, Alternatives[
_?StringQ,
TextData[_],
BoxData[_]
]]
SetFallthroughError[CellDataQ]
(*========================================================*)
(* TODO: Include XML`RawXML["..."] here? *)
HTMLFragmentQ[expr_] :=
MatchQ[expr, _?StringQ | _XMLElement | Nothing | Splice[{___?HTMLFragmentQ}]]
(*========================================================*)
PrefixListsToRules[prefixes : {{Except[_?ListQ] ...} ...}] := Module[{rules},
rules = Normal @ Map[
inner |-> PrefixListsToRules[DeleteCases[inner, {}]],
GroupBy[prefixes, First -> Rest]
];
Replace[rules, (lhs_ -> {}) :> lhs, {1}]
]
SetFallthroughError[PrefixListsToRules]
(*========================================================*)
GetWebsiteFavicon[url_?StringQ | URL[url_?StringQ]] := Module[{
domain,
apiUrl,
resp
},
(* TODO:
Is stripping the Path segment of the URL really required, and is
it perhaps also invalid? I think its probably required as long as this
function is using the Google-provided API, since otherwise there would
be an ambiguity for any query parameters that appear in `url`. Perhaps
though the URL parameter could be parameter encoded to fix that?
I.e. so that:
GetWebsiteFavicon["https://example.org/path?with_param=true"]
doesn't do naive string concatenation to get:
https://www.google.com/s2/favicons?domain=https://example.org/path?with_param=true
which invalidly has two "?" in it. We could instead URLEncode or
URLQueryEncode the domain parameter.
*)
domain = URLParse[url]["Domain"];
If[!StringQ[domain],
Raise[
NotebookWebsiteError,
"Unable to extract domain from URL ``: ``",
InputForm[url], InputForm[domain]
];
];
(* Per: https://dev.to/derlin/get-favicons-from-any-website-using-a-hidden-google-api-3p1e *)
apiUrl = StringJoin[
"https://www.google.com/s2/favicons?domain=",
domain,
"&sz=32"
];
resp = URLRead[apiUrl, TimeConstraint -> 10];
RaiseAssert[MatchQ[resp, _HTTPResponse]];
If[resp["StatusCode"] =!= 200,
(* FIXME: Improve this error handling. Could be especially common and
flaky way for a website build to fail. *)
Raise[
NotebookWebsiteError,
<| "Response" -> resp |>,
"Favicon lookup returned non-success HTTP response (status=``)",
InputForm[resp["StatusCode"]]
];
];
favicon = Import[resp];
ConfirmReplace[favicon, {
_Image :> favicon,
other_ :> (
Raise[
NotebookWebsiteError,
"Unexpected imported favicon data head: ``",
Head[other]
];
)
}]
]
(*========================================================*)
End[]
EndPackage[]