Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Is there a way to copy and paste code snippets from SE to Mathematica if these snippets are interspersed with text?

Like e.g. in Morphing Graphics, color and location in both the question and answer, there are code blocks separated by text and graphics.

Pasting this into Mathematica in n steps is tiresome. Perhaps there is some nice way to make pasting as comfortable as the other way round with the code and graphics palettes?

share|improve this question

3 Answers 3

up vote 25 down vote accepted

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.

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.

share|improve this answer
Could it grab the URL from the clipboard? –  belisarius Aug 22 '12 at 14:55
@Verde You could add a definition: importCode[] := importCode[ First@Cases[NotebookGet@ClipboardNotebook[], Cell[x_, ___] :> x, Infinity]] which will copy whatever is in the clipboard (no checks to see if it is a URL or not), but I don't think this is useful as the chances of you having forgotten and copied something else is high... pasting it as an argument shouldn't be hard and I would prefer explicit over clipboard copy :) –  The Toad Aug 22 '12 at 15:16
The call throttling problem can be circumvented if you use an API key. I've written about that before. I believe it was in my SO API answer that you were referring to in a comment above. –  Sjoerd C. de Vries Aug 26 '12 at 6:39
I think you could first filter out blockquotes before extracting code blocks, so it doesn't get messed by those blockquotes with a codeblock in it, which are used by some user for formatting output. Also IMO Shortest[...] would be enough for html parsing instead of FreeStringQ[...] condition. Maybe something like StringCases[StringReplace[#,Shortest["<blockquote>"~~__~~"</blockquote>"]:>""],S‌​hortest["<pre><code>"~~("\n"...)~~x__~~("\n"...)~~"</code></pre>"]:>x]. –  Silvia Jun 12 '13 at 11:21
@Silvia I agree that one can implement workarounds to filter out the blockquote, but I'm against that because the issue is not the code here, but the incorrect use of formatting styles. Tomorrow, someone else might start using a different tag for some other unintended purpose and we can't be catching/tracking them all. I figured it was better to educate and reason with users to use only code blocks for code than to add exceptions . Re: Shortest, yes, I think they're equivalent and looks cleaner. –  The Toad Jun 12 '13 at 12:01

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
share|improve this answer
Works splendidly! I´ll refrain from ranting about prodigious use of infix. –  Yves Klett Mar 24 '12 at 9:39
I am more of a prefix person, myself. Mind you, some of my best friends are infix users ;-) –  Yves Klett Mar 24 '12 at 9:53
@Yves so you write Map[f, #]& @ x in that case? :o) –  Mr.Wizard Mar 24 '12 at 9:55
For my sins! I am an infix addict after all. Perhaps someone could analyze submitted code for the predominant syntax flavors? I´d upvote that no end :-) –  Yves Klett Mar 24 '12 at 10:02
@Sjoerd it filters out lines that do parse, but parse to Times[___] (such as this is text and not code) or Null (such as a line return). –  Mr.Wizard Mar 26 '12 at 0:24

The StackAPI answer seems really nice, however it failed for me when I tested it, so I coded a bare bones implimentation which simply pulls out any code blocks from an arbitrary html page, without needing it to be from stackexchange, or even well formed html:

 codeBlocks[url_] := CellPrint[
   Cell[#, "Input"] & /@ 
     StringCases[Import[url, "Source"], 
     "<pre><code>" ~~ p : (Shortest[___]) ~~ "</code></pre>" :> p]]
share|improve this answer
could you share the exact call you made that failed? What error did you get? A bunch of badly formed string boxes? –  The Toad Aug 22 '12 at 13:57
@R.M I ran the code posted in the answer and the suggested command StackAPI`GetCode["http://meta.mathematica.stackexchange.com/a/307/5"], which returned a set of errors from StringCasesand StringReplace. Seems like the call to the API is failing, returning {"answers" -> {}, "page" -> 1, "pagesize" -> 30, "total" -> 0} from the Import. –  jVincent Aug 23 '12 at 8:53
For some reason, I wasn't pinged by this comment from you back in August. Anyway, the reason it failed for you is because this question was originally on meta (which is where I wrote this answer) and then it was migrated here. The suggested command pointed to the original, but since it was deleted, you couldn't retrieve it using the API (This was something I had mentioned in the post). In any case, my answer used v1.0 of the API, which was bound to fail at some point since they've deprecated it. I updated my answer today with a new version (using API v2.0), which is when I saw this comment. –  The Toad Feb 16 '13 at 0:40

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.