Message Boards Message Boards

GROUPS:

Sharing WL code JSFiddle style

Posted 3 months ago
260 Views
|
0 Replies
|
2 Total Likes
|

I decided to build off of Let Me Mathematica That For You to write something akin to JSFiddle which this week we're calling WLFiddle. It's basically the same a LMMTFY but with some helper code you need to use and it forces you to use Base64:

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
   ];
makeWLFiddle[cells : {__Cell}] :=
  With[
   {
    cc = NotebookTools`FlattenCellGroups[cells],
    key = StringJoin[ToString /@ RandomInteger[10, 15]]
    },
   StringReplace[
    URLBuild[
     "https://www.wolframcloud.com/objects/b3m2a1/WLFiddle",
     MapIndexed[
      "cell" <> ToString[#2[[1]]] ->
        Developer`EncodeBase64[cellToString[#]] &,
      cc
      ]
     ],
    key -> " "
    ]
   ];
makeWLFiddle[notebook_NotebookObject] :=
  Module[
   {
    cells = Flatten@{NotebookRead[notebook]},
    cc
    },
   If[Length@cells == 0,
    cells = First@NotebookGet[notebook]
    ];
   URLShorten[makeWLFiddle[cells]]
   ];

I also deployed the notebook where I developed that as a fiddle:

fiddle

Hope this is fun for other people too.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract