Message Boards Message Boards

Sharing WL code JSFiddle style

Posted 6 years ago

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.

POSTED BY: b3m2a1 ​ 
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