Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Note:</strong> Get an ready-made palette with this functionality <a href="http://ge.tt/9Ymx9qB?c" rel="nofollow noreferrer">here</a>.</p> <hr> <p>Arnoud's solution got me excited and impatient, so here's an improvement to it. I couldn't have done this without studying his code. This version seems to be somewhat more reliable and less prone to timeout errors, but to be honest, I know no Java at all, so any improvements are welcome.</p> <p>Most importantly: this version uploads to <code>stack.imgur.com</code> directly, so it's safe to use here on StackOverflow, without having to worry that uploaded images will disappear after a while.</p> <p>I provide three functions: </p> <ul> <li><code>stackImage</code> uploads the expression, exported as PNG, and returns the URL</li> <li><code>stackMarkdown</code> returns the markdown, ready to be copied</li> <li><code>stackCopyMarkdown</code> copies the markdown to the clipboard</li> </ul> <p>Next step: create a palette button that does this automatically for the selected graphic in the notebook. Improvements to the code are very welcome.</p> <hr> <pre><code>Needs["JLink`"] stackImage::httperr = "Server returned respose code: `1`"; stackImage::err = "Server returner error: `1`"; stackImage[g_] := Module[ {getVal, url, client, method, data, partSource, part, entity, code, response, error, result}, (* this function attempts to parse the response fro the SO server *) getVal[res_, key_String] := With[{k = "var " &lt;&gt; key &lt;&gt; " = "}, StringTrim[ First@StringCases[First@Select[res, StringMatchQ[#, k ~~ ___] &amp;], k ~~ v___ ~~ ";" :&gt; v], "'"] ]; data = ExportString[g, "PNG"]; JavaBlock[ url = "https://stackoverflow.com/upload/image"; client = JavaNew["org.apache.commons.httpclient.HttpClient"]; method = JavaNew["org.apache.commons.httpclient.methods.PostMethod", url]; partSource = JavaNew["org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource", "mmagraphics.png", MakeJavaObject[data]@toCharArray[]]; part = JavaNew["org.apache.commons.httpclient.methods.multipart.FilePart", "name", partSource]; part@setContentType["image/png"]; entity = JavaNew["org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity", {part}, method@getParams[]]; method@setRequestEntity[entity]; code = client@executeMethod[method]; response = method@getResponseBodyAsString[]; ] If[code =!= 200, Message[stackImage::httperr, code]; Return[$Failed]]; response = StringTrim /@ StringSplit[response, "\n"]; error = getVal[response, "error"]; result = getVal[response, "result"]; If[StringMatchQ[result, "http*"], result, Message[stackImage::err, error]; $Failed] ] stackMarkdown[g_] := "![Mathematica graphics](" &lt;&gt; stackImage[g] &lt;&gt; ")" stackCopyMarkdown[g_] := Module[{nb, markdown}, markdown = Check[stackMarkdown[g], $Failed]; If[markdown =!= $Failed, nb = NotebookCreate[Visible -&gt; False]; NotebookWrite[nb, Cell[markdown, "Text"]]; SelectionMove[nb, All, Notebook]; FrontEndTokenExecute[nb, "Copy"]; NotebookClose[nb]; ] ] </code></pre> <p><strong>Update:</strong></p> <p>Here's a button that will show a preview of the selection and will offer uploading (or cancelling). It requires the previous functions to be defined.</p> <pre><code>Button["Upload to SO", Module[{cell = NotebookRead@InputNotebook[], img}, If[cell =!= {}, img = Rasterize[cell]; MessageDialog[ Column[{"Upload image to StackExchange sites?", img}], {"Upload and copy MarkDown" :&gt; stackCopyMarkdown[img], "Cancel" :&gt; Null}, WindowTitle -&gt; "Upload to StackExchange"]]]] </code></pre> <p>Unfortunately I can't put the button in a palette (<code>CreatePalette</code>) because the palette dimensions will influence the rasterization. Solutions to this problem are welcome.</p> <p><strong>Update 2:</strong> </p> <p>Based on the answer to <a href="https://stackoverflow.com/questions/8353838/controlling-the-rasterize-width-for-notebook-related-expressions">this question</a>, here's a working Windows-only palette button:</p> <pre><code>button = Button["Upload to SO", Module[{sel}, FrontEndExecute[ FrontEndToken[FrontEnd`SelectedNotebook[], "CopySpecial", "MGF"]]; sel = Cases[NotebookGet@ClipboardNotebook[], RasterBox[data_, ___] :&gt; Image[data, "Byte", ColorSpace -&gt; "RGB", Magnification -&gt; 1], Infinity]; If[sel =!= {}, With[{img = First[sel]}, MessageDialog[ Column[{"Upload image to StackExchange sites?", img}], {"Upload and copy MarkDown" :&gt; stackCopyMarkdown[img], "Cancel" :&gt; Null}, WindowTitle -&gt; "Upload to StackExchange"] ] ] ] ] CreatePalette[button] </code></pre> <p>Warning: it destroys the clipboard contents even if you click cancel in the preview box.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload