NetBeans plugin: Special copy/paste

The very little I've had to paste code into this fairly new blog, was enough to give me the idea of creating an extension to NetBeans which would help to remedy this formatting nightmare. Now, in my world, there's approximately a 100:1 ratio between idea and actual realization, so I'd have to salute the awesome OpenIDE API's for making this possible spending just a few late nights and taking just 200 lines of code. Without further ado, I present, my first NetBeans plugin.


Special copy/paste
The plugin will add a couple of new actions to the context-sensitive popup-menu of the source editor, which will allow you to copy the selected text as preformatted HTML, as well as a CSS version which will preserve the formatting used in NetBeans.



Using the "Copy as HTML and CSS" menu as in the above screen dump will result in you being able to paste it directly into a website/wiki/blog and have it display like this:


/**
* @param args the command line arguments
*/

public static void main(String[] args) {
// TODO code application logic here
}



For a larger example of what the output of the plugin looks like, take a look at the plugin's own source code.

It's supposed to be language agnostic, in that it should work with all types of sources NetBeans is able to tokenize. The plugin will try to mimic the formatting of the tokens in regard to font family, keyword and literal colors. It will however *not* be able to show member-wide variables in green as NetBeans does nor show method and class decelerations in bold. This is a limitation caused by the fact that I am only using the NetBeans lexer API rather than doing any kind of semantic analysis or parsing.

If you want to give the plugin a try, download it from NetBean's Plugin Portal. Source code is available here.


Update:
It has been made clear to me through the comments, that NetBeans already contains functionality to extract source code as HTML. You can find the feature in the "File" menu under the ill named item "Print to HTML". Since it would appear other people than just me did not associate "print" with that of exporting HTML content, I have filed an issue for the relocation and/or renaming of this.

Comments

Krosan said…
This comment has been removed by the author.
ckutler said…
This is just what I need. A big time saver. I have one request for enhancement: It would be awesome if > and < were converted to & gt; and & lt; (I added spaces to prevent the markup from being displayed, I hope you know what I mean, I wasn't sure if they would be converted to > and <)
Casper Bang said…
Glad you find it useful C. Currently I am relying on Netbeans XMLUtil to escape just the < as I thought it was enough. Are you having trouble rendering the HTML/CSS the plugin outputs or is it because you would like not to rely on a pre tag? I can update the plugin to escape every > as well.
Anonymous said…
Did you use the "Print to HTML" (File menu) for your implementation?
Casper Bang said…
@karussell: No I did not, I did not know this feature. By the look of it, this plugin should be incorporated into that feature. All that's missing is a working text selection and an option to copy to clipboard rather than to a file. I think I will file a RFE for this.
Anonymous said…
hohohoo it was amazing. i can copy that source in my blog. its kind of brilliant idea :D.. :beers: for you brother..
tediscript
/k said…
Thanks.

This seems to work fine on NetBeans 6.5 RC2.

One question: Could this be easily extended to support RTF (rich text format) ? I would like to be able to copy from netbeans source code and paste into StarOffice presentations while keeping the formatting. Current this requires a two/three step process (Print to HTML, then open page and copy)...

thanks again
This plug-in does not work with NetBeans v. 6.5.1. I dont' want to install NetBeans 6.7.1, and I am waiting for the official realease of NB 6.8. What I can do, then? Thanks!
Casper Bang said…
@jhon: You can always build the plugin yourself, the source code is available. Since 6.8 is now out and I have confirmed the plugin works with it, I won't do anything further. Let me know if you still have trouble though.
Dessie said…
Doesn't work with NetBeans 6.9
Sizzle said…
...or 7.0 Beta. Since 7.0 is still Beta, this is perfectly understandable. :D
RCD Dégage said…
Works perfectly with 6.9.1, thanks this just saved me.
Anonymous said…
Thanks, It worked on 6.9. But I only see Copy as Html and CSS option and with every copy it append its version info something like this: Version:0.9 StartHTML:-1 EndHTML:-1 StartFragment:0000000111 EndFragment:0000001049 Version:0.9 StartHTML:-1 EndHTML:-1 StartFragment:0000000111 EndFragment:0000001049.
But Anyways, Thank you very much.
Unknown said…
Thanks! Working on my homework for college, I have to copy/paste a lot of Java code in MS Word doc.The plugin works nicely with NetBeans 7.3 on Windows 7 machine

Popular posts from this blog

Beware of SQLite and the Turkish Locale

Oracle SQLDeveloper 4 on Debian/Ubuntu/Mint

Rejsekort Scanner