-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathWLFiddle.wl
95 lines (76 loc) · 1.95 KB
/
WLFiddle.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
(* ::Package:: *)
(* ::Section:: *)
(*WLFiddle*)
BeginPackage["WLFiddle`"]
MakeWLFiddle::usage=
"MakeWLFiddle[nb] turns nb or its current selection into a fiddle URL
MakeWLFiddle[cells] turns a set of cells into a fiddle URL";
Begin["`Private`"]
cellToString[c:(Cell[b_BoxData, ___]|Cell[_, "Input", ___])]:=
First@FrontEndExecute@
ExportPacket[c, "InputText"];
cellToString[c:Cell[_,s_String, ___]]:=
ExportString[
<|"style"->s,
"content"->
First@FrontEndExecute@
ExportPacket[c, "PlainText"]
|>,
"JSON",
"Compact"->True
];
(* ::Text:: *)
(*WLFiddle is the real powerhouse here. It uses wlfiddle.js and wlfiddle.css to make an embedded fiddle nb.*)
$fiddleURL="https://www.wolframcloud.com/objects/b3m2a1/WLFiddle";
$embedURL="https://www.wolframcloud.com/objects/b3m2a1/WLFiddleEmbed";
$delayedURL="https://www.wolframcloud.com/objects/b3m2a1/WLDelayedFiddle";
Options[MakeWLFiddle]=
{
"ShortenURL"->True,
"BaseURL"->Automatic
};
MakeWLFiddle[cells:{__Cell}, ops:OptionsPattern[]]:=
Module[
{
cc=NotebookTools`FlattenCellGroups[cells],
key=StringJoin[ToString/@RandomInteger[10, 15]],
url,
base
},
base=
Replace[OptionValue["BaseURL"],
{
"Embed":>$embedURL,
"Delayed":>$delayedURL,
Except[_String]:>$fiddleURL
}
];
url = StringReplace[
URLBuild[
base,
MapIndexed[
"cell"<>ToString[#2[[1]]]->
Developer`EncodeBase64[cellToString[#]]&,
cc
]
],
key->" "
];
If[TrueQ@OptionValue["ShortenURL"],
URLShorten[url],
url
]
];
MakeWLFiddle[notebook_NotebookObject, ops:OptionsPattern[]]:=
Module[
{
cells=Flatten@{NotebookRead[notebook]},
cc
},
If[Length@cells==0,
cells=First@NotebookGet[notebook]
];
MakeWLFiddle[cells, ops]
];
End[]
EndPackage[]