What is meta? ×
Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 129 Stack Exchange communities.

I found a lovely Greasemonkey script ("pre select") for copying the contents of a pre block, but when I use it on code blocks on Stack Overflow it also grabs all the span elements used for syntax highlighting. Is there any way to remove said highlighting so that just the raw text can be copied?

For example:

im = Image.new("RGB", (300,300))

yields (without any line breaks):

<pre class="prettyprint"><code>
<span class="pln">im </span><span class="pun">=</span><span class="pln"> </span>
<span class="typ">Image</span><span class="pun">.</span>
<span class="kwd">new</span><span class="pun">(</span>
<span class="str">"RGB"</span><span class="pun">,</span>
<span class="pln"> </span><span class="pun">(</span>
<span class="lit">300</span><span class="pun">,</span>
<span class="lit">300</span><span class="pun">))</span>
</code></pre>
share|improve this question

2 Answers 2

up vote 3 down vote accepted

In the Greasemonkey script, replace

textarea.innerHTML = pre.innerHTML;

with

textarea.innerHTML 
  = pre.innerHTML.replace(/<br[\s]*\/?>/ig, '\n').replace(/<[^>]*>/g, '');

That replaces all <br> tags with a newline and then boldly removes all other tags, regardless if a closing tag is found for some opening tag. This seems to work for all Google Prettify examples, but I've not tested on other sites.

(I doubt you'd like to remove all highlighting, for every page. But if that would be acceptable: as the highlighting is done using JavaScript in your browser, redefining the prettyPrint() function might stop it from happening.)

share|improve this answer
    
Awesome, that's done it. Normally I hate mixing regular expressions and HTML, but the HTML within the blocks on these sites is probably so limited that it's an acceptable solution. –  Ignacio Vazquez-Abrams Apr 18 '10 at 22:22
    
Yup, boldly ignoring any structure, keeps me far away from trouble. ;-) –  Arjan Apr 18 '10 at 22:39

You could make this question more general, and post it on Stack Overflow. Just play around with the script a bit, and then let others know what you tried.

share|improve this answer
8  
(in fact don't mention it relates to stackoverflow, otherwise there is a good chance it'll come back to meta!) –  Marc Gravell Apr 18 '10 at 10:05
    
An excellent idea, but I'm ill at the moment so trying to formulate a coherent attempt at the problem is just a bit beyond me, and I don't want to just stick the question up without trying myself. –  Ignacio Vazquez-Abrams Apr 18 '10 at 22:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .