|
How to copy code snippets with Internet Explorer while preserving newlines and indentation category 'tool', language C#, created 30-Jul-2007, version V1.2, by Luc Pattyn This article also appeared on the CodeProject. |
|
License: The author hereby grants you a worldwide, non-exclusive license to use and redistribute the files and the source code in the article in any way you see fit, provided you keep the copyright notice in place; when code modifications are applied, the notice must reflect that. The author retains copyright to the article, you may not republish or otherwise make available the article, in whole or in part, without the prior written consent of the author. Disclaimer: This work is provided |
You all have seen code snippets both in CodeProject articles and CodeProject messages on one of the discussion boards. Sometimes they are formatted nicely, and sometimes they are not.
If your Internet Browser happens to be Netscape, FireFox, Safari, ... you may have no problems copying these code snippets from the CodeProject web page and pasting them into some application, Visual Studio or other; if so, the information in this article may still be of interest, but the utility presented will be of no value to you.
If on the other hand you use Microsoft Internet Explorer (version 6 or 7) or Maxthon, you probably do experience some problems: nicely formatted code snippets all too often loose their formatting, either the newlines, the indentation, or both. It all depends on circumstances, especially on how exactly you make your selection on the web page.
Before we move to browsers and selection, we need to tell a bit more about the clipboard. This powerful system component is capable of handling all kinds of data; unknown to some, it can even store the same data in several formats at once. Here are some of these formats:
This article deals with text formats only.
Now the basic idea is to allow two programs to exchange data with as rich a content as they are both capable of. In that end, the program where you copy from (not knowing what the destination is) will store the data on the clipboard in several formats, from simple text, to highly formatted text. The program where you paste, will normally consume the richest format that is available on the clipboard and supported by the program. That is how you can copy text with formatting from one MS Office program to another, and yet also paste it in Notepad.
Each Internet browser copies text in its own way.
The different versions of Microsoft Internet Explorer seem to save text to the clipboard in the four text formats I mentioned before (ASCII, Unicode, RTF, HTML). Furthermore their ASII and Unicode formats do not contain the necessary spaces, tabs and newline characters; and the RTF format is used in a complex manner.
One other browser (Maxthon) seems to behave exactly like Internet Explorer: it copies the same four text formats to the clipboard, and has the same problems IE6 and IE7 have.
The majority of non-Microsoft browsers such as Netscape Browser, Mozilla FireFox and Apple's Safari do not emit RTF at all. But their ASCII and Unicode formats are correct.
Note: Apple's Safari uses a linefeed character as a newline in its ASCII and Unicode formats, and not the carriage return (or CRLF) most of us would expect; but that is no problem whatsoever for Visual Studio.
As far as we could discover, when multiple formats are available, Visual Studio prefers the RTF format, but it does not always understand it in the same way WordPad and the RichTextBox Control do.
If no RTF data is available, Visual Studio pastes either ASCII or Unicode text; and we already established that is missing spaces, tabs and newlines when coming from Internet Explorer.
We observed WordPad does a better job at pasting CodeProject code snippets, than Visual Studio does. So we need to somehow modify and improve what is on the clipboard, before we paste it into Visual Studio.
The solution to that is a little program with at its heart a RichTextBox: if the conditions are right (that is if the four text formats are present so the source is likely Internet Explorer), we paste the clipboard's RTF data in it, and then copy it back from it; this simple operation results in a much simpler RTF representation of the same text, so much so that Visual Studio understands it completely now, including the newlines, and the indentation (for Internet Explorer: only if copied correctly to begin with).
So the LP_IERescueCode utility program is a small Windows application, with:
NotifyIcon
that offers a context menu to deal with some settings;
It is not the intention of this article to describe the program any further; the source code is available for whom is interested.
The CodeProject web site lets you publish code snippets, and offers some tags to organize it.
This is what we recommend:
<PRE> and </PRE>
); you can get these tags by clicking on the PRE label underneath the edit box
when entering a CodeProject message, or you can just type them yourself; then
paste your code in between these PRE tags.
< sign will be investigated; if it looks like the start
of an HTML tag, it will not show; so my advice is: as soon as the first
non-whitespace character following the < sign is a letter,
replace the < sign by the four characters <
<B> and </B> tags to get
some bold
text.
<CODE> </CODE> tags.With Internet Explorer one needs four steps to get it right:
When a code snippet appears perfectly formatted on the CodeProject web page, you can copy/paste it to Visual Studio with any web browser you choose while preserving its formatting (spaces, tabs, newlines). If you want to do this using Microsoft Internet Explorer or Maxthon, you should carefully select a number of entire lines, up to the end of the PRE block, and get the clipboard content processed by the LP_IECodeRescue utility.
Thanks to several CodeProject members, and in particular to Paul Conrad, George L. Jackson, and Mark Salsbery who helped a lot in pinpointing the actual problem, testing the utility and improving this article.
Perceler |
Copyright © 2012, Luc Pattyn |
Last Modified 21-May-2025 |