Wolfram Cloud Snippet Uploader
Here’s probably the easiest method I know of: use the cloud. I already use it for lots of my things. I have ~4 free accounts that I use for different distribution and testing purposes, so using it for snippets seems obvious.
A little snippet uploader can be written like so: (note that there might be a better way to write this)
exportNotebookSnippet // Clear;
Options[exportNotebookSnippet] =
Options[CloudObject];
exportNotebookSnippet[
notebook :
_NotebookObject | _CellObject | _BoxObject | {(_CellObject | \
_BoxObject) ..} | Automatic : Automatic,
uri : _String | None | Automatic : Automatic,
ops : OptionsPattern[]
] :=
Module[{nb, cells, chunk, tmp, path},
nb = Replace[notebook, Automatic :> SelectedNotebook[]];
cells =
Replace[Flatten@List@nb,
{_NotebookObject} :>
Replace[SelectedCells[nb], {} :>
Cells[nb, CellStyle -> {"Input" | "Code"}]]
];
chunk =
FrontEndExecute[
ExportPacket[
Cell[
BoxData@
Replace[
NotebookRead@cells,
Cell[BoxData[{d__}], ___] :> d,
1
]
],
"InputText"
]
][[1]];
tmp = Export[CreateFile[], chunk, "Text"];
path =
Replace[uri,
{
None :> Sequence @@ {},
Automatic :>
Switch[nb,
_NotebookObject,
Replace[FileBaseName@Quiet@NotebookFileName[nb],
$Failed :> AbsoluteCurrentValue[nb, WindowTitle]
],
_,
With[{n = ParentNotebook@cells[[1]]},
Replace[FileBaseName@Quiet@NotebookFileName[n],
$Failed :> AbsoluteCurrentValue[n, WindowTitle]
] <> "-snippet-" <>
Replace[
FirstCase[
NotebookRead@cells[[1]],
(ExpressionUUID -> s_) :> s,
None,
\[Infinity]
],
None -> ToString[cells[[1, 1]]]
]
]
]
}];
(DeleteFile[tmp]; Take[#, 1]) &@
CopyFile[tmp,
CloudObject[path, ops],
"MIMEType" -> ImportExport`GetMIMEType["Package"][[1]]
]
];
Then upload like so:
snippetObj =
exportNotebookSnippet[PreviousCell[], Permissions -> "Public"]
(*Out:*)
CloudObject["https://www.wolframcloud.com/objects/b3m2a1.testing/\
answer-snippet-167329"]
And you can conveniently import that:
Clear[exportNotebookSnippet];
CloudImport[snippetObj];
DownValues[exportNotebookSnippet]
(*Out:*)
{HoldPattern[
exportNotebookSnippet[
notebook : _NotebookObject | _CellObject | _BoxObject | \
{(_CellObject | _BoxObject) ..} | Automatic : Automatic,
uri : _String | None | Automatic : Automatic,
ops : OptionsPattern[]]] :>
Module[{nb, cells, chunk, tmp, path},
nb = Replace[notebook, Automatic :> SelectedNotebook[]];
cells = Replace[
Flatten[{nb}], {_NotebookObject} :>
Replace[SelectedCells[nb], {} :>
Cells[nb, CellStyle -> {"Input" | "Code"}]]];
chunk = FrontEndExecute[
ExportPacket[
Cell[BoxData[
Replace[NotebookRead[cells], Cell[BoxData[{d__}], ___] :> d,
1]]], "InputText"]][[1]];
tmp = Export[CreateFile[], chunk, "Text"];
path = Replace[uri, {None :> Sequence @@ {}, Automatic :> Switch[nb,
_NotebookObject,
Replace[FileBaseName[Quiet[NotebookFileName[nb]]], $Failed :>
AbsoluteCurrentValue[nb, WindowTitle]],
_,
With[{n = ParentNotebook[cells[[1]]]},
Replace[FileBaseName[Quiet[NotebookFileName[n]]], $Failed :>
AbsoluteCurrentValue[n, WindowTitle]] <> "-snippet-" <>
Replace[FirstCase[
NotebookRead[cells[[1]]], (ExpressionUUID -> s_) :> s,
None, \[Infinity]],
None -> ToString[cells[[1, 1]]]]]]}]; ((DeleteFile[tmp];
Take[#1, 1]) &)[
CopyFile[tmp, CloudObject[path, ops],
"MIMEType" -> ImportExport`GetMIMEType["Package"][[1]]]]]}