Is there a convenient way to copy/paste text-interspersed SE code snippets into Mathematica?

Code extractor using the StackExchange API

The following code uses the 2.0 version of the SE API and has also been cleaned up a bit (place it in your kernel's init.m or your custom functions package if you'd like to be able to use it anytime).

The function takes a single string argument, which is the URL obtained from the share link under a question/answer.

Example

enter image description here


importCode[url_String] := With[
  {
   filterCode = StringCases[#, ("<pre><code>" ~~ ("\n" ...) ~~ x__ ~~ ("\n" ...) ~~ "</code></pre>") /; 
        StringFreeQ[x, "<pre><code>" | "</code></pre>"] :> x] &, 
   convertEntities = StringReplace[#, {"&gt;" -> ">", "&lt;" -> "<", "&amp;" -> "&"}] &, 
   makeCodeCell = Scan[CellPrint@Cell[Defer@#, "Input", CellTags -> "Ignore"] &, Flatten@{#}] &,
   postInfo = Import[ToString@StringForm[
        "http://api.stackexchange.com/2.1/posts/`1`?site=`2`&filter=!9hnGsretg", #3, #1] & @@ 
        {First@StringCases[#, Shortest[s__] ~~ "." ~~ ___ :> s], #2, #3} & @@ 
        StringSplit[StringDrop[url, 7], "/"][[;; 3]], "JSON"]
   },
  OptionValue["items" /. postInfo, "body"] // filterCode // convertEntities // makeCodeCell]

NOTE: I don't do any rigorous error checking or check to see if you're entering a valid Stack Exchange URL or if the question/answer is deleted (deleted posts cannot be accessed via the API), etc. So if you get any errors, it might be worthwhile to check if there's something wrong on the site.

Also, SE API limits you to 300 calls/day/IP, if I remember correctly. That's quite a lot of calls for any reasonable person and ideally, you shouldn't cross that. Nevertheless, a possibility of being throttled is something to keep in mind if you also happen to be playing with the API for other purposes such as site statistics, etc.


You could do something like this:

string = "(Paste Here)"

exps = Select[
   string ~StringSplit~ "\n\n",
   SyntaxQ@# && ! MatchQ[MakeExpression@#, _@__Times | _@Null] &];

CellPrint@Cell[#, "Input"] & ~Scan~ exps

I posted a possible answer for this on meta (Download questions or chats for offline reading), but perhaps it belongs here on the main site instead. I have a paclet that downloads a stack exchange question url, and creates a notebook version where code blocks are evaluatable.

It can be downloaded from:

https://github.com/carlwoll/Stack-Exchange-Stylesheet/releases/tag/v0.1-alpha

Download the .paclet file, and then run:

PacletInstall[file]

To use, do:

<<StackExchange`
NotebookPut @ StackExchangeView["http://mathematica.stackexchange.com/q/3535/45431"]

where I use this question as an example (the NotebookPut won't be necessary when the paclet is final). The following is snippet of what the notebook output looks like:

enter image description here

I use Import[url, "XMLObject"] instead of the Stack Exchange api (since I didn't know about the api when I started), so I need to investigate the merits of using the api or not.

It is also possible to use style key tabbing (tab at the start of a cell), shift-enter and right click to modify "StackExchange" styled cells to a markdown version, a hybrid WYSIWIG version, or a deployed version (although this aspect is a bit buggy). This is what the notebook looks like after converting the snippet to the deployed version:

enter image description here

As you can see, the deployed version still needs work (h2 and * formatting)

Feed back is welcomed.